blob: 3d3c8b4c7f9a7c37a7f558a0dfa48d7ede85557b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200729static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000731static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000732static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000733static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000734static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000736static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200737#ifdef FEAT_FLOAT
738static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000744#ifdef FEAT_FLOAT
745static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
746#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200748static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200749static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100750static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100754static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000761static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
762static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100765static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000766
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000767static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000768static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static int get_env_len __ARGS((char_u **arg));
770static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
773#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
774#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
775 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000776static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000778static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100779static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000780static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static typval_T *alloc_tv __ARGS((void));
782static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static void init_tv __ARGS((typval_T *varp));
784static long get_tv_number __ARGS((typval_T *varp));
785static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000786static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static char_u *get_tv_string __ARGS((typval_T *varp));
788static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100790static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
791static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
793static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
794static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000795static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
796static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
798static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000799static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200800static int var_check_func_name __ARGS((char_u *name, int new_var));
801static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000802static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000803static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
805static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
806static int eval_fname_script __ARGS((char_u *p));
807static int eval_fname_sid __ARGS((char_u *p));
808static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000809static ufunc_T *find_func __ARGS((char_u *name));
810static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200811static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
813static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000814static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
815static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
816static int
817# ifdef __BORLANDC__
818 _RTLENTRYF
819# endif
820 prof_total_cmp __ARGS((const void *s1, const void *s2));
821static int
822# ifdef __BORLANDC__
823 _RTLENTRYF
824# endif
825 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200827static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000828static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000829static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000832static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
833static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
836static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000837static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000838static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000839static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200840static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200841static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000842
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200843
844#ifdef EBCDIC
845static int compare_func_name __ARGS((const void *s1, const void *s2));
846static void sortFunctions __ARGS(());
847#endif
848
Bram Moolenaar33570922005-01-25 22:26:29 +0000849/*
850 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000851 */
852 void
853eval_init()
854{
Bram Moolenaar33570922005-01-25 22:26:29 +0000855 int i;
856 struct vimvar *p;
857
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
859 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200860 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000861 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000862 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000863
864 for (i = 0; i < VV_LEN; ++i)
865 {
866 p = &vimvars[i];
867 STRCPY(p->vv_di.di_key, p->vv_name);
868 if (p->vv_flags & VV_RO)
869 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
870 else if (p->vv_flags & VV_RO_SBX)
871 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
872 else
873 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000874
875 /* add to v: scope dict, unless the value is not always available */
876 if (p->vv_type != VAR_UNKNOWN)
877 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000879 /* add to compat scope dict */
880 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000881 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000882 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100883 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200885
886#ifdef EBCDIC
887 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100888 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200889 */
890 sortFunctions();
891#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000892}
893
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000894#if defined(EXITFREE) || defined(PROTO)
895 void
896eval_clear()
897{
898 int i;
899 struct vimvar *p;
900
901 for (i = 0; i < VV_LEN; ++i)
902 {
903 p = &vimvars[i];
904 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000905 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000906 vim_free(p->vv_str);
907 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000908 }
909 else if (p->vv_di.di_tv.v_type == VAR_LIST)
910 {
911 list_unref(p->vv_list);
912 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 }
915 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000916 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917 hash_clear(&compat_hashtab);
918
Bram Moolenaard9fba312005-06-26 22:34:35 +0000919 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100920# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200921 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100922# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000923
924 /* global variables */
925 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000927 /* autoloaded script names */
928 ga_clear_strings(&ga_loaded);
929
Bram Moolenaarcca74132013-09-25 21:00:28 +0200930 /* Script-local variables. First clear all the variables and in a second
931 * loop free the scriptvar_T, because a variable in one script might hold
932 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200935 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200936 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200937 ga_clear(&ga_scripts);
938
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000939 /* unreferenced lists and dicts */
940 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941
942 /* functions */
943 free_all_functions();
944 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000945}
946#endif
947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 * Return the name of the executed function.
950 */
951 char_u *
952func_name(cookie)
953 void *cookie;
954{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the next breakpoint line for a funccall cookie.
960 */
961 linenr_T *
962func_breakpoint(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the address holding the debug tick for a funccall cookie.
970 */
971 int *
972func_dbg_tick(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/*
979 * Return the nesting level for a funccall cookie.
980 */
981 int
982func_level(cookie)
983 void *cookie;
984{
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000989funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000991/* pointer to list of previously used funccal, still around because some
992 * item in it is still being used. */
993funccall_T *previous_funccal = NULL;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Return TRUE when a function was ended by a ":return" command.
997 */
998 int
999current_func_returned()
1000{
1001 return current_funccal->returned;
1002}
1003
1004
1005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 * Set an internal variable to a string value. Creates the variable if it does
1007 * not already exist.
1008 */
1009 void
1010set_internal_string_var(name, value)
1011 char_u *name;
1012 char_u *value;
1013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001014 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001015 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 val = vim_strsave(value);
1018 if (val != NULL)
1019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001023 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 }
1026 }
1027}
1028
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001030static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031static char_u *redir_endp = NULL;
1032static char_u *redir_varname = NULL;
1033
1034/*
1035 * Start recording command output to a variable
1036 * Returns OK if successfully completed the setup. FAIL otherwise.
1037 */
1038 int
1039var_redir_start(name, append)
1040 char_u *name;
1041 int append; /* append to an existing variable */
1042{
1043 int save_emsg;
1044 int err;
1045 typval_T tv;
1046
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001047 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001048 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 {
1050 EMSG(_(e_invarg));
1051 return FAIL;
1052 }
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 redir_varname = vim_strsave(name);
1056 if (redir_varname == NULL)
1057 return FAIL;
1058
1059 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1060 if (redir_lval == NULL)
1061 {
1062 var_redir_stop();
1063 return FAIL;
1064 }
1065
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001066 /* The output is stored in growarray "redir_ga" until redirection ends. */
1067 ga_init2(&redir_ga, (int)sizeof(char), 500);
1068
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001070 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001071 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1073 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001074 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (redir_endp != NULL && *redir_endp != NUL)
1076 /* Trailing characters are present after the variable name */
1077 EMSG(_(e_trailing));
1078 else
1079 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001080 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001081 var_redir_stop();
1082 return FAIL;
1083 }
1084
1085 /* check if we can write to the variable: set it to or append an empty
1086 * string */
1087 save_emsg = did_emsg;
1088 did_emsg = FALSE;
1089 tv.v_type = VAR_STRING;
1090 tv.vval.v_string = (char_u *)"";
1091 if (append)
1092 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1093 else
1094 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001095 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001097 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 if (err)
1099 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001100 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 var_redir_stop();
1102 return FAIL;
1103 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104
1105 return OK;
1106}
1107
1108/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 * Append "value[value_len]" to the variable set by var_redir_start().
1110 * The actual appending is postponed until redirection ends, because the value
1111 * appended may in fact be the string we write to, changing it may cause freed
1112 * memory to be used:
1113 * :redir => foo
1114 * :let foo
1115 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 */
1117 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123
1124 if (redir_lval == NULL)
1125 return;
1126
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001128 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001130 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 {
1134 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 }
1137 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139}
1140
1141/*
1142 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 */
1145 void
1146var_redir_stop()
1147{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148 typval_T tv;
1149
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_lval != NULL)
1151 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 /* If there was no error: assign the text to the variable. */
1153 if (redir_endp != NULL)
1154 {
1155 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1156 tv.v_type = VAR_STRING;
1157 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 /* Call get_lval() again, if it's inside a Dict or List it may
1159 * have changed. */
1160 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001162 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1163 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1164 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001167 /* free the collected output */
1168 vim_free(redir_ga.ga_data);
1169 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 vim_free(redir_lval);
1172 redir_lval = NULL;
1173 }
1174 vim_free(redir_varname);
1175 redir_varname = NULL;
1176}
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178# if defined(FEAT_MBYTE) || defined(PROTO)
1179 int
1180eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1181 char_u *enc_from;
1182 char_u *enc_to;
1183 char_u *fname_from;
1184 char_u *fname_to;
1185{
1186 int err = FALSE;
1187
1188 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1189 set_vim_var_string(VV_CC_TO, enc_to, -1);
1190 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1191 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1192 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1193 err = TRUE;
1194 set_vim_var_string(VV_CC_FROM, NULL, -1);
1195 set_vim_var_string(VV_CC_TO, NULL, -1);
1196 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1197 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1198
1199 if (err)
1200 return FAIL;
1201 return OK;
1202}
1203# endif
1204
1205# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1206 int
1207eval_printexpr(fname, args)
1208 char_u *fname;
1209 char_u *args;
1210{
1211 int err = FALSE;
1212
1213 set_vim_var_string(VV_FNAME_IN, fname, -1);
1214 set_vim_var_string(VV_CMDARG, args, -1);
1215 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1216 err = TRUE;
1217 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1218 set_vim_var_string(VV_CMDARG, NULL, -1);
1219
1220 if (err)
1221 {
1222 mch_remove(fname);
1223 return FAIL;
1224 }
1225 return OK;
1226}
1227# endif
1228
1229# if defined(FEAT_DIFF) || defined(PROTO)
1230 void
1231eval_diff(origfile, newfile, outfile)
1232 char_u *origfile;
1233 char_u *newfile;
1234 char_u *outfile;
1235{
1236 int err = FALSE;
1237
1238 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1239 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1240 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1241 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1242 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1243 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1244 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1245}
1246
1247 void
1248eval_patch(origfile, difffile, outfile)
1249 char_u *origfile;
1250 char_u *difffile;
1251 char_u *outfile;
1252{
1253 int err;
1254
1255 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1256 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1257 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1258 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262}
1263# endif
1264
1265/*
1266 * Top level evaluation function, returning a boolean.
1267 * Sets "error" to TRUE if there was an error.
1268 * Return TRUE or FALSE.
1269 */
1270 int
1271eval_to_bool(arg, error, nextcmd, skip)
1272 char_u *arg;
1273 int *error;
1274 char_u **nextcmd;
1275 int skip; /* only parse, don't execute */
1276{
Bram Moolenaar33570922005-01-25 22:26:29 +00001277 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 int retval = FALSE;
1279
1280 if (skip)
1281 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001282 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 {
1286 *error = FALSE;
1287 if (!skip)
1288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001289 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001290 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 }
1293 if (skip)
1294 --emsg_skip;
1295
1296 return retval;
1297}
1298
1299/*
1300 * Top level evaluation function, returning a string. If "skip" is TRUE,
1301 * only parsing to "nextcmd" is done, without reporting errors. Return
1302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1303 */
1304 char_u *
1305eval_to_string_skip(arg, nextcmd, skip)
1306 char_u *arg;
1307 char_u **nextcmd;
1308 int skip; /* only parse, don't execute */
1309{
Bram Moolenaar33570922005-01-25 22:26:29 +00001310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 char_u *retval;
1312
1313 if (skip)
1314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 retval = NULL;
1317 else
1318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001319 retval = vim_strsave(get_tv_string(&tv));
1320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322 if (skip)
1323 --emsg_skip;
1324
1325 return retval;
1326}
1327
1328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329 * Skip over an expression at "*pp".
1330 * Return FAIL for an error, OK otherwise.
1331 */
1332 int
1333skip_expr(pp)
1334 char_u **pp;
1335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337
1338 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001339 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001340}
1341
1342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001344 * When "convert" is TRUE convert a List into a sequence of lines and convert
1345 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Return pointer to allocated memory, or NULL for failure.
1347 */
1348 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *arg;
1351 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353{
Bram Moolenaar33570922005-01-25 22:26:29 +00001354 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001357#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001361 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 retval = NULL;
1363 else
1364 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 {
1367 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001368 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001369 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001370 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001371 if (tv.vval.v_list->lv_len > 0)
1372 ga_append(&ga, NL);
1373 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 ga_append(&ga, NUL);
1375 retval = (char_u *)ga.ga_data;
1376 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001377#ifdef FEAT_FLOAT
1378 else if (convert && tv.v_type == VAR_FLOAT)
1379 {
1380 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1381 retval = vim_strsave(numbuf);
1382 }
1383#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001384 else
1385 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 * Call eval_to_string() without using current local variables and using
1394 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *arg;
1399 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401{
1402 char_u *retval;
1403 void *save_funccalp;
1404
1405 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001406 if (use_sandbox)
1407 ++sandbox;
1408 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001409 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001410 if (use_sandbox)
1411 --sandbox;
1412 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 restore_funccal(save_funccalp);
1414 return retval;
1415}
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417/*
1418 * Top level evaluation function, returning a number.
1419 * Evaluates "expr" silently.
1420 * Returns -1 for an error.
1421 */
1422 int
1423eval_to_number(expr)
1424 char_u *expr;
1425{
Bram Moolenaar33570922005-01-25 22:26:29 +00001426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001428 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 ++emsg_off;
1431
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = -1;
1434 else
1435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001436 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439 --emsg_off;
1440
1441 return retval;
1442}
1443
Bram Moolenaara40058a2005-07-11 22:42:07 +00001444/*
1445 * Prepare v: variable "idx" to be used.
1446 * Save the current typeval in "save_tv".
1447 * When not used yet add the variable to the v: hashtable.
1448 */
1449 static void
1450prepare_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 *save_tv = vimvars[idx].vv_tv;
1455 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1456 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1457}
1458
1459/*
1460 * Restore v: variable "idx" to typeval "save_tv".
1461 * When no longer defined, remove the variable from the v: hashtable.
1462 */
1463 static void
1464restore_vimvar(idx, save_tv)
1465 int idx;
1466 typval_T *save_tv;
1467{
1468 hashitem_T *hi;
1469
Bram Moolenaara40058a2005-07-11 22:42:07 +00001470 vimvars[idx].vv_tv = *save_tv;
1471 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1472 {
1473 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1474 if (HASHITEM_EMPTY(hi))
1475 EMSG2(_(e_intern2), "restore_vimvar()");
1476 else
1477 hash_remove(&vimvarht, hi);
1478 }
1479}
1480
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001481#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001482/*
1483 * Evaluate an expression to a list with suggestions.
1484 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001485 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001486 */
1487 list_T *
1488eval_spell_expr(badword, expr)
1489 char_u *badword;
1490 char_u *expr;
1491{
1492 typval_T save_val;
1493 typval_T rettv;
1494 list_T *list = NULL;
1495 char_u *p = skipwhite(expr);
1496
1497 /* Set "v:val" to the bad word. */
1498 prepare_vimvar(VV_VAL, &save_val);
1499 vimvars[VV_VAL].vv_type = VAR_STRING;
1500 vimvars[VV_VAL].vv_str = badword;
1501 if (p_verbose == 0)
1502 ++emsg_off;
1503
1504 if (eval1(&p, &rettv, TRUE) == OK)
1505 {
1506 if (rettv.v_type != VAR_LIST)
1507 clear_tv(&rettv);
1508 else
1509 list = rettv.vval.v_list;
1510 }
1511
1512 if (p_verbose == 0)
1513 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001514 restore_vimvar(VV_VAL, &save_val);
1515
1516 return list;
1517}
1518
1519/*
1520 * "list" is supposed to contain two items: a word and a number. Return the
1521 * word in "pp" and the number as the return value.
1522 * Return -1 if anything isn't right.
1523 * Used to get the good word and score from the eval_spell_expr() result.
1524 */
1525 int
1526get_spellword(list, pp)
1527 list_T *list;
1528 char_u **pp;
1529{
1530 listitem_T *li;
1531
1532 li = list->lv_first;
1533 if (li == NULL)
1534 return -1;
1535 *pp = get_tv_string(&li->li_tv);
1536
1537 li = li->li_next;
1538 if (li == NULL)
1539 return -1;
1540 return get_tv_number(&li->li_tv);
1541}
1542#endif
1543
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 * Top level evaluation function.
1546 * Returns an allocated typval_T with the result.
1547 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 */
1549 typval_T *
1550eval_expr(arg, nextcmd)
1551 char_u *arg;
1552 char_u **nextcmd;
1553{
1554 typval_T *tv;
1555
1556 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 {
1559 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001560 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001561 }
1562
1563 return tv;
1564}
1565
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 * Uses argv[argc] for the function arguments. Only Number and String
1570 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001571 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001573 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001574call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *func;
1576 int argc;
1577 char_u **argv;
1578 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001579 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
Bram Moolenaar33570922005-01-25 22:26:29 +00001582 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 long n;
1584 int len;
1585 int i;
1586 int doesrange;
1587 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001588 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001590 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001592 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 for (i = 0; i < argc; i++)
1595 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001596 /* Pass a NULL or empty argument as an empty string */
1597 if (argv[i] == NULL || *argv[i] == NUL)
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001601 continue;
1602 }
1603
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001604 if (str_arg_only)
1605 len = 0;
1606 else
1607 /* Recognize a number argument, the others must be strings. */
1608 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (len != 0 && len == (int)STRLEN(argv[i]))
1610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001611 argvars[i].v_type = VAR_NUMBER;
1612 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001616 argvars[i].v_type = VAR_STRING;
1617 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 }
1620
1621 if (safe)
1622 {
1623 save_funccalp = save_funccal();
1624 ++sandbox;
1625 }
1626
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1628 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (safe)
1632 {
1633 --sandbox;
1634 restore_funccal(save_funccalp);
1635 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 vim_free(argvars);
1637
1638 if (ret == FAIL)
1639 clear_tv(rettv);
1640
1641 return ret;
1642}
1643
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001644/*
1645 * Call vimL function "func" and return the result as a number.
1646 * Returns -1 when calling the function fails.
1647 * Uses argv[argc] for the function arguments.
1648 */
1649 long
1650call_func_retnr(func, argc, argv, safe)
1651 char_u *func;
1652 int argc;
1653 char_u **argv;
1654 int safe; /* use the sandbox */
1655{
1656 typval_T rettv;
1657 long retval;
1658
1659 /* All arguments are passed as strings, no conversion to number. */
1660 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1661 return -1;
1662
1663 retval = get_tv_number_chk(&rettv, NULL);
1664 clear_tv(&rettv);
1665 return retval;
1666}
1667
1668#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1669 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1670
Bram Moolenaar4f688582007-07-24 12:34:30 +00001671# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 * Call vimL function "func" and return the result as a string.
1674 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 * Uses argv[argc] for the function arguments.
1676 */
1677 void *
1678call_func_retstr(func, argc, argv, safe)
1679 char_u *func;
1680 int argc;
1681 char_u **argv;
1682 int safe; /* use the sandbox */
1683{
1684 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001687 /* All arguments are passed as strings, no conversion to number. */
1688 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return NULL;
1690
1691 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 return retval;
1694}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001695# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001697/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001698 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001700 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 */
1702 void *
1703call_func_retlist(func, argc, argv, safe)
1704 char_u *func;
1705 int argc;
1706 char_u **argv;
1707 int safe; /* use the sandbox */
1708{
1709 typval_T rettv;
1710
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 return NULL;
1714
1715 if (rettv.v_type != VAR_LIST)
1716 {
1717 clear_tv(&rettv);
1718 return NULL;
1719 }
1720
1721 return rettv.vval.v_list;
1722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724
1725/*
1726 * Save the current function call pointer, and set it to NULL.
1727 * Used when executing autocommands and for ":source".
1728 */
1729 void *
1730save_funccal()
1731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 current_funccal = NULL;
1735 return (void *)fc;
1736}
1737
1738 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739restore_funccal(vfc)
1740 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001742 funccall_T *fc = (funccall_T *)vfc;
1743
1744 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745}
1746
Bram Moolenaar05159a02005-02-26 23:04:13 +00001747#if defined(FEAT_PROFILE) || defined(PROTO)
1748/*
1749 * Prepare profiling for entering a child or something else that is not
1750 * counted for the script/function itself.
1751 * Should always be called in pair with prof_child_exit().
1752 */
1753 void
1754prof_child_enter(tm)
1755 proftime_T *tm; /* place to store waittime */
1756{
1757 funccall_T *fc = current_funccal;
1758
1759 if (fc != NULL && fc->func->uf_profiling)
1760 profile_start(&fc->prof_child);
1761 script_prof_save(tm);
1762}
1763
1764/*
1765 * Take care of time spent in a child.
1766 * Should always be called after prof_child_enter().
1767 */
1768 void
1769prof_child_exit(tm)
1770 proftime_T *tm; /* where waittime was stored */
1771{
1772 funccall_T *fc = current_funccal;
1773
1774 if (fc != NULL && fc->func->uf_profiling)
1775 {
1776 profile_end(&fc->prof_child);
1777 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1778 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1779 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1780 }
1781 script_prof_restore(tm);
1782}
1783#endif
1784
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_FOLDING
1787/*
1788 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1789 * it in "*cp". Doesn't give error messages.
1790 */
1791 int
1792eval_foldexpr(arg, cp)
1793 char_u *arg;
1794 int *cp;
1795{
Bram Moolenaar33570922005-01-25 22:26:29 +00001796 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 int retval;
1798 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001799 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1800 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001803 if (use_sandbox)
1804 ++sandbox;
1805 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 retval = 0;
1809 else
1810 {
1811 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 if (tv.v_type == VAR_NUMBER)
1813 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001814 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a string, check if there is a non-digit before
1819 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001820 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (!VIM_ISDIGIT(*s) && *s != '-')
1822 *cp = *s++;
1823 retval = atol((char *)s);
1824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 }
1827 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001828 if (use_sandbox)
1829 --sandbox;
1830 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 return retval;
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let" list all variable values
1838 * ":let var1 var2" list variable values
1839 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001840 * ":let var += expr" assignment command.
1841 * ":let var -= expr" assignment command.
1842 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 */
1845 void
1846ex_let(eap)
1847 exarg_T *eap;
1848{
1849 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 int var_count = 0;
1854 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001857 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaardb552d602006-03-23 22:59:57 +00001859 argend = skip_var_list(arg, &var_count, &semicolon);
1860 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001862 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1863 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001864 expr = skipwhite(argend);
1865 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1866 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001868 /*
1869 * ":let" without "=": list variables
1870 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001871 if (*arg == '[')
1872 EMSG(_(e_invarg));
1873 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001875 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001879 list_glob_vars(&first);
1880 list_buf_vars(&first);
1881 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001882#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001883 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001884#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001885 list_script_vars(&first);
1886 list_func_vars(&first);
1887 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 eap->nextcmd = check_nextcmd(arg);
1890 }
1891 else
1892 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op[0] = '=';
1894 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001895 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001897 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1898 op[0] = *expr; /* +=, -= or .= */
1899 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001901 else
1902 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (eap->skip)
1908 {
1909 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 --emsg_skip;
1912 }
1913 else if (i != FAIL)
1914 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
1919 }
1920}
1921
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922/*
1923 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1924 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 * When "nextchars" is not NULL it points to a string with characters that
1926 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1927 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 * Returns OK or FAIL;
1929 */
1930 static int
1931ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1932 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001934 int copy; /* copy values from "tv", don't move */
1935 int semicolon; /* from skip_var_list() */
1936 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938{
1939 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001942 listitem_T *item;
1943 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944
1945 if (*arg != '[')
1946 {
1947 /*
1948 * ":let var = expr" or ":for var in list"
1949 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001950 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 return OK;
1953 }
1954
1955 /*
1956 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1957 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 {
1960 EMSG(_(e_listreq));
1961 return FAIL;
1962 }
1963
1964 i = list_len(l);
1965 if (semicolon == 0 && var_count < i)
1966 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001967 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 return FAIL;
1969 }
1970 if (var_count - semicolon > i)
1971 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001972 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 return FAIL;
1974 }
1975
1976 item = l->lv_first;
1977 while (*arg != ']')
1978 {
1979 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 item = item->li_next;
1982 if (arg == NULL)
1983 return FAIL;
1984
1985 arg = skipwhite(arg);
1986 if (*arg == ';')
1987 {
1988 /* Put the rest of the list (may be empty) in the var after ';'.
1989 * Create a new list for this. */
1990 l = list_alloc();
1991 if (l == NULL)
1992 return FAIL;
1993 while (item != NULL)
1994 {
1995 list_append_tv(l, &item->li_tv);
1996 item = item->li_next;
1997 }
1998
1999 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002000 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 ltv.vval.v_list = l;
2002 l->lv_refcount = 1;
2003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002004 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2005 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 clear_tv(&ltv);
2007 if (arg == NULL)
2008 return FAIL;
2009 break;
2010 }
2011 else if (*arg != ',' && *arg != ']')
2012 {
2013 EMSG2(_(e_intern2), "ex_let_vars()");
2014 return FAIL;
2015 }
2016 }
2017
2018 return OK;
2019}
2020
2021/*
2022 * Skip over assignable variable "var" or list of variables "[var, var]".
2023 * Used for ":let varvar = expr" and ":for varvar in expr".
2024 * For "[var, var]" increment "*var_count" for each variable.
2025 * for "[var, var; var]" set "semicolon".
2026 * Return NULL for an error.
2027 */
2028 static char_u *
2029skip_var_list(arg, var_count, semicolon)
2030 char_u *arg;
2031 int *var_count;
2032 int *semicolon;
2033{
2034 char_u *p, *s;
2035
2036 if (*arg == '[')
2037 {
2038 /* "[var, var]": find the matching ']'. */
2039 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002040 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 {
2042 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2043 s = skip_var_one(p);
2044 if (s == p)
2045 {
2046 EMSG2(_(e_invarg2), p);
2047 return NULL;
2048 }
2049 ++*var_count;
2050
2051 p = skipwhite(s);
2052 if (*p == ']')
2053 break;
2054 else if (*p == ';')
2055 {
2056 if (*semicolon == 1)
2057 {
2058 EMSG(_("Double ; in list of variables"));
2059 return NULL;
2060 }
2061 *semicolon = 1;
2062 }
2063 else if (*p != ',')
2064 {
2065 EMSG2(_(e_invarg2), p);
2066 return NULL;
2067 }
2068 }
2069 return p + 1;
2070 }
2071 else
2072 return skip_var_one(arg);
2073}
2074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002077 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002078 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079 static char_u *
2080skip_var_one(arg)
2081 char_u *arg;
2082{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002083 if (*arg == '@' && arg[1] != NUL)
2084 return arg + 2;
2085 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2086 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087}
2088
Bram Moolenaara7043832005-01-21 11:56:39 +00002089/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002090 * List variables for hashtab "ht" with prefix "prefix".
2091 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002094list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099{
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 hashitem_T *hi;
2101 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 int todo;
2103
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002104 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2106 {
2107 if (!HASHITEM_EMPTY(hi))
2108 {
2109 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002110 di = HI2DI(hi);
2111 if (empty || di->di_tv.v_type != VAR_STRING
2112 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002114 }
2115 }
2116}
2117
2118/*
2119 * List global variables.
2120 */
2121 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122list_glob_vars(first)
2123 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002124{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002126}
2127
2128/*
2129 * List buffer variables.
2130 */
2131 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132list_buf_vars(first)
2133 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002134{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135 char_u numbuf[NUMBUFLEN];
2136
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002139
2140 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2142 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002143}
2144
2145/*
2146 * List window variables.
2147 */
2148 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149list_win_vars(first)
2150 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002151{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154}
2155
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002156#ifdef FEAT_WINDOWS
2157/*
2158 * List tab page variables.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_tab_vars(first)
2162 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002164 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002166}
2167#endif
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
2170 * List Vim variables.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_vim_vars(first)
2174 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177}
2178
2179/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180 * List script-local variables, if there is a script.
2181 */
2182 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183list_script_vars(first)
2184 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185{
2186 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2188 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002189}
2190
2191/*
2192 * List function variables, if there is a function.
2193 */
2194 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195list_func_vars(first)
2196 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002197{
2198 if (current_funccal != NULL)
2199 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002201}
2202
2203/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204 * List variables in "arg".
2205 */
2206 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 exarg_T *eap;
2209 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
2212 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 char_u *name_start;
2216 char_u *arg_subsc;
2217 char_u *tofree;
2218 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219
2220 while (!ends_excmd(*arg) && !got_int)
2221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002224 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2226 {
2227 emsg_severe = TRUE;
2228 EMSG(_(e_trailing));
2229 break;
2230 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* get_name_len() takes care of expanding curly braces */
2235 name_start = name = arg;
2236 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2237 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 /* This is mainly to keep test 49 working: when expanding
2240 * curly braces fails overrule the exception error message. */
2241 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 emsg_severe = TRUE;
2244 EMSG2(_(e_invarg2), arg);
2245 break;
2246 }
2247 error = TRUE;
2248 }
2249 else
2250 {
2251 if (tofree != NULL)
2252 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002253 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 else
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 /* handle d.key, l[idx], f(expr) */
2258 arg_subsc = arg;
2259 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002260 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 case 'g': list_glob_vars(first); break;
2268 case 'b': list_buf_vars(first); break;
2269 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 case 'v': list_vim_vars(first); break;
2274 case 's': list_script_vars(first); break;
2275 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 default:
2277 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 }
2280 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 {
2282 char_u numbuf[NUMBUFLEN];
2283 char_u *tf;
2284 int c;
2285 char_u *s;
2286
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002287 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 c = *arg;
2289 *arg = NUL;
2290 list_one_var_a((char_u *)"",
2291 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002292 tv.v_type,
2293 s == NULL ? (char_u *)"" : s,
2294 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 *arg = c;
2296 vim_free(tf);
2297 }
2298 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
2301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305
2306 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308
2309 return arg;
2310}
2311
2312/*
2313 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2314 * Returns a pointer to the char just after the var name.
2315 * Returns NULL if there is an error.
2316 */
2317 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002320 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002322 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324{
2325 int c1;
2326 char_u *name;
2327 char_u *p;
2328 char_u *arg_end = NULL;
2329 int len;
2330 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332
2333 /*
2334 * ":let $VAR = expr": Set environment variable.
2335 */
2336 if (*arg == '$')
2337 {
2338 /* Find the end of the name. */
2339 ++arg;
2340 name = arg;
2341 len = get_env_len(&arg);
2342 if (len == 0)
2343 EMSG2(_(e_invarg2), name - 1);
2344 else
2345 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002346 if (op != NULL && (*op == '+' || *op == '-'))
2347 EMSG2(_(e_letwrong), op);
2348 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002349 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002350 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002351 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002352 {
2353 c1 = name[len];
2354 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 p = get_tv_string_chk(tv);
2356 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 {
2358 int mustfree = FALSE;
2359 char_u *s = vim_getenv(name, &mustfree);
2360
2361 if (s != NULL)
2362 {
2363 p = tofree = concat_str(s, p);
2364 if (mustfree)
2365 vim_free(s);
2366 }
2367 }
2368 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002370 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 if (STRICMP(name, "HOME") == 0)
2372 init_homedir();
2373 else if (didset_vim && STRICMP(name, "VIM") == 0)
2374 didset_vim = FALSE;
2375 else if (didset_vimruntime
2376 && STRICMP(name, "VIMRUNTIME") == 0)
2377 didset_vimruntime = FALSE;
2378 arg_end = arg;
2379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
2384 }
2385
2386 /*
2387 * ":let &option = expr": Set option value.
2388 * ":let &l:option = expr": Set local option value.
2389 * ":let &g:option = expr": Set global option value.
2390 */
2391 else if (*arg == '&')
2392 {
2393 /* Find the end of the name. */
2394 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002395 if (p == NULL || (endchars != NULL
2396 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 EMSG(_(e_letunexp));
2398 else
2399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 long n;
2401 int opt_type;
2402 long numval;
2403 char_u *stringval = NULL;
2404 char_u *s;
2405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 c1 = *p;
2407 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408
2409 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 s = get_tv_string_chk(tv); /* != NULL if number or string */
2411 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 {
2413 opt_type = get_option_value(arg, &numval,
2414 &stringval, opt_flags);
2415 if ((opt_type == 1 && *op == '.')
2416 || (opt_type == 0 && *op != '.'))
2417 EMSG2(_(e_letwrong), op);
2418 else
2419 {
2420 if (opt_type == 1) /* number */
2421 {
2422 if (*op == '+')
2423 n = numval + n;
2424 else
2425 n = numval - n;
2426 }
2427 else if (opt_type == 0 && stringval != NULL) /* string */
2428 {
2429 s = concat_str(stringval, s);
2430 vim_free(stringval);
2431 stringval = s;
2432 }
2433 }
2434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435 if (s != NULL)
2436 {
2437 set_option_value(arg, n, s, opt_flags);
2438 arg_end = p;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444
2445 /*
2446 * ":let @r = expr": Set register contents.
2447 */
2448 else if (*arg == '@')
2449 {
2450 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 if (op != NULL && (*op == '+' || *op == '-'))
2452 EMSG2(_(e_letwrong), op);
2453 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002454 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 EMSG(_(e_letunexp));
2456 else
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 char_u *s;
2460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 p = get_tv_string_chk(tv);
2462 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002464 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 if (s != NULL)
2466 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468 vim_free(s);
2469 }
2470 }
2471 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002474 arg_end = arg + 1;
2475 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002476 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002484 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002486 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002488 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 arg_end = p;
2497 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 }
2501
2502 else
2503 EMSG2(_(e_invarg2), arg);
2504
2505 return arg_end;
2506}
2507
2508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002509 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2510 */
2511 static int
2512check_changedtick(arg)
2513 char_u *arg;
2514{
2515 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2516 {
2517 EMSG2(_(e_readonlyvar), arg);
2518 return TRUE;
2519 }
2520 return FALSE;
2521}
2522
2523/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * Get an lval: variable, Dict item or List item that can be assigned a value
2525 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2526 * "name.key", "name.key[expr]" etc.
2527 * Indexing only works if "name" is an existing List or Dictionary.
2528 * "name" points to the start of the name.
2529 * If "rettv" is not NULL it points to the value to be assigned.
2530 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2531 * wrong; must end in space or cmd separator.
2532 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533 * flags:
2534 * GLV_QUIET: do not give error messages
2535 * GLV_NO_AUTOLOAD: do not use script autoloading
2536 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002538 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 */
2541 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 typval_T *rettv;
2545 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 int unlet;
2547 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 char_u *expr_start, *expr_end;
2553 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002554 dictitem_T *v;
2555 typval_T var1;
2556 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002559 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002562 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566
2567 if (skip)
2568 {
2569 /* When skipping just find the end of the name. */
2570 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002571 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 }
2573
2574 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002575 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 if (expr_start != NULL)
2577 {
2578 /* Don't expand the name when we already know there is an error. */
2579 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2580 && *p != '[' && *p != '.')
2581 {
2582 EMSG(_(e_trailing));
2583 return NULL;
2584 }
2585
2586 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2587 if (lp->ll_exp_name == NULL)
2588 {
2589 /* Report an invalid expression in braces, unless the
2590 * expression evaluation has been cancelled due to an
2591 * aborting error, an interrupt, or an exception. */
2592 if (!aborting() && !quiet)
2593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002594 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 EMSG2(_(e_invarg2), name);
2596 return NULL;
2597 }
2598 }
2599 lp->ll_name = lp->ll_exp_name;
2600 }
2601 else
2602 lp->ll_name = name;
2603
2604 /* Without [idx] or .key we are done. */
2605 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2606 return p;
2607
2608 cc = *p;
2609 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (v == NULL && !quiet)
2612 EMSG2(_(e_undefvar), lp->ll_name);
2613 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 if (v == NULL)
2615 return NULL;
2616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 /*
2618 * Loop until no more [idx] or .key is following.
2619 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2624 && !(lp->ll_tv->v_type == VAR_DICT
2625 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 if (!quiet)
2628 EMSG(_("E689: Can only index a List or Dictionary"));
2629 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002630 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 if (!quiet)
2634 EMSG(_("E708: [:] must come last"));
2635 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 len = -1;
2639 if (*p == '.')
2640 {
2641 key = p + 1;
2642 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2643 ;
2644 if (len == 0)
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (!quiet)
2647 EMSG(_(e_emptykey));
2648 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650 p = key + len;
2651 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 else
2653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 if (*p == ':')
2657 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 else
2659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 empty1 = FALSE;
2661 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663 if (get_tv_string_chk(&var1) == NULL)
2664 {
2665 /* not a number or string */
2666 clear_tv(&var1);
2667 return NULL;
2668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670
2671 /* Optionally get the second index [ :expr]. */
2672 if (*p == ':')
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002677 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002681 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (rettv != NULL && (rettv->v_type != VAR_LIST
2683 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (!empty1)
2688 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691 p = skipwhite(p + 1);
2692 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
2695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (!empty1)
2700 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var2) == NULL)
2704 {
2705 /* not a number or string */
2706 if (!empty1)
2707 clear_tv(&var1);
2708 clear_tv(&var2);
2709 return NULL;
2710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002716
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727
2728 /* Skip to past ']'. */
2729 ++p;
2730 }
2731
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 {
2734 if (len == -1)
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (*key == NUL)
2739 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 if (!quiet)
2741 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
2745 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 lp->ll_list = NULL;
2747 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002748 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002750 /* When assigning to a scope dictionary check that a function and
2751 * variable name is valid (only variable name unless it is l: or
2752 * g: dictionary). Disallow overwriting a builtin function. */
2753 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002755 int prevval;
2756 int wrong;
2757
2758 if (len != -1)
2759 {
2760 prevval = key[len];
2761 key[len] = NUL;
2762 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002763 else
2764 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2766 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002767 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002768 || !valid_varname(key);
2769 if (len != -1)
2770 key[len] = prevval;
2771 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002772 return NULL;
2773 }
2774
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777 /* Can't add "v:" variable. */
2778 if (lp->ll_dict == &vimvardict)
2779 {
2780 EMSG2(_(e_illvar), name);
2781 return NULL;
2782 }
2783
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002788 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (len == -1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 if (len == -1)
2798 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 p = NULL;
2801 break;
2802 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 /* existing variable, need to check if it can be changed */
2804 else if (var_check_ro(lp->ll_di->di_flags, name))
2805 return NULL;
2806
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 if (len == -1)
2808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 else
2812 {
2813 /*
2814 * Get the number and item for the only or first index of the List.
2815 */
2816 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 else
2819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002820 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
2822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_list = lp->ll_tv->vval.v_list;
2825 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2826 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002828 if (lp->ll_n1 < 0)
2829 {
2830 lp->ll_n1 = 0;
2831 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2832 }
2833 }
2834 if (lp->ll_li == NULL)
2835 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842
2843 /*
2844 * May need to find the item or absolute index for the second
2845 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 * When no index given: "lp->ll_empty2" is TRUE.
2847 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002857 {
2858 if (!quiet)
2859 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 }
2864
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2866 if (lp->ll_n1 < 0)
2867 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2868 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 {
2870 if (!quiet)
2871 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002873 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
2875
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 }
2879
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return p;
2881}
2882
2883/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 */
2886 static void
2887clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889{
2890 vim_free(lp->ll_exp_name);
2891 vim_free(lp->ll_newkey);
2892}
2893
2894/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002895 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 */
2899 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002903 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002905 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906{
2907 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 listitem_T *ri;
2909 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910
2911 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 cc = *endp;
2916 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917 if (op != NULL && *op != '=')
2918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002919 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002921 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002922 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002923 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 {
2925 if (tv_op(&tv, rettv, op) == OK)
2926 set_var(lp->ll_name, &tv, FALSE);
2927 clear_tv(&tv);
2928 }
2929 }
2930 else
2931 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002933 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 else if (tv_check_lock(lp->ll_newkey == NULL
2936 ? lp->ll_tv->v_lock
2937 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2938 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 else if (lp->ll_range)
2940 {
2941 /*
2942 * Assign the List values to the list items.
2943 */
2944 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2948 else
2949 {
2950 clear_tv(&lp->ll_li->li_tv);
2951 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2952 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 ri = ri->li_next;
2954 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2955 break;
2956 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002959 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 ri = NULL;
2962 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002963 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 lp->ll_li = lp->ll_li->li_next;
2966 ++lp->ll_n1;
2967 }
2968 if (ri != NULL)
2969 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 else if (lp->ll_empty2
2971 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 : lp->ll_n1 != lp->ll_n2)
2973 EMSG(_("E711: List value has not enough items"));
2974 }
2975 else
2976 {
2977 /*
2978 * Assign to a List or Dictionary item.
2979 */
2980 if (lp->ll_newkey != NULL)
2981 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
2984 EMSG2(_(e_letwrong), op);
2985 return;
2986 }
2987
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 if (di == NULL)
2991 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002992 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2993 {
2994 vim_free(di);
2995 return;
2996 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else if (op != NULL && *op != '=')
3000 {
3001 tv_op(lp->ll_tv, rettv, op);
3002 return;
3003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003006
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /*
3008 * Assign the value to the variable or list item.
3009 */
3010 if (copy)
3011 copy_tv(rettv, lp->ll_tv);
3012 else
3013 {
3014 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003015 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003017 }
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019}
3020
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003021/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3023 * Returns OK or FAIL.
3024 */
3025 static int
3026tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003027 typval_T *tv1;
3028 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003029 char_u *op;
3030{
3031 long n;
3032 char_u numbuf[NUMBUFLEN];
3033 char_u *s;
3034
3035 /* Can't do anything with a Funcref or a Dict on the right. */
3036 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3037 {
3038 switch (tv1->v_type)
3039 {
3040 case VAR_DICT:
3041 case VAR_FUNC:
3042 break;
3043
3044 case VAR_LIST:
3045 if (*op != '+' || tv2->v_type != VAR_LIST)
3046 break;
3047 /* List += List */
3048 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3049 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3050 return OK;
3051
3052 case VAR_NUMBER:
3053 case VAR_STRING:
3054 if (tv2->v_type == VAR_LIST)
3055 break;
3056 if (*op == '+' || *op == '-')
3057 {
3058 /* nr += nr or nr -= nr*/
3059 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003060#ifdef FEAT_FLOAT
3061 if (tv2->v_type == VAR_FLOAT)
3062 {
3063 float_T f = n;
3064
3065 if (*op == '+')
3066 f += tv2->vval.v_float;
3067 else
3068 f -= tv2->vval.v_float;
3069 clear_tv(tv1);
3070 tv1->v_type = VAR_FLOAT;
3071 tv1->vval.v_float = f;
3072 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003074#endif
3075 {
3076 if (*op == '+')
3077 n += get_tv_number(tv2);
3078 else
3079 n -= get_tv_number(tv2);
3080 clear_tv(tv1);
3081 tv1->v_type = VAR_NUMBER;
3082 tv1->vval.v_number = n;
3083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 }
3085 else
3086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003087 if (tv2->v_type == VAR_FLOAT)
3088 break;
3089
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 /* str .= str */
3091 s = get_tv_string(tv1);
3092 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_STRING;
3095 tv1->vval.v_string = s;
3096 }
3097 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098
3099#ifdef FEAT_FLOAT
3100 case VAR_FLOAT:
3101 {
3102 float_T f;
3103
3104 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3105 && tv2->v_type != VAR_NUMBER
3106 && tv2->v_type != VAR_STRING))
3107 break;
3108 if (tv2->v_type == VAR_FLOAT)
3109 f = tv2->vval.v_float;
3110 else
3111 f = get_tv_number(tv2);
3112 if (*op == '+')
3113 tv1->vval.v_float += f;
3114 else
3115 tv1->vval.v_float -= f;
3116 }
3117 return OK;
3118#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003119 }
3120 }
3121
3122 EMSG2(_(e_letwrong), op);
3123 return FAIL;
3124}
3125
3126/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127 * Add a watcher to a list.
3128 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003129 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003131 list_T *l;
3132 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003133{
3134 lw->lw_next = l->lv_watch;
3135 l->lv_watch = lw;
3136}
3137
3138/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003139 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140 * No warning when it isn't found...
3141 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003142 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 list_T *l;
3145 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003146{
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148
3149 lwp = &l->lv_watch;
3150 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3151 {
3152 if (lw == lwrem)
3153 {
3154 *lwp = lw->lw_next;
3155 break;
3156 }
3157 lwp = &lw->lw_next;
3158 }
3159}
3160
3161/*
3162 * Just before removing an item from a list: advance watchers to the next
3163 * item.
3164 */
3165 static void
3166list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 list_T *l;
3168 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171
3172 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3173 if (lw->lw_item == item)
3174 lw->lw_item = item->li_next;
3175}
3176
3177/*
3178 * Evaluate the expression used in a ":for var in expr" command.
3179 * "arg" points to "var".
3180 * Set "*errp" to TRUE for an error, FALSE otherwise;
3181 * Return a pointer that holds the info. Null when there is an error.
3182 */
3183 void *
3184eval_for_line(arg, errp, nextcmdp, skip)
3185 char_u *arg;
3186 int *errp;
3187 char_u **nextcmdp;
3188 int skip;
3189{
Bram Moolenaar33570922005-01-25 22:26:29 +00003190 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003192 typval_T tv;
3193 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194
3195 *errp = TRUE; /* default: there is an error */
3196
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 if (fi == NULL)
3199 return NULL;
3200
3201 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3202 if (expr == NULL)
3203 return fi;
3204
3205 expr = skipwhite(expr);
3206 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3207 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003208 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 return fi;
3210 }
3211
3212 if (skip)
3213 ++emsg_skip;
3214 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3215 {
3216 *errp = FALSE;
3217 if (!skip)
3218 {
3219 l = tv.vval.v_list;
3220 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003221 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003223 clear_tv(&tv);
3224 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 else
3226 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003227 /* No need to increment the refcount, it's already set for the
3228 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 fi->fi_list = l;
3230 list_add_watch(l, &fi->fi_lw);
3231 fi->fi_lw.lw_item = l->lv_first;
3232 }
3233 }
3234 }
3235 if (skip)
3236 --emsg_skip;
3237
3238 return fi;
3239}
3240
3241/*
3242 * Use the first item in a ":for" list. Advance to the next.
3243 * Assign the values to the variable (list). "arg" points to the first one.
3244 * Return TRUE when a valid item was found, FALSE when at end of list or
3245 * something wrong.
3246 */
3247 int
3248next_for_item(fi_void, arg)
3249 void *fi_void;
3250 char_u *arg;
3251{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003252 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 item = fi->fi_lw.lw_item;
3257 if (item == NULL)
3258 result = FALSE;
3259 else
3260 {
3261 fi->fi_lw.lw_item = item->li_next;
3262 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3263 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3264 }
3265 return result;
3266}
3267
3268/*
3269 * Free the structure used to store info used by ":for".
3270 */
3271 void
3272free_for_info(fi_void)
3273 void *fi_void;
3274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003277 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 list_unref(fi->fi_list);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 vim_free(fi);
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3286
3287 void
3288set_context_for_expression(xp, arg, cmdidx)
3289 expand_T *xp;
3290 char_u *arg;
3291 cmdidx_T cmdidx;
3292{
3293 int got_eq = FALSE;
3294 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (cmdidx == CMD_let)
3298 {
3299 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003303 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 {
3305 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 if (vim_iswhite(*p))
3308 break;
3309 }
3310 return;
3311 }
3312 }
3313 else
3314 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3315 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 while ((xp->xp_pattern = vim_strpbrk(arg,
3317 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3318 {
3319 c = *xp->xp_pattern;
3320 if (c == '&')
3321 {
3322 c = xp->xp_pattern[1];
3323 if (c == '&')
3324 {
3325 ++xp->xp_pattern;
3326 xp->xp_context = cmdidx != CMD_let || got_eq
3327 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3328 }
3329 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003332 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3333 xp->xp_pattern += 2;
3334
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 else if (c == '$')
3338 {
3339 /* environment variable */
3340 xp->xp_context = EXPAND_ENV_VARS;
3341 }
3342 else if (c == '=')
3343 {
3344 got_eq = TRUE;
3345 xp->xp_context = EXPAND_EXPRESSION;
3346 }
3347 else if (c == '<'
3348 && xp->xp_context == EXPAND_FUNCTIONS
3349 && vim_strchr(xp->xp_pattern, '(') == NULL)
3350 {
3351 /* Function name can start with "<SNR>" */
3352 break;
3353 }
3354 else if (cmdidx != CMD_let || got_eq)
3355 {
3356 if (c == '"') /* string */
3357 {
3358 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3359 if (c == '\\' && xp->xp_pattern[1] != NUL)
3360 ++xp->xp_pattern;
3361 xp->xp_context = EXPAND_NOTHING;
3362 }
3363 else if (c == '\'') /* literal string */
3364 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3367 /* skip */ ;
3368 xp->xp_context = EXPAND_NOTHING;
3369 }
3370 else if (c == '|')
3371 {
3372 if (xp->xp_pattern[1] == '|')
3373 {
3374 ++xp->xp_pattern;
3375 xp->xp_context = EXPAND_EXPRESSION;
3376 }
3377 else
3378 xp->xp_context = EXPAND_COMMANDS;
3379 }
3380 else
3381 xp->xp_context = EXPAND_EXPRESSION;
3382 }
3383 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 /* Doesn't look like something valid, expand as an expression
3385 * anyway. */
3386 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 arg = xp->xp_pattern;
3388 if (*arg != NUL)
3389 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3390 /* skip */ ;
3391 }
3392 xp->xp_pattern = arg;
3393}
3394
3395#endif /* FEAT_CMDL_COMPL */
3396
3397/*
3398 * ":1,25call func(arg1, arg2)" function call.
3399 */
3400 void
3401ex_call(eap)
3402 exarg_T *eap;
3403{
3404 char_u *arg = eap->arg;
3405 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003407 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003409 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 linenr_T lnum;
3411 int doesrange;
3412 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003413 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 if (eap->skip)
3416 {
3417 /* trans_function_name() doesn't work well when skipping, use eval0()
3418 * instead to skip to any following command, e.g. for:
3419 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003420 ++emsg_skip;
3421 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3422 clear_tv(&rettv);
3423 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003424 return;
3425 }
3426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003427 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003428 if (fudi.fd_newkey != NULL)
3429 {
3430 /* Still need to give an error message for missing key. */
3431 EMSG2(_(e_dictkey), fudi.fd_newkey);
3432 vim_free(fudi.fd_newkey);
3433 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 if (tofree == NULL)
3435 return;
3436
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003437 /* Increase refcount on dictionary, it could get deleted when evaluating
3438 * the arguments. */
3439 if (fudi.fd_dict != NULL)
3440 ++fudi.fd_dict->dv_refcount;
3441
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003442 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003443 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003444 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar532c7802005-01-27 14:44:31 +00003446 /* Skip white space to allow ":call func ()". Not good, but required for
3447 * backward compatibility. */
3448 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003449 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 if (*startarg != '(')
3452 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003453 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 goto end;
3455 }
3456
3457 /*
3458 * When skipping, evaluate the function once, to find the end of the
3459 * arguments.
3460 * When the function takes a range, this is discovered after the first
3461 * call, and the loop is broken.
3462 */
3463 if (eap->skip)
3464 {
3465 ++emsg_skip;
3466 lnum = eap->line2; /* do it once, also with an invalid range */
3467 }
3468 else
3469 lnum = eap->line1;
3470 for ( ; lnum <= eap->line2; ++lnum)
3471 {
3472 if (!eap->skip && eap->addr_count > 0)
3473 {
3474 curwin->w_cursor.lnum = lnum;
3475 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003476#ifdef FEAT_VIRTUALEDIT
3477 curwin->w_cursor.coladd = 0;
3478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003481 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003482 eap->line1, eap->line2, &doesrange,
3483 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 failed = TRUE;
3486 break;
3487 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003488
3489 /* Handle a function returning a Funcref, Dictionary or List. */
3490 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3491 {
3492 failed = TRUE;
3493 break;
3494 }
3495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (doesrange || eap->skip)
3498 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003499
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003501 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003502 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003503 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (aborting())
3505 break;
3506 }
3507 if (eap->skip)
3508 --emsg_skip;
3509
3510 if (!failed)
3511 {
3512 /* Check for trailing illegal characters and a following command. */
3513 if (!ends_excmd(*arg))
3514 {
3515 emsg_severe = TRUE;
3516 EMSG(_(e_trailing));
3517 }
3518 else
3519 eap->nextcmd = check_nextcmd(arg);
3520 }
3521
3522end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003524 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
3527/*
3528 * ":unlet[!] var1 ... " command.
3529 */
3530 void
3531ex_unlet(eap)
3532 exarg_T *eap;
3533{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003534 ex_unletlock(eap, eap->arg, 0);
3535}
3536
3537/*
3538 * ":lockvar" and ":unlockvar" commands
3539 */
3540 void
3541ex_lockvar(eap)
3542 exarg_T *eap;
3543{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003545 int deep = 2;
3546
3547 if (eap->forceit)
3548 deep = -1;
3549 else if (vim_isdigit(*arg))
3550 {
3551 deep = getdigits(&arg);
3552 arg = skipwhite(arg);
3553 }
3554
3555 ex_unletlock(eap, arg, deep);
3556}
3557
3558/*
3559 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3560 */
3561 static void
3562ex_unletlock(eap, argstart, deep)
3563 exarg_T *eap;
3564 char_u *argstart;
3565 int deep;
3566{
3567 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003570 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 do
3573 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003575 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003576 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003577 if (lv.ll_name == NULL)
3578 error = TRUE; /* error but continue parsing */
3579 if (name_end == NULL || (!vim_iswhite(*name_end)
3580 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003582 if (name_end != NULL)
3583 {
3584 emsg_severe = TRUE;
3585 EMSG(_(e_trailing));
3586 }
3587 if (!(eap->skip || error))
3588 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 break;
3590 }
3591
3592 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 {
3594 if (eap->cmdidx == CMD_unlet)
3595 {
3596 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3597 error = TRUE;
3598 }
3599 else
3600 {
3601 if (do_lock_var(&lv, name_end, deep,
3602 eap->cmdidx == CMD_lockvar) == FAIL)
3603 error = TRUE;
3604 }
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 if (!eap->skip)
3608 clear_lval(&lv);
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 arg = skipwhite(name_end);
3611 } while (!ends_excmd(*arg));
3612
3613 eap->nextcmd = check_nextcmd(arg);
3614}
3615
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003618 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003620 int forceit;
3621{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 int ret = OK;
3623 int cc;
3624
3625 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 cc = *name_end;
3628 *name_end = NUL;
3629
3630 /* Normal name or expanded name. */
3631 if (check_changedtick(lp->ll_name))
3632 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003633 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3638 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 else if (lp->ll_range)
3640 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003641 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642
3643 /* Delete a range of List items. */
3644 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3645 {
3646 li = lp->ll_li->li_next;
3647 listitem_remove(lp->ll_list, lp->ll_li);
3648 lp->ll_li = li;
3649 ++lp->ll_n1;
3650 }
3651 }
3652 else
3653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a List item. */
3656 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003659 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 }
3661
3662 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663}
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaar33570922005-01-25 22:26:29 +00003674 hashtab_T *ht;
3675 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003676 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003677 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 ht = find_var_ht(name, &varname);
3680 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 hi = hash_find(ht, varname);
3683 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003684 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003685 di = HI2DI(hi);
3686 if (var_check_fixed(di->di_flags, name)
3687 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 return FAIL;
3689 delete_var(ht, hi);
3690 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 if (forceit)
3694 return OK;
3695 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697}
3698
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699/*
3700 * Lock or unlock variable indicated by "lp".
3701 * "deep" is the levels to go (-1 for unlimited);
3702 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3703 */
3704 static int
3705do_lock_var(lp, name_end, deep, lock)
3706 lval_T *lp;
3707 char_u *name_end;
3708 int deep;
3709 int lock;
3710{
3711 int ret = OK;
3712 int cc;
3713 dictitem_T *di;
3714
3715 if (deep == 0) /* nothing to do */
3716 return OK;
3717
3718 if (lp->ll_tv == NULL)
3719 {
3720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
3726 else
3727 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003728 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 if (di == NULL)
3730 ret = FAIL;
3731 else
3732 {
3733 if (lock)
3734 di->di_flags |= DI_FLAGS_LOCK;
3735 else
3736 di->di_flags &= ~DI_FLAGS_LOCK;
3737 item_lock(&di->di_tv, deep, lock);
3738 }
3739 }
3740 *name_end = cc;
3741 }
3742 else if (lp->ll_range)
3743 {
3744 listitem_T *li = lp->ll_li;
3745
3746 /* (un)lock a range of List items. */
3747 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3748 {
3749 item_lock(&li->li_tv, deep, lock);
3750 li = li->li_next;
3751 ++lp->ll_n1;
3752 }
3753 }
3754 else if (lp->ll_list != NULL)
3755 /* (un)lock a List item. */
3756 item_lock(&lp->ll_li->li_tv, deep, lock);
3757 else
3758 /* un(lock) a Dictionary item. */
3759 item_lock(&lp->ll_di->di_tv, deep, lock);
3760
3761 return ret;
3762}
3763
3764/*
3765 * Lock or unlock an item. "deep" is nr of levels to go.
3766 */
3767 static void
3768item_lock(tv, deep, lock)
3769 typval_T *tv;
3770 int deep;
3771 int lock;
3772{
3773 static int recurse = 0;
3774 list_T *l;
3775 listitem_T *li;
3776 dict_T *d;
3777 hashitem_T *hi;
3778 int todo;
3779
3780 if (recurse >= DICT_MAXNEST)
3781 {
3782 EMSG(_("E743: variable nested too deep for (un)lock"));
3783 return;
3784 }
3785 if (deep == 0)
3786 return;
3787 ++recurse;
3788
3789 /* lock/unlock the item itself */
3790 if (lock)
3791 tv->v_lock |= VAR_LOCKED;
3792 else
3793 tv->v_lock &= ~VAR_LOCKED;
3794
3795 switch (tv->v_type)
3796 {
3797 case VAR_LIST:
3798 if ((l = tv->vval.v_list) != NULL)
3799 {
3800 if (lock)
3801 l->lv_lock |= VAR_LOCKED;
3802 else
3803 l->lv_lock &= ~VAR_LOCKED;
3804 if (deep < 0 || deep > 1)
3805 /* recursive: lock/unlock the items the List contains */
3806 for (li = l->lv_first; li != NULL; li = li->li_next)
3807 item_lock(&li->li_tv, deep - 1, lock);
3808 }
3809 break;
3810 case VAR_DICT:
3811 if ((d = tv->vval.v_dict) != NULL)
3812 {
3813 if (lock)
3814 d->dv_lock |= VAR_LOCKED;
3815 else
3816 d->dv_lock &= ~VAR_LOCKED;
3817 if (deep < 0 || deep > 1)
3818 {
3819 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003820 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3822 {
3823 if (!HASHITEM_EMPTY(hi))
3824 {
3825 --todo;
3826 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3827 }
3828 }
3829 }
3830 }
3831 }
3832 --recurse;
3833}
3834
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003836 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3837 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003838 */
3839 static int
3840tv_islocked(tv)
3841 typval_T *tv;
3842{
3843 return (tv->v_lock & VAR_LOCKED)
3844 || (tv->v_type == VAR_LIST
3845 && tv->vval.v_list != NULL
3846 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3847 || (tv->v_type == VAR_DICT
3848 && tv->vval.v_dict != NULL
3849 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3850}
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3853/*
3854 * Delete all "menutrans_" variables.
3855 */
3856 void
3857del_menutrans_vars()
3858{
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
Bram Moolenaar33570922005-01-25 22:26:29 +00003862 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003863 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003865 {
3866 if (!HASHITEM_EMPTY(hi))
3867 {
3868 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3870 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003871 }
3872 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003873 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875#endif
3876
3877#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3878
3879/*
3880 * Local string buffer for the next two functions to store a variable name
3881 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3882 * get_user_var_name().
3883 */
3884
3885static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3886
3887static char_u *varnamebuf = NULL;
3888static int varnamebuflen = 0;
3889
3890/*
3891 * Function to concatenate a prefix and a variable name.
3892 */
3893 static char_u *
3894cat_prefix_varname(prefix, name)
3895 int prefix;
3896 char_u *name;
3897{
3898 int len;
3899
3900 len = (int)STRLEN(name) + 3;
3901 if (len > varnamebuflen)
3902 {
3903 vim_free(varnamebuf);
3904 len += 10; /* some additional space */
3905 varnamebuf = alloc(len);
3906 if (varnamebuf == NULL)
3907 {
3908 varnamebuflen = 0;
3909 return NULL;
3910 }
3911 varnamebuflen = len;
3912 }
3913 *varnamebuf = prefix;
3914 varnamebuf[1] = ':';
3915 STRCPY(varnamebuf + 2, name);
3916 return varnamebuf;
3917}
3918
3919/*
3920 * Function given to ExpandGeneric() to obtain the list of user defined
3921 * (global/buffer/window/built-in) variable names.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 char_u *
3924get_user_var_name(xp, idx)
3925 expand_T *xp;
3926 int idx;
3927{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003928 static long_u gdone;
3929 static long_u bdone;
3930 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931#ifdef FEAT_WINDOWS
3932 static long_u tdone;
3933#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003934 static int vidx;
3935 static hashitem_T *hi;
3936 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003941#ifdef FEAT_WINDOWS
3942 tdone = 0;
3943#endif
3944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003945
3946 /* Global variables */
3947 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003949 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003951 else
3952 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 while (HASHITEM_EMPTY(hi))
3954 ++hi;
3955 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3956 return cat_prefix_varname('g', hi->hi_key);
3957 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
3960 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003961 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 else
3967 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003972 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return (char_u *)"b:changedtick";
3976 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003977
3978 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003979 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003982 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003983 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003984 else
3985 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003986 while (HASHITEM_EMPTY(hi))
3987 ++hi;
3988 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003990
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991#ifdef FEAT_WINDOWS
3992 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003993 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003994 if (tdone < ht->ht_used)
3995 {
3996 if (tdone++ == 0)
3997 hi = ht->ht_array;
3998 else
3999 ++hi;
4000 while (HASHITEM_EMPTY(hi))
4001 ++hi;
4002 return cat_prefix_varname('t', hi->hi_key);
4003 }
4004#endif
4005
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 /* v: variables */
4007 if (vidx < VV_LEN)
4008 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 vim_free(varnamebuf);
4011 varnamebuf = NULL;
4012 varnamebuflen = 0;
4013 return NULL;
4014}
4015
4016#endif /* FEAT_CMDL_COMPL */
4017
4018/*
4019 * types for expressions.
4020 */
4021typedef enum
4022{
4023 TYPE_UNKNOWN = 0
4024 , TYPE_EQUAL /* == */
4025 , TYPE_NEQUAL /* != */
4026 , TYPE_GREATER /* > */
4027 , TYPE_GEQUAL /* >= */
4028 , TYPE_SMALLER /* < */
4029 , TYPE_SEQUAL /* <= */
4030 , TYPE_MATCH /* =~ */
4031 , TYPE_NOMATCH /* !~ */
4032} exptype_T;
4033
4034/*
4035 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4038 */
4039
4040/*
4041 * Handle zero level expression.
4042 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004044 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * Return OK or FAIL.
4046 */
4047 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004048eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 char_u **nextcmd;
4052 int evaluate;
4053{
4054 int ret;
4055 char_u *p;
4056
4057 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (ret == FAIL || !ends_excmd(*p))
4060 {
4061 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /*
4064 * Report the invalid expression unless the expression evaluation has
4065 * been cancelled due to an aborting error, an interrupt, or an
4066 * exception.
4067 */
4068 if (!aborting())
4069 EMSG2(_(e_invexpr2), arg);
4070 ret = FAIL;
4071 }
4072 if (nextcmd != NULL)
4073 *nextcmd = check_nextcmd(p);
4074
4075 return ret;
4076}
4077
4078/*
4079 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004080 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 *
4082 * "arg" must point to the first non-white of the expression.
4083 * "arg" is advanced to the next non-white after the recognized expression.
4084 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004085 * Note: "rettv.v_lock" is not set.
4086 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 * Return OK or FAIL.
4088 */
4089 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 int evaluate;
4094{
4095 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 /*
4099 * Get the first variable.
4100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 if ((*arg)[0] == '?')
4105 {
4106 result = FALSE;
4107 if (evaluate)
4108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int error = FALSE;
4110
4111 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114 if (error)
4115 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /*
4119 * Get the second variable.
4120 */
4121 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124
4125 /*
4126 * Check for the ":".
4127 */
4128 if ((*arg)[0] != ':')
4129 {
4130 EMSG(_("E109: Missing ':' after '?'"));
4131 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004132 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 return FAIL;
4134 }
4135
4136 /*
4137 * Get the third variable.
4138 */
4139 *arg = skipwhite(*arg + 1);
4140 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4141 {
4142 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 return FAIL;
4145 }
4146 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004147 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149
4150 return OK;
4151}
4152
4153/*
4154 * Handle first level expression:
4155 * expr2 || expr2 || expr2 logical OR
4156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
4160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004163eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 int evaluate;
4167{
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 long result;
4170 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 /*
4180 * Repeat until there is no following "||".
4181 */
4182 first = TRUE;
4183 result = FALSE;
4184 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4185 {
4186 if (evaluate && first)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 first = FALSE;
4194 }
4195
4196 /*
4197 * Get the second variable.
4198 */
4199 *arg = skipwhite(*arg + 2);
4200 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4201 return FAIL;
4202
4203 /*
4204 * Compute the result.
4205 */
4206 if (evaluate && !result)
4207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 if (error)
4212 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 if (evaluate)
4215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219 }
4220
4221 return OK;
4222}
4223
4224/*
4225 * Handle second level expression:
4226 * expr3 && expr3 && expr3 logical AND
4227 *
4228 * "arg" must point to the first non-white of the expression.
4229 * "arg" is advanced to the next non-white after the recognized expression.
4230 *
4231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 int evaluate;
4238{
Bram Moolenaar33570922005-01-25 22:26:29 +00004239 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 long result;
4241 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * Get the first variable.
4246 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Repeat until there is no following "&&".
4252 */
4253 first = TRUE;
4254 result = TRUE;
4255 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4256 {
4257 if (evaluate && first)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 first = FALSE;
4265 }
4266
4267 /*
4268 * Get the second variable.
4269 */
4270 *arg = skipwhite(*arg + 2);
4271 if (eval4(arg, &var2, evaluate && result) == FAIL)
4272 return FAIL;
4273
4274 /*
4275 * Compute the result.
4276 */
4277 if (evaluate && result)
4278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004281 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 if (error)
4283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 if (evaluate)
4286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 rettv->v_type = VAR_NUMBER;
4288 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 }
4291
4292 return OK;
4293}
4294
4295/*
4296 * Handle third level expression:
4297 * var1 == var2
4298 * var1 =~ var2
4299 * var1 != var2
4300 * var1 !~ var2
4301 * var1 > var2
4302 * var1 >= var2
4303 * var1 < var2
4304 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 * var1 is var2
4306 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 int evaluate;
4318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u *p;
4321 int i;
4322 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 int len = 2;
4325 long n1, n2;
4326 char_u *s1, *s2;
4327 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4328 regmatch_T regmatch;
4329 int ic;
4330 char_u *save_cpo;
4331
4332 /*
4333 * Get the first variable.
4334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return FAIL;
4337
4338 p = *arg;
4339 switch (p[0])
4340 {
4341 case '=': if (p[1] == '=')
4342 type = TYPE_EQUAL;
4343 else if (p[1] == '~')
4344 type = TYPE_MATCH;
4345 break;
4346 case '!': if (p[1] == '=')
4347 type = TYPE_NEQUAL;
4348 else if (p[1] == '~')
4349 type = TYPE_NOMATCH;
4350 break;
4351 case '>': if (p[1] != '=')
4352 {
4353 type = TYPE_GREATER;
4354 len = 1;
4355 }
4356 else
4357 type = TYPE_GEQUAL;
4358 break;
4359 case '<': if (p[1] != '=')
4360 {
4361 type = TYPE_SMALLER;
4362 len = 1;
4363 }
4364 else
4365 type = TYPE_SEQUAL;
4366 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004367 case 'i': if (p[1] == 's')
4368 {
4369 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4370 len = 5;
4371 if (!vim_isIDc(p[len]))
4372 {
4373 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4374 type_is = TRUE;
4375 }
4376 }
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379
4380 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004381 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 */
4383 if (type != TYPE_UNKNOWN)
4384 {
4385 /* extra question mark appended: ignore case */
4386 if (p[len] == '?')
4387 {
4388 ic = TRUE;
4389 ++len;
4390 }
4391 /* extra '#' appended: match case */
4392 else if (p[len] == '#')
4393 {
4394 ic = FALSE;
4395 ++len;
4396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004397 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 else
4399 ic = p_ic;
4400
4401 /*
4402 * Get the second variable.
4403 */
4404 *arg = skipwhite(p + len);
4405 if (eval5(arg, &var2, evaluate) == FAIL)
4406 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409 }
4410
4411 if (evaluate)
4412 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 if (type_is && rettv->v_type != var2.v_type)
4414 {
4415 /* For "is" a different type always means FALSE, for "notis"
4416 * it means TRUE. */
4417 n1 = (type == TYPE_NEQUAL);
4418 }
4419 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4420 {
4421 if (type_is)
4422 {
4423 n1 = (rettv->v_type == var2.v_type
4424 && rettv->vval.v_list == var2.vval.v_list);
4425 if (type == TYPE_NEQUAL)
4426 n1 = !n1;
4427 }
4428 else if (rettv->v_type != var2.v_type
4429 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4430 {
4431 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004432 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004434 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 clear_tv(rettv);
4436 clear_tv(&var2);
4437 return FAIL;
4438 }
4439 else
4440 {
4441 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004442 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4443 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 if (type == TYPE_NEQUAL)
4445 n1 = !n1;
4446 }
4447 }
4448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004449 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4450 {
4451 if (type_is)
4452 {
4453 n1 = (rettv->v_type == var2.v_type
4454 && rettv->vval.v_dict == var2.vval.v_dict);
4455 if (type == TYPE_NEQUAL)
4456 n1 = !n1;
4457 }
4458 else if (rettv->v_type != var2.v_type
4459 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4460 {
4461 if (rettv->v_type != var2.v_type)
4462 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4463 else
4464 EMSG(_("E736: Invalid operation for Dictionary"));
4465 clear_tv(rettv);
4466 clear_tv(&var2);
4467 return FAIL;
4468 }
4469 else
4470 {
4471 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004472 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4473 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 if (type == TYPE_NEQUAL)
4475 n1 = !n1;
4476 }
4477 }
4478
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4480 {
4481 if (rettv->v_type != var2.v_type
4482 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4483 {
4484 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004485 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004487 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 clear_tv(rettv);
4489 clear_tv(&var2);
4490 return FAIL;
4491 }
4492 else
4493 {
4494 /* Compare two Funcrefs for being equal or unequal. */
4495 if (rettv->vval.v_string == NULL
4496 || var2.vval.v_string == NULL)
4497 n1 = FALSE;
4498 else
4499 n1 = STRCMP(rettv->vval.v_string,
4500 var2.vval.v_string) == 0;
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 }
4505
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506#ifdef FEAT_FLOAT
4507 /*
4508 * If one of the two variables is a float, compare as a float.
4509 * When using "=~" or "!~", always compare as string.
4510 */
4511 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4512 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4513 {
4514 float_T f1, f2;
4515
4516 if (rettv->v_type == VAR_FLOAT)
4517 f1 = rettv->vval.v_float;
4518 else
4519 f1 = get_tv_number(rettv);
4520 if (var2.v_type == VAR_FLOAT)
4521 f2 = var2.vval.v_float;
4522 else
4523 f2 = get_tv_number(&var2);
4524 n1 = FALSE;
4525 switch (type)
4526 {
4527 case TYPE_EQUAL: n1 = (f1 == f2); break;
4528 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4529 case TYPE_GREATER: n1 = (f1 > f2); break;
4530 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4531 case TYPE_SMALLER: n1 = (f1 < f2); break;
4532 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4533 case TYPE_UNKNOWN:
4534 case TYPE_MATCH:
4535 case TYPE_NOMATCH: break; /* avoid gcc warning */
4536 }
4537 }
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 /*
4541 * If one of the two variables is a number, compare as a number.
4542 * When using "=~" or "!~", always compare as string.
4543 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 n1 = get_tv_number(rettv);
4548 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 switch (type)
4550 {
4551 case TYPE_EQUAL: n1 = (n1 == n2); break;
4552 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4553 case TYPE_GREATER: n1 = (n1 > n2); break;
4554 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4555 case TYPE_SMALLER: n1 = (n1 < n2); break;
4556 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4557 case TYPE_UNKNOWN:
4558 case TYPE_MATCH:
4559 case TYPE_NOMATCH: break; /* avoid gcc warning */
4560 }
4561 }
4562 else
4563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004564 s1 = get_tv_string_buf(rettv, buf1);
4565 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4567 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4568 else
4569 i = 0;
4570 n1 = FALSE;
4571 switch (type)
4572 {
4573 case TYPE_EQUAL: n1 = (i == 0); break;
4574 case TYPE_NEQUAL: n1 = (i != 0); break;
4575 case TYPE_GREATER: n1 = (i > 0); break;
4576 case TYPE_GEQUAL: n1 = (i >= 0); break;
4577 case TYPE_SMALLER: n1 = (i < 0); break;
4578 case TYPE_SEQUAL: n1 = (i <= 0); break;
4579
4580 case TYPE_MATCH:
4581 case TYPE_NOMATCH:
4582 /* avoid 'l' flag in 'cpoptions' */
4583 save_cpo = p_cpo;
4584 p_cpo = (char_u *)"";
4585 regmatch.regprog = vim_regcomp(s2,
4586 RE_MAGIC + RE_STRING);
4587 regmatch.rm_ic = ic;
4588 if (regmatch.regprog != NULL)
4589 {
4590 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004591 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (type == TYPE_NOMATCH)
4593 n1 = !n1;
4594 }
4595 p_cpo = save_cpo;
4596 break;
4597
4598 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4599 }
4600 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601 clear_tv(rettv);
4602 clear_tv(&var2);
4603 rettv->v_type = VAR_NUMBER;
4604 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607
4608 return OK;
4609}
4610
4611/*
4612 * Handle fourth level expression:
4613 * + number addition
4614 * - number subtraction
4615 * . string concatenation
4616 *
4617 * "arg" must point to the first non-white of the expression.
4618 * "arg" is advanced to the next non-white after the recognized expression.
4619 *
4620 * Return OK or FAIL.
4621 */
4622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004623eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int evaluate;
4627{
Bram Moolenaar33570922005-01-25 22:26:29 +00004628 typval_T var2;
4629 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int op;
4631 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632#ifdef FEAT_FLOAT
4633 float_T f1 = 0, f2 = 0;
4634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 char_u *s1, *s2;
4636 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4637 char_u *p;
4638
4639 /*
4640 * Get the first variable.
4641 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004642 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FAIL;
4644
4645 /*
4646 * Repeat computing, until no '+', '-' or '.' is following.
4647 */
4648 for (;;)
4649 {
4650 op = **arg;
4651 if (op != '+' && op != '-' && op != '.')
4652 break;
4653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654 if ((op != '+' || rettv->v_type != VAR_LIST)
4655#ifdef FEAT_FLOAT
4656 && (op == '.' || rettv->v_type != VAR_FLOAT)
4657#endif
4658 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004659 {
4660 /* For "list + ...", an illegal use of the first operand as
4661 * a number cannot be determined before evaluating the 2nd
4662 * operand: if this is also a list, all is ok.
4663 * For "something . ...", "something - ..." or "non-list + ...",
4664 * we know that the first operand needs to be a string or number
4665 * without evaluating the 2nd operand. So check before to avoid
4666 * side effects after an error. */
4667 if (evaluate && get_tv_string_chk(rettv) == NULL)
4668 {
4669 clear_tv(rettv);
4670 return FAIL;
4671 }
4672 }
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 /*
4675 * Get the second variable.
4676 */
4677 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004678 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 return FAIL;
4682 }
4683
4684 if (evaluate)
4685 {
4686 /*
4687 * Compute the result.
4688 */
4689 if (op == '.')
4690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4692 s2 = get_tv_string_buf_chk(&var2, buf2);
4693 if (s2 == NULL) /* type error ? */
4694 {
4695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 return FAIL;
4698 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004699 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 rettv->v_type = VAR_STRING;
4702 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004704 else if (op == '+' && rettv->v_type == VAR_LIST
4705 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 {
4707 /* concatenate Lists */
4708 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4709 &var3) == FAIL)
4710 {
4711 clear_tv(rettv);
4712 clear_tv(&var2);
4713 return FAIL;
4714 }
4715 clear_tv(rettv);
4716 *rettv = var3;
4717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 int error = FALSE;
4721
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722#ifdef FEAT_FLOAT
4723 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 f1 = rettv->vval.v_float;
4726 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 else
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 n1 = get_tv_number_chk(rettv, &error);
4732 if (error)
4733 {
4734 /* This can only happen for "list + non-list". For
4735 * "non-list + ..." or "something - ...", we returned
4736 * before evaluating the 2nd operand. */
4737 clear_tv(rettv);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (var2.v_type == VAR_FLOAT)
4742 f1 = n1;
4743#endif
4744 }
4745#ifdef FEAT_FLOAT
4746 if (var2.v_type == VAR_FLOAT)
4747 {
4748 f2 = var2.vval.v_float;
4749 n2 = 0;
4750 }
4751 else
4752#endif
4753 {
4754 n2 = get_tv_number_chk(&var2, &error);
4755 if (error)
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
4761#ifdef FEAT_FLOAT
4762 if (rettv->v_type == VAR_FLOAT)
4763 f2 = n2;
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767
4768#ifdef FEAT_FLOAT
4769 /* If there is a float on either side the result is a float. */
4770 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4771 {
4772 if (op == '+')
4773 f1 = f1 + f2;
4774 else
4775 f1 = f1 - f2;
4776 rettv->v_type = VAR_FLOAT;
4777 rettv->vval.v_float = f1;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#endif
4781 {
4782 if (op == '+')
4783 n1 = n1 + n2;
4784 else
4785 n1 = n1 - n2;
4786 rettv->v_type = VAR_NUMBER;
4787 rettv->vval.v_number = n1;
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004790 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792 }
4793 return OK;
4794}
4795
4796/*
4797 * Handle fifth level expression:
4798 * * number multiplication
4799 * / number division
4800 * % number modulo
4801 *
4802 * "arg" must point to the first non-white of the expression.
4803 * "arg" is advanced to the next non-white after the recognized expression.
4804 *
4805 * Return OK or FAIL.
4806 */
4807 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
Bram Moolenaar33570922005-01-25 22:26:29 +00004814 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 int op;
4816 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817#ifdef FEAT_FLOAT
4818 int use_float = FALSE;
4819 float_T f1 = 0, f2;
4820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 /*
4824 * Get the first variable.
4825 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return FAIL;
4828
4829 /*
4830 * Repeat computing, until no '*', '/' or '%' is following.
4831 */
4832 for (;;)
4833 {
4834 op = **arg;
4835 if (op != '*' && op != '/' && op != '%')
4836 break;
4837
4838 if (evaluate)
4839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#ifdef FEAT_FLOAT
4841 if (rettv->v_type == VAR_FLOAT)
4842 {
4843 f1 = rettv->vval.v_float;
4844 use_float = TRUE;
4845 n1 = 0;
4846 }
4847 else
4848#endif
4849 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004850 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 if (error)
4852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 else
4855 n1 = 0;
4856
4857 /*
4858 * Get the second variable.
4859 */
4860 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 if (evaluate)
4865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 if (var2.v_type == VAR_FLOAT)
4868 {
4869 if (!use_float)
4870 {
4871 f1 = n1;
4872 use_float = TRUE;
4873 }
4874 f2 = var2.vval.v_float;
4875 n2 = 0;
4876 }
4877 else
4878#endif
4879 {
4880 n2 = get_tv_number_chk(&var2, &error);
4881 clear_tv(&var2);
4882 if (error)
4883 return FAIL;
4884#ifdef FEAT_FLOAT
4885 if (use_float)
4886 f2 = n2;
4887#endif
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
4891 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004892 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#ifdef FEAT_FLOAT
4895 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 if (op == '*')
4898 f1 = f1 * f2;
4899 else if (op == '/')
4900 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004901# ifdef VMS
4902 /* VMS crashes on divide by zero, work around it */
4903 if (f2 == 0.0)
4904 {
4905 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004906 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004907 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004908 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004910 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004911 }
4912 else
4913 f1 = f1 / f2;
4914# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915 /* We rely on the floating point library to handle divide
4916 * by zero to result in "inf" and not a crash. */
4917 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004918# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004921 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004922 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923 return FAIL;
4924 }
4925 rettv->v_type = VAR_FLOAT;
4926 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931 if (op == '*')
4932 n1 = n1 * n2;
4933 else if (op == '/')
4934 {
4935 if (n2 == 0) /* give an error message? */
4936 {
4937 if (n1 == 0)
4938 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4939 else if (n1 < 0)
4940 n1 = -0x7fffffffL;
4941 else
4942 n1 = 0x7fffffffL;
4943 }
4944 else
4945 n1 = n1 / n2;
4946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004948 {
4949 if (n2 == 0) /* give an error message? */
4950 n1 = 0;
4951 else
4952 n1 = n1 % n2;
4953 }
4954 rettv->v_type = VAR_NUMBER;
4955 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
4958 }
4959
4960 return OK;
4961}
4962
4963/*
4964 * Handle sixth level expression:
4965 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004966 * "string" string constant
4967 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 * &option-name option value
4969 * @r register contents
4970 * identifier variable value
4971 * function() function call
4972 * $VAR environment variable
4973 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004974 * [expr, expr] List
4975 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 *
4977 * Also handle:
4978 * ! in front logical NOT
4979 * - in front unary minus
4980 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004981 * trailing [] subscript in String or List
4982 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *
4984 * "arg" must point to the first non-white of the expression.
4985 * "arg" is advanced to the next non-white after the recognized expression.
4986 *
4987 * Return OK or FAIL.
4988 */
4989 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004990eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004994 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 long n;
4997 int len;
4998 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 char_u *start_leader, *end_leader;
5000 int ret = OK;
5001 char_u *alias;
5002
5003 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
5009 /*
5010 * Skip '!' and '-' characters. They are handled later.
5011 */
5012 start_leader = *arg;
5013 while (**arg == '!' || **arg == '-' || **arg == '+')
5014 *arg = skipwhite(*arg + 1);
5015 end_leader = *arg;
5016
5017 switch (**arg)
5018 {
5019 /*
5020 * Number constant.
5021 */
5022 case '0':
5023 case '1':
5024 case '2':
5025 case '3':
5026 case '4':
5027 case '5':
5028 case '6':
5029 case '7':
5030 case '8':
5031 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 {
5033#ifdef FEAT_FLOAT
5034 char_u *p = skipdigits(*arg + 1);
5035 int get_float = FALSE;
5036
5037 /* We accept a float when the format matches
5038 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005039 * strict to avoid backwards compatibility problems.
5040 * Don't look for a float after the "." operator, so that
5041 * ":let vers = 1.2.3" doesn't fail. */
5042 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 get_float = TRUE;
5045 p = skipdigits(p + 2);
5046 if (*p == 'e' || *p == 'E')
5047 {
5048 ++p;
5049 if (*p == '-' || *p == '+')
5050 ++p;
5051 if (!vim_isdigit(*p))
5052 get_float = FALSE;
5053 else
5054 p = skipdigits(p + 1);
5055 }
5056 if (ASCII_ISALPHA(*p) || *p == '.')
5057 get_float = FALSE;
5058 }
5059 if (get_float)
5060 {
5061 float_T f;
5062
5063 *arg += string2float(*arg, &f);
5064 if (evaluate)
5065 {
5066 rettv->v_type = VAR_FLOAT;
5067 rettv->vval.v_float = f;
5068 }
5069 }
5070 else
5071#endif
5072 {
5073 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5074 *arg += len;
5075 if (evaluate)
5076 {
5077 rettv->v_type = VAR_NUMBER;
5078 rettv->vval.v_number = n;
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
5084 /*
5085 * String constant: "string".
5086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089
5090 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005091 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 break;
5095
5096 /*
5097 * List: [expr, expr]
5098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101
5102 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005103 * Dictionary: {key: val, key: val}
5104 */
5105 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5106 break;
5107
5108 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005109 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005111 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /*
5115 * Environment variable: $VAR.
5116 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /*
5121 * Register contents: @r.
5122 */
5123 case '@': ++*arg;
5124 if (evaluate)
5125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005126 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005127 rettv->vval.v_string = get_reg_contents(**arg,
5128 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 if (**arg != NUL)
5131 ++*arg;
5132 break;
5133
5134 /*
5135 * nested expression: (expression).
5136 */
5137 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (**arg == ')')
5140 ++*arg;
5141 else if (ret == OK)
5142 {
5143 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ret = FAIL;
5146 }
5147 break;
5148
Bram Moolenaar8c711452005-01-14 21:53:12 +00005149 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152
5153 if (ret == NOTDONE)
5154 {
5155 /*
5156 * Must be a variable or function name.
5157 * Can also be a curly-braces kind of name: {expr}.
5158 */
5159 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 if (alias != NULL)
5162 s = alias;
5163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005164 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 ret = FAIL;
5166 else
5167 {
5168 if (**arg == '(') /* recursive! */
5169 {
5170 /* If "s" is the name of a variable of type VAR_FUNC
5171 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005172 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173
5174 /* Invoke the function. */
5175 ret = get_func_tv(s, len, rettv, arg,
5176 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005177 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005178
5179 /* If evaluate is FALSE rettv->v_type was not set in
5180 * get_func_tv, but it's needed in handle_subscript() to parse
5181 * what follows. So set it here. */
5182 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5183 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005184 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005185 rettv->v_type = VAR_FUNC;
5186 }
5187
Bram Moolenaar8c711452005-01-14 21:53:12 +00005188 /* Stop the expression evaluation when immediately
5189 * aborting on error, or when an interrupt occurred or
5190 * an exception was thrown but not caught. */
5191 if (aborting())
5192 {
5193 if (ret == OK)
5194 clear_tv(rettv);
5195 ret = FAIL;
5196 }
5197 }
5198 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005199 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005200 else
5201 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005203 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005204 }
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 *arg = skipwhite(*arg);
5207
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005208 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5209 * expr(expr). */
5210 if (ret == OK)
5211 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 /*
5214 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5215 */
5216 if (ret == OK && evaluate && end_leader > start_leader)
5217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 int val = 0;
5220#ifdef FEAT_FLOAT
5221 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005223 if (rettv->v_type == VAR_FLOAT)
5224 f = rettv->vval.v_float;
5225 else
5226#endif
5227 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 clear_tv(rettv);
5231 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 else
5234 {
5235 while (end_leader > start_leader)
5236 {
5237 --end_leader;
5238 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 {
5240#ifdef FEAT_FLOAT
5241 if (rettv->v_type == VAR_FLOAT)
5242 f = !f;
5243 else
5244#endif
5245 val = !val;
5246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005248 {
5249#ifdef FEAT_FLOAT
5250 if (rettv->v_type == VAR_FLOAT)
5251 f = -f;
5252 else
5253#endif
5254 val = -val;
5255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005256 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005257#ifdef FEAT_FLOAT
5258 if (rettv->v_type == VAR_FLOAT)
5259 {
5260 clear_tv(rettv);
5261 rettv->vval.v_float = f;
5262 }
5263 else
5264#endif
5265 {
5266 clear_tv(rettv);
5267 rettv->v_type = VAR_NUMBER;
5268 rettv->vval.v_number = val;
5269 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272
5273 return ret;
5274}
5275
5276/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005277 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5278 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5280 */
5281 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287{
5288 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005289 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 long len = -1;
5292 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005296 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (verbose)
5299 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 return FAIL;
5301 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005302#ifdef FEAT_FLOAT
5303 else if (rettv->v_type == VAR_FLOAT)
5304 {
5305 if (verbose)
5306 EMSG(_(e_float_as_string));
5307 return FAIL;
5308 }
5309#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313 /*
5314 * dict.name
5315 */
5316 key = *arg + 1;
5317 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5318 ;
5319 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322 }
5323 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005324 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 /*
5326 * something[idx]
5327 *
5328 * Get the (first) variable from inside the [].
5329 */
5330 *arg = skipwhite(*arg + 1);
5331 if (**arg == ':')
5332 empty1 = TRUE;
5333 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5334 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5336 {
5337 /* not a number or string */
5338 clear_tv(&var1);
5339 return FAIL;
5340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341
5342 /*
5343 * Get the second variable from inside the [:].
5344 */
5345 if (**arg == ':')
5346 {
5347 range = TRUE;
5348 *arg = skipwhite(*arg + 1);
5349 if (**arg == ']')
5350 empty2 = TRUE;
5351 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005353 if (!empty1)
5354 clear_tv(&var1);
5355 return FAIL;
5356 }
5357 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5358 {
5359 /* not a number or string */
5360 if (!empty1)
5361 clear_tv(&var1);
5362 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 return FAIL;
5364 }
5365 }
5366
5367 /* Check for the ']'. */
5368 if (**arg != ']')
5369 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005370 if (verbose)
5371 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 clear_tv(&var1);
5373 if (range)
5374 clear_tv(&var2);
5375 return FAIL;
5376 }
5377 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379
5380 if (evaluate)
5381 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 n1 = 0;
5383 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 n1 = get_tv_number(&var1);
5386 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 if (range)
5389 {
5390 if (empty2)
5391 n2 = -1;
5392 else
5393 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 n2 = get_tv_number(&var2);
5395 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 }
5397 }
5398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
5401 case VAR_NUMBER:
5402 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005403 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 len = (long)STRLEN(s);
5405 if (range)
5406 {
5407 /* The resulting variable is a substring. If the indexes
5408 * are out of range the result is empty. */
5409 if (n1 < 0)
5410 {
5411 n1 = len + n1;
5412 if (n1 < 0)
5413 n1 = 0;
5414 }
5415 if (n2 < 0)
5416 n2 = len + n2;
5417 else if (n2 >= len)
5418 n2 = len;
5419 if (n1 >= len || n2 < 0 || n1 > n2)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5423 }
5424 else
5425 {
5426 /* The resulting variable is a string of a single
5427 * character. If the index is too big or negative the
5428 * result is empty. */
5429 if (n1 >= len || n1 < 0)
5430 s = NULL;
5431 else
5432 s = vim_strnsave(s + n1, 1);
5433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 clear_tv(rettv);
5435 rettv->v_type = VAR_STRING;
5436 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 break;
5438
5439 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 if (n1 < 0)
5442 n1 = len + n1;
5443 if (!empty1 && (n1 < 0 || n1 >= len))
5444 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005445 /* For a range we allow invalid values and return an empty
5446 * list. A list index out of range is an error. */
5447 if (!range)
5448 {
5449 if (verbose)
5450 EMSGN(_(e_listidx), n1);
5451 return FAIL;
5452 }
5453 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455 if (range)
5456 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 list_T *l;
5458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459
5460 if (n2 < 0)
5461 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005462 else if (n2 >= len)
5463 n2 = len - 1;
5464 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 l = list_alloc();
5467 if (l == NULL)
5468 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 n1 <= n2; ++n1)
5471 {
5472 if (list_append_tv(l, &item->li_tv) == FAIL)
5473 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005474 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return FAIL;
5476 }
5477 item = item->li_next;
5478 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 clear_tv(rettv);
5480 rettv->v_type = VAR_LIST;
5481 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005482 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 else
5485 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005486 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 clear_tv(rettv);
5488 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491
5492 case VAR_DICT:
5493 if (range)
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 if (len == -1)
5498 clear_tv(&var1);
5499 return FAIL;
5500 }
5501 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503
5504 if (len == -1)
5505 {
5506 key = get_tv_string(&var1);
5507 if (*key == NUL)
5508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005509 if (verbose)
5510 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 }
5515
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005518 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005519 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 if (len == -1)
5521 clear_tv(&var1);
5522 if (item == NULL)
5523 return FAIL;
5524
5525 copy_tv(&item->di_tv, &var1);
5526 clear_tv(rettv);
5527 *rettv = var1;
5528 }
5529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 return OK;
5534}
5535
5536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 * Get an option value.
5538 * "arg" points to the '&' or '+' before the option name.
5539 * "arg" is advanced to character after the option name.
5540 * Return OK or FAIL.
5541 */
5542 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005545 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 int evaluate;
5547{
5548 char_u *option_end;
5549 long numval;
5550 char_u *stringval;
5551 int opt_type;
5552 int c;
5553 int working = (**arg == '+'); /* has("+option") */
5554 int ret = OK;
5555 int opt_flags;
5556
5557 /*
5558 * Isolate the option name and find its value.
5559 */
5560 option_end = find_option_end(arg, &opt_flags);
5561 if (option_end == NULL)
5562 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 EMSG2(_("E112: Option name missing: %s"), *arg);
5565 return FAIL;
5566 }
5567
5568 if (!evaluate)
5569 {
5570 *arg = option_end;
5571 return OK;
5572 }
5573
5574 c = *option_end;
5575 *option_end = NUL;
5576 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578
5579 if (opt_type == -3) /* invalid name */
5580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 EMSG2(_("E113: Unknown option: %s"), *arg);
5583 ret = FAIL;
5584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 if (opt_type == -2) /* hidden string option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_STRING;
5590 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else if (opt_type == -1) /* hidden number option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_NUMBER;
5595 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 else if (opt_type == 1) /* number option */
5598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 rettv->v_type = VAR_NUMBER;
5600 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else /* string option */
5603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 rettv->v_type = VAR_STRING;
5605 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607 }
5608 else if (working && (opt_type == -2 || opt_type == -1))
5609 ret = FAIL;
5610
5611 *option_end = c; /* put back for error messages */
5612 *arg = option_end;
5613
5614 return ret;
5615}
5616
5617/*
5618 * Allocate a variable for a string constant.
5619 * Return OK or FAIL.
5620 */
5621 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int evaluate;
5626{
5627 char_u *p;
5628 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 int extra = 0;
5630
5631 /*
5632 * Find the end of the string, skipping backslashed characters.
5633 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005634 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
5636 if (*p == '\\' && p[1] != NUL)
5637 {
5638 ++p;
5639 /* A "\<x>" form occupies at least 4 characters, and produces up
5640 * to 6 characters: reserve space for 2 extra */
5641 if (*p == '<')
5642 extra += 2;
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
5645
5646 if (*p != '"')
5647 {
5648 EMSG2(_("E114: Missing quote: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 /* If only parsing, set *arg and return here */
5653 if (!evaluate)
5654 {
5655 *arg = p + 1;
5656 return OK;
5657 }
5658
5659 /*
5660 * Copy the string into allocated memory, handling backslashed
5661 * characters.
5662 */
5663 name = alloc((unsigned)(p - *arg + extra));
5664 if (name == NULL)
5665 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666 rettv->v_type = VAR_STRING;
5667 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\')
5672 {
5673 switch (*++p)
5674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 case 'b': *name++ = BS; ++p; break;
5676 case 'e': *name++ = ESC; ++p; break;
5677 case 'f': *name++ = FF; ++p; break;
5678 case 'n': *name++ = NL; ++p; break;
5679 case 'r': *name++ = CAR; ++p; break;
5680 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
5682 case 'X': /* hex: "\x1", "\x12" */
5683 case 'x':
5684 case 'u': /* Unicode: "\u0023" */
5685 case 'U':
5686 if (vim_isxdigit(p[1]))
5687 {
5688 int n, nr;
5689 int c = toupper(*p);
5690
5691 if (c == 'X')
5692 n = 2;
5693 else
5694 n = 4;
5695 nr = 0;
5696 while (--n >= 0 && vim_isxdigit(p[1]))
5697 {
5698 ++p;
5699 nr = (nr << 4) + hex2nr(*p);
5700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#ifdef FEAT_MBYTE
5703 /* For "\u" store the number according to
5704 * 'encoding'. */
5705 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
5708#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712
5713 /* octal: "\1", "\12", "\123" */
5714 case '0':
5715 case '1':
5716 case '2':
5717 case '3':
5718 case '4':
5719 case '5':
5720 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 case '7': *name = *p++ - '0';
5722 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 *name = (*name << 3) + *p++ - '0';
5725 if (*p >= '0' && *p <= '7')
5726 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 break;
5730
5731 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005732 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (extra != 0)
5734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 /* FALLTHROUGH */
5739
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 break;
5742 }
5743 }
5744 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 *arg = p + 1;
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return OK;
5752}
5753
5754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 * Return OK or FAIL.
5757 */
5758 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005759get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005761 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 int evaluate;
5763{
5764 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005765 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 int reduce = 0;
5767
5768 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 */
5771 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 break;
5777 ++reduce;
5778 ++p;
5779 }
5780 }
5781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 return FAIL;
5786 }
5787
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 if (!evaluate)
5790 {
5791 *arg = p + 1;
5792 return OK;
5793 }
5794
5795 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 */
5798 str = alloc((unsigned)((p - *arg) - reduce));
5799 if (str == NULL)
5800 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 rettv->v_type = VAR_STRING;
5802 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 break;
5810 ++p;
5811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 *arg = p + 1;
5816
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005817 return OK;
5818}
5819
5820/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005821 * Allocate a variable for a List and fill it from "*arg".
5822 * Return OK or FAIL.
5823 */
5824 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005825get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005827 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 int evaluate;
5829{
Bram Moolenaar33570922005-01-25 22:26:29 +00005830 list_T *l = NULL;
5831 typval_T tv;
5832 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833
5834 if (evaluate)
5835 {
5836 l = list_alloc();
5837 if (l == NULL)
5838 return FAIL;
5839 }
5840
5841 *arg = skipwhite(*arg + 1);
5842 while (**arg != ']' && **arg != NUL)
5843 {
5844 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5845 goto failret;
5846 if (evaluate)
5847 {
5848 item = listitem_alloc();
5849 if (item != NULL)
5850 {
5851 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005852 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 list_append(l, item);
5854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005855 else
5856 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 }
5858
5859 if (**arg == ']')
5860 break;
5861 if (**arg != ',')
5862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 goto failret;
5865 }
5866 *arg = skipwhite(*arg + 1);
5867 }
5868
5869 if (**arg != ']')
5870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872failret:
5873 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005874 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 return FAIL;
5876 }
5877
5878 *arg = skipwhite(*arg + 1);
5879 if (evaluate)
5880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 rettv->v_type = VAR_LIST;
5882 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883 ++l->lv_refcount;
5884 }
5885
5886 return OK;
5887}
5888
5889/*
5890 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005891 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005893 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894list_alloc()
5895{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 list_T *l;
5897
5898 l = (list_T *)alloc_clear(sizeof(list_T));
5899 if (l != NULL)
5900 {
5901 /* Prepend the list to the list of lists for garbage collection. */
5902 if (first_list != NULL)
5903 first_list->lv_used_prev = l;
5904 l->lv_used_prev = NULL;
5905 l->lv_used_next = first_list;
5906 first_list = l;
5907 }
5908 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909}
5910
5911/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005912 * Allocate an empty list for a return value.
5913 * Returns OK or FAIL.
5914 */
5915 static int
5916rettv_list_alloc(rettv)
5917 typval_T *rettv;
5918{
5919 list_T *l = list_alloc();
5920
5921 if (l == NULL)
5922 return FAIL;
5923
5924 rettv->vval.v_list = l;
5925 rettv->v_type = VAR_LIST;
5926 ++l->lv_refcount;
5927 return OK;
5928}
5929
5930/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931 * Unreference a list: decrement the reference count and free it when it
5932 * becomes zero.
5933 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005934 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005936 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 if (l != NULL && --l->lv_refcount <= 0)
5939 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940}
5941
5942/*
5943 * Free a list, including all items it points to.
5944 * Ignores the reference count.
5945 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005946 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005947list_free(l, recurse)
5948 list_T *l;
5949 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950{
Bram Moolenaar33570922005-01-25 22:26:29 +00005951 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005953 /* Remove the list from the list of lists for garbage collection. */
5954 if (l->lv_used_prev == NULL)
5955 first_list = l->lv_used_next;
5956 else
5957 l->lv_used_prev->lv_used_next = l->lv_used_next;
5958 if (l->lv_used_next != NULL)
5959 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5960
Bram Moolenaard9fba312005-06-26 22:34:35 +00005961 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it. */
5964 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005965 if (recurse || (item->li_tv.v_type != VAR_LIST
5966 && item->li_tv.v_type != VAR_DICT))
5967 clear_tv(&item->li_tv);
5968 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 }
5970 vim_free(l);
5971}
5972
5973/*
5974 * Allocate a list item.
5975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005976 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977listitem_alloc()
5978{
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980}
5981
5982/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005983 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005985 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 vim_free(item);
5991}
5992
5993/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005994 * Remove a list item from a List and free it. Also clears the value.
5995 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005996 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005997listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005998 list_T *l;
5999 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006000{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006001 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006002 listitem_free(item);
6003}
6004
6005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Get the number of items in a list.
6007 */
6008 static long
6009list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 if (l == NULL)
6013 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006014 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 * Return TRUE when two lists have exactly the same values.
6019 */
6020 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 list_T *l1;
6023 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006025 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026{
Bram Moolenaar33570922005-01-25 22:26:29 +00006027 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006028
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006029 if (l1 == NULL || l2 == NULL)
6030 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006031 if (l1 == l2)
6032 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 if (list_len(l1) != list_len(l2))
6034 return FALSE;
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 for (item1 = l1->lv_first, item2 = l2->lv_first;
6037 item1 != NULL && item2 != NULL;
6038 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006040 return FALSE;
6041 return item1 == NULL && item2 == NULL;
6042}
6043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006044#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6045 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006046/*
6047 * Return the dictitem that an entry in a hashtable points to.
6048 */
6049 dictitem_T *
6050dict_lookup(hi)
6051 hashitem_T *hi;
6052{
6053 return HI2DI(hi);
6054}
6055#endif
6056
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006057/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 * Return TRUE when two dictionaries have exactly the same key/values.
6059 */
6060 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 dict_T *d1;
6063 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006064 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 hashitem_T *hi;
6068 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006071 if (d1 == NULL || d2 == NULL)
6072 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006073 if (d1 == d2)
6074 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075 if (dict_len(d1) != dict_len(d2))
6076 return FALSE;
6077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006078 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006079 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006081 if (!HASHITEM_EMPTY(hi))
6082 {
6083 item2 = dict_find(d2, hi->hi_key, -1);
6084 if (item2 == NULL)
6085 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006087 return FALSE;
6088 --todo;
6089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006090 }
6091 return TRUE;
6092}
6093
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094static int tv_equal_recurse_limit;
6095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006098 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006099 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100 */
6101 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006102tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 typval_T *tv1;
6104 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105 int ic; /* ignore case */
6106 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
6108 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006109 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006110 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006113 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006114 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006115
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 * recursiveness to a limit. We guess they are equal then.
6118 * A fixed limit has the problem of still taking an awful long time.
6119 * Reduce the limit every time running into it. That should work fine for
6120 * deeply linked structures that are not recursively linked and catch
6121 * recursiveness quickly. */
6122 if (!recursive)
6123 tv_equal_recurse_limit = 1000;
6124 if (recursive_cnt >= tv_equal_recurse_limit)
6125 {
6126 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006127 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006128 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129
6130 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 ++recursive_cnt;
6134 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6135 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006136 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 ++recursive_cnt;
6140 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6141 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006142 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 case VAR_FUNC:
6145 return (tv1->vval.v_string != NULL
6146 && tv2->vval.v_string != NULL
6147 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6148
6149 case VAR_NUMBER:
6150 return tv1->vval.v_number == tv2->vval.v_number;
6151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006152#ifdef FEAT_FLOAT
6153 case VAR_FLOAT:
6154 return tv1->vval.v_float == tv2->vval.v_float;
6155#endif
6156
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006157 case VAR_STRING:
6158 s1 = get_tv_string_buf(tv1, buf1);
6159 s2 = get_tv_string_buf(tv2, buf2);
6160 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006161 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006162
6163 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 return TRUE;
6165}
6166
6167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 * Locate item with index "n" in list "l" and return it.
6169 * A negative index is counted from the end; -1 is the last item.
6170 * Returns NULL when "n" is out of range.
6171 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006172 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006174 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 long n;
6176{
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 long idx;
6179
6180 if (l == NULL)
6181 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182
6183 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006184 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006185 n = l->lv_len + n;
6186
6187 /* Check for index out of range. */
6188 if (n < 0 || n >= l->lv_len)
6189 return NULL;
6190
6191 /* When there is a cached index may start search from there. */
6192 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_idx / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else if (n > (l->lv_idx + l->lv_len) / 2)
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
6206 else
6207 {
6208 /* closest to the cached index */
6209 item = l->lv_idx_item;
6210 idx = l->lv_idx;
6211 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006212 }
6213 else
6214 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006215 if (n < l->lv_len / 2)
6216 {
6217 /* closest to the start of the list */
6218 item = l->lv_first;
6219 idx = 0;
6220 }
6221 else
6222 {
6223 /* closest to the end of the list */
6224 item = l->lv_last;
6225 idx = l->lv_len - 1;
6226 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006228
6229 while (n > idx)
6230 {
6231 /* search forward */
6232 item = item->li_next;
6233 ++idx;
6234 }
6235 while (n < idx)
6236 {
6237 /* search backward */
6238 item = item->li_prev;
6239 --idx;
6240 }
6241
6242 /* cache the used index */
6243 l->lv_idx = idx;
6244 l->lv_idx_item = item;
6245
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 return item;
6247}
6248
6249/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 * Get list item "l[idx]" as a number.
6251 */
6252 static long
6253list_find_nr(l, idx, errorp)
6254 list_T *l;
6255 long idx;
6256 int *errorp; /* set to TRUE when something wrong */
6257{
6258 listitem_T *li;
6259
6260 li = list_find(l, idx);
6261 if (li == NULL)
6262 {
6263 if (errorp != NULL)
6264 *errorp = TRUE;
6265 return -1L;
6266 }
6267 return get_tv_number_chk(&li->li_tv, errorp);
6268}
6269
6270/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006271 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6272 */
6273 char_u *
6274list_find_str(l, idx)
6275 list_T *l;
6276 long idx;
6277{
6278 listitem_T *li;
6279
6280 li = list_find(l, idx - 1);
6281 if (li == NULL)
6282 {
6283 EMSGN(_(e_listidx), idx);
6284 return NULL;
6285 }
6286 return get_tv_string(&li->li_tv);
6287}
6288
6289/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290 * Locate "item" list "l" and return its index.
6291 * Returns -1 when "item" is not in the list.
6292 */
6293 static long
6294list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006297{
6298 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006299 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006300
6301 if (l == NULL)
6302 return -1;
6303 idx = 0;
6304 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6305 ++idx;
6306 if (li == NULL)
6307 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006308 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006309}
6310
6311/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 * Append item "item" to the end of list "l".
6313 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006314 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 list_T *l;
6317 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318{
6319 if (l->lv_last == NULL)
6320 {
6321 /* empty list */
6322 l->lv_first = item;
6323 l->lv_last = item;
6324 item->li_prev = NULL;
6325 }
6326 else
6327 {
6328 l->lv_last->li_next = item;
6329 item->li_prev = l->lv_last;
6330 l->lv_last = item;
6331 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 item->li_next = NULL;
6334}
6335
6336/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006337 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006340 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006341list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 list_T *l;
6343 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346
Bram Moolenaar05159a02005-02-26 23:04:13 +00006347 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006349 copy_tv(tv, &li->li_tv);
6350 list_append(l, li);
6351 return OK;
6352}
6353
6354/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006355 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006356 * Return FAIL when out of memory.
6357 */
6358 int
6359list_append_dict(list, dict)
6360 list_T *list;
6361 dict_T *dict;
6362{
6363 listitem_T *li = listitem_alloc();
6364
6365 if (li == NULL)
6366 return FAIL;
6367 li->li_tv.v_type = VAR_DICT;
6368 li->li_tv.v_lock = 0;
6369 li->li_tv.vval.v_dict = dict;
6370 list_append(list, li);
6371 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 return OK;
6373}
6374
6375/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 * Returns FAIL when out of memory.
6379 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006380 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006382 list_T *l;
6383 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006384 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006385{
6386 listitem_T *li = listitem_alloc();
6387
6388 if (li == NULL)
6389 return FAIL;
6390 list_append(l, li);
6391 li->li_tv.v_type = VAR_STRING;
6392 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006393 if (str == NULL)
6394 li->li_tv.vval.v_string = NULL;
6395 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006396 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006397 return FAIL;
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006402 * Append "n" to list "l".
6403 * Returns FAIL when out of memory.
6404 */
6405 static int
6406list_append_number(l, n)
6407 list_T *l;
6408 varnumber_T n;
6409{
6410 listitem_T *li;
6411
6412 li = listitem_alloc();
6413 if (li == NULL)
6414 return FAIL;
6415 li->li_tv.v_type = VAR_NUMBER;
6416 li->li_tv.v_lock = 0;
6417 li->li_tv.vval.v_number = n;
6418 list_append(l, li);
6419 return OK;
6420}
6421
6422/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 * If "item" is NULL append at the end.
6425 * Return FAIL when out of memory.
6426 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006427 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 list_T *l;
6430 typval_T *tv;
6431 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432{
Bram Moolenaar33570922005-01-25 22:26:29 +00006433 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434
6435 if (ni == NULL)
6436 return FAIL;
6437 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006438 list_insert(l, ni, item);
6439 return OK;
6440}
6441
6442 void
6443list_insert(l, ni, item)
6444 list_T *l;
6445 listitem_T *ni;
6446 listitem_T *item;
6447{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 if (item == NULL)
6449 /* Append new item at end of list. */
6450 list_append(l, ni);
6451 else
6452 {
6453 /* Insert new item before existing item. */
6454 ni->li_prev = item->li_prev;
6455 ni->li_next = item;
6456 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006459 ++l->lv_idx;
6460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006462 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 l->lv_idx_item = NULL;
6465 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469}
6470
6471/*
6472 * Extend "l1" with "l2".
6473 * If "bef" is NULL append at the end, otherwise insert before this item.
6474 * Returns FAIL when out of memory.
6475 */
6476 static int
6477list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 list_T *l1;
6479 list_T *l2;
6480 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006481{
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006483 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006485 /* We also quit the loop when we have inserted the original item count of
6486 * the list, avoid a hang when we extend a list with itself. */
6487 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006488 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6489 return FAIL;
6490 return OK;
6491}
6492
6493/*
6494 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6495 * Return FAIL when out of memory.
6496 */
6497 static int
6498list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l1;
6500 list_T *l2;
6501 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006505 if (l1 == NULL || l2 == NULL)
6506 return FAIL;
6507
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (l == NULL)
6511 return FAIL;
6512 tv->v_type = VAR_LIST;
6513 tv->vval.v_list = l;
6514
6515 /* append all items from the second list */
6516 return list_extend(l, l2, NULL);
6517}
6518
6519/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006521 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523 * Returns NULL when out of memory.
6524 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006525 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530{
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *copy;
6532 listitem_T *item;
6533 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534
6535 if (orig == NULL)
6536 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537
6538 copy = list_alloc();
6539 if (copy != NULL)
6540 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006541 if (copyID != 0)
6542 {
6543 /* Do this before adding the items, because one of the items may
6544 * refer back to this list. */
6545 orig->lv_copyID = copyID;
6546 orig->lv_copylist = copy;
6547 }
6548 for (item = orig->lv_first; item != NULL && !got_int;
6549 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 {
6551 ni = listitem_alloc();
6552 if (ni == NULL)
6553 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006555 {
6556 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6557 {
6558 vim_free(ni);
6559 break;
6560 }
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006563 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 list_append(copy, ni);
6565 }
6566 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 if (item != NULL)
6568 {
6569 list_unref(copy);
6570 copy = NULL;
6571 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572 }
6573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 return copy;
6575}
6576
6577/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006579 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006580 * This used to be called list_remove, but that conflicts with a Sun header
6581 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006583 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006584vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *l;
6586 listitem_T *item;
6587 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006588{
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 /* notify watchers */
6592 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006594 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 list_fix_watch(l, ip);
6596 if (ip == item2)
6597 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006599
6600 if (item2->li_next == NULL)
6601 l->lv_last = item->li_prev;
6602 else
6603 item2->li_next->li_prev = item->li_prev;
6604 if (item->li_prev == NULL)
6605 l->lv_first = item2->li_next;
6606 else
6607 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006608 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609}
6610
6611/*
6612 * Return an allocated string with the string representation of a list.
6613 * May return NULL.
6614 */
6615 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006616list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006618 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619{
6620 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621
6622 if (tv->vval.v_list == NULL)
6623 return NULL;
6624 ga_init2(&ga, (int)sizeof(char), 80);
6625 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006626 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006627 {
6628 vim_free(ga.ga_data);
6629 return NULL;
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 ga_append(&ga, ']');
6632 ga_append(&ga, NUL);
6633 return (char_u *)ga.ga_data;
6634}
6635
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006636typedef struct join_S {
6637 char_u *s;
6638 char_u *tofree;
6639} join_T;
6640
6641 static int
6642list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6643 garray_T *gap; /* to store the result in */
6644 list_T *l;
6645 char_u *sep;
6646 int echo_style;
6647 int copyID;
6648 garray_T *join_gap; /* to keep each list item string */
6649{
6650 int i;
6651 join_T *p;
6652 int len;
6653 int sumlen = 0;
6654 int first = TRUE;
6655 char_u *tofree;
6656 char_u numbuf[NUMBUFLEN];
6657 listitem_T *item;
6658 char_u *s;
6659
6660 /* Stringify each item in the list. */
6661 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6662 {
6663 if (echo_style)
6664 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6665 else
6666 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6667 if (s == NULL)
6668 return FAIL;
6669
6670 len = (int)STRLEN(s);
6671 sumlen += len;
6672
6673 ga_grow(join_gap, 1);
6674 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6675 if (tofree != NULL || s != numbuf)
6676 {
6677 p->s = s;
6678 p->tofree = tofree;
6679 }
6680 else
6681 {
6682 p->s = vim_strnsave(s, len);
6683 p->tofree = p->s;
6684 }
6685
6686 line_breakcheck();
6687 }
6688
6689 /* Allocate result buffer with its total size, avoid re-allocation and
6690 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6691 if (join_gap->ga_len >= 2)
6692 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6693 if (ga_grow(gap, sumlen + 2) == FAIL)
6694 return FAIL;
6695
6696 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6697 {
6698 if (first)
6699 first = FALSE;
6700 else
6701 ga_concat(gap, sep);
6702 p = ((join_T *)join_gap->ga_data) + i;
6703
6704 if (p->s != NULL)
6705 ga_concat(gap, p->s);
6706 line_breakcheck();
6707 }
6708
6709 return OK;
6710}
6711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006714 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006717 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006718list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006719 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006721 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006722 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006723 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006724{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 garray_T join_ga;
6726 int retval;
6727 join_T *p;
6728 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6731 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6732
6733 /* Dispose each item in join_ga. */
6734 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006735 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 p = (join_T *)join_ga.ga_data;
6737 for (i = 0; i < join_ga.ga_len; ++i)
6738 {
6739 vim_free(p->tofree);
6740 ++p;
6741 }
6742 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006744
6745 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006746}
6747
6748/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749 * Garbage collection for lists and dictionaries.
6750 *
6751 * We use reference counts to be able to free most items right away when they
6752 * are no longer used. But for composite items it's possible that it becomes
6753 * unused while the reference count is > 0: When there is a recursive
6754 * reference. Example:
6755 * :let l = [1, 2, 3]
6756 * :let d = {9: l}
6757 * :let l[1] = d
6758 *
6759 * Since this is quite unusual we handle this with garbage collection: every
6760 * once in a while find out which lists and dicts are not referenced from any
6761 * variable.
6762 *
6763 * Here is a good reference text about garbage collection (refers to Python
6764 * but it applies to all reference-counting mechanisms):
6765 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006766 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767
6768/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006769 * Do garbage collection for lists and dicts.
6770 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006771 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 int
6773garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006775 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776 buf_T *buf;
6777 win_T *wp;
6778 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006779 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006780 int did_free;
6781 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 tabpage_T *tp;
6784#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006786 /* Only do this once. */
6787 want_garbage_collect = FALSE;
6788 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006789 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006791 /* We advance by two because we add one for items referenced through
6792 * previous_funccal. */
6793 current_copyID += COPYID_INC;
6794 copyID = current_copyID;
6795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 /*
6797 * 1. Go through all accessible variables and mark all lists and dicts
6798 * with copyID.
6799 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006800
6801 /* Don't free variables in the previous_funccal list unless they are only
6802 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006803 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006804 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6805 {
6806 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6807 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6808 }
6809
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 /* script-local variables */
6811 for (i = 1; i <= ga_scripts.ga_len; ++i)
6812 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6813
6814 /* buffer-local variables */
6815 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006816 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006817
6818 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006819 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006820 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006821#ifdef FEAT_AUTOCMD
6822 if (aucmd_win != NULL)
6823 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6824#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826#ifdef FEAT_WINDOWS
6827 /* tabpage-local variables */
6828 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006829 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006830#endif
6831
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 /* global variables */
6833 set_ref_in_ht(&globvarht, copyID);
6834
6835 /* function-local variables */
6836 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6837 {
6838 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6839 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6840 }
6841
Bram Moolenaard812df62008-11-09 12:46:09 +00006842 /* v: vars */
6843 set_ref_in_ht(&vimvarht, copyID);
6844
Bram Moolenaar1dced572012-04-05 16:54:08 +02006845#ifdef FEAT_LUA
6846 set_ref_in_lua(copyID);
6847#endif
6848
Bram Moolenaardb913952012-06-29 12:54:53 +02006849#ifdef FEAT_PYTHON
6850 set_ref_in_python(copyID);
6851#endif
6852
6853#ifdef FEAT_PYTHON3
6854 set_ref_in_python3(copyID);
6855#endif
6856
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006857 /*
6858 * 2. Free lists and dictionaries that are not referenced.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 did_free = free_unref_items(copyID);
6861
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006862 /*
6863 * 3. Check if any funccal can be freed now.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 for (pfc = &previous_funccal; *pfc != NULL; )
6866 {
6867 if (can_free_funccal(*pfc, copyID))
6868 {
6869 fc = *pfc;
6870 *pfc = fc->caller;
6871 free_funccal(fc, TRUE);
6872 did_free = TRUE;
6873 did_free_funccal = TRUE;
6874 }
6875 else
6876 pfc = &(*pfc)->caller;
6877 }
6878 if (did_free_funccal)
6879 /* When a funccal was freed some more items might be garbage
6880 * collected, so run again. */
6881 (void)garbage_collect();
6882
6883 return did_free;
6884}
6885
6886/*
6887 * Free lists and dictionaries that are no longer referenced.
6888 */
6889 static int
6890free_unref_items(copyID)
6891 int copyID;
6892{
6893 dict_T *dd;
6894 list_T *ll;
6895 int did_free = FALSE;
6896
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 */
6900 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006901 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006903 /* Free the Dictionary and ordinary items it contains, but don't
6904 * recurse into Lists and Dictionaries, they will be in the list
6905 * of dicts or list of lists. */
6906 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 did_free = TRUE;
6908
6909 /* restart, next dict may also have been freed */
6910 dd = first_dict;
6911 }
6912 else
6913 dd = dd->dv_used_next;
6914
6915 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006916 * Go through the list of lists and free items without the copyID.
6917 * But don't free a list that has a watcher (used in a for loop), these
6918 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 */
6920 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006921 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6922 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006924 /* Free the List and ordinary items it contains, but don't recurse
6925 * into Lists and Dictionaries, they will be in the list of dicts
6926 * or list of lists. */
6927 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928 did_free = TRUE;
6929
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006930 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 ll = first_list;
6932 }
6933 else
6934 ll = ll->lv_used_next;
6935
6936 return did_free;
6937}
6938
6939/*
6940 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6941 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006942 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943set_ref_in_ht(ht, copyID)
6944 hashtab_T *ht;
6945 int copyID;
6946{
6947 int todo;
6948 hashitem_T *hi;
6949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006950 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 for (hi = ht->ht_array; todo > 0; ++hi)
6952 if (!HASHITEM_EMPTY(hi))
6953 {
6954 --todo;
6955 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6956 }
6957}
6958
6959/*
6960 * Mark all lists and dicts referenced through list "l" with "copyID".
6961 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006962 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963set_ref_in_list(l, copyID)
6964 list_T *l;
6965 int copyID;
6966{
6967 listitem_T *li;
6968
6969 for (li = l->lv_first; li != NULL; li = li->li_next)
6970 set_ref_in_item(&li->li_tv, copyID);
6971}
6972
6973/*
6974 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6975 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006976 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977set_ref_in_item(tv, copyID)
6978 typval_T *tv;
6979 int copyID;
6980{
6981 dict_T *dd;
6982 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 switch (tv->v_type)
6985 {
6986 case VAR_DICT:
6987 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006988 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006989 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 /* Didn't see this dict yet. */
6991 dd->dv_copyID = copyID;
6992 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995
6996 case VAR_LIST:
6997 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006998 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006999 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /* Didn't see this list yet. */
7001 ll->lv_copyID = copyID;
7002 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007003 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007005 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007007}
7008
7009/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007010 * Allocate an empty header for a dictionary.
7011 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007012 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013dict_alloc()
7014{
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007016
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007019 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007020 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 if (first_dict != NULL)
7022 first_dict->dv_used_prev = d;
7023 d->dv_used_next = first_dict;
7024 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007025 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026
Bram Moolenaar33570922005-01-25 22:26:29 +00007027 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007028 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007029 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007030 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007031 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007032 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034}
7035
7036/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007037 * Allocate an empty dict for a return value.
7038 * Returns OK or FAIL.
7039 */
7040 static int
7041rettv_dict_alloc(rettv)
7042 typval_T *rettv;
7043{
7044 dict_T *d = dict_alloc();
7045
7046 if (d == NULL)
7047 return FAIL;
7048
7049 rettv->vval.v_dict = d;
7050 rettv->v_type = VAR_DICT;
7051 ++d->dv_refcount;
7052 return OK;
7053}
7054
7055
7056/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057 * Unreference a Dictionary: decrement the reference count and free it when it
7058 * becomes zero.
7059 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007060 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007064 if (d != NULL && --d->dv_refcount <= 0)
7065 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007066}
7067
7068/*
7069 * Free a Dictionary, including all items it contains.
7070 * Ignores the reference count.
7071 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007072 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007073dict_free(d, recurse)
7074 dict_T *d;
7075 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007078 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007079 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081 /* Remove the dict from the list of dicts for garbage collection. */
7082 if (d->dv_used_prev == NULL)
7083 first_dict = d->dv_used_next;
7084 else
7085 d->dv_used_prev->dv_used_next = d->dv_used_next;
7086 if (d->dv_used_next != NULL)
7087 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7088
7089 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007090 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007091 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007092 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 if (!HASHITEM_EMPTY(hi))
7095 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007096 /* Remove the item before deleting it, just in case there is
7097 * something recursive causing trouble. */
7098 di = HI2DI(hi);
7099 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007100 if (recurse || (di->di_tv.v_type != VAR_LIST
7101 && di->di_tv.v_type != VAR_DICT))
7102 clear_tv(&di->di_tv);
7103 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007104 --todo;
7105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108 vim_free(d);
7109}
7110
7111/*
7112 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 * The "key" is copied to the new item.
7114 * Note that the value of the item "di_tv" still needs to be initialized!
7115 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007117 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118dictitem_alloc(key)
7119 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007120{
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007123 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007124 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007125 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007127 di->di_flags = 0;
7128 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130}
7131
7132/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007133 * Make a copy of a Dictionary item.
7134 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007135 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007136dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138{
Bram Moolenaar33570922005-01-25 22:26:29 +00007139 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007141 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7142 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007143 if (di != NULL)
7144 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007146 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147 copy_tv(&org->di_tv, &di->di_tv);
7148 }
7149 return di;
7150}
7151
7152/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153 * Remove item "item" from Dictionary "dict" and free it.
7154 */
7155 static void
7156dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *dict;
7158 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161
Bram Moolenaar33570922005-01-25 22:26:29 +00007162 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 if (HASHITEM_EMPTY(hi))
7164 EMSG2(_(e_intern2), "dictitem_remove()");
7165 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 dictitem_free(item);
7168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Free a dict item. Also clears the value.
7172 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007173 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 clear_tv(&item->di_tv);
7178 vim_free(item);
7179}
7180
7181/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007182 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7183 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 * Returns NULL when out of memory.
7186 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007190 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007191 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192{
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 dict_T *copy;
7194 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007196 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197
7198 if (orig == NULL)
7199 return NULL;
7200
7201 copy = dict_alloc();
7202 if (copy != NULL)
7203 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 if (copyID != 0)
7205 {
7206 orig->dv_copyID = copyID;
7207 orig->dv_copydict = copy;
7208 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007209 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007210 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 --todo;
7215
7216 di = dictitem_alloc(hi->hi_key);
7217 if (di == NULL)
7218 break;
7219 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 {
7221 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7222 copyID) == FAIL)
7223 {
7224 vim_free(di);
7225 break;
7226 }
7227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007228 else
7229 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7230 if (dict_add(copy, di) == FAIL)
7231 {
7232 dictitem_free(di);
7233 break;
7234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007239 if (todo > 0)
7240 {
7241 dict_unref(copy);
7242 copy = NULL;
7243 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007244 }
7245
7246 return copy;
7247}
7248
7249/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007251 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007253 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 dict_T *d;
7256 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257{
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007259}
7260
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007262 * Add a number or string entry to dictionary "d".
7263 * When "str" is NULL use number "nr", otherwise use "str".
7264 * Returns FAIL when out of memory and when key already exists.
7265 */
7266 int
7267dict_add_nr_str(d, key, nr, str)
7268 dict_T *d;
7269 char *key;
7270 long nr;
7271 char_u *str;
7272{
7273 dictitem_T *item;
7274
7275 item = dictitem_alloc((char_u *)key);
7276 if (item == NULL)
7277 return FAIL;
7278 item->di_tv.v_lock = 0;
7279 if (str == NULL)
7280 {
7281 item->di_tv.v_type = VAR_NUMBER;
7282 item->di_tv.vval.v_number = nr;
7283 }
7284 else
7285 {
7286 item->di_tv.v_type = VAR_STRING;
7287 item->di_tv.vval.v_string = vim_strsave(str);
7288 }
7289 if (dict_add(d, item) == FAIL)
7290 {
7291 dictitem_free(item);
7292 return FAIL;
7293 }
7294 return OK;
7295}
7296
7297/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007298 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007299 * Returns FAIL when out of memory and when key already exists.
7300 */
7301 int
7302dict_add_list(d, key, list)
7303 dict_T *d;
7304 char *key;
7305 list_T *list;
7306{
7307 dictitem_T *item;
7308
7309 item = dictitem_alloc((char_u *)key);
7310 if (item == NULL)
7311 return FAIL;
7312 item->di_tv.v_lock = 0;
7313 item->di_tv.v_type = VAR_LIST;
7314 item->di_tv.vval.v_list = list;
7315 if (dict_add(d, item) == FAIL)
7316 {
7317 dictitem_free(item);
7318 return FAIL;
7319 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007320 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007321 return OK;
7322}
7323
7324/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 * Get the number of items in a Dictionary.
7326 */
7327 static long
7328dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007329 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331 if (d == NULL)
7332 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007334}
7335
7336/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 * Find item "key[len]" in Dictionary "d".
7338 * If "len" is negative use strlen(key).
7339 * Returns NULL when not found.
7340 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007341 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007343 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 char_u *key;
7345 int len;
7346{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347#define AKEYLEN 200
7348 char_u buf[AKEYLEN];
7349 char_u *akey;
7350 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007352
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (len < 0)
7354 akey = key;
7355 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007356 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 tofree = akey = vim_strnsave(key, len);
7358 if (akey == NULL)
7359 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007360 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007361 else
7362 {
7363 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007364 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007365 akey = buf;
7366 }
7367
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 vim_free(tofree);
7370 if (HASHITEM_EMPTY(hi))
7371 return NULL;
7372 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373}
7374
7375/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007376 * Get a string item from a dictionary.
7377 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007378 * Returns NULL if the entry doesn't exist or out of memory.
7379 */
7380 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007381get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007382 dict_T *d;
7383 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007384 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007385{
7386 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007387 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007388
7389 di = dict_find(d, key, -1);
7390 if (di == NULL)
7391 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007392 s = get_tv_string(&di->di_tv);
7393 if (save && s != NULL)
7394 s = vim_strsave(s);
7395 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007396}
7397
7398/*
7399 * Get a number item from a dictionary.
7400 * Returns 0 if the entry doesn't exist or out of memory.
7401 */
7402 long
7403get_dict_number(d, key)
7404 dict_T *d;
7405 char_u *key;
7406{
7407 dictitem_T *di;
7408
7409 di = dict_find(d, key, -1);
7410 if (di == NULL)
7411 return 0;
7412 return get_tv_number(&di->di_tv);
7413}
7414
7415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 * Return an allocated string with the string representation of a Dictionary.
7417 * May return NULL.
7418 */
7419 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007420dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007422 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423{
7424 garray_T ga;
7425 int first = TRUE;
7426 char_u *tofree;
7427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007429 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 return NULL;
7435 ga_init2(&ga, (int)sizeof(char), 80);
7436 ga_append(&ga, '{');
7437
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007438 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007439 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007443 --todo;
7444
7445 if (first)
7446 first = FALSE;
7447 else
7448 ga_concat(&ga, (char_u *)", ");
7449
7450 tofree = string_quote(hi->hi_key, FALSE);
7451 if (tofree != NULL)
7452 {
7453 ga_concat(&ga, tofree);
7454 vim_free(tofree);
7455 }
7456 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458 if (s != NULL)
7459 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007461 if (s == NULL)
7462 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007465 if (todo > 0)
7466 {
7467 vim_free(ga.ga_data);
7468 return NULL;
7469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
7471 ga_append(&ga, '}');
7472 ga_append(&ga, NUL);
7473 return (char_u *)ga.ga_data;
7474}
7475
7476/*
7477 * Allocate a variable for a Dictionary and fill it from "*arg".
7478 * Return OK or FAIL. Returns NOTDONE for {expr}.
7479 */
7480 static int
7481get_dict_tv(arg, rettv, evaluate)
7482 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 int evaluate;
7485{
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 dict_T *d = NULL;
7487 typval_T tvkey;
7488 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007489 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493
7494 /*
7495 * First check if it's not a curly-braces thing: {expr}.
7496 * Must do this without evaluating, otherwise a function may be called
7497 * twice. Unfortunately this means we need to call eval1() twice for the
7498 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007499 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 if (*start != '}')
7502 {
7503 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7504 return FAIL;
7505 if (*start == '}')
7506 return NOTDONE;
7507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 if (evaluate)
7510 {
7511 d = dict_alloc();
7512 if (d == NULL)
7513 return FAIL;
7514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 tvkey.v_type = VAR_UNKNOWN;
7516 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517
7518 *arg = skipwhite(*arg + 1);
7519 while (**arg != '}' && **arg != NUL)
7520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 goto failret;
7523 if (**arg != ':')
7524 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007525 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 goto failret;
7528 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007529 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007531 key = get_tv_string_buf_chk(&tvkey, buf);
7532 if (key == NULL || *key == NUL)
7533 {
7534 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7535 if (key != NULL)
7536 EMSG(_(e_emptykey));
7537 clear_tv(&tvkey);
7538 goto failret;
7539 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541
7542 *arg = skipwhite(*arg + 1);
7543 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7544 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007545 if (evaluate)
7546 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 goto failret;
7548 }
7549 if (evaluate)
7550 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007552 if (item != NULL)
7553 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007554 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 clear_tv(&tv);
7557 goto failret;
7558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 item = dictitem_alloc(key);
7560 clear_tv(&tvkey);
7561 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007564 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 if (dict_add(d, item) == FAIL)
7566 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 }
7568 }
7569
7570 if (**arg == '}')
7571 break;
7572 if (**arg != ',')
7573 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007574 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 goto failret;
7576 }
7577 *arg = skipwhite(*arg + 1);
7578 }
7579
7580 if (**arg != '}')
7581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007582 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583failret:
7584 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007585 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 return FAIL;
7587 }
7588
7589 *arg = skipwhite(*arg + 1);
7590 if (evaluate)
7591 {
7592 rettv->v_type = VAR_DICT;
7593 rettv->vval.v_dict = d;
7594 ++d->dv_refcount;
7595 }
7596
7597 return OK;
7598}
7599
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 * Return a string with the string representation of a variable.
7602 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007603 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007604 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007605 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007606 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007607 */
7608 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007610 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007611 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007612 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 static int recurse = 0;
7616 char_u *r = NULL;
7617
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007620 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621 *tofree = NULL;
7622 return NULL;
7623 }
7624 ++recurse;
7625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007626 switch (tv->v_type)
7627 {
7628 case VAR_FUNC:
7629 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 r = tv->vval.v_string;
7631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007633 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007634 if (tv->vval.v_list == NULL)
7635 {
7636 *tofree = NULL;
7637 r = NULL;
7638 }
7639 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7640 {
7641 *tofree = NULL;
7642 r = (char_u *)"[...]";
7643 }
7644 else
7645 {
7646 tv->vval.v_list->lv_copyID = copyID;
7647 *tofree = list2string(tv, copyID);
7648 r = *tofree;
7649 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007651
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 if (tv->vval.v_dict == NULL)
7654 {
7655 *tofree = NULL;
7656 r = NULL;
7657 }
7658 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7659 {
7660 *tofree = NULL;
7661 r = (char_u *)"{...}";
7662 }
7663 else
7664 {
7665 tv->vval.v_dict->dv_copyID = copyID;
7666 *tofree = dict2string(tv, copyID);
7667 r = *tofree;
7668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671 case VAR_STRING:
7672 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 *tofree = NULL;
7674 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007675 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007677#ifdef FEAT_FLOAT
7678 case VAR_FLOAT:
7679 *tofree = NULL;
7680 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7681 r = numbuf;
7682 break;
7683#endif
7684
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689
7690 --recurse;
7691 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692}
7693
7694/*
7695 * Return a string with the string representation of a variable.
7696 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7697 * "numbuf" is used for a number.
7698 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007699 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 */
7701 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007702tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 char_u **tofree;
7705 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007706 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007707{
7708 switch (tv->v_type)
7709 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 case VAR_FUNC:
7711 *tofree = string_quote(tv->vval.v_string, TRUE);
7712 return *tofree;
7713 case VAR_STRING:
7714 *tofree = string_quote(tv->vval.v_string, FALSE);
7715 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007716#ifdef FEAT_FLOAT
7717 case VAR_FLOAT:
7718 *tofree = NULL;
7719 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7720 return numbuf;
7721#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007725 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007727 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007728 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007729 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007730}
7731
7732/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 * Return string "str" in ' quotes, doubling ' characters.
7734 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007736 */
7737 static char_u *
7738string_quote(str, function)
7739 char_u *str;
7740 int function;
7741{
Bram Moolenaar33570922005-01-25 22:26:29 +00007742 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007743 char_u *p, *r, *s;
7744
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 len = (function ? 13 : 3);
7746 if (str != NULL)
7747 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007748 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007749 for (p = str; *p != NUL; mb_ptr_adv(p))
7750 if (*p == '\'')
7751 ++len;
7752 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 s = r = alloc(len);
7754 if (r != NULL)
7755 {
7756 if (function)
7757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 r += 10;
7760 }
7761 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 if (str != NULL)
7764 for (p = str; *p != NUL; )
7765 {
7766 if (*p == '\'')
7767 *r++ = '\'';
7768 MB_COPY_CHAR(p, r);
7769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 if (function)
7772 *r++ = ')';
7773 *r++ = NUL;
7774 }
7775 return s;
7776}
7777
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007778#ifdef FEAT_FLOAT
7779/*
7780 * Convert the string "text" to a floating point number.
7781 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7782 * this always uses a decimal point.
7783 * Returns the length of the text that was consumed.
7784 */
7785 static int
7786string2float(text, value)
7787 char_u *text;
7788 float_T *value; /* result stored here */
7789{
7790 char *s = (char *)text;
7791 float_T f;
7792
7793 f = strtod(s, &s);
7794 *value = f;
7795 return (int)((char_u *)s - text);
7796}
7797#endif
7798
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 * Get the value of an environment variable.
7801 * "arg" is pointing to the '$'. It is advanced to after the name.
7802 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007803 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 */
7805 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007806get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 int evaluate;
7810{
7811 char_u *string = NULL;
7812 int len;
7813 int cc;
7814 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007815 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 ++*arg;
7818 name = *arg;
7819 len = get_env_len(arg);
7820 if (evaluate)
7821 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007822 if (len == 0)
7823 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007824
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007825 cc = name[len];
7826 name[len] = NUL;
7827 /* first try vim_getenv(), fast for normal environment vars */
7828 string = vim_getenv(name, &mustfree);
7829 if (string != NULL && *string != NUL)
7830 {
7831 if (!mustfree)
7832 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007834 else
7835 {
7836 if (mustfree)
7837 vim_free(string);
7838
7839 /* next try expanding things like $VIM and ${HOME} */
7840 string = expand_env_save(name - 1);
7841 if (string != NULL && *string == '$')
7842 {
7843 vim_free(string);
7844 string = NULL;
7845 }
7846 }
7847 name[len] = cc;
7848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007849 rettv->v_type = VAR_STRING;
7850 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852
7853 return OK;
7854}
7855
7856/*
7857 * Array with names and number of arguments of all internal functions
7858 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7859 */
7860static struct fst
7861{
7862 char *f_name; /* function name */
7863 char f_min_argc; /* minimal number of arguments */
7864 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007866 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867} functions[] =
7868{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#ifdef FEAT_FLOAT
7870 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007871 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007873 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007874 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"append", 2, 2, f_append},
7876 {"argc", 0, 0, f_argc},
7877 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007878 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007880 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007882 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007885 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"bufexists", 1, 1, f_bufexists},
7887 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7888 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7889 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7890 {"buflisted", 1, 1, f_buflisted},
7891 {"bufloaded", 1, 1, f_bufloaded},
7892 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007893 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"bufwinnr", 1, 1, f_bufwinnr},
7895 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007896 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007897 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007899#ifdef FEAT_FLOAT
7900 {"ceil", 1, 1, f_ceil},
7901#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007902 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007903 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007905 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007907#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007908 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007909 {"complete_add", 1, 1, f_complete_add},
7910 {"complete_check", 0, 0, f_complete_check},
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#ifdef FEAT_FLOAT
7915 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007916 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007920 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007921 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"delete", 1, 1, f_delete},
7923 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007924 {"diff_filler", 1, 1, f_diff_filler},
7925 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007926 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"eventhandler", 0, 0, f_eventhandler},
7930 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007931 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007933#ifdef FEAT_FLOAT
7934 {"exp", 1, 1, f_exp},
7935#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007936 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007937 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007938 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7940 {"filereadable", 1, 1, f_filereadable},
7941 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007943 {"finddir", 1, 3, f_finddir},
7944 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007945#ifdef FEAT_FLOAT
7946 {"float2nr", 1, 1, f_float2nr},
7947 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007948 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007949#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007950 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {"fnamemodify", 2, 2, f_fnamemodify},
7952 {"foldclosed", 1, 1, f_foldclosed},
7953 {"foldclosedend", 1, 1, f_foldclosedend},
7954 {"foldlevel", 1, 1, f_foldlevel},
7955 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007956 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007959 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007961 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007962 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {"getchar", 0, 1, f_getchar},
7964 {"getcharmod", 0, 0, f_getcharmod},
7965 {"getcmdline", 0, 0, f_getcmdline},
7966 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007967 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007969 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007970 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"getfsize", 1, 1, f_getfsize},
7972 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007973 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007974 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007975 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007976 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007977 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007978 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007979 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007980 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007982 {"gettabvar", 2, 3, f_gettabvar},
7983 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"getwinposx", 0, 0, f_getwinposx},
7985 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007986 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007987 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02007988 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007991 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007992 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7994 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7995 {"histadd", 2, 2, f_histadd},
7996 {"histdel", 1, 2, f_histdel},
7997 {"histget", 1, 2, f_histget},
7998 {"histnr", 1, 1, f_histnr},
7999 {"hlID", 1, 1, f_hlID},
8000 {"hlexists", 1, 1, f_hlexists},
8001 {"hostname", 0, 0, f_hostname},
8002 {"iconv", 3, 3, f_iconv},
8003 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008005 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008007 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"inputrestore", 0, 0, f_inputrestore},
8009 {"inputsave", 0, 0, f_inputsave},
8010 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008011 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008012 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008014 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008016 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"libcall", 3, 3, f_libcall},
8021 {"libcallnr", 3, 3, f_libcallnr},
8022 {"line", 1, 1, f_line},
8023 {"line2byte", 1, 1, f_line2byte},
8024 {"lispindent", 1, 1, f_lispindent},
8025 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008026#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008027 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008028 {"log10", 1, 1, f_log10},
8029#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008030#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008031 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008032#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008033 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008034 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008035 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008036 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008037 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008038 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008039 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008040 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008041 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008042 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008043 {"max", 1, 1, f_max},
8044 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008045#ifdef vim_mkdir
8046 {"mkdir", 1, 3, f_mkdir},
8047#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008048 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008049#ifdef FEAT_MZSCHEME
8050 {"mzeval", 1, 1, f_mzeval},
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008053 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008054 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008055 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008056#ifdef FEAT_FLOAT
8057 {"pow", 2, 2, f_pow},
8058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008060 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008061 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008062#ifdef FEAT_PYTHON3
8063 {"py3eval", 1, 1, f_py3eval},
8064#endif
8065#ifdef FEAT_PYTHON
8066 {"pyeval", 1, 1, f_pyeval},
8067#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008069 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008070 {"reltime", 0, 2, f_reltime},
8071 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 {"remote_expr", 2, 3, f_remote_expr},
8073 {"remote_foreground", 1, 1, f_remote_foreground},
8074 {"remote_peek", 1, 2, f_remote_peek},
8075 {"remote_read", 1, 1, f_remote_read},
8076 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008077 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008079 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008081 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082#ifdef FEAT_FLOAT
8083 {"round", 1, 1, f_round},
8084#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008085 {"screenattr", 2, 2, f_screenattr},
8086 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008087 {"screencol", 0, 0, f_screencol},
8088 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008089 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008090 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008091 {"searchpair", 3, 7, f_searchpair},
8092 {"searchpairpos", 3, 7, f_searchpairpos},
8093 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"server2client", 2, 2, f_server2client},
8095 {"serverlist", 0, 0, f_serverlist},
8096 {"setbufvar", 3, 3, f_setbufvar},
8097 {"setcmdpos", 1, 1, f_setcmdpos},
8098 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008099 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008100 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008101 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008102 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008104 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008105 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008107#ifdef FEAT_CRYPT
8108 {"sha256", 1, 1, f_sha256},
8109#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008110 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008111 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008113#ifdef FEAT_FLOAT
8114 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008115 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008117 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008118 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008119 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008120 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008121 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008122#ifdef FEAT_FLOAT
8123 {"sqrt", 1, 1, f_sqrt},
8124 {"str2float", 1, 1, f_str2float},
8125#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008126 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008127 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008128 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129#ifdef HAVE_STRFTIME
8130 {"strftime", 1, 2, f_strftime},
8131#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008132 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008133 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {"strlen", 1, 1, f_strlen},
8135 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008136 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008138 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008139 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"substitute", 4, 4, f_substitute},
8141 {"synID", 3, 3, f_synID},
8142 {"synIDattr", 2, 3, f_synIDattr},
8143 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008144 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008145 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008146 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008147 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008148 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008149 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008150 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008151 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008152 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008153#ifdef FEAT_FLOAT
8154 {"tan", 1, 1, f_tan},
8155 {"tanh", 1, 1, f_tanh},
8156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008158 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"tolower", 1, 1, f_tolower},
8160 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008161 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#ifdef FEAT_FLOAT
8163 {"trunc", 1, 1, f_trunc},
8164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008166 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008167 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008168 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008169 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"virtcol", 1, 1, f_virtcol},
8171 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008172 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"winbufnr", 1, 1, f_winbufnr},
8174 {"wincol", 0, 0, f_wincol},
8175 {"winheight", 1, 1, f_winheight},
8176 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008177 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008179 {"winrestview", 1, 1, f_winrestview},
8180 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008182 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008183 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184};
8185
8186#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8187
8188/*
8189 * Function given to ExpandGeneric() to obtain the list of internal
8190 * or user defined function names.
8191 */
8192 char_u *
8193get_function_name(xp, idx)
8194 expand_T *xp;
8195 int idx;
8196{
8197 static int intidx = -1;
8198 char_u *name;
8199
8200 if (idx == 0)
8201 intidx = -1;
8202 if (intidx < 0)
8203 {
8204 name = get_user_func_name(xp, idx);
8205 if (name != NULL)
8206 return name;
8207 }
8208 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8209 {
8210 STRCPY(IObuff, functions[intidx].f_name);
8211 STRCAT(IObuff, "(");
8212 if (functions[intidx].f_max_argc == 0)
8213 STRCAT(IObuff, ")");
8214 return IObuff;
8215 }
8216
8217 return NULL;
8218}
8219
8220/*
8221 * Function given to ExpandGeneric() to obtain the list of internal or
8222 * user defined variable or function names.
8223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 char_u *
8225get_expr_name(xp, idx)
8226 expand_T *xp;
8227 int idx;
8228{
8229 static int intidx = -1;
8230 char_u *name;
8231
8232 if (idx == 0)
8233 intidx = -1;
8234 if (intidx < 0)
8235 {
8236 name = get_function_name(xp, idx);
8237 if (name != NULL)
8238 return name;
8239 }
8240 return get_user_var_name(xp, ++intidx);
8241}
8242
8243#endif /* FEAT_CMDL_COMPL */
8244
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008245#if defined(EBCDIC) || defined(PROTO)
8246/*
8247 * Compare struct fst by function name.
8248 */
8249 static int
8250compare_func_name(s1, s2)
8251 const void *s1;
8252 const void *s2;
8253{
8254 struct fst *p1 = (struct fst *)s1;
8255 struct fst *p2 = (struct fst *)s2;
8256
8257 return STRCMP(p1->f_name, p2->f_name);
8258}
8259
8260/*
8261 * Sort the function table by function name.
8262 * The sorting of the table above is ASCII dependant.
8263 * On machines using EBCDIC we have to sort it.
8264 */
8265 static void
8266sortFunctions()
8267{
8268 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269
8270 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8271}
8272#endif
8273
8274
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275/*
8276 * Find internal function in table above.
8277 * Return index, or -1 if not found
8278 */
8279 static int
8280find_internal_func(name)
8281 char_u *name; /* name of the function */
8282{
8283 int first = 0;
8284 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8285 int cmp;
8286 int x;
8287
8288 /*
8289 * Find the function name in the table. Binary search.
8290 */
8291 while (first <= last)
8292 {
8293 x = first + ((unsigned)(last - first) >> 1);
8294 cmp = STRCMP(name, functions[x].f_name);
8295 if (cmp < 0)
8296 last = x - 1;
8297 else if (cmp > 0)
8298 first = x + 1;
8299 else
8300 return x;
8301 }
8302 return -1;
8303}
8304
8305/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8307 * name it contains, otherwise return "name".
8308 */
8309 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008310deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008311 char_u *name;
8312 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008313 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314{
Bram Moolenaar33570922005-01-25 22:26:29 +00008315 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316 int cc;
8317
8318 cc = name[*lenp];
8319 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008320 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 {
8326 *lenp = 0;
8327 return (char_u *)""; /* just in case */
8328 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008329 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 }
8332
8333 return name;
8334}
8335
8336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 * Allocate a variable for the result of a function.
8338 * Return OK or FAIL.
8339 */
8340 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008341get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8342 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 char_u *name; /* name of the function */
8344 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 char_u **arg; /* argument, pointing to the '(' */
8347 linenr_T firstline; /* first line of range */
8348 linenr_T lastline; /* last line of range */
8349 int *doesrange; /* return: function handled range */
8350 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 char_u *argp;
8354 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008355 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 int argcount = 0; /* number of arguments found */
8357
8358 /*
8359 * Get the arguments.
8360 */
8361 argp = *arg;
8362 while (argcount < MAX_FUNC_ARGS)
8363 {
8364 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8365 if (*argp == ')' || *argp == ',' || *argp == NUL)
8366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8368 {
8369 ret = FAIL;
8370 break;
8371 }
8372 ++argcount;
8373 if (*argp != ',')
8374 break;
8375 }
8376 if (*argp == ')')
8377 ++argp;
8378 else
8379 ret = FAIL;
8380
8381 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 {
8386 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008387 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008388 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008389 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394
8395 *arg = skipwhite(argp);
8396 return ret;
8397}
8398
8399
8400/*
8401 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008402 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008403 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 */
8405 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008406call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008407 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008408 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008412 typval_T *argvars; /* vars for arguments, must have "argcount"
8413 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 linenr_T firstline; /* first line of range */
8415 linenr_T lastline; /* last line of range */
8416 int *doesrange; /* return: function handled range */
8417 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008418 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#define ERROR_UNKNOWN 0
8422#define ERROR_TOOMANY 1
8423#define ERROR_TOOFEW 2
8424#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008425#define ERROR_DICT 4
8426#define ERROR_NONE 5
8427#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 int error = ERROR_NONE;
8429 int i;
8430 int llen;
8431 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432#define FLEN_FIXED 40
8433 char_u fname_buf[FLEN_FIXED + 1];
8434 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008435 char_u *name;
8436
8437 /* Make a copy of the name, if it comes from a funcref variable it could
8438 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008439 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008440 if (name == NULL)
8441 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
8443 /*
8444 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8445 * Change <SNR>123_name() to K_SNR 123_name().
8446 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 llen = eval_fname_script(name);
8449 if (llen > 0)
8450 {
8451 fname_buf[0] = K_SPECIAL;
8452 fname_buf[1] = KS_EXTRA;
8453 fname_buf[2] = (int)KE_SNR;
8454 i = 3;
8455 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8456 {
8457 if (current_SID <= 0)
8458 error = ERROR_SCRIPT;
8459 else
8460 {
8461 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8462 i = (int)STRLEN(fname_buf);
8463 }
8464 }
8465 if (i + STRLEN(name + llen) < FLEN_FIXED)
8466 {
8467 STRCPY(fname_buf + i, name + llen);
8468 fname = fname_buf;
8469 }
8470 else
8471 {
8472 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8473 if (fname == NULL)
8474 error = ERROR_OTHER;
8475 else
8476 {
8477 mch_memmove(fname, fname_buf, (size_t)i);
8478 STRCPY(fname + i, name + llen);
8479 }
8480 }
8481 }
8482 else
8483 fname = name;
8484
8485 *doesrange = FALSE;
8486
8487
8488 /* execute the function if no errors detected and executing */
8489 if (evaluate && error == ERROR_NONE)
8490 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008491 char_u *rfname = fname;
8492
8493 /* Ignore "g:" before a function name. */
8494 if (fname[0] == 'g' && fname[1] == ':')
8495 rfname = fname + 2;
8496
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008497 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8498 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 error = ERROR_UNKNOWN;
8500
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008501 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {
8503 /*
8504 * User defined function.
8505 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008506 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008509 /* Trigger FuncUndefined event, may load the function. */
8510 if (fp == NULL
8511 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008512 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008513 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008515 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008516 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 }
8518#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008519 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008520 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008521 {
8522 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008523 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008524 }
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (fp != NULL)
8527 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008528 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008530 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008534 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008535 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 else
8537 {
8538 /*
8539 * Call the user function.
8540 * Save and restore search patterns, script variables and
8541 * redo buffer.
8542 */
8543 save_search_patterns();
8544 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008545 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008547 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008548 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8549 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8550 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008551 /* Function was unreferenced while being used, free it
8552 * now. */
8553 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 restoreRedobuff();
8555 restore_search_patterns();
8556 error = ERROR_NONE;
8557 }
8558 }
8559 }
8560 else
8561 {
8562 /*
8563 * Find the function name in the table, call its implementation.
8564 */
8565 i = find_internal_func(fname);
8566 if (i >= 0)
8567 {
8568 if (argcount < functions[i].f_min_argc)
8569 error = ERROR_TOOFEW;
8570 else if (argcount > functions[i].f_max_argc)
8571 error = ERROR_TOOMANY;
8572 else
8573 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008575 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 error = ERROR_NONE;
8577 }
8578 }
8579 }
8580 /*
8581 * The function call (or "FuncUndefined" autocommand sequence) might
8582 * have been aborted by an error, an interrupt, or an explicitly thrown
8583 * exception that has not been caught so far. This situation can be
8584 * tested for by calling aborting(). For an error in an internal
8585 * function or for the "E132" error in call_user_func(), however, the
8586 * throw point at which the "force_abort" flag (temporarily reset by
8587 * emsg()) is normally updated has not been reached yet. We need to
8588 * update that flag first to make aborting() reliable.
8589 */
8590 update_force_abort();
8591 }
8592 if (error == ERROR_NONE)
8593 ret = OK;
8594
8595 /*
8596 * Report an error unless the argument evaluation or function call has been
8597 * cancelled due to an aborting error, an interrupt, or an exception.
8598 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 if (!aborting())
8600 {
8601 switch (error)
8602 {
8603 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008604 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008605 break;
8606 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008607 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008608 break;
8609 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008610 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008611 name);
8612 break;
8613 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008614 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008615 name);
8616 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008618 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 name);
8620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008621 }
8622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (fname != name && fname != fname_buf)
8625 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008626 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627
8628 return ret;
8629}
8630
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008631/*
8632 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008633 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008634 */
8635 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008636emsg_funcname(ermsg, name)
8637 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008638 char_u *name;
8639{
8640 char_u *p;
8641
8642 if (*name == K_SPECIAL)
8643 p = concat_str((char_u *)"<SNR>", name + 3);
8644 else
8645 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008646 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008647 if (p != name)
8648 vim_free(p);
8649}
8650
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008651/*
8652 * Return TRUE for a non-zero Number and a non-empty String.
8653 */
8654 static int
8655non_zero_arg(argvars)
8656 typval_T *argvars;
8657{
8658 return ((argvars[0].v_type == VAR_NUMBER
8659 && argvars[0].vval.v_number != 0)
8660 || (argvars[0].v_type == VAR_STRING
8661 && argvars[0].vval.v_string != NULL
8662 && *argvars[0].vval.v_string != NUL));
8663}
8664
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665/*********************************************
8666 * Implementation of the built-in functions
8667 */
8668
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008669#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8671
8672/*
8673 * Get the float value of "argvars[0]" into "f".
8674 * Returns FAIL when the argument is not a Number or Float.
8675 */
8676 static int
8677get_float_arg(argvars, f)
8678 typval_T *argvars;
8679 float_T *f;
8680{
8681 if (argvars[0].v_type == VAR_FLOAT)
8682 {
8683 *f = argvars[0].vval.v_float;
8684 return OK;
8685 }
8686 if (argvars[0].v_type == VAR_NUMBER)
8687 {
8688 *f = (float_T)argvars[0].vval.v_number;
8689 return OK;
8690 }
8691 EMSG(_("E808: Number or Float required"));
8692 return FAIL;
8693}
8694
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695/*
8696 * "abs(expr)" function
8697 */
8698 static void
8699f_abs(argvars, rettv)
8700 typval_T *argvars;
8701 typval_T *rettv;
8702{
8703 if (argvars[0].v_type == VAR_FLOAT)
8704 {
8705 rettv->v_type = VAR_FLOAT;
8706 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8707 }
8708 else
8709 {
8710 varnumber_T n;
8711 int error = FALSE;
8712
8713 n = get_tv_number_chk(&argvars[0], &error);
8714 if (error)
8715 rettv->vval.v_number = -1;
8716 else if (n > 0)
8717 rettv->vval.v_number = n;
8718 else
8719 rettv->vval.v_number = -n;
8720 }
8721}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008722
8723/*
8724 * "acos()" function
8725 */
8726 static void
8727f_acos(argvars, rettv)
8728 typval_T *argvars;
8729 typval_T *rettv;
8730{
8731 float_T f;
8732
8733 rettv->v_type = VAR_FLOAT;
8734 if (get_float_arg(argvars, &f) == OK)
8735 rettv->vval.v_float = acos(f);
8736 else
8737 rettv->vval.v_float = 0.0;
8738}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008739#endif
8740
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008742 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 */
8744 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008746 typval_T *argvars;
8747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008752 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008754 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008755 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008756 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008758 }
8759 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760 EMSG(_(e_listreq));
8761}
8762
8763/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008764 * "and(expr, expr)" function
8765 */
8766 static void
8767f_and(argvars, rettv)
8768 typval_T *argvars;
8769 typval_T *rettv;
8770{
8771 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8772 & get_tv_number_chk(&argvars[1], NULL);
8773}
8774
8775/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 * "append(lnum, string/list)" function
8777 */
8778 static void
8779f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 typval_T *argvars;
8781 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008782{
8783 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008784 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 list_T *l = NULL;
8786 listitem_T *li = NULL;
8787 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788 long added = 0;
8789
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008790 /* When coming here from Insert mode, sync undo, so that this can be
8791 * undone separately from what was previously inserted. */
8792 if (u_sync_once == 2)
8793 {
8794 u_sync_once = 1; /* notify that u_sync() was called */
8795 u_sync(TRUE);
8796 }
8797
Bram Moolenaar0d660222005-01-07 21:51:51 +00008798 lnum = get_tv_lnum(argvars);
8799 if (lnum >= 0
8800 && lnum <= curbuf->b_ml.ml_line_count
8801 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008802 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008805 l = argvars[1].vval.v_list;
8806 if (l == NULL)
8807 return;
8808 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008810 for (;;)
8811 {
8812 if (l == NULL)
8813 tv = &argvars[1]; /* append a string */
8814 else if (li == NULL)
8815 break; /* end of list */
8816 else
8817 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818 line = get_tv_string_chk(tv);
8819 if (line == NULL) /* type error */
8820 {
8821 rettv->vval.v_number = 1; /* Failed */
8822 break;
8823 }
8824 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008825 ++added;
8826 if (l == NULL)
8827 break;
8828 li = li->li_next;
8829 }
8830
8831 appended_lines_mark(lnum, added);
8832 if (curwin->w_cursor.lnum > lnum)
8833 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 else
8836 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839/*
8840 * "argc()" function
8841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008844 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
8850/*
8851 * "argidx()" function
8852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861/*
8862 * "argv(nr)" function
8863 */
8864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868{
8869 int idx;
8870
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008871 if (argvars[0].v_type != VAR_UNKNOWN)
8872 {
8873 idx = get_tv_number_chk(&argvars[0], NULL);
8874 if (idx >= 0 && idx < ARGCOUNT)
8875 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8876 else
8877 rettv->vval.v_string = NULL;
8878 rettv->v_type = VAR_STRING;
8879 }
8880 else if (rettv_list_alloc(rettv) == OK)
8881 for (idx = 0; idx < ARGCOUNT; ++idx)
8882 list_append_string(rettv->vval.v_list,
8883 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008886#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008887/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008888 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008889 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008890 static void
8891f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008892 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008893 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008894{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008895 float_T f;
8896
8897 rettv->v_type = VAR_FLOAT;
8898 if (get_float_arg(argvars, &f) == OK)
8899 rettv->vval.v_float = asin(f);
8900 else
8901 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008902}
8903
8904/*
8905 * "atan()" function
8906 */
8907 static void
8908f_atan(argvars, rettv)
8909 typval_T *argvars;
8910 typval_T *rettv;
8911{
8912 float_T f;
8913
8914 rettv->v_type = VAR_FLOAT;
8915 if (get_float_arg(argvars, &f) == OK)
8916 rettv->vval.v_float = atan(f);
8917 else
8918 rettv->vval.v_float = 0.0;
8919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008920
8921/*
8922 * "atan2()" function
8923 */
8924 static void
8925f_atan2(argvars, rettv)
8926 typval_T *argvars;
8927 typval_T *rettv;
8928{
8929 float_T fx, fy;
8930
8931 rettv->v_type = VAR_FLOAT;
8932 if (get_float_arg(argvars, &fx) == OK
8933 && get_float_arg(&argvars[1], &fy) == OK)
8934 rettv->vval.v_float = atan2(fx, fy);
8935 else
8936 rettv->vval.v_float = 0.0;
8937}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008938#endif
8939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940/*
8941 * "browse(save, title, initdir, default)" function
8942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008945 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948#ifdef FEAT_BROWSE
8949 int save;
8950 char_u *title;
8951 char_u *initdir;
8952 char_u *defname;
8953 char_u buf[NUMBUFLEN];
8954 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008957 save = get_tv_number_chk(&argvars[0], &error);
8958 title = get_tv_string_chk(&argvars[1]);
8959 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8960 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 if (error || title == NULL || initdir == NULL || defname == NULL)
8963 rettv->vval.v_string = NULL;
8964 else
8965 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008966 do_browse(save ? BROWSE_SAVE : 0,
8967 title, defname, NULL, initdir, NULL, curbuf);
8968#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008969 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972}
8973
8974/*
8975 * "browsedir(title, initdir)" function
8976 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008978f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008979 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008981{
8982#ifdef FEAT_BROWSE
8983 char_u *title;
8984 char_u *initdir;
8985 char_u buf[NUMBUFLEN];
8986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008987 title = get_tv_string_chk(&argvars[0]);
8988 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008990 if (title == NULL || initdir == NULL)
8991 rettv->vval.v_string = NULL;
8992 else
8993 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008994 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
Bram Moolenaar33570922005-01-25 22:26:29 +00009001static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003/*
9004 * Find a buffer by number or exact name.
9005 */
9006 static buf_T *
9007find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009008 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 if (avar->v_type == VAR_NUMBER)
9013 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009014 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009017 if (buf == NULL)
9018 {
9019 /* No full path name match, try a match with a URL or a "nofile"
9020 * buffer, these don't use the full path. */
9021 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9022 if (buf->b_fname != NULL
9023 && (path_with_url(buf->b_fname)
9024#ifdef FEAT_QUICKFIX
9025 || bt_nofile(buf)
9026#endif
9027 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009029 break;
9030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 return buf;
9033}
9034
9035/*
9036 * "bufexists(expr)" function
9037 */
9038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *argvars;
9041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "buflisted(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
9056 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
9061 * "bufloaded(expr)" function
9062 */
9063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009065 typval_T *argvars;
9066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 buf_T *buf;
9069
9070 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072}
9073
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009074static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076/*
9077 * Get buffer by number or pattern.
9078 */
9079 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009080get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009082 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 int save_magic;
9086 char_u *save_cpo;
9087 buf_T *buf;
9088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 if (tv->v_type == VAR_NUMBER)
9090 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009091 if (tv->v_type != VAR_STRING)
9092 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 if (name == NULL || *name == NUL)
9094 return curbuf;
9095 if (name[0] == '$' && name[1] == NUL)
9096 return lastbuf;
9097
9098 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9099 save_magic = p_magic;
9100 p_magic = TRUE;
9101 save_cpo = p_cpo;
9102 p_cpo = (char_u *)"";
9103
9104 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009105 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106
9107 p_magic = save_magic;
9108 p_cpo = save_cpo;
9109
9110 /* If not found, try expanding the name, like done for bufexists(). */
9111 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 return buf;
9115}
9116
9117/*
9118 * "bufname(expr)" function
9119 */
9120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009122 typval_T *argvars;
9123 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 buf_T *buf;
9126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009129 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009132 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 --emsg_off;
9136}
9137
9138/*
9139 * "bufnr(expr)" function
9140 */
9141 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009143 typval_T *argvars;
9144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009147 int error = FALSE;
9148 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009152 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009153 --emsg_off;
9154
9155 /* If the buffer isn't found and the second argument is not zero create a
9156 * new buffer. */
9157 if (buf == NULL
9158 && argvars[1].v_type != VAR_UNKNOWN
9159 && get_tv_number_chk(&argvars[1], &error) != 0
9160 && !error
9161 && (name = get_tv_string_chk(&argvars[0])) != NULL
9162 && !error)
9163 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9164
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169}
9170
9171/*
9172 * "bufwinnr(nr)" function
9173 */
9174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179#ifdef FEAT_WINDOWS
9180 win_T *wp;
9181 int winnr = 0;
9182#endif
9183 buf_T *buf;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009187 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_WINDOWS
9189 for (wp = firstwin; wp; wp = wp->w_next)
9190 {
9191 ++winnr;
9192 if (wp->w_buffer == buf)
9193 break;
9194 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#endif
9199 --emsg_off;
9200}
9201
9202/*
9203 * "byte2line(byte)" function
9204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009207 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212#else
9213 long boff = 0;
9214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 (linenr_T)0, &boff);
9221#endif
9222}
9223
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009224 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009225byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009226 typval_T *argvars;
9227 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009228 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009229{
9230#ifdef FEAT_MBYTE
9231 char_u *t;
9232#endif
9233 char_u *str;
9234 long idx;
9235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 str = get_tv_string_chk(&argvars[0]);
9237 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009238 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240 return;
9241
9242#ifdef FEAT_MBYTE
9243 t = str;
9244 for ( ; idx > 0; idx--)
9245 {
9246 if (*t == NUL) /* EOL reached */
9247 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009248 if (enc_utf8 && comp)
9249 t += utf_ptr2len(t);
9250 else
9251 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009252 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009253 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009254#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009255 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009257#endif
9258}
9259
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009260/*
9261 * "byteidx()" function
9262 */
9263 static void
9264f_byteidx(argvars, rettv)
9265 typval_T *argvars;
9266 typval_T *rettv;
9267{
9268 byteidx(argvars, rettv, FALSE);
9269}
9270
9271/*
9272 * "byteidxcomp()" function
9273 */
9274 static void
9275f_byteidxcomp(argvars, rettv)
9276 typval_T *argvars;
9277 typval_T *rettv;
9278{
9279 byteidx(argvars, rettv, TRUE);
9280}
9281
Bram Moolenaardb913952012-06-29 12:54:53 +02009282 int
9283func_call(name, args, selfdict, rettv)
9284 char_u *name;
9285 typval_T *args;
9286 dict_T *selfdict;
9287 typval_T *rettv;
9288{
9289 listitem_T *item;
9290 typval_T argv[MAX_FUNC_ARGS + 1];
9291 int argc = 0;
9292 int dummy;
9293 int r = 0;
9294
9295 for (item = args->vval.v_list->lv_first; item != NULL;
9296 item = item->li_next)
9297 {
9298 if (argc == MAX_FUNC_ARGS)
9299 {
9300 EMSG(_("E699: Too many arguments"));
9301 break;
9302 }
9303 /* Make a copy of each argument. This is needed to be able to set
9304 * v_lock to VAR_FIXED in the copy without changing the original list.
9305 */
9306 copy_tv(&item->li_tv, &argv[argc++]);
9307 }
9308
9309 if (item == NULL)
9310 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9311 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9312 &dummy, TRUE, selfdict);
9313
9314 /* Free the arguments. */
9315 while (argc > 0)
9316 clear_tv(&argv[--argc]);
9317
9318 return r;
9319}
9320
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009321/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009322 * "call(func, arglist)" function
9323 */
9324 static void
9325f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009328{
9329 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009330 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009331
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332 if (argvars[1].v_type != VAR_LIST)
9333 {
9334 EMSG(_(e_listreq));
9335 return;
9336 }
9337 if (argvars[1].vval.v_list == NULL)
9338 return;
9339
9340 if (argvars[0].v_type == VAR_FUNC)
9341 func = argvars[0].vval.v_string;
9342 else
9343 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009344 if (*func == NUL)
9345 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009346
Bram Moolenaare9a41262005-01-15 22:18:47 +00009347 if (argvars[2].v_type != VAR_UNKNOWN)
9348 {
9349 if (argvars[2].v_type != VAR_DICT)
9350 {
9351 EMSG(_(e_dictreq));
9352 return;
9353 }
9354 selfdict = argvars[2].vval.v_dict;
9355 }
9356
Bram Moolenaardb913952012-06-29 12:54:53 +02009357 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009358}
9359
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#ifdef FEAT_FLOAT
9361/*
9362 * "ceil({float})" function
9363 */
9364 static void
9365f_ceil(argvars, rettv)
9366 typval_T *argvars;
9367 typval_T *rettv;
9368{
9369 float_T f;
9370
9371 rettv->v_type = VAR_FLOAT;
9372 if (get_float_arg(argvars, &f) == OK)
9373 rettv->vval.v_float = ceil(f);
9374 else
9375 rettv->vval.v_float = 0.0;
9376}
9377#endif
9378
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009379/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009380 * "changenr()" function
9381 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009382 static void
9383f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009384 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009385 typval_T *rettv;
9386{
9387 rettv->vval.v_number = curbuf->b_u_seq_cur;
9388}
9389
9390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 * "char2nr(string)" function
9392 */
9393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009395 typval_T *argvars;
9396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398#ifdef FEAT_MBYTE
9399 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009400 {
9401 int utf8 = 0;
9402
9403 if (argvars[1].v_type != VAR_UNKNOWN)
9404 utf8 = get_tv_number_chk(&argvars[1], NULL);
9405
9406 if (utf8)
9407 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9408 else
9409 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 else
9412#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414}
9415
9416/*
9417 * "cindent(lnum)" function
9418 */
9419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009421 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424#ifdef FEAT_CINDENT
9425 pos_T pos;
9426 linenr_T lnum;
9427
9428 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9431 {
9432 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 curwin->w_cursor = pos;
9435 }
9436 else
9437#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439}
9440
9441/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009442 * "clearmatches()" function
9443 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009444 static void
9445f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009446 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009448{
9449#ifdef FEAT_SEARCH_EXTRA
9450 clear_matches(curwin);
9451#endif
9452}
9453
9454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 * "col(string)" function
9456 */
9457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009458f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *argvars;
9460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
9462 colnr_T col = 0;
9463 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009464 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009466 fp = var2fpos(&argvars[0], FALSE, &fnum);
9467 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 {
9469 if (fp->col == MAXCOL)
9470 {
9471 /* '> can be MAXCOL, get the length of the line then */
9472 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009473 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 else
9475 col = MAXCOL;
9476 }
9477 else
9478 {
9479 col = fp->col + 1;
9480#ifdef FEAT_VIRTUALEDIT
9481 /* col(".") when the cursor is on the NUL at the end of the line
9482 * because of "coladd" can be seen as an extra column. */
9483 if (virtual_active() && fp == &curwin->w_cursor)
9484 {
9485 char_u *p = ml_get_cursor();
9486
9487 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9488 curwin->w_virtcol - curwin->w_cursor.coladd))
9489 {
9490# ifdef FEAT_MBYTE
9491 int l;
9492
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009493 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 col += l;
9495# else
9496 if (*p != NUL && p[1] == NUL)
9497 ++col;
9498# endif
9499 }
9500 }
9501#endif
9502 }
9503 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
Bram Moolenaar572cb562005-08-05 21:35:02 +00009507#if defined(FEAT_INS_EXPAND)
9508/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009509 * "complete()" function
9510 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009511 static void
9512f_complete(argvars, rettv)
9513 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009514 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009515{
9516 int startcol;
9517
9518 if ((State & INSERT) == 0)
9519 {
9520 EMSG(_("E785: complete() can only be used in Insert mode"));
9521 return;
9522 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009523
9524 /* Check for undo allowed here, because if something was already inserted
9525 * the line was already saved for undo and this check isn't done. */
9526 if (!undo_allowed())
9527 return;
9528
Bram Moolenaarade00832006-03-10 21:46:58 +00009529 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9530 {
9531 EMSG(_(e_invarg));
9532 return;
9533 }
9534
9535 startcol = get_tv_number_chk(&argvars[0], NULL);
9536 if (startcol <= 0)
9537 return;
9538
9539 set_completion(startcol - 1, argvars[1].vval.v_list);
9540}
9541
9542/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009543 * "complete_add()" function
9544 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009545 static void
9546f_complete_add(argvars, rettv)
9547 typval_T *argvars;
9548 typval_T *rettv;
9549{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009550 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009551}
9552
9553/*
9554 * "complete_check()" function
9555 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009556 static void
9557f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009558 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009559 typval_T *rettv;
9560{
9561 int saved = RedrawingDisabled;
9562
9563 RedrawingDisabled = 0;
9564 ins_compl_check_keys(0);
9565 rettv->vval.v_number = compl_interrupted;
9566 RedrawingDisabled = saved;
9567}
9568#endif
9569
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570/*
9571 * "confirm(message, buttons[, default [, type]])" function
9572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009575 typval_T *argvars UNUSED;
9576 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9579 char_u *message;
9580 char_u *buttons = NULL;
9581 char_u buf[NUMBUFLEN];
9582 char_u buf2[NUMBUFLEN];
9583 int def = 1;
9584 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 char_u *typestr;
9586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 message = get_tv_string_chk(&argvars[0]);
9589 if (message == NULL)
9590 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009593 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9594 if (buttons == NULL)
9595 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009599 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9602 if (typestr == NULL)
9603 error = TRUE;
9604 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 switch (TOUPPER_ASC(*typestr))
9607 {
9608 case 'E': type = VIM_ERROR; break;
9609 case 'Q': type = VIM_QUESTION; break;
9610 case 'I': type = VIM_INFO; break;
9611 case 'W': type = VIM_WARNING; break;
9612 case 'G': type = VIM_GENERIC; break;
9613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
9615 }
9616 }
9617 }
9618
9619 if (buttons == NULL || *buttons == NUL)
9620 buttons = (char_u *)_("&Ok");
9621
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009622 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009623 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009624 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625#endif
9626}
9627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628/*
9629 * "copy()" function
9630 */
9631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009633 typval_T *argvars;
9634 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009635{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009636 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009637}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009639#ifdef FEAT_FLOAT
9640/*
9641 * "cos()" function
9642 */
9643 static void
9644f_cos(argvars, rettv)
9645 typval_T *argvars;
9646 typval_T *rettv;
9647{
9648 float_T f;
9649
9650 rettv->v_type = VAR_FLOAT;
9651 if (get_float_arg(argvars, &f) == OK)
9652 rettv->vval.v_float = cos(f);
9653 else
9654 rettv->vval.v_float = 0.0;
9655}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009656
9657/*
9658 * "cosh()" function
9659 */
9660 static void
9661f_cosh(argvars, rettv)
9662 typval_T *argvars;
9663 typval_T *rettv;
9664{
9665 float_T f;
9666
9667 rettv->v_type = VAR_FLOAT;
9668 if (get_float_arg(argvars, &f) == OK)
9669 rettv->vval.v_float = cosh(f);
9670 else
9671 rettv->vval.v_float = 0.0;
9672}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009673#endif
9674
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009676 * "count()" function
9677 */
9678 static void
9679f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 typval_T *argvars;
9681 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009682{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009683 long n = 0;
9684 int ic = FALSE;
9685
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009688 listitem_T *li;
9689 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009691
Bram Moolenaare9a41262005-01-15 22:18:47 +00009692 if ((l = argvars[0].vval.v_list) != NULL)
9693 {
9694 li = l->lv_first;
9695 if (argvars[2].v_type != VAR_UNKNOWN)
9696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 int error = FALSE;
9698
9699 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009700 if (argvars[3].v_type != VAR_UNKNOWN)
9701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 idx = get_tv_number_chk(&argvars[3], &error);
9703 if (!error)
9704 {
9705 li = list_find(l, idx);
9706 if (li == NULL)
9707 EMSGN(_(e_listidx), idx);
9708 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 if (error)
9711 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009712 }
9713
9714 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009715 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009716 ++n;
9717 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009718 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009719 else if (argvars[0].v_type == VAR_DICT)
9720 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009721 int todo;
9722 dict_T *d;
9723 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009725 if ((d = argvars[0].vval.v_dict) != NULL)
9726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 int error = FALSE;
9728
Bram Moolenaare9a41262005-01-15 22:18:47 +00009729 if (argvars[2].v_type != VAR_UNKNOWN)
9730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 if (argvars[3].v_type != VAR_UNKNOWN)
9733 EMSG(_(e_invarg));
9734 }
9735
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009736 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009737 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009738 {
9739 if (!HASHITEM_EMPTY(hi))
9740 {
9741 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009742 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009743 ++n;
9744 }
9745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 }
9747 }
9748 else
9749 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009750 rettv->vval.v_number = n;
9751}
9752
9753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9755 *
9756 * Checks the existence of a cscope connection.
9757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009760 typval_T *argvars UNUSED;
9761 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
9763#ifdef FEAT_CSCOPE
9764 int num = 0;
9765 char_u *dbpath = NULL;
9766 char_u *prepend = NULL;
9767 char_u buf[NUMBUFLEN];
9768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009769 if (argvars[0].v_type != VAR_UNKNOWN
9770 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 num = (int)get_tv_number(&argvars[0]);
9773 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 }
9777
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779#endif
9780}
9781
9782/*
9783 * "cursor(lnum, col)" function
9784 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009785 * Moves the cursor to the specified line and column.
9786 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009790 typval_T *argvars;
9791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792{
9793 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009794#ifdef FEAT_VIRTUALEDIT
9795 long coladd = 0;
9796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009798 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009799 if (argvars[1].v_type == VAR_UNKNOWN)
9800 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009801 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009802
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009803 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009804 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009805 line = pos.lnum;
9806 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009807#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009808 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009809#endif
9810 }
9811 else
9812 {
9813 line = get_tv_lnum(argvars);
9814 col = get_tv_number_chk(&argvars[1], NULL);
9815#ifdef FEAT_VIRTUALEDIT
9816 if (argvars[2].v_type != VAR_UNKNOWN)
9817 coladd = get_tv_number_chk(&argvars[2], NULL);
9818#endif
9819 }
9820 if (line < 0 || col < 0
9821#ifdef FEAT_VIRTUALEDIT
9822 || coladd < 0
9823#endif
9824 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009825 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (line > 0)
9827 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (col > 0)
9829 curwin->w_cursor.col = col - 1;
9830#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009831 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832#endif
9833
9834 /* Make sure the cursor is in a valid position. */
9835 check_cursor();
9836#ifdef FEAT_MBYTE
9837 /* Correct cursor for multi-byte character. */
9838 if (has_mbyte)
9839 mb_adjust_cursor();
9840#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009841
9842 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009843 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844}
9845
9846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009847 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 */
9849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009850f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009851 typval_T *argvars;
9852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009854 int noref = 0;
9855
9856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009858 if (noref < 0 || noref > 1)
9859 EMSG(_(e_invarg));
9860 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009861 {
9862 current_copyID += COPYID_INC;
9863 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
9868 * "delete()" function
9869 */
9870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009871f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009872 typval_T *argvars;
9873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879}
9880
9881/*
9882 * "did_filetype()" function
9883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009886 typval_T *argvars UNUSED;
9887 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#endif
9892}
9893
9894/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895 * "diff_filler()" function
9896 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009898f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009899 typval_T *argvars UNUSED;
9900 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901{
9902#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009904#endif
9905}
9906
9907/*
9908 * "diff_hlID()" function
9909 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009912 typval_T *argvars UNUSED;
9913 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914{
9915#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009917 static linenr_T prev_lnum = 0;
9918 static int changedtick = 0;
9919 static int fnum = 0;
9920 static int change_start = 0;
9921 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009922 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009923 int filler_lines;
9924 int col;
9925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 if (lnum < 0) /* ignore type error in {lnum} arg */
9927 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009928 if (lnum != prev_lnum
9929 || changedtick != curbuf->b_changedtick
9930 || fnum != curbuf->b_fnum)
9931 {
9932 /* New line, buffer, change: need to get the values. */
9933 filler_lines = diff_check(curwin, lnum);
9934 if (filler_lines < 0)
9935 {
9936 if (filler_lines == -1)
9937 {
9938 change_start = MAXCOL;
9939 change_end = -1;
9940 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9941 hlID = HLF_ADD; /* added line */
9942 else
9943 hlID = HLF_CHD; /* changed line */
9944 }
9945 else
9946 hlID = HLF_ADD; /* added line */
9947 }
9948 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009949 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009950 prev_lnum = lnum;
9951 changedtick = curbuf->b_changedtick;
9952 fnum = curbuf->b_fnum;
9953 }
9954
9955 if (hlID == HLF_CHD || hlID == HLF_TXD)
9956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009958 if (col >= change_start && col <= change_end)
9959 hlID = HLF_TXD; /* changed text */
9960 else
9961 hlID = HLF_CHD; /* changed line */
9962 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009963 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009964#endif
9965}
9966
9967/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009968 * "empty({expr})" function
9969 */
9970 static void
9971f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 typval_T *argvars;
9973 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009974{
9975 int n;
9976
9977 switch (argvars[0].v_type)
9978 {
9979 case VAR_STRING:
9980 case VAR_FUNC:
9981 n = argvars[0].vval.v_string == NULL
9982 || *argvars[0].vval.v_string == NUL;
9983 break;
9984 case VAR_NUMBER:
9985 n = argvars[0].vval.v_number == 0;
9986 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009987#ifdef FEAT_FLOAT
9988 case VAR_FLOAT:
9989 n = argvars[0].vval.v_float == 0.0;
9990 break;
9991#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009992 case VAR_LIST:
9993 n = argvars[0].vval.v_list == NULL
9994 || argvars[0].vval.v_list->lv_first == NULL;
9995 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 case VAR_DICT:
9997 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009998 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010000 default:
10001 EMSG2(_(e_intern2), "f_empty()");
10002 n = 0;
10003 }
10004
10005 rettv->vval.v_number = n;
10006}
10007
10008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 * "escape({string}, {chars})" function
10010 */
10011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010013 typval_T *argvars;
10014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015{
10016 char_u buf[NUMBUFLEN];
10017
Bram Moolenaar758711c2005-02-02 23:11:38 +000010018 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10019 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
10023/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010024 * "eval()" function
10025 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010026 static void
10027f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010028 typval_T *argvars;
10029 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010030{
10031 char_u *s;
10032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 s = get_tv_string_chk(&argvars[0]);
10034 if (s != NULL)
10035 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010037 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10038 {
10039 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010040 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010042 else if (*s != NUL)
10043 EMSG(_(e_trailing));
10044}
10045
10046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 * "eventhandler()" function
10048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010051 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055}
10056
10057/*
10058 * "executable()" function
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010065 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10066}
10067
10068/*
10069 * "exepath()" function
10070 */
10071 static void
10072f_exepath(argvars, rettv)
10073 typval_T *argvars;
10074 typval_T *rettv;
10075{
10076 char_u *p = NULL;
10077
10078 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10079 rettv->v_type = VAR_STRING;
10080 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081}
10082
10083/*
10084 * "exists()" function
10085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 char_u *p;
10092 char_u *name;
10093 int n = FALSE;
10094 int len = 0;
10095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 if (*p == '$') /* environment variable */
10098 {
10099 /* first try "normal" environment variables (fast) */
10100 if (mch_getenv(p + 1) != NULL)
10101 n = TRUE;
10102 else
10103 {
10104 /* try expanding things like $VIM and ${HOME} */
10105 p = expand_env_save(p);
10106 if (p != NULL && *p != '$')
10107 n = TRUE;
10108 vim_free(p);
10109 }
10110 }
10111 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010113 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010114 if (*skipwhite(p) != NUL)
10115 n = FALSE; /* trailing garbage */
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 else if (*p == '*') /* internal or user defined function */
10118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010119 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 }
10121 else if (*p == ':')
10122 {
10123 n = cmd_exists(p + 1);
10124 }
10125 else if (*p == '#')
10126 {
10127#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010128 if (p[1] == '#')
10129 n = autocmd_supported(p + 2);
10130 else
10131 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#endif
10133 }
10134 else /* internal variable */
10135 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010136 char_u *tofree;
10137 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010139 /* get_name_len() takes care of expanding curly braces */
10140 name = p;
10141 len = get_name_len(&p, &tofree, TRUE, FALSE);
10142 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010144 if (tofree != NULL)
10145 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010146 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010147 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010149 /* handle d.key, l[idx], f(expr) */
10150 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10151 if (n)
10152 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
10154 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010155 if (*p != NUL)
10156 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010158 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 }
10160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162}
10163
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010164#ifdef FEAT_FLOAT
10165/*
10166 * "exp()" function
10167 */
10168 static void
10169f_exp(argvars, rettv)
10170 typval_T *argvars;
10171 typval_T *rettv;
10172{
10173 float_T f;
10174
10175 rettv->v_type = VAR_FLOAT;
10176 if (get_float_arg(argvars, &f) == OK)
10177 rettv->vval.v_float = exp(f);
10178 else
10179 rettv->vval.v_float = 0.0;
10180}
10181#endif
10182
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183/*
10184 * "expand()" function
10185 */
10186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010188 typval_T *argvars;
10189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 char_u *s;
10192 int len;
10193 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010194 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010197 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010199 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010200 if (argvars[1].v_type != VAR_UNKNOWN
10201 && argvars[2].v_type != VAR_UNKNOWN
10202 && get_tv_number_chk(&argvars[2], &error)
10203 && !error)
10204 {
10205 rettv->v_type = VAR_LIST;
10206 rettv->vval.v_list = NULL;
10207 }
10208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 if (*s == '%' || *s == '#' || *s == '<')
10211 {
10212 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010213 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010215 if (rettv->v_type == VAR_LIST)
10216 {
10217 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10218 list_append_string(rettv->vval.v_list, result, -1);
10219 else
10220 vim_free(result);
10221 }
10222 else
10223 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 }
10225 else
10226 {
10227 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010228 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 if (argvars[1].v_type != VAR_UNKNOWN
10230 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010231 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010232 if (!error)
10233 {
10234 ExpandInit(&xpc);
10235 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010236 if (p_wic)
10237 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010238 if (rettv->v_type == VAR_STRING)
10239 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10240 options, WILD_ALL);
10241 else if (rettv_list_alloc(rettv) != FAIL)
10242 {
10243 int i;
10244
10245 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10246 for (i = 0; i < xpc.xp_numfiles; i++)
10247 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10248 ExpandCleanup(&xpc);
10249 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 }
10251 else
10252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 }
10254}
10255
10256/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010257 * Go over all entries in "d2" and add them to "d1".
10258 * When "action" is "error" then a duplicate key is an error.
10259 * When "action" is "force" then a duplicate key is overwritten.
10260 * Otherwise duplicate keys are ignored ("action" is "keep").
10261 */
10262 void
10263dict_extend(d1, d2, action)
10264 dict_T *d1;
10265 dict_T *d2;
10266 char_u *action;
10267{
10268 dictitem_T *di1;
10269 hashitem_T *hi2;
10270 int todo;
10271
10272 todo = (int)d2->dv_hashtab.ht_used;
10273 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10274 {
10275 if (!HASHITEM_EMPTY(hi2))
10276 {
10277 --todo;
10278 di1 = dict_find(d1, hi2->hi_key, -1);
10279 if (d1->dv_scope != 0)
10280 {
10281 /* Disallow replacing a builtin function in l: and g:.
10282 * Check the key to be valid when adding to any
10283 * scope. */
10284 if (d1->dv_scope == VAR_DEF_SCOPE
10285 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10286 && var_check_func_name(hi2->hi_key,
10287 di1 == NULL))
10288 break;
10289 if (!valid_varname(hi2->hi_key))
10290 break;
10291 }
10292 if (di1 == NULL)
10293 {
10294 di1 = dictitem_copy(HI2DI(hi2));
10295 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10296 dictitem_free(di1);
10297 }
10298 else if (*action == 'e')
10299 {
10300 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10301 break;
10302 }
10303 else if (*action == 'f' && HI2DI(hi2) != di1)
10304 {
10305 clear_tv(&di1->di_tv);
10306 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10307 }
10308 }
10309 }
10310}
10311
10312/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010313 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010315 */
10316 static void
10317f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010318 typval_T *argvars;
10319 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010321 char *arg_errmsg = N_("extend() argument");
10322
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010324 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010325 list_T *l1, *l2;
10326 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010329
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 l1 = argvars[0].vval.v_list;
10331 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010332 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010333 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010334 {
10335 if (argvars[2].v_type != VAR_UNKNOWN)
10336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 before = get_tv_number_chk(&argvars[2], &error);
10338 if (error)
10339 return; /* type error; errmsg already given */
10340
Bram Moolenaar758711c2005-02-02 23:11:38 +000010341 if (before == l1->lv_len)
10342 item = NULL;
10343 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010344 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010345 item = list_find(l1, before);
10346 if (item == NULL)
10347 {
10348 EMSGN(_(e_listidx), before);
10349 return;
10350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351 }
10352 }
10353 else
10354 item = NULL;
10355 list_extend(l1, l2, item);
10356
Bram Moolenaare9a41262005-01-15 22:18:47 +000010357 copy_tv(&argvars[0], rettv);
10358 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010359 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010360 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10361 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010362 dict_T *d1, *d2;
10363 char_u *action;
10364 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365
10366 d1 = argvars[0].vval.v_dict;
10367 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010368 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010369 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010370 {
10371 /* Check the third argument. */
10372 if (argvars[2].v_type != VAR_UNKNOWN)
10373 {
10374 static char *(av[]) = {"keep", "force", "error"};
10375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 action = get_tv_string_chk(&argvars[2]);
10377 if (action == NULL)
10378 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 for (i = 0; i < 3; ++i)
10380 if (STRCMP(action, av[i]) == 0)
10381 break;
10382 if (i == 3)
10383 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010384 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 return;
10386 }
10387 }
10388 else
10389 action = (char_u *)"force";
10390
Bram Moolenaara9922d62013-05-30 13:01:18 +020010391 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010392
Bram Moolenaare9a41262005-01-15 22:18:47 +000010393 copy_tv(&argvars[0], rettv);
10394 }
10395 }
10396 else
10397 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010398}
10399
10400/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010401 * "feedkeys()" function
10402 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010403 static void
10404f_feedkeys(argvars, rettv)
10405 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010406 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010407{
10408 int remap = TRUE;
10409 char_u *keys, *flags;
10410 char_u nbuf[NUMBUFLEN];
10411 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010412 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010413
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010414 /* This is not allowed in the sandbox. If the commands would still be
10415 * executed in the sandbox it would be OK, but it probably happens later,
10416 * when "sandbox" is no longer set. */
10417 if (check_secure())
10418 return;
10419
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010420 keys = get_tv_string(&argvars[0]);
10421 if (*keys != NUL)
10422 {
10423 if (argvars[1].v_type != VAR_UNKNOWN)
10424 {
10425 flags = get_tv_string_buf(&argvars[1], nbuf);
10426 for ( ; *flags != NUL; ++flags)
10427 {
10428 switch (*flags)
10429 {
10430 case 'n': remap = FALSE; break;
10431 case 'm': remap = TRUE; break;
10432 case 't': typed = TRUE; break;
10433 }
10434 }
10435 }
10436
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010437 /* Need to escape K_SPECIAL and CSI before putting the string in the
10438 * typeahead buffer. */
10439 keys_esc = vim_strsave_escape_csi(keys);
10440 if (keys_esc != NULL)
10441 {
10442 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010443 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010444 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010445 if (vgetc_busy)
10446 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010447 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010448 }
10449}
10450
10451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 * "filereadable()" function
10453 */
10454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010456 typval_T *argvars;
10457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010459 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 char_u *p;
10461 int n;
10462
Bram Moolenaarc236c162008-07-13 17:41:49 +000010463#ifndef O_NONBLOCK
10464# define O_NONBLOCK 0
10465#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010467 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10468 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
10470 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010471 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 }
10473 else
10474 n = FALSE;
10475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477}
10478
10479/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010480 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 * rights to write into.
10482 */
10483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010485 typval_T *argvars;
10486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010488 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489}
10490
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010491static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010492
10493 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010494findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010495 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010497 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010498{
10499#ifdef FEAT_SEARCHPATH
10500 char_u *fname;
10501 char_u *fresult = NULL;
10502 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10503 char_u *p;
10504 char_u pathbuf[NUMBUFLEN];
10505 int count = 1;
10506 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010507 int error = FALSE;
10508#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010509
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010510 rettv->vval.v_string = NULL;
10511 rettv->v_type = VAR_STRING;
10512
10513#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010515
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010518 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10519 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010520 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 else
10522 {
10523 if (*p != NUL)
10524 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010527 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010529 }
10530
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010531 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10532 error = TRUE;
10533
10534 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 do
10537 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010538 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010539 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010540 fresult = find_file_in_path_option(first ? fname : NULL,
10541 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010542 0, first, path,
10543 find_what,
10544 curbuf->b_ffname,
10545 find_what == FINDFILE_DIR
10546 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010548
10549 if (fresult != NULL && rettv->v_type == VAR_LIST)
10550 list_append_string(rettv->vval.v_list, fresult, -1);
10551
10552 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010554
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010555 if (rettv->v_type == VAR_STRING)
10556 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010557#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010558}
10559
Bram Moolenaar33570922005-01-25 22:26:29 +000010560static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10561static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010562
10563/*
10564 * Implementation of map() and filter().
10565 */
10566 static void
10567filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010568 typval_T *argvars;
10569 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010570 int map;
10571{
10572 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 listitem_T *li, *nli;
10575 list_T *l = NULL;
10576 dictitem_T *di;
10577 hashtab_T *ht;
10578 hashitem_T *hi;
10579 dict_T *d = NULL;
10580 typval_T save_val;
10581 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010583 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010584 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10585 char *arg_errmsg = (map ? N_("map() argument")
10586 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010587 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010588 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010589
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010591 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010592 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010593 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 return;
10595 }
10596 else if (argvars[0].v_type == VAR_DICT)
10597 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010598 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010599 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 return;
10601 }
10602 else
10603 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010604 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 return;
10606 }
10607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 expr = get_tv_string_buf_chk(&argvars[1], buf);
10609 /* On type errors, the preceding call has already displayed an error
10610 * message. Avoid a misleading error message for an empty string that
10611 * was not passed as argument. */
10612 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 prepare_vimvar(VV_VAL, &save_val);
10615 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010616
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010617 /* We reset "did_emsg" to be able to detect whether an error
10618 * occurred during evaluation of the expression. */
10619 save_did_emsg = did_emsg;
10620 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010621
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010622 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 vimvars[VV_KEY].vv_type = VAR_STRING;
10626
10627 ht = &d->dv_hashtab;
10628 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010629 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010630 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 if (!HASHITEM_EMPTY(hi))
10633 {
10634 --todo;
10635 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010636 if (tv_check_lock(di->di_tv.v_lock,
10637 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010638 break;
10639 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010640 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010641 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 break;
10643 if (!map && rem)
10644 dictitem_remove(d, di);
10645 clear_tv(&vimvars[VV_KEY].vv_tv);
10646 }
10647 }
10648 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 }
10650 else
10651 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010652 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010654 for (li = l->lv_first; li != NULL; li = nli)
10655 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010656 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010657 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010659 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010660 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010661 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010662 break;
10663 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010664 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010665 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010667 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010668
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010669 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010671
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010672 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010673 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674
10675 copy_tv(&argvars[0], rettv);
10676}
10677
10678 static int
10679filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010680 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010681 char_u *expr;
10682 int map;
10683 int *remp;
10684{
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010686 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010687 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010688
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010690 s = expr;
10691 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010692 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 if (*s != NUL) /* check for trailing chars after expr */
10694 {
10695 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010696 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010697 }
10698 if (map)
10699 {
10700 /* map(): replace the list item value */
10701 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010702 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010703 *tv = rettv;
10704 }
10705 else
10706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 int error = FALSE;
10708
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010711 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 /* On type error, nothing has been removed; return FAIL to stop the
10713 * loop. The error message was given by get_tv_number_chk(). */
10714 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010715 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010716 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010717 retval = OK;
10718theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010719 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010720 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010721}
10722
10723/*
10724 * "filter()" function
10725 */
10726 static void
10727f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010728 typval_T *argvars;
10729 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010730{
10731 filter_map(argvars, rettv, FALSE);
10732}
10733
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735 * "finddir({fname}[, {path}[, {count}]])" function
10736 */
10737 static void
10738f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 typval_T *argvars;
10740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010741{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010742 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010743}
10744
10745/*
10746 * "findfile({fname}[, {path}[, {count}]])" function
10747 */
10748 static void
10749f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010750 typval_T *argvars;
10751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010752{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010753 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754}
10755
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010756#ifdef FEAT_FLOAT
10757/*
10758 * "float2nr({float})" function
10759 */
10760 static void
10761f_float2nr(argvars, rettv)
10762 typval_T *argvars;
10763 typval_T *rettv;
10764{
10765 float_T f;
10766
10767 if (get_float_arg(argvars, &f) == OK)
10768 {
10769 if (f < -0x7fffffff)
10770 rettv->vval.v_number = -0x7fffffff;
10771 else if (f > 0x7fffffff)
10772 rettv->vval.v_number = 0x7fffffff;
10773 else
10774 rettv->vval.v_number = (varnumber_T)f;
10775 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010776}
10777
10778/*
10779 * "floor({float})" function
10780 */
10781 static void
10782f_floor(argvars, rettv)
10783 typval_T *argvars;
10784 typval_T *rettv;
10785{
10786 float_T f;
10787
10788 rettv->v_type = VAR_FLOAT;
10789 if (get_float_arg(argvars, &f) == OK)
10790 rettv->vval.v_float = floor(f);
10791 else
10792 rettv->vval.v_float = 0.0;
10793}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010794
10795/*
10796 * "fmod()" function
10797 */
10798 static void
10799f_fmod(argvars, rettv)
10800 typval_T *argvars;
10801 typval_T *rettv;
10802{
10803 float_T fx, fy;
10804
10805 rettv->v_type = VAR_FLOAT;
10806 if (get_float_arg(argvars, &fx) == OK
10807 && get_float_arg(&argvars[1], &fy) == OK)
10808 rettv->vval.v_float = fmod(fx, fy);
10809 else
10810 rettv->vval.v_float = 0.0;
10811}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010812#endif
10813
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010815 * "fnameescape({string})" function
10816 */
10817 static void
10818f_fnameescape(argvars, rettv)
10819 typval_T *argvars;
10820 typval_T *rettv;
10821{
10822 rettv->vval.v_string = vim_strsave_fnameescape(
10823 get_tv_string(&argvars[0]), FALSE);
10824 rettv->v_type = VAR_STRING;
10825}
10826
10827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 * "fnamemodify({fname}, {mods})" function
10829 */
10830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010832 typval_T *argvars;
10833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835 char_u *fname;
10836 char_u *mods;
10837 int usedlen = 0;
10838 int len;
10839 char_u *fbuf = NULL;
10840 char_u buf[NUMBUFLEN];
10841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 fname = get_tv_string_chk(&argvars[0]);
10843 mods = get_tv_string_buf_chk(&argvars[1], buf);
10844 if (fname == NULL || mods == NULL)
10845 fname = NULL;
10846 else
10847 {
10848 len = (int)STRLEN(fname);
10849 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010854 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 vim_free(fbuf);
10858}
10859
Bram Moolenaar33570922005-01-25 22:26:29 +000010860static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861
10862/*
10863 * "foldclosed()" function
10864 */
10865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010867 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010869 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
10871#ifdef FEAT_FOLDING
10872 linenr_T lnum;
10873 linenr_T first, last;
10874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10877 {
10878 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10879 {
10880 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 return;
10885 }
10886 }
10887#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889}
10890
10891/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010892 * "foldclosed()" function
10893 */
10894 static void
10895f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 typval_T *argvars;
10897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010898{
10899 foldclosed_both(argvars, rettv, FALSE);
10900}
10901
10902/*
10903 * "foldclosedend()" function
10904 */
10905 static void
10906f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *argvars;
10908 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010909{
10910 foldclosed_both(argvars, rettv, TRUE);
10911}
10912
10913/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 * "foldlevel()" function
10915 */
10916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010918 typval_T *argvars UNUSED;
10919 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921#ifdef FEAT_FOLDING
10922 linenr_T lnum;
10923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928}
10929
10930/*
10931 * "foldtext()" function
10932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
10938#ifdef FEAT_FOLDING
10939 linenr_T lnum;
10940 char_u *s;
10941 char_u *r;
10942 int len;
10943 char *txt;
10944#endif
10945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946 rettv->v_type = VAR_STRING;
10947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010949 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10950 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10951 <= curbuf->b_ml.ml_line_count
10952 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 {
10954 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010955 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10956 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 {
10958 if (!linewhite(lnum))
10959 break;
10960 ++lnum;
10961 }
10962
10963 /* Find interesting text in this line. */
10964 s = skipwhite(ml_get(lnum));
10965 /* skip C comment-start */
10966 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010969 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010971 {
10972 s = skipwhite(ml_get(lnum + 1));
10973 if (*s == '*')
10974 s = skipwhite(s + 1);
10975 }
10976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 txt = _("+-%s%3ld lines: ");
10978 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 + 20 /* for %3ld */
10981 + STRLEN(s))); /* concatenated */
10982 if (r != NULL)
10983 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10985 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10986 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 len = (int)STRLEN(r);
10988 STRCAT(r, s);
10989 /* remove 'foldmarker' and 'commentstring' */
10990 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 }
10993 }
10994#endif
10995}
10996
10997/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010998 * "foldtextresult(lnum)" function
10999 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011002 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011004{
11005#ifdef FEAT_FOLDING
11006 linenr_T lnum;
11007 char_u *text;
11008 char_u buf[51];
11009 foldinfo_T foldinfo;
11010 int fold_count;
11011#endif
11012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->v_type = VAR_STRING;
11014 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011015#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 /* treat illegal types and illegal string values for {lnum} the same */
11018 if (lnum < 0)
11019 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011020 fold_count = foldedCount(curwin, lnum, &foldinfo);
11021 if (fold_count > 0)
11022 {
11023 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11024 &foldinfo, buf);
11025 if (text == buf)
11026 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011027 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011028 }
11029#endif
11030}
11031
11032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 * "foreground()" function
11034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011037 typval_T *argvars UNUSED;
11038 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040#ifdef FEAT_GUI
11041 if (gui.in_use)
11042 gui_mch_set_foreground();
11043#else
11044# ifdef WIN32
11045 win32_set_foreground();
11046# endif
11047#endif
11048}
11049
11050/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011051 * "function()" function
11052 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011054f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011055 typval_T *argvars;
11056 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011057{
11058 char_u *s;
11059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011060 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011061 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011062 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011063 /* Don't check an autoload name for existence here. */
11064 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011065 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011066 else
11067 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011068 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011069 {
11070 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011071 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011072
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011073 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11074 * also be called from another script. Using trans_function_name()
11075 * would also work, but some plugins depend on the name being
11076 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011077 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011078 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011079 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011080 if (rettv->vval.v_string != NULL)
11081 {
11082 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011083 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011084 }
11085 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011086 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011087 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011089 }
11090}
11091
11092/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011093 * "garbagecollect()" function
11094 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011095 static void
11096f_garbagecollect(argvars, rettv)
11097 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011098 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011099{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011100 /* This is postponed until we are back at the toplevel, because we may be
11101 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11102 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011103
11104 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11105 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011106}
11107
11108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109 * "get()" function
11110 */
11111 static void
11112f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011113 typval_T *argvars;
11114 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115{
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 listitem_T *li;
11117 list_T *l;
11118 dictitem_T *di;
11119 dict_T *d;
11120 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 int error = FALSE;
11127
11128 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11129 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011130 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011132 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011133 else if (argvars[0].v_type == VAR_DICT)
11134 {
11135 if ((d = argvars[0].vval.v_dict) != NULL)
11136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011137 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 if (di != NULL)
11139 tv = &di->di_tv;
11140 }
11141 }
11142 else
11143 EMSG2(_(e_listdictarg), "get()");
11144
11145 if (tv == NULL)
11146 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 copy_tv(&argvars[2], rettv);
11149 }
11150 else
11151 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152}
11153
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011155
11156/*
11157 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011158 * Return a range (from start to end) of lines in rettv from the specified
11159 * buffer.
11160 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011161 */
11162 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011164 buf_T *buf;
11165 linenr_T start;
11166 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011167 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011168 typval_T *rettv;
11169{
11170 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011171
Bram Moolenaar959a1432013-12-14 12:17:38 +010011172 rettv->v_type = VAR_STRING;
11173 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011174 if (retlist && rettv_list_alloc(rettv) == FAIL)
11175 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011176
11177 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11178 return;
11179
11180 if (!retlist)
11181 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011182 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11183 p = ml_get_buf(buf, start, FALSE);
11184 else
11185 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011186 rettv->vval.v_string = vim_strsave(p);
11187 }
11188 else
11189 {
11190 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011191 return;
11192
11193 if (start < 1)
11194 start = 1;
11195 if (end > buf->b_ml.ml_line_count)
11196 end = buf->b_ml.ml_line_count;
11197 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011198 if (list_append_string(rettv->vval.v_list,
11199 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011200 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011201 }
11202}
11203
11204/*
11205 * "getbufline()" function
11206 */
11207 static void
11208f_getbufline(argvars, rettv)
11209 typval_T *argvars;
11210 typval_T *rettv;
11211{
11212 linenr_T lnum;
11213 linenr_T end;
11214 buf_T *buf;
11215
11216 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11217 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011218 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011219 --emsg_off;
11220
Bram Moolenaar661b1822005-07-28 22:36:45 +000011221 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011222 if (argvars[2].v_type == VAR_UNKNOWN)
11223 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011224 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011225 end = get_tv_lnum_buf(&argvars[2], buf);
11226
Bram Moolenaar342337a2005-07-21 21:11:17 +000011227 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011228}
11229
Bram Moolenaar0d660222005-01-07 21:51:51 +000011230/*
11231 * "getbufvar()" function
11232 */
11233 static void
11234f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 typval_T *argvars;
11236 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237{
11238 buf_T *buf;
11239 buf_T *save_curbuf;
11240 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011241 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011242 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11245 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011247 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011249 rettv->v_type = VAR_STRING;
11250 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011251
11252 if (buf != NULL && varname != NULL)
11253 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011254 /* set curbuf to be our buf, temporarily */
11255 save_curbuf = curbuf;
11256 curbuf = buf;
11257
Bram Moolenaar0d660222005-01-07 21:51:51 +000011258 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011259 {
11260 if (get_option_tv(&varname, rettv, TRUE) == OK)
11261 done = TRUE;
11262 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011263 else if (STRCMP(varname, "changedtick") == 0)
11264 {
11265 rettv->v_type = VAR_NUMBER;
11266 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011267 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011268 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011269 else
11270 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011271 /* Look up the variable. */
11272 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11273 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11274 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011275 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011276 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011278 done = TRUE;
11279 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011281
11282 /* restore previous notion of curbuf */
11283 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284 }
11285
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011286 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11287 /* use the default value */
11288 copy_tv(&argvars[2], rettv);
11289
Bram Moolenaar0d660222005-01-07 21:51:51 +000011290 --emsg_off;
11291}
11292
11293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 * "getchar()" function
11295 */
11296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *argvars;
11299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300{
11301 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011304 /* Position the cursor. Needed after a message that ends in a space. */
11305 windgoto(msg_row, msg_col);
11306
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 ++no_mapping;
11308 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011309 for (;;)
11310 {
11311 if (argvars[0].v_type == VAR_UNKNOWN)
11312 /* getchar(): blocking wait. */
11313 n = safe_vgetc();
11314 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11315 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011316 n = vpeekc_any();
11317 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011318 /* illegal argument or getchar(0) and no char avail: return zero */
11319 n = 0;
11320 else
11321 /* getchar(0) and char avail: return char */
11322 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011323
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011324 if (n == K_IGNORE)
11325 continue;
11326 break;
11327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 --no_mapping;
11329 --allow_keys;
11330
Bram Moolenaar219b8702006-11-01 14:32:36 +000011331 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11332 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11333 vimvars[VV_MOUSE_COL].vv_nr = 0;
11334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 if (IS_SPECIAL(n) || mod_mask != 0)
11337 {
11338 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11339 int i = 0;
11340
11341 /* Turn a special key into three bytes, plus modifier. */
11342 if (mod_mask != 0)
11343 {
11344 temp[i++] = K_SPECIAL;
11345 temp[i++] = KS_MODIFIER;
11346 temp[i++] = mod_mask;
11347 }
11348 if (IS_SPECIAL(n))
11349 {
11350 temp[i++] = K_SPECIAL;
11351 temp[i++] = K_SECOND(n);
11352 temp[i++] = K_THIRD(n);
11353 }
11354#ifdef FEAT_MBYTE
11355 else if (has_mbyte)
11356 i += (*mb_char2bytes)(n, temp + i);
11357#endif
11358 else
11359 temp[i++] = n;
11360 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->v_type = VAR_STRING;
11362 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011363
11364#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011365 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011366 {
11367 int row = mouse_row;
11368 int col = mouse_col;
11369 win_T *win;
11370 linenr_T lnum;
11371# ifdef FEAT_WINDOWS
11372 win_T *wp;
11373# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011374 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011375
11376 if (row >= 0 && col >= 0)
11377 {
11378 /* Find the window at the mouse coordinates and compute the
11379 * text position. */
11380 win = mouse_find_win(&row, &col);
11381 (void)mouse_comp_pos(win, &row, &col, &lnum);
11382# ifdef FEAT_WINDOWS
11383 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011384 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011385# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011386 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011387 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11388 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11389 }
11390 }
11391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 }
11393}
11394
11395/*
11396 * "getcharmod()" function
11397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011400 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404}
11405
11406/*
11407 * "getcmdline()" function
11408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011411 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->v_type = VAR_STRING;
11415 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417
11418/*
11419 * "getcmdpos()" function
11420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011423 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427}
11428
11429/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011430 * "getcmdtype()" function
11431 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011432 static void
11433f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011434 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011435 typval_T *rettv;
11436{
11437 rettv->v_type = VAR_STRING;
11438 rettv->vval.v_string = alloc(2);
11439 if (rettv->vval.v_string != NULL)
11440 {
11441 rettv->vval.v_string[0] = get_cmdline_type();
11442 rettv->vval.v_string[1] = NUL;
11443 }
11444}
11445
11446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 * "getcwd()" function
11448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011454 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011456 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011457 rettv->vval.v_string = NULL;
11458 cwd = alloc(MAXPATHL);
11459 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011461 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11462 {
11463 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011465 if (rettv->vval.v_string != NULL)
11466 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011468 }
11469 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 }
11471}
11472
11473/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011474 * "getfontname()" function
11475 */
11476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011478 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011479 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011480{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->v_type = VAR_STRING;
11482 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011483#ifdef FEAT_GUI
11484 if (gui.in_use)
11485 {
11486 GuiFont font;
11487 char_u *name = NULL;
11488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011489 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011490 {
11491 /* Get the "Normal" font. Either the name saved by
11492 * hl_set_font_name() or from the font ID. */
11493 font = gui.norm_font;
11494 name = hl_get_font_name();
11495 }
11496 else
11497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011499 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11500 return;
11501 font = gui_mch_get_font(name, FALSE);
11502 if (font == NOFONT)
11503 return; /* Invalid font name, return empty string. */
11504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011505 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011506 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011507 gui_mch_free_font(font);
11508 }
11509#endif
11510}
11511
11512/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011513 * "getfperm({fname})" function
11514 */
11515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 typval_T *argvars;
11518 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011519{
11520 char_u *fname;
11521 struct stat st;
11522 char_u *perm = NULL;
11523 char_u flags[] = "rwx";
11524 int i;
11525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011529 if (mch_stat((char *)fname, &st) >= 0)
11530 {
11531 perm = vim_strsave((char_u *)"---------");
11532 if (perm != NULL)
11533 {
11534 for (i = 0; i < 9; i++)
11535 {
11536 if (st.st_mode & (1 << (8 - i)))
11537 perm[i] = flags[i % 3];
11538 }
11539 }
11540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011542}
11543
11544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545 * "getfsize({fname})" function
11546 */
11547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T *argvars;
11550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551{
11552 char_u *fname;
11553 struct stat st;
11554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558
11559 if (mch_stat((char *)fname, &st) >= 0)
11560 {
11561 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011566
11567 /* non-perfect check for overflow */
11568 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11569 rettv->vval.v_number = -2;
11570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 }
11572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574}
11575
11576/*
11577 * "getftime({fname})" function
11578 */
11579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011580f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T *argvars;
11582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583{
11584 char_u *fname;
11585 struct stat st;
11586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588
11589 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011590 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593}
11594
11595/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011596 * "getftype({fname})" function
11597 */
11598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 typval_T *argvars;
11601 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011602{
11603 char_u *fname;
11604 struct stat st;
11605 char_u *type = NULL;
11606 char *t;
11607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011608 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011611 if (mch_lstat((char *)fname, &st) >= 0)
11612 {
11613#ifdef S_ISREG
11614 if (S_ISREG(st.st_mode))
11615 t = "file";
11616 else if (S_ISDIR(st.st_mode))
11617 t = "dir";
11618# ifdef S_ISLNK
11619 else if (S_ISLNK(st.st_mode))
11620 t = "link";
11621# endif
11622# ifdef S_ISBLK
11623 else if (S_ISBLK(st.st_mode))
11624 t = "bdev";
11625# endif
11626# ifdef S_ISCHR
11627 else if (S_ISCHR(st.st_mode))
11628 t = "cdev";
11629# endif
11630# ifdef S_ISFIFO
11631 else if (S_ISFIFO(st.st_mode))
11632 t = "fifo";
11633# endif
11634# ifdef S_ISSOCK
11635 else if (S_ISSOCK(st.st_mode))
11636 t = "fifo";
11637# endif
11638 else
11639 t = "other";
11640#else
11641# ifdef S_IFMT
11642 switch (st.st_mode & S_IFMT)
11643 {
11644 case S_IFREG: t = "file"; break;
11645 case S_IFDIR: t = "dir"; break;
11646# ifdef S_IFLNK
11647 case S_IFLNK: t = "link"; break;
11648# endif
11649# ifdef S_IFBLK
11650 case S_IFBLK: t = "bdev"; break;
11651# endif
11652# ifdef S_IFCHR
11653 case S_IFCHR: t = "cdev"; break;
11654# endif
11655# ifdef S_IFIFO
11656 case S_IFIFO: t = "fifo"; break;
11657# endif
11658# ifdef S_IFSOCK
11659 case S_IFSOCK: t = "socket"; break;
11660# endif
11661 default: t = "other";
11662 }
11663# else
11664 if (mch_isdir(fname))
11665 t = "dir";
11666 else
11667 t = "file";
11668# endif
11669#endif
11670 type = vim_strsave((char_u *)t);
11671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011673}
11674
11675/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011676 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011677 */
11678 static void
11679f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011680 typval_T *argvars;
11681 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011682{
11683 linenr_T lnum;
11684 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011685 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011686
11687 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011688 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011689 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011690 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011691 retlist = FALSE;
11692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011693 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011694 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011695 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011696 retlist = TRUE;
11697 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011698
Bram Moolenaar342337a2005-07-21 21:11:17 +000011699 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700}
11701
11702/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011703 * "getmatches()" function
11704 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011705 static void
11706f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011707 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011708 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011709{
11710#ifdef FEAT_SEARCH_EXTRA
11711 dict_T *dict;
11712 matchitem_T *cur = curwin->w_match_head;
11713
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011714 if (rettv_list_alloc(rettv) == OK)
11715 {
11716 while (cur != NULL)
11717 {
11718 dict = dict_alloc();
11719 if (dict == NULL)
11720 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011721 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11722 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11723 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11724 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11725 list_append_dict(rettv->vval.v_list, dict);
11726 cur = cur->next;
11727 }
11728 }
11729#endif
11730}
11731
11732/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011733 * "getpid()" function
11734 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011735 static void
11736f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011737 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011738 typval_T *rettv;
11739{
11740 rettv->vval.v_number = mch_get_pid();
11741}
11742
11743/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011744 * "getpos(string)" function
11745 */
11746 static void
11747f_getpos(argvars, rettv)
11748 typval_T *argvars;
11749 typval_T *rettv;
11750{
11751 pos_T *fp;
11752 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011753 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011754
11755 if (rettv_list_alloc(rettv) == OK)
11756 {
11757 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011758 fp = var2fpos(&argvars[0], TRUE, &fnum);
11759 if (fnum != -1)
11760 list_append_number(l, (varnumber_T)fnum);
11761 else
11762 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011763 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11764 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011765 list_append_number(l, (fp != NULL)
11766 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011767 : (varnumber_T)0);
11768 list_append_number(l,
11769#ifdef FEAT_VIRTUALEDIT
11770 (fp != NULL) ? (varnumber_T)fp->coladd :
11771#endif
11772 (varnumber_T)0);
11773 }
11774 else
11775 rettv->vval.v_number = FALSE;
11776}
11777
11778/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011779 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011780 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011781 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011782f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011783 typval_T *argvars UNUSED;
11784 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011785{
11786#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011787 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011788#endif
11789
Bram Moolenaar2641f772005-03-25 21:58:17 +000011790#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011791 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011792 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011793 wp = NULL;
11794 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11795 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011796 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011797 if (wp == NULL)
11798 return;
11799 }
11800
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011801 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011802 }
11803#endif
11804}
11805
11806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 * "getreg()" function
11808 */
11809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011811 typval_T *argvars;
11812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813{
11814 char_u *strregname;
11815 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011816 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011817 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011820 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 strregname = get_tv_string_chk(&argvars[0]);
11823 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011824 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011827 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11828 return_list = get_tv_number_chk(&argvars[2], &error);
11829 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011831 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011832 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011833
11834 if (error)
11835 return;
11836
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 regname = (strregname == NULL ? '"' : *strregname);
11838 if (regname == 0)
11839 regname = '"';
11840
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011841 if (return_list)
11842 {
11843 rettv->v_type = VAR_LIST;
11844 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11845 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11846 }
11847 else
11848 {
11849 rettv->v_type = VAR_STRING;
11850 rettv->vval.v_string = get_reg_contents(regname,
11851 arg2 ? GREG_EXPR_SRC : 0);
11852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853}
11854
11855/*
11856 * "getregtype()" function
11857 */
11858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011859f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011860 typval_T *argvars;
11861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862{
11863 char_u *strregname;
11864 int regname;
11865 char_u buf[NUMBUFLEN + 2];
11866 long reglen = 0;
11867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011868 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 {
11870 strregname = get_tv_string_chk(&argvars[0]);
11871 if (strregname == NULL) /* type error; errmsg already given */
11872 {
11873 rettv->v_type = VAR_STRING;
11874 rettv->vval.v_string = NULL;
11875 return;
11876 }
11877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878 else
11879 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881
11882 regname = (strregname == NULL ? '"' : *strregname);
11883 if (regname == 0)
11884 regname = '"';
11885
11886 buf[0] = NUL;
11887 buf[1] = NUL;
11888 switch (get_reg_type(regname, &reglen))
11889 {
11890 case MLINE: buf[0] = 'V'; break;
11891 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 case MBLOCK:
11893 buf[0] = Ctrl_V;
11894 sprintf((char *)buf + 1, "%ld", reglen + 1);
11895 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011897 rettv->v_type = VAR_STRING;
11898 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899}
11900
11901/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011902 * "gettabvar()" function
11903 */
11904 static void
11905f_gettabvar(argvars, rettv)
11906 typval_T *argvars;
11907 typval_T *rettv;
11908{
11909 tabpage_T *tp;
11910 dictitem_T *v;
11911 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011912 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011913
11914 rettv->v_type = VAR_STRING;
11915 rettv->vval.v_string = NULL;
11916
11917 varname = get_tv_string_chk(&argvars[1]);
11918 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11919 if (tp != NULL && varname != NULL)
11920 {
11921 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011922 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011923 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011924 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011925 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011926 done = TRUE;
11927 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011928 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011929
11930 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011931 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011932}
11933
11934/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011935 * "gettabwinvar()" function
11936 */
11937 static void
11938f_gettabwinvar(argvars, rettv)
11939 typval_T *argvars;
11940 typval_T *rettv;
11941{
11942 getwinvar(argvars, rettv, 1);
11943}
11944
11945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 * "getwinposx()" function
11947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011950 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954#ifdef FEAT_GUI
11955 if (gui.in_use)
11956 {
11957 int x, y;
11958
11959 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011960 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 }
11962#endif
11963}
11964
11965/*
11966 * "getwinposy()" function
11967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011973 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974#ifdef FEAT_GUI
11975 if (gui.in_use)
11976 {
11977 int x, y;
11978
11979 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 }
11982#endif
11983}
11984
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011985/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011986 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011987 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011988 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011989find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011990 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011991 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011992{
11993#ifdef FEAT_WINDOWS
11994 win_T *wp;
11995#endif
11996 int nr;
11997
11998 nr = get_tv_number_chk(vp, NULL);
11999
12000#ifdef FEAT_WINDOWS
12001 if (nr < 0)
12002 return NULL;
12003 if (nr == 0)
12004 return curwin;
12005
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012006 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12007 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012008 if (--nr <= 0)
12009 break;
12010 return wp;
12011#else
12012 if (nr == 0 || nr == 1)
12013 return curwin;
12014 return NULL;
12015#endif
12016}
12017
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018/*
12019 * "getwinvar()" function
12020 */
12021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012023 typval_T *argvars;
12024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012026 getwinvar(argvars, rettv, 0);
12027}
12028
12029/*
12030 * getwinvar() and gettabwinvar()
12031 */
12032 static void
12033getwinvar(argvars, rettv, off)
12034 typval_T *argvars;
12035 typval_T *rettv;
12036 int off; /* 1 for gettabwinvar() */
12037{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 win_T *win, *oldcurwin;
12039 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012040 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012041 tabpage_T *tp = NULL;
12042 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012043 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012045#ifdef FEAT_WINDOWS
12046 if (off == 1)
12047 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12048 else
12049 tp = curtab;
12050#endif
12051 win = find_win_by_nr(&argvars[off], tp);
12052 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012053 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012055 rettv->v_type = VAR_STRING;
12056 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057
12058 if (win != NULL && varname != NULL)
12059 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012060 /* Set curwin to be our win, temporarily. Also set the tabpage,
12061 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012062 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012063
Bram Moolenaar071d4272004-06-13 20:20:40 +000012064 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012065 {
12066 if (get_option_tv(&varname, rettv, 1) == OK)
12067 done = TRUE;
12068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 else
12070 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012071 /* Look up the variable. */
12072 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12073 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012075 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012076 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012077 done = TRUE;
12078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012080
12081 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012082 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 }
12084
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012085 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12086 /* use the default return value */
12087 copy_tv(&argvars[off + 2], rettv);
12088
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 --emsg_off;
12090}
12091
12092/*
12093 * "glob()" function
12094 */
12095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012096f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012097 typval_T *argvars;
12098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012100 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012102 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012104 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012105 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012107 if (argvars[1].v_type != VAR_UNKNOWN)
12108 {
12109 if (get_tv_number_chk(&argvars[1], &error))
12110 options |= WILD_KEEP_ALL;
12111 if (argvars[2].v_type != VAR_UNKNOWN
12112 && get_tv_number_chk(&argvars[2], &error))
12113 {
12114 rettv->v_type = VAR_LIST;
12115 rettv->vval.v_list = NULL;
12116 }
12117 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012118 if (!error)
12119 {
12120 ExpandInit(&xpc);
12121 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012122 if (p_wic)
12123 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012124 if (rettv->v_type == VAR_STRING)
12125 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012126 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012127 else if (rettv_list_alloc(rettv) != FAIL)
12128 {
12129 int i;
12130
12131 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12132 NULL, options, WILD_ALL_KEEP);
12133 for (i = 0; i < xpc.xp_numfiles; i++)
12134 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12135
12136 ExpandCleanup(&xpc);
12137 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012138 }
12139 else
12140 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141}
12142
12143/*
12144 * "globpath()" function
12145 */
12146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 typval_T *argvars;
12149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012151 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012154 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012155 garray_T ga;
12156 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012158 /* When the optional second argument is non-zero, don't remove matches
12159 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012161 if (argvars[2].v_type != VAR_UNKNOWN)
12162 {
12163 if (get_tv_number_chk(&argvars[2], &error))
12164 flags |= WILD_KEEP_ALL;
12165 if (argvars[3].v_type != VAR_UNKNOWN
12166 && get_tv_number_chk(&argvars[3], &error))
12167 {
12168 rettv->v_type = VAR_LIST;
12169 rettv->vval.v_list = NULL;
12170 }
12171 }
12172 if (file != NULL && !error)
12173 {
12174 ga_init2(&ga, (int)sizeof(char_u *), 10);
12175 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12176 if (rettv->v_type == VAR_STRING)
12177 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12178 else if (rettv_list_alloc(rettv) != FAIL)
12179 for (i = 0; i < ga.ga_len; ++i)
12180 list_append_string(rettv->vval.v_list,
12181 ((char_u **)(ga.ga_data))[i], -1);
12182 ga_clear_strings(&ga);
12183 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012185 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186}
12187
12188/*
12189 * "has()" function
12190 */
12191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012193 typval_T *argvars;
12194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195{
12196 int i;
12197 char_u *name;
12198 int n = FALSE;
12199 static char *(has_list[]) =
12200 {
12201#ifdef AMIGA
12202 "amiga",
12203# ifdef FEAT_ARP
12204 "arp",
12205# endif
12206#endif
12207#ifdef __BEOS__
12208 "beos",
12209#endif
12210#ifdef MSDOS
12211# ifdef DJGPP
12212 "dos32",
12213# else
12214 "dos16",
12215# endif
12216#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012217#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 "mac",
12219#endif
12220#if defined(MACOS_X_UNIX)
12221 "macunix",
12222#endif
12223#ifdef OS2
12224 "os2",
12225#endif
12226#ifdef __QNX__
12227 "qnx",
12228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229#ifdef UNIX
12230 "unix",
12231#endif
12232#ifdef VMS
12233 "vms",
12234#endif
12235#ifdef WIN16
12236 "win16",
12237#endif
12238#ifdef WIN32
12239 "win32",
12240#endif
12241#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12242 "win32unix",
12243#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012244#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 "win64",
12246#endif
12247#ifdef EBCDIC
12248 "ebcdic",
12249#endif
12250#ifndef CASE_INSENSITIVE_FILENAME
12251 "fname_case",
12252#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012253#ifdef HAVE_ACL
12254 "acl",
12255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256#ifdef FEAT_ARABIC
12257 "arabic",
12258#endif
12259#ifdef FEAT_AUTOCMD
12260 "autocmd",
12261#endif
12262#ifdef FEAT_BEVAL
12263 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012264# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12265 "balloon_multiline",
12266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267#endif
12268#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12269 "builtin_terms",
12270# ifdef ALL_BUILTIN_TCAPS
12271 "all_builtin_terms",
12272# endif
12273#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012274#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12275 || defined(FEAT_GUI_W32) \
12276 || defined(FEAT_GUI_MOTIF))
12277 "browsefilter",
12278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279#ifdef FEAT_BYTEOFF
12280 "byte_offset",
12281#endif
12282#ifdef FEAT_CINDENT
12283 "cindent",
12284#endif
12285#ifdef FEAT_CLIENTSERVER
12286 "clientserver",
12287#endif
12288#ifdef FEAT_CLIPBOARD
12289 "clipboard",
12290#endif
12291#ifdef FEAT_CMDL_COMPL
12292 "cmdline_compl",
12293#endif
12294#ifdef FEAT_CMDHIST
12295 "cmdline_hist",
12296#endif
12297#ifdef FEAT_COMMENTS
12298 "comments",
12299#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012300#ifdef FEAT_CONCEAL
12301 "conceal",
12302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303#ifdef FEAT_CRYPT
12304 "cryptv",
12305#endif
12306#ifdef FEAT_CSCOPE
12307 "cscope",
12308#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012309#ifdef FEAT_CURSORBIND
12310 "cursorbind",
12311#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012312#ifdef CURSOR_SHAPE
12313 "cursorshape",
12314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315#ifdef DEBUG
12316 "debug",
12317#endif
12318#ifdef FEAT_CON_DIALOG
12319 "dialog_con",
12320#endif
12321#ifdef FEAT_GUI_DIALOG
12322 "dialog_gui",
12323#endif
12324#ifdef FEAT_DIFF
12325 "diff",
12326#endif
12327#ifdef FEAT_DIGRAPHS
12328 "digraphs",
12329#endif
12330#ifdef FEAT_DND
12331 "dnd",
12332#endif
12333#ifdef FEAT_EMACS_TAGS
12334 "emacs_tags",
12335#endif
12336 "eval", /* always present, of course! */
12337#ifdef FEAT_EX_EXTRA
12338 "ex_extra",
12339#endif
12340#ifdef FEAT_SEARCH_EXTRA
12341 "extra_search",
12342#endif
12343#ifdef FEAT_FKMAP
12344 "farsi",
12345#endif
12346#ifdef FEAT_SEARCHPATH
12347 "file_in_path",
12348#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012349#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012350 "filterpipe",
12351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352#ifdef FEAT_FIND_ID
12353 "find_in_path",
12354#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012355#ifdef FEAT_FLOAT
12356 "float",
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_FOLDING
12359 "folding",
12360#endif
12361#ifdef FEAT_FOOTER
12362 "footer",
12363#endif
12364#if !defined(USE_SYSTEM) && defined(UNIX)
12365 "fork",
12366#endif
12367#ifdef FEAT_GETTEXT
12368 "gettext",
12369#endif
12370#ifdef FEAT_GUI
12371 "gui",
12372#endif
12373#ifdef FEAT_GUI_ATHENA
12374# ifdef FEAT_GUI_NEXTAW
12375 "gui_neXtaw",
12376# else
12377 "gui_athena",
12378# endif
12379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380#ifdef FEAT_GUI_GTK
12381 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012384#ifdef FEAT_GUI_GNOME
12385 "gui_gnome",
12386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387#ifdef FEAT_GUI_MAC
12388 "gui_mac",
12389#endif
12390#ifdef FEAT_GUI_MOTIF
12391 "gui_motif",
12392#endif
12393#ifdef FEAT_GUI_PHOTON
12394 "gui_photon",
12395#endif
12396#ifdef FEAT_GUI_W16
12397 "gui_win16",
12398#endif
12399#ifdef FEAT_GUI_W32
12400 "gui_win32",
12401#endif
12402#ifdef FEAT_HANGULIN
12403 "hangul_input",
12404#endif
12405#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12406 "iconv",
12407#endif
12408#ifdef FEAT_INS_EXPAND
12409 "insert_expand",
12410#endif
12411#ifdef FEAT_JUMPLIST
12412 "jumplist",
12413#endif
12414#ifdef FEAT_KEYMAP
12415 "keymap",
12416#endif
12417#ifdef FEAT_LANGMAP
12418 "langmap",
12419#endif
12420#ifdef FEAT_LIBCALL
12421 "libcall",
12422#endif
12423#ifdef FEAT_LINEBREAK
12424 "linebreak",
12425#endif
12426#ifdef FEAT_LISP
12427 "lispindent",
12428#endif
12429#ifdef FEAT_LISTCMDS
12430 "listcmds",
12431#endif
12432#ifdef FEAT_LOCALMAP
12433 "localmap",
12434#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012435#ifdef FEAT_LUA
12436# ifndef DYNAMIC_LUA
12437 "lua",
12438# endif
12439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440#ifdef FEAT_MENU
12441 "menu",
12442#endif
12443#ifdef FEAT_SESSION
12444 "mksession",
12445#endif
12446#ifdef FEAT_MODIFY_FNAME
12447 "modify_fname",
12448#endif
12449#ifdef FEAT_MOUSE
12450 "mouse",
12451#endif
12452#ifdef FEAT_MOUSESHAPE
12453 "mouseshape",
12454#endif
12455#if defined(UNIX) || defined(VMS)
12456# ifdef FEAT_MOUSE_DEC
12457 "mouse_dec",
12458# endif
12459# ifdef FEAT_MOUSE_GPM
12460 "mouse_gpm",
12461# endif
12462# ifdef FEAT_MOUSE_JSB
12463 "mouse_jsbterm",
12464# endif
12465# ifdef FEAT_MOUSE_NET
12466 "mouse_netterm",
12467# endif
12468# ifdef FEAT_MOUSE_PTERM
12469 "mouse_pterm",
12470# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012471# ifdef FEAT_MOUSE_SGR
12472 "mouse_sgr",
12473# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012474# ifdef FEAT_SYSMOUSE
12475 "mouse_sysmouse",
12476# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012477# ifdef FEAT_MOUSE_URXVT
12478 "mouse_urxvt",
12479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480# ifdef FEAT_MOUSE_XTERM
12481 "mouse_xterm",
12482# endif
12483#endif
12484#ifdef FEAT_MBYTE
12485 "multi_byte",
12486#endif
12487#ifdef FEAT_MBYTE_IME
12488 "multi_byte_ime",
12489#endif
12490#ifdef FEAT_MULTI_LANG
12491 "multi_lang",
12492#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012493#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012494#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012495 "mzscheme",
12496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498#ifdef FEAT_OLE
12499 "ole",
12500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#ifdef FEAT_PATH_EXTRA
12502 "path_extra",
12503#endif
12504#ifdef FEAT_PERL
12505#ifndef DYNAMIC_PERL
12506 "perl",
12507#endif
12508#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012509#ifdef FEAT_PERSISTENT_UNDO
12510 "persistent_undo",
12511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512#ifdef FEAT_PYTHON
12513#ifndef DYNAMIC_PYTHON
12514 "python",
12515#endif
12516#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012517#ifdef FEAT_PYTHON3
12518#ifndef DYNAMIC_PYTHON3
12519 "python3",
12520#endif
12521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522#ifdef FEAT_POSTSCRIPT
12523 "postscript",
12524#endif
12525#ifdef FEAT_PRINTER
12526 "printer",
12527#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012528#ifdef FEAT_PROFILE
12529 "profile",
12530#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012531#ifdef FEAT_RELTIME
12532 "reltime",
12533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534#ifdef FEAT_QUICKFIX
12535 "quickfix",
12536#endif
12537#ifdef FEAT_RIGHTLEFT
12538 "rightleft",
12539#endif
12540#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12541 "ruby",
12542#endif
12543#ifdef FEAT_SCROLLBIND
12544 "scrollbind",
12545#endif
12546#ifdef FEAT_CMDL_INFO
12547 "showcmd",
12548 "cmdline_info",
12549#endif
12550#ifdef FEAT_SIGNS
12551 "signs",
12552#endif
12553#ifdef FEAT_SMARTINDENT
12554 "smartindent",
12555#endif
12556#ifdef FEAT_SNIFF
12557 "sniff",
12558#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012559#ifdef STARTUPTIME
12560 "startuptime",
12561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562#ifdef FEAT_STL_OPT
12563 "statusline",
12564#endif
12565#ifdef FEAT_SUN_WORKSHOP
12566 "sun_workshop",
12567#endif
12568#ifdef FEAT_NETBEANS_INTG
12569 "netbeans_intg",
12570#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012571#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012572 "spell",
12573#endif
12574#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575 "syntax",
12576#endif
12577#if defined(USE_SYSTEM) || !defined(UNIX)
12578 "system",
12579#endif
12580#ifdef FEAT_TAG_BINS
12581 "tag_binary",
12582#endif
12583#ifdef FEAT_TAG_OLDSTATIC
12584 "tag_old_static",
12585#endif
12586#ifdef FEAT_TAG_ANYWHITE
12587 "tag_any_white",
12588#endif
12589#ifdef FEAT_TCL
12590# ifndef DYNAMIC_TCL
12591 "tcl",
12592# endif
12593#endif
12594#ifdef TERMINFO
12595 "terminfo",
12596#endif
12597#ifdef FEAT_TERMRESPONSE
12598 "termresponse",
12599#endif
12600#ifdef FEAT_TEXTOBJ
12601 "textobjects",
12602#endif
12603#ifdef HAVE_TGETENT
12604 "tgetent",
12605#endif
12606#ifdef FEAT_TITLE
12607 "title",
12608#endif
12609#ifdef FEAT_TOOLBAR
12610 "toolbar",
12611#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012612#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12613 "unnamedplus",
12614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615#ifdef FEAT_USR_CMDS
12616 "user-commands", /* was accidentally included in 5.4 */
12617 "user_commands",
12618#endif
12619#ifdef FEAT_VIMINFO
12620 "viminfo",
12621#endif
12622#ifdef FEAT_VERTSPLIT
12623 "vertsplit",
12624#endif
12625#ifdef FEAT_VIRTUALEDIT
12626 "virtualedit",
12627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012628 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629#ifdef FEAT_VISUALEXTRA
12630 "visualextra",
12631#endif
12632#ifdef FEAT_VREPLACE
12633 "vreplace",
12634#endif
12635#ifdef FEAT_WILDIGN
12636 "wildignore",
12637#endif
12638#ifdef FEAT_WILDMENU
12639 "wildmenu",
12640#endif
12641#ifdef FEAT_WINDOWS
12642 "windows",
12643#endif
12644#ifdef FEAT_WAK
12645 "winaltkeys",
12646#endif
12647#ifdef FEAT_WRITEBACKUP
12648 "writebackup",
12649#endif
12650#ifdef FEAT_XIM
12651 "xim",
12652#endif
12653#ifdef FEAT_XFONTSET
12654 "xfontset",
12655#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012656#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012657 "xpm",
12658 "xpm_w32", /* for backward compatibility */
12659#else
12660# if defined(HAVE_XPM)
12661 "xpm",
12662# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664#ifdef USE_XSMP
12665 "xsmp",
12666#endif
12667#ifdef USE_XSMP_INTERACT
12668 "xsmp_interact",
12669#endif
12670#ifdef FEAT_XCLIPBOARD
12671 "xterm_clipboard",
12672#endif
12673#ifdef FEAT_XTERM_SAVE
12674 "xterm_save",
12675#endif
12676#if defined(UNIX) && defined(FEAT_X11)
12677 "X11",
12678#endif
12679 NULL
12680 };
12681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 for (i = 0; has_list[i] != NULL; ++i)
12684 if (STRICMP(name, has_list[i]) == 0)
12685 {
12686 n = TRUE;
12687 break;
12688 }
12689
12690 if (n == FALSE)
12691 {
12692 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012693 {
12694 if (name[5] == '-'
12695 && STRLEN(name) > 11
12696 && vim_isdigit(name[6])
12697 && vim_isdigit(name[8])
12698 && vim_isdigit(name[10]))
12699 {
12700 int major = atoi((char *)name + 6);
12701 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012702
12703 /* Expect "patch-9.9.01234". */
12704 n = (major < VIM_VERSION_MAJOR
12705 || (major == VIM_VERSION_MAJOR
12706 && (minor < VIM_VERSION_MINOR
12707 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012708 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012709 }
12710 else
12711 n = has_patch(atoi((char *)name + 5));
12712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 else if (STRICMP(name, "vim_starting") == 0)
12714 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012715#ifdef FEAT_MBYTE
12716 else if (STRICMP(name, "multi_byte_encoding") == 0)
12717 n = has_mbyte;
12718#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012719#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12720 else if (STRICMP(name, "balloon_multiline") == 0)
12721 n = multiline_balloon_available();
12722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723#ifdef DYNAMIC_TCL
12724 else if (STRICMP(name, "tcl") == 0)
12725 n = tcl_enabled(FALSE);
12726#endif
12727#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12728 else if (STRICMP(name, "iconv") == 0)
12729 n = iconv_enabled(FALSE);
12730#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012731#ifdef DYNAMIC_LUA
12732 else if (STRICMP(name, "lua") == 0)
12733 n = lua_enabled(FALSE);
12734#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012735#ifdef DYNAMIC_MZSCHEME
12736 else if (STRICMP(name, "mzscheme") == 0)
12737 n = mzscheme_enabled(FALSE);
12738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739#ifdef DYNAMIC_RUBY
12740 else if (STRICMP(name, "ruby") == 0)
12741 n = ruby_enabled(FALSE);
12742#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012743#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744#ifdef DYNAMIC_PYTHON
12745 else if (STRICMP(name, "python") == 0)
12746 n = python_enabled(FALSE);
12747#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012748#endif
12749#ifdef FEAT_PYTHON3
12750#ifdef DYNAMIC_PYTHON3
12751 else if (STRICMP(name, "python3") == 0)
12752 n = python3_enabled(FALSE);
12753#endif
12754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755#ifdef DYNAMIC_PERL
12756 else if (STRICMP(name, "perl") == 0)
12757 n = perl_enabled(FALSE);
12758#endif
12759#ifdef FEAT_GUI
12760 else if (STRICMP(name, "gui_running") == 0)
12761 n = (gui.in_use || gui.starting);
12762# ifdef FEAT_GUI_W32
12763 else if (STRICMP(name, "gui_win32s") == 0)
12764 n = gui_is_win32s();
12765# endif
12766# ifdef FEAT_BROWSE
12767 else if (STRICMP(name, "browse") == 0)
12768 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12769# endif
12770#endif
12771#ifdef FEAT_SYN_HL
12772 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012773 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774#endif
12775#if defined(WIN3264)
12776 else if (STRICMP(name, "win95") == 0)
12777 n = mch_windows95();
12778#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012779#ifdef FEAT_NETBEANS_INTG
12780 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012781 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 }
12784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786}
12787
12788/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012789 * "has_key()" function
12790 */
12791 static void
12792f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012793 typval_T *argvars;
12794 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012795{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012796 if (argvars[0].v_type != VAR_DICT)
12797 {
12798 EMSG(_(e_dictreq));
12799 return;
12800 }
12801 if (argvars[0].vval.v_dict == NULL)
12802 return;
12803
12804 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012805 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012806}
12807
12808/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012809 * "haslocaldir()" function
12810 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012811 static void
12812f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012813 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012814 typval_T *rettv;
12815{
12816 rettv->vval.v_number = (curwin->w_localdir != NULL);
12817}
12818
12819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 * "hasmapto()" function
12821 */
12822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
12827 char_u *name;
12828 char_u *mode;
12829 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012830 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012833 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 mode = (char_u *)"nvo";
12835 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012838 if (argvars[2].v_type != VAR_UNKNOWN)
12839 abbr = get_tv_number(&argvars[2]);
12840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841
Bram Moolenaar2c932302006-03-18 21:42:09 +000012842 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846}
12847
12848/*
12849 * "histadd()" function
12850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855{
12856#ifdef FEAT_CMDHIST
12857 int histype;
12858 char_u *str;
12859 char_u buf[NUMBUFLEN];
12860#endif
12861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 if (check_restricted() || check_secure())
12864 return;
12865#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12867 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 if (histype >= 0)
12869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 if (*str != NUL)
12872 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012873 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 return;
12877 }
12878 }
12879#endif
12880}
12881
12882/*
12883 * "histdel()" function
12884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012887 typval_T *argvars UNUSED;
12888 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889{
12890#ifdef FEAT_CMDHIST
12891 int n;
12892 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012893 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012895 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12896 if (str == NULL)
12897 n = 0;
12898 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012900 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012901 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 else
12906 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 get_tv_string_buf(&argvars[1], buf));
12909 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910#endif
12911}
12912
12913/*
12914 * "histget()" function
12915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012918 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
12921#ifdef FEAT_CMDHIST
12922 int type;
12923 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012924 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012926 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12927 if (str == NULL)
12928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012930 {
12931 type = get_histtype(str);
12932 if (argvars[1].v_type == VAR_UNKNOWN)
12933 idx = get_history_idx(type);
12934 else
12935 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12936 /* -1 on type error */
12937 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943}
12944
12945/*
12946 * "histnr()" function
12947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012949f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012950 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
12953 int i;
12954
12955#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956 char_u *history = get_tv_string_chk(&argvars[0]);
12957
12958 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 if (i >= HIST_CMD && i < HIST_COUNT)
12960 i = get_history_idx(i);
12961 else
12962#endif
12963 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965}
12966
12967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968 * "highlightID(name)" function
12969 */
12970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 typval_T *argvars;
12973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976}
12977
12978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012979 * "highlight_exists()" function
12980 */
12981 static void
12982f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012983 typval_T *argvars;
12984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012985{
12986 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12987}
12988
12989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 * "hostname()" function
12991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012994 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996{
12997 char_u hostname[256];
12998
12999 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000 rettv->v_type = VAR_STRING;
13001 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002}
13003
13004/*
13005 * iconv() function
13006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013008f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013009 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011{
13012#ifdef FEAT_MBYTE
13013 char_u buf1[NUMBUFLEN];
13014 char_u buf2[NUMBUFLEN];
13015 char_u *from, *to, *str;
13016 vimconv_T vimconv;
13017#endif
13018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019 rettv->v_type = VAR_STRING;
13020 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021
13022#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 str = get_tv_string(&argvars[0]);
13024 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13025 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 vimconv.vc_type = CONV_NONE;
13027 convert_setup(&vimconv, from, to);
13028
13029 /* If the encodings are equal, no conversion needed. */
13030 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
13035 convert_setup(&vimconv, NULL, NULL);
13036 vim_free(from);
13037 vim_free(to);
13038#endif
13039}
13040
13041/*
13042 * "indent()" function
13043 */
13044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013046 typval_T *argvars;
13047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
13049 linenr_T lnum;
13050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013051 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056}
13057
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013058/*
13059 * "index()" function
13060 */
13061 static void
13062f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013063 typval_T *argvars;
13064 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013065{
Bram Moolenaar33570922005-01-25 22:26:29 +000013066 list_T *l;
13067 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013068 long idx = 0;
13069 int ic = FALSE;
13070
13071 rettv->vval.v_number = -1;
13072 if (argvars[0].v_type != VAR_LIST)
13073 {
13074 EMSG(_(e_listreq));
13075 return;
13076 }
13077 l = argvars[0].vval.v_list;
13078 if (l != NULL)
13079 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013080 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013083 int error = FALSE;
13084
Bram Moolenaar758711c2005-02-02 23:11:38 +000013085 /* Start at specified item. Use the cached index that list_find()
13086 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013088 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013089 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013090 ic = get_tv_number_chk(&argvars[3], &error);
13091 if (error)
13092 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013093 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013094
Bram Moolenaar758711c2005-02-02 23:11:38 +000013095 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013096 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013097 {
13098 rettv->vval.v_number = idx;
13099 break;
13100 }
13101 }
13102}
13103
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104static int inputsecret_flag = 0;
13105
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013106static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13107
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013109 * This function is used by f_input() and f_inputdialog() functions. The third
13110 * argument to f_input() specifies the type of completion to use at the
13111 * prompt. The third argument to f_inputdialog() specifies the value to return
13112 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 */
13114 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013115get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *argvars;
13117 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013118 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 char_u *p = NULL;
13122 int c;
13123 char_u buf[NUMBUFLEN];
13124 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013126 int xp_type = EXPAND_NOTHING;
13127 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013129 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131
13132#ifdef NO_CONSOLE_INPUT
13133 /* While starting up, there is no place to enter text. */
13134 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136#endif
13137
13138 cmd_silent = FALSE; /* Want to see the prompt. */
13139 if (prompt != NULL)
13140 {
13141 /* Only the part of the message after the last NL is considered as
13142 * prompt for the command line */
13143 p = vim_strrchr(prompt, '\n');
13144 if (p == NULL)
13145 p = prompt;
13146 else
13147 {
13148 ++p;
13149 c = *p;
13150 *p = NUL;
13151 msg_start();
13152 msg_clr_eos();
13153 msg_puts_attr(prompt, echo_attr);
13154 msg_didout = FALSE;
13155 msg_starthere();
13156 *p = c;
13157 }
13158 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013160 if (argvars[1].v_type != VAR_UNKNOWN)
13161 {
13162 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13163 if (defstr != NULL)
13164 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013166 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013167 {
13168 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013169 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013170 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013171
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013172 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013173 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013174
Bram Moolenaar4463f292005-09-25 22:20:24 +000013175 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13176 if (xp_name == NULL)
13177 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013179 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013180
Bram Moolenaar4463f292005-09-25 22:20:24 +000013181 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13182 &xp_arg) == FAIL)
13183 return;
13184 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013185 }
13186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013187 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013188 {
13189# ifdef FEAT_EX_EXTRA
13190 int save_ex_normal_busy = ex_normal_busy;
13191 ex_normal_busy = 0;
13192# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013194 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13195 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013196# ifdef FEAT_EX_EXTRA
13197 ex_normal_busy = save_ex_normal_busy;
13198# endif
13199 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013200 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013201 && argvars[1].v_type != VAR_UNKNOWN
13202 && argvars[2].v_type != VAR_UNKNOWN)
13203 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13204 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013205
13206 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 /* since the user typed this, no need to wait for return */
13209 need_wait_return = FALSE;
13210 msg_didout = FALSE;
13211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 cmd_silent = cmd_silent_save;
13213}
13214
13215/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013216 * "input()" function
13217 * Also handles inputsecret() when inputsecret is set.
13218 */
13219 static void
13220f_input(argvars, rettv)
13221 typval_T *argvars;
13222 typval_T *rettv;
13223{
13224 get_user_input(argvars, rettv, FALSE);
13225}
13226
13227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 * "inputdialog()" function
13229 */
13230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 typval_T *argvars;
13233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234{
13235#if defined(FEAT_GUI_TEXTDIALOG)
13236 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13237 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13238 {
13239 char_u *message;
13240 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013241 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013243 message = get_tv_string_chk(&argvars[0]);
13244 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013245 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013246 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 else
13248 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 if (message != NULL && defstr != NULL
13250 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013251 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 else
13254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013255 if (message != NULL && defstr != NULL
13256 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013257 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 rettv->vval.v_string = vim_strsave(
13259 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 }
13265 else
13266#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013267 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268}
13269
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013270/*
13271 * "inputlist()" function
13272 */
13273 static void
13274f_inputlist(argvars, rettv)
13275 typval_T *argvars;
13276 typval_T *rettv;
13277{
13278 listitem_T *li;
13279 int selected;
13280 int mouse_used;
13281
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013282#ifdef NO_CONSOLE_INPUT
13283 /* While starting up, there is no place to enter text. */
13284 if (no_console_input())
13285 return;
13286#endif
13287 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13288 {
13289 EMSG2(_(e_listarg), "inputlist()");
13290 return;
13291 }
13292
13293 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013294 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013295 lines_left = Rows; /* avoid more prompt */
13296 msg_scroll = TRUE;
13297 msg_clr_eos();
13298
13299 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13300 {
13301 msg_puts(get_tv_string(&li->li_tv));
13302 msg_putchar('\n');
13303 }
13304
13305 /* Ask for choice. */
13306 selected = prompt_for_number(&mouse_used);
13307 if (mouse_used)
13308 selected -= lines_left;
13309
13310 rettv->vval.v_number = selected;
13311}
13312
13313
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13315
13316/*
13317 * "inputrestore()" function
13318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323{
13324 if (ga_userinput.ga_len > 0)
13325 {
13326 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13328 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013329 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330 }
13331 else if (p_verbose > 1)
13332 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013333 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013334 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 }
13336}
13337
13338/*
13339 * "inputsave()" function
13340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013342f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013346 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 if (ga_grow(&ga_userinput, 1) == OK)
13348 {
13349 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13350 + ga_userinput.ga_len);
13351 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013352 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 }
13354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013355 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356}
13357
13358/*
13359 * "inputsecret()" function
13360 */
13361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365{
13366 ++cmdline_star;
13367 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 --cmdline_star;
13370 --inputsecret_flag;
13371}
13372
13373/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013374 * "insert()" function
13375 */
13376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 typval_T *argvars;
13379 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013380{
13381 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013382 listitem_T *item;
13383 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013385
13386 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013387 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013388 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013389 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013390 {
13391 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013392 before = get_tv_number_chk(&argvars[2], &error);
13393 if (error)
13394 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013395
Bram Moolenaar758711c2005-02-02 23:11:38 +000013396 if (before == l->lv_len)
13397 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013398 else
13399 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013400 item = list_find(l, before);
13401 if (item == NULL)
13402 {
13403 EMSGN(_(e_listidx), before);
13404 l = NULL;
13405 }
13406 }
13407 if (l != NULL)
13408 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013409 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013410 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013411 }
13412 }
13413}
13414
13415/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013416 * "invert(expr)" function
13417 */
13418 static void
13419f_invert(argvars, rettv)
13420 typval_T *argvars;
13421 typval_T *rettv;
13422{
13423 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13424}
13425
13426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 * "isdirectory()" function
13428 */
13429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013430f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013431 typval_T *argvars;
13432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013434 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435}
13436
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013437/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013438 * "islocked()" function
13439 */
13440 static void
13441f_islocked(argvars, rettv)
13442 typval_T *argvars;
13443 typval_T *rettv;
13444{
13445 lval_T lv;
13446 char_u *end;
13447 dictitem_T *di;
13448
13449 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013450 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13451 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013452 if (end != NULL && lv.ll_name != NULL)
13453 {
13454 if (*end != NUL)
13455 EMSG(_(e_trailing));
13456 else
13457 {
13458 if (lv.ll_tv == NULL)
13459 {
13460 if (check_changedtick(lv.ll_name))
13461 rettv->vval.v_number = 1; /* always locked */
13462 else
13463 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013464 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013465 if (di != NULL)
13466 {
13467 /* Consider a variable locked when:
13468 * 1. the variable itself is locked
13469 * 2. the value of the variable is locked.
13470 * 3. the List or Dict value is locked.
13471 */
13472 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13473 || tv_islocked(&di->di_tv));
13474 }
13475 }
13476 }
13477 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013478 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013479 else if (lv.ll_newkey != NULL)
13480 EMSG2(_(e_dictkey), lv.ll_newkey);
13481 else if (lv.ll_list != NULL)
13482 /* List item. */
13483 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13484 else
13485 /* Dictionary item. */
13486 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13487 }
13488 }
13489
13490 clear_lval(&lv);
13491}
13492
Bram Moolenaar33570922005-01-25 22:26:29 +000013493static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013494
13495/*
13496 * Turn a dict into a list:
13497 * "what" == 0: list of keys
13498 * "what" == 1: list of values
13499 * "what" == 2: list of items
13500 */
13501 static void
13502dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *argvars;
13504 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013505 int what;
13506{
Bram Moolenaar33570922005-01-25 22:26:29 +000013507 list_T *l2;
13508 dictitem_T *di;
13509 hashitem_T *hi;
13510 listitem_T *li;
13511 listitem_T *li2;
13512 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013513 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013514
Bram Moolenaar8c711452005-01-14 21:53:12 +000013515 if (argvars[0].v_type != VAR_DICT)
13516 {
13517 EMSG(_(e_dictreq));
13518 return;
13519 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013520 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013521 return;
13522
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013523 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013524 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013525
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013526 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013527 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013528 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013529 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013530 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013531 --todo;
13532 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013533
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013534 li = listitem_alloc();
13535 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013536 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013537 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013538
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013539 if (what == 0)
13540 {
13541 /* keys() */
13542 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013543 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013544 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13545 }
13546 else if (what == 1)
13547 {
13548 /* values() */
13549 copy_tv(&di->di_tv, &li->li_tv);
13550 }
13551 else
13552 {
13553 /* items() */
13554 l2 = list_alloc();
13555 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013556 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013557 li->li_tv.vval.v_list = l2;
13558 if (l2 == NULL)
13559 break;
13560 ++l2->lv_refcount;
13561
13562 li2 = listitem_alloc();
13563 if (li2 == NULL)
13564 break;
13565 list_append(l2, li2);
13566 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013567 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013568 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13569
13570 li2 = listitem_alloc();
13571 if (li2 == NULL)
13572 break;
13573 list_append(l2, li2);
13574 copy_tv(&di->di_tv, &li2->li_tv);
13575 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013576 }
13577 }
13578}
13579
13580/*
13581 * "items(dict)" function
13582 */
13583 static void
13584f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013585 typval_T *argvars;
13586 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013587{
13588 dict_list(argvars, rettv, 2);
13589}
13590
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013592 * "join()" function
13593 */
13594 static void
13595f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *argvars;
13597 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013598{
13599 garray_T ga;
13600 char_u *sep;
13601
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013602 if (argvars[0].v_type != VAR_LIST)
13603 {
13604 EMSG(_(e_listreq));
13605 return;
13606 }
13607 if (argvars[0].vval.v_list == NULL)
13608 return;
13609 if (argvars[1].v_type == VAR_UNKNOWN)
13610 sep = (char_u *)" ";
13611 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013613
13614 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013615
13616 if (sep != NULL)
13617 {
13618 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013619 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013620 ga_append(&ga, NUL);
13621 rettv->vval.v_string = (char_u *)ga.ga_data;
13622 }
13623 else
13624 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013625}
13626
13627/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013628 * "keys()" function
13629 */
13630 static void
13631f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013634{
13635 dict_list(argvars, rettv, 0);
13636}
13637
13638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 * "last_buffer_nr()" function.
13640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013643 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645{
13646 int n = 0;
13647 buf_T *buf;
13648
13649 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13650 if (n < buf->b_fnum)
13651 n = buf->b_fnum;
13652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013654}
13655
13656/*
13657 * "len()" function
13658 */
13659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013660f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013663{
13664 switch (argvars[0].v_type)
13665 {
13666 case VAR_STRING:
13667 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 rettv->vval.v_number = (varnumber_T)STRLEN(
13669 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013670 break;
13671 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013673 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013674 case VAR_DICT:
13675 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13676 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013677 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013678 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013679 break;
13680 }
13681}
13682
Bram Moolenaar33570922005-01-25 22:26:29 +000013683static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013684
13685 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013687 typval_T *argvars;
13688 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013689 int type;
13690{
13691#ifdef FEAT_LIBCALL
13692 char_u *string_in;
13693 char_u **string_result;
13694 int nr_result;
13695#endif
13696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013698 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013700
13701 if (check_restricted() || check_secure())
13702 return;
13703
13704#ifdef FEAT_LIBCALL
13705 /* The first two args must be strings, otherwise its meaningless */
13706 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13707 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013708 string_in = NULL;
13709 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013710 string_in = argvars[2].vval.v_string;
13711 if (type == VAR_NUMBER)
13712 string_result = NULL;
13713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013715 if (mch_libcall(argvars[0].vval.v_string,
13716 argvars[1].vval.v_string,
13717 string_in,
13718 argvars[2].vval.v_number,
13719 string_result,
13720 &nr_result) == OK
13721 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013723 }
13724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725}
13726
13727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013728 * "libcall()" function
13729 */
13730 static void
13731f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 typval_T *argvars;
13733 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013734{
13735 libcall_common(argvars, rettv, VAR_STRING);
13736}
13737
13738/*
13739 * "libcallnr()" function
13740 */
13741 static void
13742f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013743 typval_T *argvars;
13744 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013745{
13746 libcall_common(argvars, rettv, VAR_NUMBER);
13747}
13748
13749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750 * "line(string)" function
13751 */
13752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 typval_T *argvars;
13755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
13757 linenr_T lnum = 0;
13758 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013759 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013761 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 if (fp != NULL)
13763 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013764 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765}
13766
13767/*
13768 * "line2byte(lnum)" function
13769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013771f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013772 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774{
13775#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777#else
13778 linenr_T lnum;
13779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013780 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013784 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13785 if (rettv->vval.v_number >= 0)
13786 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#endif
13788}
13789
13790/*
13791 * "lispindent(lnum)" function
13792 */
13793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013794f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797{
13798#ifdef FEAT_LISP
13799 pos_T pos;
13800 linenr_T lnum;
13801
13802 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013803 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13805 {
13806 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013807 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808 curwin->w_cursor = pos;
13809 }
13810 else
13811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813}
13814
13815/*
13816 * "localtime()" function
13817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013819f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013823 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824}
13825
Bram Moolenaar33570922005-01-25 22:26:29 +000013826static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
13828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013829get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013830 typval_T *argvars;
13831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 int exact;
13833{
13834 char_u *keys;
13835 char_u *which;
13836 char_u buf[NUMBUFLEN];
13837 char_u *keys_buf = NULL;
13838 char_u *rhs;
13839 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013840 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013841 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013842 mapblock_T *mp;
13843 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844
13845 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013846 rettv->v_type = VAR_STRING;
13847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 if (*keys == NUL)
13851 return;
13852
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013853 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013855 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013856 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013857 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013858 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013859 if (argvars[3].v_type != VAR_UNKNOWN)
13860 get_dict = get_tv_number(&argvars[3]);
13861 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 else
13864 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 if (which == NULL)
13866 return;
13867
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868 mode = get_map_mode(&which, 0);
13869
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013870 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013871 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013873
13874 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013876 /* Return a string. */
13877 if (rhs != NULL)
13878 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879
Bram Moolenaarbd743252010-10-20 21:23:33 +020013880 }
13881 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13882 {
13883 /* Return a dictionary. */
13884 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13885 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13886 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887
Bram Moolenaarbd743252010-10-20 21:23:33 +020013888 dict_add_nr_str(dict, "lhs", 0L, lhs);
13889 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13890 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13891 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13892 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13893 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13894 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013895 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013896 dict_add_nr_str(dict, "mode", 0L, mapmode);
13897
13898 vim_free(lhs);
13899 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900 }
13901}
13902
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013903#ifdef FEAT_FLOAT
13904/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013905 * "log()" function
13906 */
13907 static void
13908f_log(argvars, rettv)
13909 typval_T *argvars;
13910 typval_T *rettv;
13911{
13912 float_T f;
13913
13914 rettv->v_type = VAR_FLOAT;
13915 if (get_float_arg(argvars, &f) == OK)
13916 rettv->vval.v_float = log(f);
13917 else
13918 rettv->vval.v_float = 0.0;
13919}
13920
13921/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013922 * "log10()" function
13923 */
13924 static void
13925f_log10(argvars, rettv)
13926 typval_T *argvars;
13927 typval_T *rettv;
13928{
13929 float_T f;
13930
13931 rettv->v_type = VAR_FLOAT;
13932 if (get_float_arg(argvars, &f) == OK)
13933 rettv->vval.v_float = log10(f);
13934 else
13935 rettv->vval.v_float = 0.0;
13936}
13937#endif
13938
Bram Moolenaar1dced572012-04-05 16:54:08 +020013939#ifdef FEAT_LUA
13940/*
13941 * "luaeval()" function
13942 */
13943 static void
13944f_luaeval(argvars, rettv)
13945 typval_T *argvars;
13946 typval_T *rettv;
13947{
13948 char_u *str;
13949 char_u buf[NUMBUFLEN];
13950
13951 str = get_tv_string_buf(&argvars[0], buf);
13952 do_luaeval(str, argvars + 1, rettv);
13953}
13954#endif
13955
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013957 * "map()" function
13958 */
13959 static void
13960f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013961 typval_T *argvars;
13962 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013963{
13964 filter_map(argvars, rettv, TRUE);
13965}
13966
13967/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013968 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 */
13970 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013972 typval_T *argvars;
13973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013975 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976}
13977
13978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 */
13981 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013983 typval_T *argvars;
13984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013986 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987}
13988
Bram Moolenaar33570922005-01-25 22:26:29 +000013989static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990
13991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013993 typval_T *argvars;
13994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 int type;
13996{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013997 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013998 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013999 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 char_u *pat;
14001 regmatch_T regmatch;
14002 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014003 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 char_u *save_cpo;
14005 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014006 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014007 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014008 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014009 list_T *l = NULL;
14010 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014011 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014012 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013
14014 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14015 save_cpo = p_cpo;
14016 p_cpo = (char_u *)"";
14017
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014018 rettv->vval.v_number = -1;
14019 if (type == 3)
14020 {
14021 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014022 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014023 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014024 }
14025 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 rettv->v_type = VAR_STRING;
14028 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014031 if (argvars[0].v_type == VAR_LIST)
14032 {
14033 if ((l = argvars[0].vval.v_list) == NULL)
14034 goto theend;
14035 li = l->lv_first;
14036 }
14037 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014038 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014039 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014040 len = (long)STRLEN(str);
14041 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014043 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14044 if (pat == NULL)
14045 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 int error = FALSE;
14050
14051 start = get_tv_number_chk(&argvars[2], &error);
14052 if (error)
14053 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014054 if (l != NULL)
14055 {
14056 li = list_find(l, start);
14057 if (li == NULL)
14058 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014059 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014060 }
14061 else
14062 {
14063 if (start < 0)
14064 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014065 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014066 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014067 /* When "count" argument is there ignore matches before "start",
14068 * otherwise skip part of the string. Differs when pattern is "^"
14069 * or "\<". */
14070 if (argvars[3].v_type != VAR_UNKNOWN)
14071 startcol = start;
14072 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014073 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014074 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014075 len -= start;
14076 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014077 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014078
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014079 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 nth = get_tv_number_chk(&argvars[3], &error);
14081 if (error)
14082 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 }
14084
14085 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14086 if (regmatch.regprog != NULL)
14087 {
14088 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014089
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014090 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014091 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014092 if (l != NULL)
14093 {
14094 if (li == NULL)
14095 {
14096 match = FALSE;
14097 break;
14098 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014099 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014100 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014101 if (str == NULL)
14102 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014103 }
14104
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014105 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014106
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014107 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014108 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014109 if (l == NULL && !match)
14110 break;
14111
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014112 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014113 if (l != NULL)
14114 {
14115 li = li->li_next;
14116 ++idx;
14117 }
14118 else
14119 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014120#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014121 startcol = (colnr_T)(regmatch.startp[0]
14122 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014123#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014124 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014125#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014126 if (startcol > (colnr_T)len
14127 || str + startcol <= regmatch.startp[0])
14128 {
14129 match = FALSE;
14130 break;
14131 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014132 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014133 }
14134
14135 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014137 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014138 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014139 int i;
14140
14141 /* return list with matched string and submatches */
14142 for (i = 0; i < NSUBEXP; ++i)
14143 {
14144 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014145 {
14146 if (list_append_string(rettv->vval.v_list,
14147 (char_u *)"", 0) == FAIL)
14148 break;
14149 }
14150 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014151 regmatch.startp[i],
14152 (int)(regmatch.endp[i] - regmatch.startp[i]))
14153 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014154 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014155 }
14156 }
14157 else if (type == 2)
14158 {
14159 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014160 if (l != NULL)
14161 copy_tv(&li->li_tv, rettv);
14162 else
14163 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014165 }
14166 else if (l != NULL)
14167 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 else
14169 {
14170 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 (varnumber_T)(regmatch.startp[0] - str);
14173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014174 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014176 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 }
14178 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014179 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 }
14181
14182theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014183 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 p_cpo = save_cpo;
14185}
14186
14187/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014188 * "match()" function
14189 */
14190 static void
14191f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 typval_T *argvars;
14193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194{
14195 find_some_match(argvars, rettv, 1);
14196}
14197
14198/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014199 * "matchadd()" function
14200 */
14201 static void
14202f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014203 typval_T *argvars UNUSED;
14204 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014205{
14206#ifdef FEAT_SEARCH_EXTRA
14207 char_u buf[NUMBUFLEN];
14208 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14209 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14210 int prio = 10; /* default priority */
14211 int id = -1;
14212 int error = FALSE;
14213
14214 rettv->vval.v_number = -1;
14215
14216 if (grp == NULL || pat == NULL)
14217 return;
14218 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014219 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014220 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014221 if (argvars[3].v_type != VAR_UNKNOWN)
14222 id = get_tv_number_chk(&argvars[3], &error);
14223 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014224 if (error == TRUE)
14225 return;
14226 if (id >= 1 && id <= 3)
14227 {
14228 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14229 return;
14230 }
14231
14232 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14233#endif
14234}
14235
14236/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014237 * "matcharg()" function
14238 */
14239 static void
14240f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014241 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014242 typval_T *rettv;
14243{
14244 if (rettv_list_alloc(rettv) == OK)
14245 {
14246#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014247 int id = get_tv_number(&argvars[0]);
14248 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014249
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014250 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014251 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014252 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14253 {
14254 list_append_string(rettv->vval.v_list,
14255 syn_id2name(m->hlg_id), -1);
14256 list_append_string(rettv->vval.v_list, m->pattern, -1);
14257 }
14258 else
14259 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014260 list_append_string(rettv->vval.v_list, NULL, -1);
14261 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014262 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014263 }
14264#endif
14265 }
14266}
14267
14268/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014269 * "matchdelete()" function
14270 */
14271 static void
14272f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014273 typval_T *argvars UNUSED;
14274 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014275{
14276#ifdef FEAT_SEARCH_EXTRA
14277 rettv->vval.v_number = match_delete(curwin,
14278 (int)get_tv_number(&argvars[0]), TRUE);
14279#endif
14280}
14281
14282/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014283 * "matchend()" function
14284 */
14285 static void
14286f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014287 typval_T *argvars;
14288 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014289{
14290 find_some_match(argvars, rettv, 0);
14291}
14292
14293/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014294 * "matchlist()" function
14295 */
14296 static void
14297f_matchlist(argvars, rettv)
14298 typval_T *argvars;
14299 typval_T *rettv;
14300{
14301 find_some_match(argvars, rettv, 3);
14302}
14303
14304/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014305 * "matchstr()" function
14306 */
14307 static void
14308f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014309 typval_T *argvars;
14310 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014311{
14312 find_some_match(argvars, rettv, 2);
14313}
14314
Bram Moolenaar33570922005-01-25 22:26:29 +000014315static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014316
14317 static void
14318max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 typval_T *argvars;
14320 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014321 int domax;
14322{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014323 long n = 0;
14324 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014326
14327 if (argvars[0].v_type == VAR_LIST)
14328 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 list_T *l;
14330 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014331
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014332 l = argvars[0].vval.v_list;
14333 if (l != NULL)
14334 {
14335 li = l->lv_first;
14336 if (li != NULL)
14337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014339 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014340 {
14341 li = li->li_next;
14342 if (li == NULL)
14343 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014345 if (domax ? i > n : i < n)
14346 n = i;
14347 }
14348 }
14349 }
14350 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014351 else if (argvars[0].v_type == VAR_DICT)
14352 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014353 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014354 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014355 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014356 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014357
14358 d = argvars[0].vval.v_dict;
14359 if (d != NULL)
14360 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014361 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014362 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014364 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014366 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014368 if (first)
14369 {
14370 n = i;
14371 first = FALSE;
14372 }
14373 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014374 n = i;
14375 }
14376 }
14377 }
14378 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014379 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014380 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014382}
14383
14384/*
14385 * "max()" function
14386 */
14387 static void
14388f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014389 typval_T *argvars;
14390 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014391{
14392 max_min(argvars, rettv, TRUE);
14393}
14394
14395/*
14396 * "min()" function
14397 */
14398 static void
14399f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014400 typval_T *argvars;
14401 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014402{
14403 max_min(argvars, rettv, FALSE);
14404}
14405
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014406static int mkdir_recurse __ARGS((char_u *dir, int prot));
14407
14408/*
14409 * Create the directory in which "dir" is located, and higher levels when
14410 * needed.
14411 */
14412 static int
14413mkdir_recurse(dir, prot)
14414 char_u *dir;
14415 int prot;
14416{
14417 char_u *p;
14418 char_u *updir;
14419 int r = FAIL;
14420
14421 /* Get end of directory name in "dir".
14422 * We're done when it's "/" or "c:/". */
14423 p = gettail_sep(dir);
14424 if (p <= get_past_head(dir))
14425 return OK;
14426
14427 /* If the directory exists we're done. Otherwise: create it.*/
14428 updir = vim_strnsave(dir, (int)(p - dir));
14429 if (updir == NULL)
14430 return FAIL;
14431 if (mch_isdir(updir))
14432 r = OK;
14433 else if (mkdir_recurse(updir, prot) == OK)
14434 r = vim_mkdir_emsg(updir, prot);
14435 vim_free(updir);
14436 return r;
14437}
14438
14439#ifdef vim_mkdir
14440/*
14441 * "mkdir()" function
14442 */
14443 static void
14444f_mkdir(argvars, rettv)
14445 typval_T *argvars;
14446 typval_T *rettv;
14447{
14448 char_u *dir;
14449 char_u buf[NUMBUFLEN];
14450 int prot = 0755;
14451
14452 rettv->vval.v_number = FAIL;
14453 if (check_restricted() || check_secure())
14454 return;
14455
14456 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014457 if (*dir == NUL)
14458 rettv->vval.v_number = FAIL;
14459 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014460 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014461 if (*gettail(dir) == NUL)
14462 /* remove trailing slashes */
14463 *gettail_sep(dir) = NUL;
14464
14465 if (argvars[1].v_type != VAR_UNKNOWN)
14466 {
14467 if (argvars[2].v_type != VAR_UNKNOWN)
14468 prot = get_tv_number_chk(&argvars[2], NULL);
14469 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14470 mkdir_recurse(dir, prot);
14471 }
14472 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014473 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014474}
14475#endif
14476
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 * "mode()" function
14479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014482 typval_T *argvars;
14483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014485 char_u buf[3];
14486
14487 buf[1] = NUL;
14488 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 if (VIsual_active)
14491 {
14492 if (VIsual_select)
14493 buf[0] = VIsual_mode + 's' - 'v';
14494 else
14495 buf[0] = VIsual_mode;
14496 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014497 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014498 || State == CONFIRM)
14499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014501 if (State == ASKMORE)
14502 buf[1] = 'm';
14503 else if (State == CONFIRM)
14504 buf[1] = '?';
14505 }
14506 else if (State == EXTERNCMD)
14507 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 else if (State & INSERT)
14509 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014510#ifdef FEAT_VREPLACE
14511 if (State & VREPLACE_FLAG)
14512 {
14513 buf[0] = 'R';
14514 buf[1] = 'v';
14515 }
14516 else
14517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 if (State & REPLACE_FLAG)
14519 buf[0] = 'R';
14520 else
14521 buf[0] = 'i';
14522 }
14523 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014526 if (exmode_active)
14527 buf[1] = 'v';
14528 }
14529 else if (exmode_active)
14530 {
14531 buf[0] = 'c';
14532 buf[1] = 'e';
14533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014537 if (finish_op)
14538 buf[1] = 'o';
14539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014541 /* Clear out the minor mode when the argument is not a non-zero number or
14542 * non-empty string. */
14543 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014544 buf[1] = NUL;
14545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_string = vim_strsave(buf);
14547 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014548}
14549
Bram Moolenaar429fa852013-04-15 12:27:36 +020014550#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014551/*
14552 * "mzeval()" function
14553 */
14554 static void
14555f_mzeval(argvars, rettv)
14556 typval_T *argvars;
14557 typval_T *rettv;
14558{
14559 char_u *str;
14560 char_u buf[NUMBUFLEN];
14561
14562 str = get_tv_string_buf(&argvars[0], buf);
14563 do_mzeval(str, rettv);
14564}
Bram Moolenaar75676462013-01-30 14:55:42 +010014565
14566 void
14567mzscheme_call_vim(name, args, rettv)
14568 char_u *name;
14569 typval_T *args;
14570 typval_T *rettv;
14571{
14572 typval_T argvars[3];
14573
14574 argvars[0].v_type = VAR_STRING;
14575 argvars[0].vval.v_string = name;
14576 copy_tv(args, &argvars[1]);
14577 argvars[2].v_type = VAR_UNKNOWN;
14578 f_call(argvars, rettv);
14579 clear_tv(&argvars[1]);
14580}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014581#endif
14582
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584 * "nextnonblank()" function
14585 */
14586 static void
14587f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014588 typval_T *argvars;
14589 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590{
14591 linenr_T lnum;
14592
14593 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014596 {
14597 lnum = 0;
14598 break;
14599 }
14600 if (*skipwhite(ml_get(lnum)) != NUL)
14601 break;
14602 }
14603 rettv->vval.v_number = lnum;
14604}
14605
14606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607 * "nr2char()" function
14608 */
14609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014610f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014611 typval_T *argvars;
14612 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613{
14614 char_u buf[NUMBUFLEN];
14615
14616#ifdef FEAT_MBYTE
14617 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014618 {
14619 int utf8 = 0;
14620
14621 if (argvars[1].v_type != VAR_UNKNOWN)
14622 utf8 = get_tv_number_chk(&argvars[1], NULL);
14623 if (utf8)
14624 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14625 else
14626 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 else
14629#endif
14630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014631 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 buf[1] = NUL;
14633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014634 rettv->v_type = VAR_STRING;
14635 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014636}
14637
14638/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014639 * "or(expr, expr)" function
14640 */
14641 static void
14642f_or(argvars, rettv)
14643 typval_T *argvars;
14644 typval_T *rettv;
14645{
14646 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14647 | get_tv_number_chk(&argvars[1], NULL);
14648}
14649
14650/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014651 * "pathshorten()" function
14652 */
14653 static void
14654f_pathshorten(argvars, rettv)
14655 typval_T *argvars;
14656 typval_T *rettv;
14657{
14658 char_u *p;
14659
14660 rettv->v_type = VAR_STRING;
14661 p = get_tv_string_chk(&argvars[0]);
14662 if (p == NULL)
14663 rettv->vval.v_string = NULL;
14664 else
14665 {
14666 p = vim_strsave(p);
14667 rettv->vval.v_string = p;
14668 if (p != NULL)
14669 shorten_dir(p);
14670 }
14671}
14672
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014673#ifdef FEAT_FLOAT
14674/*
14675 * "pow()" function
14676 */
14677 static void
14678f_pow(argvars, rettv)
14679 typval_T *argvars;
14680 typval_T *rettv;
14681{
14682 float_T fx, fy;
14683
14684 rettv->v_type = VAR_FLOAT;
14685 if (get_float_arg(argvars, &fx) == OK
14686 && get_float_arg(&argvars[1], &fy) == OK)
14687 rettv->vval.v_float = pow(fx, fy);
14688 else
14689 rettv->vval.v_float = 0.0;
14690}
14691#endif
14692
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014694 * "prevnonblank()" function
14695 */
14696 static void
14697f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014698 typval_T *argvars;
14699 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014700{
14701 linenr_T lnum;
14702
14703 lnum = get_tv_lnum(argvars);
14704 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14705 lnum = 0;
14706 else
14707 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14708 --lnum;
14709 rettv->vval.v_number = lnum;
14710}
14711
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014712#ifdef HAVE_STDARG_H
14713/* This dummy va_list is here because:
14714 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14715 * - locally in the function results in a "used before set" warning
14716 * - using va_start() to initialize it gives "function with fixed args" error */
14717static va_list ap;
14718#endif
14719
Bram Moolenaar8c711452005-01-14 21:53:12 +000014720/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014721 * "printf()" function
14722 */
14723 static void
14724f_printf(argvars, rettv)
14725 typval_T *argvars;
14726 typval_T *rettv;
14727{
14728 rettv->v_type = VAR_STRING;
14729 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014730#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014731 {
14732 char_u buf[NUMBUFLEN];
14733 int len;
14734 char_u *s;
14735 int saved_did_emsg = did_emsg;
14736 char *fmt;
14737
14738 /* Get the required length, allocate the buffer and do it for real. */
14739 did_emsg = FALSE;
14740 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014741 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014742 if (!did_emsg)
14743 {
14744 s = alloc(len + 1);
14745 if (s != NULL)
14746 {
14747 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014748 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014749 }
14750 }
14751 did_emsg |= saved_did_emsg;
14752 }
14753#endif
14754}
14755
14756/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014757 * "pumvisible()" function
14758 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014759 static void
14760f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014761 typval_T *argvars UNUSED;
14762 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014763{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014764#ifdef FEAT_INS_EXPAND
14765 if (pum_visible())
14766 rettv->vval.v_number = 1;
14767#endif
14768}
14769
Bram Moolenaardb913952012-06-29 12:54:53 +020014770#ifdef FEAT_PYTHON3
14771/*
14772 * "py3eval()" function
14773 */
14774 static void
14775f_py3eval(argvars, rettv)
14776 typval_T *argvars;
14777 typval_T *rettv;
14778{
14779 char_u *str;
14780 char_u buf[NUMBUFLEN];
14781
14782 str = get_tv_string_buf(&argvars[0], buf);
14783 do_py3eval(str, rettv);
14784}
14785#endif
14786
14787#ifdef FEAT_PYTHON
14788/*
14789 * "pyeval()" function
14790 */
14791 static void
14792f_pyeval(argvars, rettv)
14793 typval_T *argvars;
14794 typval_T *rettv;
14795{
14796 char_u *str;
14797 char_u buf[NUMBUFLEN];
14798
14799 str = get_tv_string_buf(&argvars[0], buf);
14800 do_pyeval(str, rettv);
14801}
14802#endif
14803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014804/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014805 * "range()" function
14806 */
14807 static void
14808f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014809 typval_T *argvars;
14810 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014811{
14812 long start;
14813 long end;
14814 long stride = 1;
14815 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014816 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014819 if (argvars[1].v_type == VAR_UNKNOWN)
14820 {
14821 end = start - 1;
14822 start = 0;
14823 }
14824 else
14825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014826 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014829 }
14830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 if (error)
14832 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014833 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014834 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014835 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014836 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014837 else
14838 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014839 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014840 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014841 if (list_append_number(rettv->vval.v_list,
14842 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014843 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014844 }
14845}
14846
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014847/*
14848 * "readfile()" function
14849 */
14850 static void
14851f_readfile(argvars, rettv)
14852 typval_T *argvars;
14853 typval_T *rettv;
14854{
14855 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014856 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 char_u *fname;
14858 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014859 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14860 int io_size = sizeof(buf);
14861 int readlen; /* size of last fread() */
14862 char_u *prev = NULL; /* previously read bytes, if any */
14863 long prevlen = 0; /* length of data in prev */
14864 long prevsize = 0; /* size of prev buffer */
14865 long maxline = MAXLNUM;
14866 long cnt = 0;
14867 char_u *p; /* position in buf */
14868 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014869
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014870 if (argvars[1].v_type != VAR_UNKNOWN)
14871 {
14872 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14873 binary = TRUE;
14874 if (argvars[2].v_type != VAR_UNKNOWN)
14875 maxline = get_tv_number(&argvars[2]);
14876 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014877
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014878 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014879 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014880
14881 /* Always open the file in binary mode, library functions have a mind of
14882 * their own about CR-LF conversion. */
14883 fname = get_tv_string(&argvars[0]);
14884 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14885 {
14886 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14887 return;
14888 }
14889
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014890 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014891 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014892 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014893
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014894 /* This for loop processes what was read, but is also entered at end
14895 * of file so that either:
14896 * - an incomplete line gets written
14897 * - a "binary" file gets an empty line at the end if it ends in a
14898 * newline. */
14899 for (p = buf, start = buf;
14900 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14901 ++p)
14902 {
14903 if (*p == '\n' || readlen <= 0)
14904 {
14905 listitem_T *li;
14906 char_u *s = NULL;
14907 long_u len = p - start;
14908
14909 /* Finished a line. Remove CRs before NL. */
14910 if (readlen > 0 && !binary)
14911 {
14912 while (len > 0 && start[len - 1] == '\r')
14913 --len;
14914 /* removal may cross back to the "prev" string */
14915 if (len == 0)
14916 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14917 --prevlen;
14918 }
14919 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014920 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014921 else
14922 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014923 /* Change "prev" buffer to be the right size. This way
14924 * the bytes are only copied once, and very long lines are
14925 * allocated only once. */
14926 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014927 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014928 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014929 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014930 prev = NULL; /* the list will own the string */
14931 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014932 }
14933 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 if (s == NULL)
14935 {
14936 do_outofmem_msg((long_u) prevlen + len + 1);
14937 failed = TRUE;
14938 break;
14939 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014940
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014941 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014942 {
14943 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014944 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945 break;
14946 }
14947 li->li_tv.v_type = VAR_STRING;
14948 li->li_tv.v_lock = 0;
14949 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014950 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014951
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014952 start = p + 1; /* step over newline */
14953 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014954 break;
14955 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014956 else if (*p == NUL)
14957 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014958#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014959 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14960 * when finding the BF and check the previous two bytes. */
14961 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014962 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014963 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14964 * + 1, these may be in the "prev" string. */
14965 char_u back1 = p >= buf + 1 ? p[-1]
14966 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14967 char_u back2 = p >= buf + 2 ? p[-2]
14968 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14969 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014970
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014971 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014972 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014973 char_u *dest = p - 2;
14974
14975 /* Usually a BOM is at the beginning of a file, and so at
14976 * the beginning of a line; then we can just step over it.
14977 */
14978 if (start == dest)
14979 start = p + 1;
14980 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014981 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014982 /* have to shuffle buf to close gap */
14983 int adjust_prevlen = 0;
14984
14985 if (dest < buf)
14986 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014987 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014988 dest = buf;
14989 }
14990 if (readlen > p - buf + 1)
14991 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14992 readlen -= 3 - adjust_prevlen;
14993 prevlen -= adjust_prevlen;
14994 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014995 }
14996 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014997 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014998#endif
14999 } /* for */
15000
15001 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15002 break;
15003 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015004 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015005 /* There's part of a line in buf, store it in "prev". */
15006 if (p - start + prevlen >= prevsize)
15007 {
15008 /* need bigger "prev" buffer */
15009 char_u *newprev;
15010
15011 /* A common use case is ordinary text files and "prev" gets a
15012 * fragment of a line, so the first allocation is made
15013 * small, to avoid repeatedly 'allocing' large and
15014 * 'reallocing' small. */
15015 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015016 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015017 else
15018 {
15019 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015020 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015021 prevsize = grow50pc > growmin ? grow50pc : growmin;
15022 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015023 newprev = prev == NULL ? alloc(prevsize)
15024 : vim_realloc(prev, prevsize);
15025 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015026 {
15027 do_outofmem_msg((long_u)prevsize);
15028 failed = TRUE;
15029 break;
15030 }
15031 prev = newprev;
15032 }
15033 /* Add the line part to end of "prev". */
15034 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015035 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015036 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015037 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015038
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015039 /*
15040 * For a negative line count use only the lines at the end of the file,
15041 * free the rest.
15042 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015043 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015044 while (cnt > -maxline)
15045 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015046 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015047 --cnt;
15048 }
15049
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015050 if (failed)
15051 {
15052 list_free(rettv->vval.v_list, TRUE);
15053 /* readfile doc says an empty list is returned on error */
15054 rettv->vval.v_list = list_alloc();
15055 }
15056
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015057 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015058 fclose(fd);
15059}
15060
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015061#if defined(FEAT_RELTIME)
15062static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15063
15064/*
15065 * Convert a List to proftime_T.
15066 * Return FAIL when there is something wrong.
15067 */
15068 static int
15069list2proftime(arg, tm)
15070 typval_T *arg;
15071 proftime_T *tm;
15072{
15073 long n1, n2;
15074 int error = FALSE;
15075
15076 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15077 || arg->vval.v_list->lv_len != 2)
15078 return FAIL;
15079 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15080 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15081# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015082 tm->HighPart = n1;
15083 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015084# else
15085 tm->tv_sec = n1;
15086 tm->tv_usec = n2;
15087# endif
15088 return error ? FAIL : OK;
15089}
15090#endif /* FEAT_RELTIME */
15091
15092/*
15093 * "reltime()" function
15094 */
15095 static void
15096f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015097 typval_T *argvars UNUSED;
15098 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015099{
15100#ifdef FEAT_RELTIME
15101 proftime_T res;
15102 proftime_T start;
15103
15104 if (argvars[0].v_type == VAR_UNKNOWN)
15105 {
15106 /* No arguments: get current time. */
15107 profile_start(&res);
15108 }
15109 else if (argvars[1].v_type == VAR_UNKNOWN)
15110 {
15111 if (list2proftime(&argvars[0], &res) == FAIL)
15112 return;
15113 profile_end(&res);
15114 }
15115 else
15116 {
15117 /* Two arguments: compute the difference. */
15118 if (list2proftime(&argvars[0], &start) == FAIL
15119 || list2proftime(&argvars[1], &res) == FAIL)
15120 return;
15121 profile_sub(&res, &start);
15122 }
15123
15124 if (rettv_list_alloc(rettv) == OK)
15125 {
15126 long n1, n2;
15127
15128# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015129 n1 = res.HighPart;
15130 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015131# else
15132 n1 = res.tv_sec;
15133 n2 = res.tv_usec;
15134# endif
15135 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15136 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15137 }
15138#endif
15139}
15140
15141/*
15142 * "reltimestr()" function
15143 */
15144 static void
15145f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015146 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015147 typval_T *rettv;
15148{
15149#ifdef FEAT_RELTIME
15150 proftime_T tm;
15151#endif
15152
15153 rettv->v_type = VAR_STRING;
15154 rettv->vval.v_string = NULL;
15155#ifdef FEAT_RELTIME
15156 if (list2proftime(&argvars[0], &tm) == OK)
15157 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15158#endif
15159}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015160
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15162static void make_connection __ARGS((void));
15163static int check_connection __ARGS((void));
15164
15165 static void
15166make_connection()
15167{
15168 if (X_DISPLAY == NULL
15169# ifdef FEAT_GUI
15170 && !gui.in_use
15171# endif
15172 )
15173 {
15174 x_force_connect = TRUE;
15175 setup_term_clip();
15176 x_force_connect = FALSE;
15177 }
15178}
15179
15180 static int
15181check_connection()
15182{
15183 make_connection();
15184 if (X_DISPLAY == NULL)
15185 {
15186 EMSG(_("E240: No connection to Vim server"));
15187 return FAIL;
15188 }
15189 return OK;
15190}
15191#endif
15192
15193#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015194static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195
15196 static void
15197remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 typval_T *argvars;
15199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 int expr;
15201{
15202 char_u *server_name;
15203 char_u *keys;
15204 char_u *r = NULL;
15205 char_u buf[NUMBUFLEN];
15206# ifdef WIN32
15207 HWND w;
15208# else
15209 Window w;
15210# endif
15211
15212 if (check_restricted() || check_secure())
15213 return;
15214
15215# ifdef FEAT_X11
15216 if (check_connection() == FAIL)
15217 return;
15218# endif
15219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 server_name = get_tv_string_chk(&argvars[0]);
15221 if (server_name == NULL)
15222 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 keys = get_tv_string_buf(&argvars[1], buf);
15224# ifdef WIN32
15225 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15226# else
15227 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15228 < 0)
15229# endif
15230 {
15231 if (r != NULL)
15232 EMSG(r); /* sending worked but evaluation failed */
15233 else
15234 EMSG2(_("E241: Unable to send to %s"), server_name);
15235 return;
15236 }
15237
15238 rettv->vval.v_string = r;
15239
15240 if (argvars[2].v_type != VAR_UNKNOWN)
15241 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015242 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015243 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015244 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015246 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015247 v.di_tv.v_type = VAR_STRING;
15248 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 idvar = get_tv_string_chk(&argvars[2]);
15250 if (idvar != NULL)
15251 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253 }
15254}
15255#endif
15256
15257/*
15258 * "remote_expr()" function
15259 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260 static void
15261f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015263 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015264{
15265 rettv->v_type = VAR_STRING;
15266 rettv->vval.v_string = NULL;
15267#ifdef FEAT_CLIENTSERVER
15268 remote_common(argvars, rettv, TRUE);
15269#endif
15270}
15271
15272/*
15273 * "remote_foreground()" function
15274 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015275 static void
15276f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015277 typval_T *argvars UNUSED;
15278 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280#ifdef FEAT_CLIENTSERVER
15281# ifdef WIN32
15282 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015283 {
15284 char_u *server_name = get_tv_string_chk(&argvars[0]);
15285
15286 if (server_name != NULL)
15287 serverForeground(server_name);
15288 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015289# else
15290 /* Send a foreground() expression to the server. */
15291 argvars[1].v_type = VAR_STRING;
15292 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15293 argvars[2].v_type = VAR_UNKNOWN;
15294 remote_common(argvars, rettv, TRUE);
15295 vim_free(argvars[1].vval.v_string);
15296# endif
15297#endif
15298}
15299
Bram Moolenaar0d660222005-01-07 21:51:51 +000015300 static void
15301f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015302 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015303 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015304{
15305#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307 char_u *s = NULL;
15308# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015309 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015310# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015312
15313 if (check_restricted() || check_secure())
15314 {
15315 rettv->vval.v_number = -1;
15316 return;
15317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015318 serverid = get_tv_string_chk(&argvars[0]);
15319 if (serverid == NULL)
15320 {
15321 rettv->vval.v_number = -1;
15322 return; /* type error; errmsg already given */
15323 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015325 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015326 if (n == 0)
15327 rettv->vval.v_number = -1;
15328 else
15329 {
15330 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15331 rettv->vval.v_number = (s != NULL);
15332 }
15333# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015334 if (check_connection() == FAIL)
15335 return;
15336
15337 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015338 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015339# endif
15340
15341 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015343 char_u *retvar;
15344
Bram Moolenaar33570922005-01-25 22:26:29 +000015345 v.di_tv.v_type = VAR_STRING;
15346 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015347 retvar = get_tv_string_chk(&argvars[1]);
15348 if (retvar != NULL)
15349 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015351 }
15352#else
15353 rettv->vval.v_number = -1;
15354#endif
15355}
15356
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357 static void
15358f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015359 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015360 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015361{
15362 char_u *r = NULL;
15363
15364#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015365 char_u *serverid = get_tv_string_chk(&argvars[0]);
15366
15367 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368 {
15369# ifdef WIN32
15370 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015371 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015372
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015373 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015374 if (n != 0)
15375 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15376 if (r == NULL)
15377# else
15378 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015379 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015380# endif
15381 EMSG(_("E277: Unable to read a server reply"));
15382 }
15383#endif
15384 rettv->v_type = VAR_STRING;
15385 rettv->vval.v_string = r;
15386}
15387
15388/*
15389 * "remote_send()" function
15390 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015391 static void
15392f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015393 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015394 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015395{
15396 rettv->v_type = VAR_STRING;
15397 rettv->vval.v_string = NULL;
15398#ifdef FEAT_CLIENTSERVER
15399 remote_common(argvars, rettv, FALSE);
15400#endif
15401}
15402
15403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015404 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015405 */
15406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015408 typval_T *argvars;
15409 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015410{
Bram Moolenaar33570922005-01-25 22:26:29 +000015411 list_T *l;
15412 listitem_T *item, *item2;
15413 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015414 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015415 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015416 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015417 dict_T *d;
15418 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015419 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015420
Bram Moolenaar8c711452005-01-14 21:53:12 +000015421 if (argvars[0].v_type == VAR_DICT)
15422 {
15423 if (argvars[2].v_type != VAR_UNKNOWN)
15424 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015425 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015426 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 key = get_tv_string_chk(&argvars[1]);
15429 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015431 di = dict_find(d, key, -1);
15432 if (di == NULL)
15433 EMSG2(_(e_dictkey), key);
15434 else
15435 {
15436 *rettv = di->di_tv;
15437 init_tv(&di->di_tv);
15438 dictitem_remove(d, di);
15439 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015441 }
15442 }
15443 else if (argvars[0].v_type != VAR_LIST)
15444 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015445 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015446 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015448 int error = FALSE;
15449
15450 idx = get_tv_number_chk(&argvars[1], &error);
15451 if (error)
15452 ; /* type error: do nothing, errmsg already given */
15453 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015454 EMSGN(_(e_listidx), idx);
15455 else
15456 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015457 if (argvars[2].v_type == VAR_UNKNOWN)
15458 {
15459 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015460 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015461 *rettv = item->li_tv;
15462 vim_free(item);
15463 }
15464 else
15465 {
15466 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015467 end = get_tv_number_chk(&argvars[2], &error);
15468 if (error)
15469 ; /* type error: do nothing */
15470 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015471 EMSGN(_(e_listidx), end);
15472 else
15473 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015474 int cnt = 0;
15475
15476 for (li = item; li != NULL; li = li->li_next)
15477 {
15478 ++cnt;
15479 if (li == item2)
15480 break;
15481 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015482 if (li == NULL) /* didn't find "item2" after "item" */
15483 EMSG(_(e_invrange));
15484 else
15485 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015486 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015487 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015488 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015489 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015490 l->lv_first = item;
15491 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015492 item->li_prev = NULL;
15493 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015494 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015495 }
15496 }
15497 }
15498 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015499 }
15500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501}
15502
15503/*
15504 * "rename({from}, {to})" function
15505 */
15506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *argvars;
15509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510{
15511 char_u buf[NUMBUFLEN];
15512
15513 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15517 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518}
15519
15520/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015521 * "repeat()" function
15522 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015523 static void
15524f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015525 typval_T *argvars;
15526 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015527{
15528 char_u *p;
15529 int n;
15530 int slen;
15531 int len;
15532 char_u *r;
15533 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015534
15535 n = get_tv_number(&argvars[1]);
15536 if (argvars[0].v_type == VAR_LIST)
15537 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015538 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015539 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015540 if (list_extend(rettv->vval.v_list,
15541 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015542 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015543 }
15544 else
15545 {
15546 p = get_tv_string(&argvars[0]);
15547 rettv->v_type = VAR_STRING;
15548 rettv->vval.v_string = NULL;
15549
15550 slen = (int)STRLEN(p);
15551 len = slen * n;
15552 if (len <= 0)
15553 return;
15554
15555 r = alloc(len + 1);
15556 if (r != NULL)
15557 {
15558 for (i = 0; i < n; i++)
15559 mch_memmove(r + i * slen, p, (size_t)slen);
15560 r[len] = NUL;
15561 }
15562
15563 rettv->vval.v_string = r;
15564 }
15565}
15566
15567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 * "resolve()" function
15569 */
15570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015572 typval_T *argvars;
15573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574{
15575 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015576#ifdef HAVE_READLINK
15577 char_u *buf = NULL;
15578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581#ifdef FEAT_SHORTCUT
15582 {
15583 char_u *v = NULL;
15584
15585 v = mch_resolve_shortcut(p);
15586 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015587 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015589 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 }
15591#else
15592# ifdef HAVE_READLINK
15593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 char_u *cpy;
15595 int len;
15596 char_u *remain = NULL;
15597 char_u *q;
15598 int is_relative_to_current = FALSE;
15599 int has_trailing_pathsep = FALSE;
15600 int limit = 100;
15601
15602 p = vim_strsave(p);
15603
15604 if (p[0] == '.' && (vim_ispathsep(p[1])
15605 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15606 is_relative_to_current = TRUE;
15607
15608 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015609 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015612 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614
15615 q = getnextcomp(p);
15616 if (*q != NUL)
15617 {
15618 /* Separate the first path component in "p", and keep the
15619 * remainder (beginning with the path separator). */
15620 remain = vim_strsave(q - 1);
15621 q[-1] = NUL;
15622 }
15623
Bram Moolenaard9462e32011-04-11 21:35:11 +020015624 buf = alloc(MAXPATHL + 1);
15625 if (buf == NULL)
15626 goto fail;
15627
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 for (;;)
15629 {
15630 for (;;)
15631 {
15632 len = readlink((char *)p, (char *)buf, MAXPATHL);
15633 if (len <= 0)
15634 break;
15635 buf[len] = NUL;
15636
15637 if (limit-- == 0)
15638 {
15639 vim_free(p);
15640 vim_free(remain);
15641 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 goto fail;
15644 }
15645
15646 /* Ensure that the result will have a trailing path separator
15647 * if the argument has one. */
15648 if (remain == NULL && has_trailing_pathsep)
15649 add_pathsep(buf);
15650
15651 /* Separate the first path component in the link value and
15652 * concatenate the remainders. */
15653 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15654 if (*q != NUL)
15655 {
15656 if (remain == NULL)
15657 remain = vim_strsave(q - 1);
15658 else
15659 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015660 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 if (cpy != NULL)
15662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 vim_free(remain);
15664 remain = cpy;
15665 }
15666 }
15667 q[-1] = NUL;
15668 }
15669
15670 q = gettail(p);
15671 if (q > p && *q == NUL)
15672 {
15673 /* Ignore trailing path separator. */
15674 q[-1] = NUL;
15675 q = gettail(p);
15676 }
15677 if (q > p && !mch_isFullName(buf))
15678 {
15679 /* symlink is relative to directory of argument */
15680 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15681 if (cpy != NULL)
15682 {
15683 STRCPY(cpy, p);
15684 STRCPY(gettail(cpy), buf);
15685 vim_free(p);
15686 p = cpy;
15687 }
15688 }
15689 else
15690 {
15691 vim_free(p);
15692 p = vim_strsave(buf);
15693 }
15694 }
15695
15696 if (remain == NULL)
15697 break;
15698
15699 /* Append the first path component of "remain" to "p". */
15700 q = getnextcomp(remain + 1);
15701 len = q - remain - (*q != NUL);
15702 cpy = vim_strnsave(p, STRLEN(p) + len);
15703 if (cpy != NULL)
15704 {
15705 STRNCAT(cpy, remain, len);
15706 vim_free(p);
15707 p = cpy;
15708 }
15709 /* Shorten "remain". */
15710 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015711 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 else
15713 {
15714 vim_free(remain);
15715 remain = NULL;
15716 }
15717 }
15718
15719 /* If the result is a relative path name, make it explicitly relative to
15720 * the current directory if and only if the argument had this form. */
15721 if (!vim_ispathsep(*p))
15722 {
15723 if (is_relative_to_current
15724 && *p != NUL
15725 && !(p[0] == '.'
15726 && (p[1] == NUL
15727 || vim_ispathsep(p[1])
15728 || (p[1] == '.'
15729 && (p[2] == NUL
15730 || vim_ispathsep(p[2]))))))
15731 {
15732 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015733 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 if (cpy != NULL)
15735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 vim_free(p);
15737 p = cpy;
15738 }
15739 }
15740 else if (!is_relative_to_current)
15741 {
15742 /* Strip leading "./". */
15743 q = p;
15744 while (q[0] == '.' && vim_ispathsep(q[1]))
15745 q += 2;
15746 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015747 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 }
15749 }
15750
15751 /* Ensure that the result will have no trailing path separator
15752 * if the argument had none. But keep "/" or "//". */
15753 if (!has_trailing_pathsep)
15754 {
15755 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015756 if (after_pathsep(p, q))
15757 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 }
15759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 }
15762# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015763 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764# endif
15765#endif
15766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015767 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768
15769#ifdef HAVE_READLINK
15770fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015771 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774}
15775
15776/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 */
15779 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015780f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 typval_T *argvars;
15782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783{
Bram Moolenaar33570922005-01-25 22:26:29 +000015784 list_T *l;
15785 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaar0d660222005-01-07 21:51:51 +000015787 if (argvars[0].v_type != VAR_LIST)
15788 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015789 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015790 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015791 {
15792 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015793 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015794 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015795 while (li != NULL)
15796 {
15797 ni = li->li_prev;
15798 list_append(l, li);
15799 li = ni;
15800 }
15801 rettv->vval.v_list = l;
15802 rettv->v_type = VAR_LIST;
15803 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015804 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806}
15807
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015808#define SP_NOMOVE 0x01 /* don't move cursor */
15809#define SP_REPEAT 0x02 /* repeat to find outer pair */
15810#define SP_RETCOUNT 0x04 /* return matchcount */
15811#define SP_SETPCMARK 0x08 /* set previous context mark */
15812#define SP_START 0x10 /* accept match at start position */
15813#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15814#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015815
Bram Moolenaar33570922005-01-25 22:26:29 +000015816static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817
15818/*
15819 * Get flags for a search function.
15820 * Possibly sets "p_ws".
15821 * Returns BACKWARD, FORWARD or zero (for an error).
15822 */
15823 static int
15824get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015826 int *flagsp;
15827{
15828 int dir = FORWARD;
15829 char_u *flags;
15830 char_u nbuf[NUMBUFLEN];
15831 int mask;
15832
15833 if (varp->v_type != VAR_UNKNOWN)
15834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 flags = get_tv_string_buf_chk(varp, nbuf);
15836 if (flags == NULL)
15837 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015838 while (*flags != NUL)
15839 {
15840 switch (*flags)
15841 {
15842 case 'b': dir = BACKWARD; break;
15843 case 'w': p_ws = TRUE; break;
15844 case 'W': p_ws = FALSE; break;
15845 default: mask = 0;
15846 if (flagsp != NULL)
15847 switch (*flags)
15848 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015849 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015850 case 'e': mask = SP_END; break;
15851 case 'm': mask = SP_RETCOUNT; break;
15852 case 'n': mask = SP_NOMOVE; break;
15853 case 'p': mask = SP_SUBPAT; break;
15854 case 'r': mask = SP_REPEAT; break;
15855 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015856 }
15857 if (mask == 0)
15858 {
15859 EMSG2(_(e_invarg2), flags);
15860 dir = 0;
15861 }
15862 else
15863 *flagsp |= mask;
15864 }
15865 if (dir == 0)
15866 break;
15867 ++flags;
15868 }
15869 }
15870 return dir;
15871}
15872
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015874 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015875 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015876 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015877search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015879 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015880 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015882 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 char_u *pat;
15884 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015885 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 int save_p_ws = p_ws;
15887 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015888 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015889 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015890 proftime_T tm;
15891#ifdef FEAT_RELTIME
15892 long time_limit = 0;
15893#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015894 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015895 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015897 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015898 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015899 if (dir == 0)
15900 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015901 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015902 if (flags & SP_START)
15903 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015904 if (flags & SP_END)
15905 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015906
Bram Moolenaar76929292008-01-06 19:07:36 +000015907 /* Optional arguments: line number to stop searching and timeout. */
15908 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015909 {
15910 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15911 if (lnum_stop < 0)
15912 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015913#ifdef FEAT_RELTIME
15914 if (argvars[3].v_type != VAR_UNKNOWN)
15915 {
15916 time_limit = get_tv_number_chk(&argvars[3], NULL);
15917 if (time_limit < 0)
15918 goto theend;
15919 }
15920#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015921 }
15922
Bram Moolenaar76929292008-01-06 19:07:36 +000015923#ifdef FEAT_RELTIME
15924 /* Set the time limit, if there is one. */
15925 profile_setlimit(time_limit, &tm);
15926#endif
15927
Bram Moolenaar231334e2005-07-25 20:46:57 +000015928 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015929 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015930 * Check to make sure only those flags are set.
15931 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15932 * flags cannot be set. Check for that condition also.
15933 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015934 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015935 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015938 goto theend;
15939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015941 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015942 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015943 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015944 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015946 if (flags & SP_SUBPAT)
15947 retval = subpatnum;
15948 else
15949 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015950 if (flags & SP_SETPCMARK)
15951 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015953 if (match_pos != NULL)
15954 {
15955 /* Store the match cursor position */
15956 match_pos->lnum = pos.lnum;
15957 match_pos->col = pos.col + 1;
15958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015959 /* "/$" will put the cursor after the end of the line, may need to
15960 * correct that here */
15961 check_cursor();
15962 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015963
15964 /* If 'n' flag is used: restore cursor position. */
15965 if (flags & SP_NOMOVE)
15966 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015967 else
15968 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015969theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015971
15972 return retval;
15973}
15974
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015975#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015976
15977/*
15978 * round() is not in C90, use ceil() or floor() instead.
15979 */
15980 float_T
15981vim_round(f)
15982 float_T f;
15983{
15984 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15985}
15986
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015987/*
15988 * "round({float})" function
15989 */
15990 static void
15991f_round(argvars, rettv)
15992 typval_T *argvars;
15993 typval_T *rettv;
15994{
15995 float_T f;
15996
15997 rettv->v_type = VAR_FLOAT;
15998 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015999 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016000 else
16001 rettv->vval.v_float = 0.0;
16002}
16003#endif
16004
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016005/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016006 * "screenattr()" function
16007 */
16008 static void
16009f_screenattr(argvars, rettv)
16010 typval_T *argvars UNUSED;
16011 typval_T *rettv;
16012{
16013 int row;
16014 int col;
16015 int c;
16016
16017 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16018 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16019 if (row < 0 || row >= screen_Rows
16020 || col < 0 || col >= screen_Columns)
16021 c = -1;
16022 else
16023 c = ScreenAttrs[LineOffset[row] + col];
16024 rettv->vval.v_number = c;
16025}
16026
16027/*
16028 * "screenchar()" function
16029 */
16030 static void
16031f_screenchar(argvars, rettv)
16032 typval_T *argvars UNUSED;
16033 typval_T *rettv;
16034{
16035 int row;
16036 int col;
16037 int off;
16038 int c;
16039
16040 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16041 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16042 if (row < 0 || row >= screen_Rows
16043 || col < 0 || col >= screen_Columns)
16044 c = -1;
16045 else
16046 {
16047 off = LineOffset[row] + col;
16048#ifdef FEAT_MBYTE
16049 if (enc_utf8 && ScreenLinesUC[off] != 0)
16050 c = ScreenLinesUC[off];
16051 else
16052#endif
16053 c = ScreenLines[off];
16054 }
16055 rettv->vval.v_number = c;
16056}
16057
16058/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016059 * "screencol()" function
16060 *
16061 * First column is 1 to be consistent with virtcol().
16062 */
16063 static void
16064f_screencol(argvars, rettv)
16065 typval_T *argvars UNUSED;
16066 typval_T *rettv;
16067{
16068 rettv->vval.v_number = screen_screencol() + 1;
16069}
16070
16071/*
16072 * "screenrow()" function
16073 */
16074 static void
16075f_screenrow(argvars, rettv)
16076 typval_T *argvars UNUSED;
16077 typval_T *rettv;
16078{
16079 rettv->vval.v_number = screen_screenrow() + 1;
16080}
16081
16082/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016083 * "search()" function
16084 */
16085 static void
16086f_search(argvars, rettv)
16087 typval_T *argvars;
16088 typval_T *rettv;
16089{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016090 int flags = 0;
16091
16092 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093}
16094
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016096 * "searchdecl()" function
16097 */
16098 static void
16099f_searchdecl(argvars, rettv)
16100 typval_T *argvars;
16101 typval_T *rettv;
16102{
16103 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016104 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016105 int error = FALSE;
16106 char_u *name;
16107
16108 rettv->vval.v_number = 1; /* default: FAIL */
16109
16110 name = get_tv_string_chk(&argvars[0]);
16111 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016112 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016113 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016114 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16115 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16116 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016117 if (!error && name != NULL)
16118 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016119 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016120}
16121
16122/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016123 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016125 static int
16126searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016127 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016128 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129{
16130 char_u *spat, *mpat, *epat;
16131 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 int dir;
16134 int flags = 0;
16135 char_u nbuf1[NUMBUFLEN];
16136 char_u nbuf2[NUMBUFLEN];
16137 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016138 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016139 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016140 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016143 spat = get_tv_string_chk(&argvars[0]);
16144 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16145 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16146 if (spat == NULL || mpat == NULL || epat == NULL)
16147 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 /* Handle the optional fourth argument: flags */
16150 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016151 if (dir == 0)
16152 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016153
16154 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016155 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16156 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016157 if ((flags & (SP_END | SP_SUBPAT)) != 0
16158 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016159 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016160 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016161 goto theend;
16162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016164 /* Using 'r' implies 'W', otherwise it doesn't work. */
16165 if (flags & SP_REPEAT)
16166 p_ws = FALSE;
16167
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016168 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016169 if (argvars[3].v_type == VAR_UNKNOWN
16170 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 skip = (char_u *)"";
16172 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016174 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016175 if (argvars[5].v_type != VAR_UNKNOWN)
16176 {
16177 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16178 if (lnum_stop < 0)
16179 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016180#ifdef FEAT_RELTIME
16181 if (argvars[6].v_type != VAR_UNKNOWN)
16182 {
16183 time_limit = get_tv_number_chk(&argvars[6], NULL);
16184 if (time_limit < 0)
16185 goto theend;
16186 }
16187#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016188 }
16189 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016190 if (skip == NULL)
16191 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016193 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016194 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016195
16196theend:
16197 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016198
16199 return retval;
16200}
16201
16202/*
16203 * "searchpair()" function
16204 */
16205 static void
16206f_searchpair(argvars, rettv)
16207 typval_T *argvars;
16208 typval_T *rettv;
16209{
16210 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16211}
16212
16213/*
16214 * "searchpairpos()" function
16215 */
16216 static void
16217f_searchpairpos(argvars, rettv)
16218 typval_T *argvars;
16219 typval_T *rettv;
16220{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016221 pos_T match_pos;
16222 int lnum = 0;
16223 int col = 0;
16224
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016225 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016226 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016227
16228 if (searchpair_cmn(argvars, &match_pos) > 0)
16229 {
16230 lnum = match_pos.lnum;
16231 col = match_pos.col;
16232 }
16233
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016234 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16235 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016236}
16237
16238/*
16239 * Search for a start/middle/end thing.
16240 * Used by searchpair(), see its documentation for the details.
16241 * Returns 0 or -1 for no match,
16242 */
16243 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016244do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16245 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016246 char_u *spat; /* start pattern */
16247 char_u *mpat; /* middle pattern */
16248 char_u *epat; /* end pattern */
16249 int dir; /* BACKWARD or FORWARD */
16250 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016251 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016252 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016253 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016254 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016255{
16256 char_u *save_cpo;
16257 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16258 long retval = 0;
16259 pos_T pos;
16260 pos_T firstpos;
16261 pos_T foundpos;
16262 pos_T save_cursor;
16263 pos_T save_pos;
16264 int n;
16265 int r;
16266 int nest = 1;
16267 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016268 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016269 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016270
16271 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16272 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016273 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016274
Bram Moolenaar76929292008-01-06 19:07:36 +000016275#ifdef FEAT_RELTIME
16276 /* Set the time limit, if there is one. */
16277 profile_setlimit(time_limit, &tm);
16278#endif
16279
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016280 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16281 * start/middle/end (pat3, for the top pair). */
16282 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16283 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16284 if (pat2 == NULL || pat3 == NULL)
16285 goto theend;
16286 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16287 if (*mpat == NUL)
16288 STRCPY(pat3, pat2);
16289 else
16290 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16291 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016292 if (flags & SP_START)
16293 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016294
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 save_cursor = curwin->w_cursor;
16296 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016297 clearpos(&firstpos);
16298 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 pat = pat3;
16300 for (;;)
16301 {
16302 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016303 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16305 /* didn't find it or found the first match again: FAIL */
16306 break;
16307
16308 if (firstpos.lnum == 0)
16309 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016310 if (equalpos(pos, foundpos))
16311 {
16312 /* Found the same position again. Can happen with a pattern that
16313 * has "\zs" at the end and searching backwards. Advance one
16314 * character and try again. */
16315 if (dir == BACKWARD)
16316 decl(&pos);
16317 else
16318 incl(&pos);
16319 }
16320 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016322 /* clear the start flag to avoid getting stuck here */
16323 options &= ~SEARCH_START;
16324
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 /* If the skip pattern matches, ignore this match. */
16326 if (*skip != NUL)
16327 {
16328 save_pos = curwin->w_cursor;
16329 curwin->w_cursor = pos;
16330 r = eval_to_bool(skip, &err, NULL, FALSE);
16331 curwin->w_cursor = save_pos;
16332 if (err)
16333 {
16334 /* Evaluating {skip} caused an error, break here. */
16335 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016336 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 break;
16338 }
16339 if (r)
16340 continue;
16341 }
16342
16343 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16344 {
16345 /* Found end when searching backwards or start when searching
16346 * forward: nested pair. */
16347 ++nest;
16348 pat = pat2; /* nested, don't search for middle */
16349 }
16350 else
16351 {
16352 /* Found end when searching forward or start when searching
16353 * backward: end of (nested) pair; or found middle in outer pair. */
16354 if (--nest == 1)
16355 pat = pat3; /* outer level, search for middle */
16356 }
16357
16358 if (nest == 0)
16359 {
16360 /* Found the match: return matchcount or line number. */
16361 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016362 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016364 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016365 if (flags & SP_SETPCMARK)
16366 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 curwin->w_cursor = pos;
16368 if (!(flags & SP_REPEAT))
16369 break;
16370 nest = 1; /* search for next unmatched */
16371 }
16372 }
16373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016374 if (match_pos != NULL)
16375 {
16376 /* Store the match cursor position */
16377 match_pos->lnum = curwin->w_cursor.lnum;
16378 match_pos->col = curwin->w_cursor.col + 1;
16379 }
16380
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016382 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 curwin->w_cursor = save_cursor;
16384
16385theend:
16386 vim_free(pat2);
16387 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016388 if (p_cpo == empty_option)
16389 p_cpo = save_cpo;
16390 else
16391 /* Darn, evaluating the {skip} expression changed the value. */
16392 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016393
16394 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395}
16396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016397/*
16398 * "searchpos()" function
16399 */
16400 static void
16401f_searchpos(argvars, rettv)
16402 typval_T *argvars;
16403 typval_T *rettv;
16404{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016405 pos_T match_pos;
16406 int lnum = 0;
16407 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016408 int n;
16409 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016411 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016412 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016413
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016414 n = search_cmn(argvars, &match_pos, &flags);
16415 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016416 {
16417 lnum = match_pos.lnum;
16418 col = match_pos.col;
16419 }
16420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016421 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16422 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016423 if (flags & SP_SUBPAT)
16424 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016425}
16426
16427
Bram Moolenaar0d660222005-01-07 21:51:51 +000016428 static void
16429f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016430 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433#ifdef FEAT_CLIENTSERVER
16434 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 char_u *server = get_tv_string_chk(&argvars[0]);
16436 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 if (server == NULL || reply == NULL)
16440 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441 if (check_restricted() || check_secure())
16442 return;
16443# ifdef FEAT_X11
16444 if (check_connection() == FAIL)
16445 return;
16446# endif
16447
16448 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016449 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016450 EMSG(_("E258: Unable to send to client"));
16451 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016452 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016453 rettv->vval.v_number = 0;
16454#else
16455 rettv->vval.v_number = -1;
16456#endif
16457}
16458
Bram Moolenaar0d660222005-01-07 21:51:51 +000016459 static void
16460f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016461 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016463{
16464 char_u *r = NULL;
16465
16466#ifdef FEAT_CLIENTSERVER
16467# ifdef WIN32
16468 r = serverGetVimNames();
16469# else
16470 make_connection();
16471 if (X_DISPLAY != NULL)
16472 r = serverGetVimNames(X_DISPLAY);
16473# endif
16474#endif
16475 rettv->v_type = VAR_STRING;
16476 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016477}
16478
16479/*
16480 * "setbufvar()" function
16481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016483f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016484 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016485 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016486{
16487 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 char_u nbuf[NUMBUFLEN];
16492
16493 if (check_restricted() || check_secure())
16494 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016495 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16496 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016497 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498 varp = &argvars[2];
16499
16500 if (buf != NULL && varname != NULL && varp != NULL)
16501 {
16502 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504
16505 if (*varname == '&')
16506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 long numval;
16508 char_u *strval;
16509 int error = FALSE;
16510
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016512 numval = get_tv_number_chk(varp, &error);
16513 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016514 if (!error && strval != NULL)
16515 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016516 }
16517 else
16518 {
16519 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16520 if (bufvarname != NULL)
16521 {
16522 STRCPY(bufvarname, "b:");
16523 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016524 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 vim_free(bufvarname);
16526 }
16527 }
16528
16529 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532}
16533
16534/*
16535 * "setcmdpos()" function
16536 */
16537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016538f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016539 typval_T *argvars;
16540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016542 int pos = (int)get_tv_number(&argvars[0]) - 1;
16543
16544 if (pos >= 0)
16545 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546}
16547
16548/*
16549 * "setline()" function
16550 */
16551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016552f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016553 typval_T *argvars;
16554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555{
16556 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016557 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016558 list_T *l = NULL;
16559 listitem_T *li = NULL;
16560 long added = 0;
16561 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016563 lnum = get_tv_lnum(&argvars[0]);
16564 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016565 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016566 l = argvars[1].vval.v_list;
16567 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016569 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016571
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016572 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016573 for (;;)
16574 {
16575 if (l != NULL)
16576 {
16577 /* list argument, get next string */
16578 if (li == NULL)
16579 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016580 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016581 li = li->li_next;
16582 }
16583
16584 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016586 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016587
16588 /* When coming here from Insert mode, sync undo, so that this can be
16589 * undone separately from what was previously inserted. */
16590 if (u_sync_once == 2)
16591 {
16592 u_sync_once = 1; /* notify that u_sync() was called */
16593 u_sync(TRUE);
16594 }
16595
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016596 if (lnum <= curbuf->b_ml.ml_line_count)
16597 {
16598 /* existing line, replace it */
16599 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16600 {
16601 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016602 if (lnum == curwin->w_cursor.lnum)
16603 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016604 rettv->vval.v_number = 0; /* OK */
16605 }
16606 }
16607 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16608 {
16609 /* lnum is one past the last line, append the line */
16610 ++added;
16611 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16612 rettv->vval.v_number = 0; /* OK */
16613 }
16614
16615 if (l == NULL) /* only one string argument */
16616 break;
16617 ++lnum;
16618 }
16619
16620 if (added > 0)
16621 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016622}
16623
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016624static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16625
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016627 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016628 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016629 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016630set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016631 win_T *wp UNUSED;
16632 typval_T *list_arg UNUSED;
16633 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016634 typval_T *rettv;
16635{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016636#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016637 char_u *act;
16638 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016639#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016640
Bram Moolenaar2641f772005-03-25 21:58:17 +000016641 rettv->vval.v_number = -1;
16642
16643#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016644 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016645 EMSG(_(e_listreq));
16646 else
16647 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016648 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016649
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016650 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016651 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016652 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016653 if (act == NULL)
16654 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016655 if (*act == 'a' || *act == 'r')
16656 action = *act;
16657 }
16658
Bram Moolenaar81484f42012-12-05 15:16:47 +010016659 if (l != NULL && set_errorlist(wp, l, action,
16660 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016661 rettv->vval.v_number = 0;
16662 }
16663#endif
16664}
16665
16666/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016667 * "setloclist()" function
16668 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016669 static void
16670f_setloclist(argvars, rettv)
16671 typval_T *argvars;
16672 typval_T *rettv;
16673{
16674 win_T *win;
16675
16676 rettv->vval.v_number = -1;
16677
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016678 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016679 if (win != NULL)
16680 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16681}
16682
16683/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016684 * "setmatches()" function
16685 */
16686 static void
16687f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016688 typval_T *argvars UNUSED;
16689 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016690{
16691#ifdef FEAT_SEARCH_EXTRA
16692 list_T *l;
16693 listitem_T *li;
16694 dict_T *d;
16695
16696 rettv->vval.v_number = -1;
16697 if (argvars[0].v_type != VAR_LIST)
16698 {
16699 EMSG(_(e_listreq));
16700 return;
16701 }
16702 if ((l = argvars[0].vval.v_list) != NULL)
16703 {
16704
16705 /* To some extent make sure that we are dealing with a list from
16706 * "getmatches()". */
16707 li = l->lv_first;
16708 while (li != NULL)
16709 {
16710 if (li->li_tv.v_type != VAR_DICT
16711 || (d = li->li_tv.vval.v_dict) == NULL)
16712 {
16713 EMSG(_(e_invarg));
16714 return;
16715 }
16716 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16717 && dict_find(d, (char_u *)"pattern", -1) != NULL
16718 && dict_find(d, (char_u *)"priority", -1) != NULL
16719 && dict_find(d, (char_u *)"id", -1) != NULL))
16720 {
16721 EMSG(_(e_invarg));
16722 return;
16723 }
16724 li = li->li_next;
16725 }
16726
16727 clear_matches(curwin);
16728 li = l->lv_first;
16729 while (li != NULL)
16730 {
16731 d = li->li_tv.vval.v_dict;
16732 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16733 get_dict_string(d, (char_u *)"pattern", FALSE),
16734 (int)get_dict_number(d, (char_u *)"priority"),
16735 (int)get_dict_number(d, (char_u *)"id"));
16736 li = li->li_next;
16737 }
16738 rettv->vval.v_number = 0;
16739 }
16740#endif
16741}
16742
16743/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016744 * "setpos()" function
16745 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016746 static void
16747f_setpos(argvars, rettv)
16748 typval_T *argvars;
16749 typval_T *rettv;
16750{
16751 pos_T pos;
16752 int fnum;
16753 char_u *name;
16754
Bram Moolenaar08250432008-02-13 11:42:46 +000016755 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016756 name = get_tv_string_chk(argvars);
16757 if (name != NULL)
16758 {
16759 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16760 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016761 if (--pos.col < 0)
16762 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016763 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016764 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016765 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016766 if (fnum == curbuf->b_fnum)
16767 {
16768 curwin->w_cursor = pos;
16769 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016770 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016771 }
16772 else
16773 EMSG(_(e_invarg));
16774 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016775 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16776 {
16777 /* set mark */
16778 if (setmark_pos(name[1], &pos, fnum) == OK)
16779 rettv->vval.v_number = 0;
16780 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016781 else
16782 EMSG(_(e_invarg));
16783 }
16784 }
16785}
16786
16787/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016788 * "setqflist()" function
16789 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016790 static void
16791f_setqflist(argvars, rettv)
16792 typval_T *argvars;
16793 typval_T *rettv;
16794{
16795 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16796}
16797
16798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 * "setreg()" function
16800 */
16801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016802f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016803 typval_T *argvars;
16804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016805{
16806 int regname;
16807 char_u *strregname;
16808 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016809 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 int append;
16811 char_u yank_type;
16812 long block_len;
16813
16814 block_len = -1;
16815 yank_type = MAUTO;
16816 append = FALSE;
16817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016818 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016819 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016821 if (strregname == NULL)
16822 return; /* type error; errmsg already given */
16823 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 if (regname == 0 || regname == '@')
16825 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016829 stropt = get_tv_string_chk(&argvars[2]);
16830 if (stropt == NULL)
16831 return; /* type error */
16832 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 switch (*stropt)
16834 {
16835 case 'a': case 'A': /* append */
16836 append = TRUE;
16837 break;
16838 case 'v': case 'c': /* character-wise selection */
16839 yank_type = MCHAR;
16840 break;
16841 case 'V': case 'l': /* line-wise selection */
16842 yank_type = MLINE;
16843 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 case 'b': case Ctrl_V: /* block-wise selection */
16845 yank_type = MBLOCK;
16846 if (VIM_ISDIGIT(stropt[1]))
16847 {
16848 ++stropt;
16849 block_len = getdigits(&stropt) - 1;
16850 --stropt;
16851 }
16852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 }
16854 }
16855
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016856 if (argvars[1].v_type == VAR_LIST)
16857 {
16858 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016859 char_u **allocval;
16860 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016861 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016862 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016863 int len = argvars[1].vval.v_list->lv_len;
16864 listitem_T *li;
16865
Bram Moolenaar7d647822014-04-05 21:28:56 +020016866 /* First half: use for pointers to result lines; second half: use for
16867 * pointers to allocated copies. */
16868 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016869 if (lstval == NULL)
16870 return;
16871 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016872 allocval = lstval + len + 2;
16873 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016874
16875 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16876 li = li->li_next)
16877 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016878 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016879 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016880 goto free_lstval;
16881 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016882 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016883 /* Need to make a copy, next get_tv_string_buf_chk() will
16884 * overwrite the string. */
16885 strval = vim_strsave(buf);
16886 if (strval == NULL)
16887 goto free_lstval;
16888 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016889 }
16890 *curval++ = strval;
16891 }
16892 *curval++ = NULL;
16893
16894 write_reg_contents_lst(regname, lstval, -1,
16895 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016896free_lstval:
16897 while (curallocval > allocval)
16898 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016899 vim_free(lstval);
16900 }
16901 else
16902 {
16903 strval = get_tv_string_chk(&argvars[1]);
16904 if (strval == NULL)
16905 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016909 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910}
16911
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016912/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016913 * "settabvar()" function
16914 */
16915 static void
16916f_settabvar(argvars, rettv)
16917 typval_T *argvars;
16918 typval_T *rettv;
16919{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016920#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016921 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016922 tabpage_T *tp;
16923#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016924 char_u *varname, *tabvarname;
16925 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016926
16927 rettv->vval.v_number = 0;
16928
16929 if (check_restricted() || check_secure())
16930 return;
16931
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016932#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016933 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016934#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016935 varname = get_tv_string_chk(&argvars[1]);
16936 varp = &argvars[2];
16937
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016938 if (varname != NULL && varp != NULL
16939#ifdef FEAT_WINDOWS
16940 && tp != NULL
16941#endif
16942 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016943 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016944#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016945 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016946 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016947#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016948
16949 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16950 if (tabvarname != NULL)
16951 {
16952 STRCPY(tabvarname, "t:");
16953 STRCPY(tabvarname + 2, varname);
16954 set_var(tabvarname, varp, TRUE);
16955 vim_free(tabvarname);
16956 }
16957
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016958#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016959 /* Restore current tabpage */
16960 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016961 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016962#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016963 }
16964}
16965
16966/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016967 * "settabwinvar()" function
16968 */
16969 static void
16970f_settabwinvar(argvars, rettv)
16971 typval_T *argvars;
16972 typval_T *rettv;
16973{
16974 setwinvar(argvars, rettv, 1);
16975}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
16977/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016978 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016981f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 typval_T *argvars;
16983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016985 setwinvar(argvars, rettv, 0);
16986}
16987
16988/*
16989 * "setwinvar()" and "settabwinvar()" functions
16990 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016991
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016992 static void
16993setwinvar(argvars, rettv, off)
16994 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016995 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016996 int off;
16997{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 win_T *win;
16999#ifdef FEAT_WINDOWS
17000 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017001 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002#endif
17003 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017006 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007
17008 if (check_restricted() || check_secure())
17009 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017010
17011#ifdef FEAT_WINDOWS
17012 if (off == 1)
17013 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17014 else
17015 tp = curtab;
17016#endif
17017 win = find_win_by_nr(&argvars[off], tp);
17018 varname = get_tv_string_chk(&argvars[off + 1]);
17019 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020
17021 if (win != NULL && varname != NULL && varp != NULL)
17022 {
17023#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017024 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017025 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026#endif
17027
17028 if (*varname == '&')
17029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017030 long numval;
17031 char_u *strval;
17032 int error = FALSE;
17033
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 numval = get_tv_number_chk(varp, &error);
17036 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 if (!error && strval != NULL)
17038 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 }
17040 else
17041 {
17042 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17043 if (winvarname != NULL)
17044 {
17045 STRCPY(winvarname, "w:");
17046 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017047 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 vim_free(winvarname);
17049 }
17050 }
17051
17052#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017053 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054#endif
17055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056}
17057
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017058#ifdef FEAT_CRYPT
17059/*
17060 * "sha256({string})" function
17061 */
17062 static void
17063f_sha256(argvars, rettv)
17064 typval_T *argvars;
17065 typval_T *rettv;
17066{
17067 char_u *p;
17068
17069 p = get_tv_string(&argvars[0]);
17070 rettv->vval.v_string = vim_strsave(
17071 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17072 rettv->v_type = VAR_STRING;
17073}
17074#endif /* FEAT_CRYPT */
17075
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017077 * "shellescape({string})" function
17078 */
17079 static void
17080f_shellescape(argvars, rettv)
17081 typval_T *argvars;
17082 typval_T *rettv;
17083{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017084 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017085 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017086 rettv->v_type = VAR_STRING;
17087}
17088
17089/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017090 * shiftwidth() function
17091 */
17092 static void
17093f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017094 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017095 typval_T *rettv;
17096{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017097 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017098}
17099
17100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 */
17103 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 typval_T *argvars;
17106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 p = get_tv_string(&argvars[0]);
17111 rettv->vval.v_string = vim_strsave(p);
17112 simplify_filename(rettv->vval.v_string); /* simplify in place */
17113 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114}
17115
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017116#ifdef FEAT_FLOAT
17117/*
17118 * "sin()" function
17119 */
17120 static void
17121f_sin(argvars, rettv)
17122 typval_T *argvars;
17123 typval_T *rettv;
17124{
17125 float_T f;
17126
17127 rettv->v_type = VAR_FLOAT;
17128 if (get_float_arg(argvars, &f) == OK)
17129 rettv->vval.v_float = sin(f);
17130 else
17131 rettv->vval.v_float = 0.0;
17132}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017133
17134/*
17135 * "sinh()" function
17136 */
17137 static void
17138f_sinh(argvars, rettv)
17139 typval_T *argvars;
17140 typval_T *rettv;
17141{
17142 float_T f;
17143
17144 rettv->v_type = VAR_FLOAT;
17145 if (get_float_arg(argvars, &f) == OK)
17146 rettv->vval.v_float = sinh(f);
17147 else
17148 rettv->vval.v_float = 0.0;
17149}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017150#endif
17151
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152static int
17153#ifdef __BORLANDC__
17154 _RTLENTRYF
17155#endif
17156 item_compare __ARGS((const void *s1, const void *s2));
17157static int
17158#ifdef __BORLANDC__
17159 _RTLENTRYF
17160#endif
17161 item_compare2 __ARGS((const void *s1, const void *s2));
17162
17163static int item_compare_ic;
17164static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017165static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017167static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168#define ITEM_COMPARE_FAIL 999
17169
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017171 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 static int
17174#ifdef __BORLANDC__
17175_RTLENTRYF
17176#endif
17177item_compare(s1, s2)
17178 const void *s1;
17179 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 char_u *p1, *p2;
17182 char_u *tofree1, *tofree2;
17183 int res;
17184 char_u numbuf1[NUMBUFLEN];
17185 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017187 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17188 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017189 if (p1 == NULL)
17190 p1 = (char_u *)"";
17191 if (p2 == NULL)
17192 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193 if (item_compare_ic)
17194 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196 res = STRCMP(p1, p2);
17197 vim_free(tofree1);
17198 vim_free(tofree2);
17199 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200}
17201
17202 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017203#ifdef __BORLANDC__
17204_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206item_compare2(s1, s2)
17207 const void *s1;
17208 const void *s2;
17209{
17210 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017211 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017212 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017213 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 /* shortcut after failure in previous call; compare all items equal */
17216 if (item_compare_func_err)
17217 return 0;
17218
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017219 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17220 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17222 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223
17224 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017225 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017226 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17227 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017228 clear_tv(&argv[0]);
17229 clear_tv(&argv[1]);
17230
17231 if (res == FAIL)
17232 res = ITEM_COMPARE_FAIL;
17233 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17235 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017236 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237 clear_tv(&rettv);
17238 return res;
17239}
17240
17241/*
17242 * "sort({list})" function
17243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017245do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017246 typval_T *argvars;
17247 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017248 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249{
Bram Moolenaar33570922005-01-25 22:26:29 +000017250 list_T *l;
17251 listitem_T *li;
17252 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 long len;
17254 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017257 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 else
17259 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017261 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017262 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017263 return;
17264 rettv->vval.v_list = l;
17265 rettv->v_type = VAR_LIST;
17266 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267
Bram Moolenaar0d660222005-01-07 21:51:51 +000017268 len = list_len(l);
17269 if (len <= 1)
17270 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271
Bram Moolenaar0d660222005-01-07 21:51:51 +000017272 item_compare_ic = FALSE;
17273 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017274 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017275 if (argvars[1].v_type != VAR_UNKNOWN)
17276 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017277 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 else
17281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017282 int error = FALSE;
17283
17284 i = get_tv_number_chk(&argvars[1], &error);
17285 if (error)
17286 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017287 if (i == 1)
17288 item_compare_ic = TRUE;
17289 else
17290 item_compare_func = get_tv_string(&argvars[1]);
17291 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017292
17293 if (argvars[2].v_type != VAR_UNKNOWN)
17294 {
17295 /* optional third argument: {dict} */
17296 if (argvars[2].v_type != VAR_DICT)
17297 {
17298 EMSG(_(e_dictreq));
17299 return;
17300 }
17301 item_compare_selfdict = argvars[2].vval.v_dict;
17302 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304
Bram Moolenaar0d660222005-01-07 21:51:51 +000017305 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017306 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017307 if (ptrs == NULL)
17308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309
Bram Moolenaar327aa022014-03-25 18:24:23 +010017310 i = 0;
17311 if (sort)
17312 {
17313 /* sort(): ptrs will be the list to sort */
17314 for (li = l->lv_first; li != NULL; li = li->li_next)
17315 ptrs[i++] = li;
17316
17317 item_compare_func_err = FALSE;
17318 /* test the compare function */
17319 if (item_compare_func != NULL
17320 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017322 EMSG(_("E702: Sort compare function failed"));
17323 else
17324 {
17325 /* Sort the array with item pointers. */
17326 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17327 item_compare_func == NULL ? item_compare : item_compare2);
17328
17329 if (!item_compare_func_err)
17330 {
17331 /* Clear the List and append the items in sorted order. */
17332 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17333 l->lv_len = 0;
17334 for (i = 0; i < len; ++i)
17335 list_append(l, ptrs[i]);
17336 }
17337 }
17338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017340 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017341 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17342
17343 /* f_uniq(): ptrs will be a stack of items to remove */
17344 item_compare_func_err = FALSE;
17345 item_compare_func_ptr = item_compare_func
17346 ? item_compare2 : item_compare;
17347
17348 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17349 li = li->li_next)
17350 {
17351 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17352 == 0)
17353 ptrs[i++] = li;
17354 if (item_compare_func_err)
17355 {
17356 EMSG(_("E882: Uniq compare function failed"));
17357 break;
17358 }
17359 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017361 if (!item_compare_func_err)
17362 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017363 while (--i >= 0)
17364 {
17365 li = ptrs[i]->li_next;
17366 ptrs[i]->li_next = li->li_next;
17367 if (li->li_next != NULL)
17368 li->li_next->li_prev = ptrs[i];
17369 else
17370 l->lv_last = ptrs[i];
17371 list_fix_watch(l, li);
17372 listitem_free(li);
17373 l->lv_len--;
17374 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017375 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017376 }
17377
17378 vim_free(ptrs);
17379 }
17380}
17381
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017382/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017383 * "sort({list})" function
17384 */
17385 static void
17386f_sort(argvars, rettv)
17387 typval_T *argvars;
17388 typval_T *rettv;
17389{
17390 do_sort_uniq(argvars, rettv, TRUE);
17391}
17392
17393/*
17394 * "uniq({list})" function
17395 */
17396 static void
17397f_uniq(argvars, rettv)
17398 typval_T *argvars;
17399 typval_T *rettv;
17400{
17401 do_sort_uniq(argvars, rettv, FALSE);
17402}
17403
17404/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017405 * "soundfold({word})" function
17406 */
17407 static void
17408f_soundfold(argvars, rettv)
17409 typval_T *argvars;
17410 typval_T *rettv;
17411{
17412 char_u *s;
17413
17414 rettv->v_type = VAR_STRING;
17415 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017416#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017417 rettv->vval.v_string = eval_soundfold(s);
17418#else
17419 rettv->vval.v_string = vim_strsave(s);
17420#endif
17421}
17422
17423/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017424 * "spellbadword()" function
17425 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017426 static void
17427f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017428 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017429 typval_T *rettv;
17430{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017431 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017432 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017433 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017434
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017435 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017436 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017437
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017438#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017439 if (argvars[0].v_type == VAR_UNKNOWN)
17440 {
17441 /* Find the start and length of the badly spelled word. */
17442 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17443 if (len != 0)
17444 word = ml_get_cursor();
17445 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017446 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017447 {
17448 char_u *str = get_tv_string_chk(&argvars[0]);
17449 int capcol = -1;
17450
17451 if (str != NULL)
17452 {
17453 /* Check the argument for spelling. */
17454 while (*str != NUL)
17455 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017456 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017457 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017458 {
17459 word = str;
17460 break;
17461 }
17462 str += len;
17463 }
17464 }
17465 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017466#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017467
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017468 list_append_string(rettv->vval.v_list, word, len);
17469 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017470 attr == HLF_SPB ? "bad" :
17471 attr == HLF_SPR ? "rare" :
17472 attr == HLF_SPL ? "local" :
17473 attr == HLF_SPC ? "caps" :
17474 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017475}
17476
17477/*
17478 * "spellsuggest()" function
17479 */
17480 static void
17481f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017482 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017483 typval_T *rettv;
17484{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017485#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017486 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017487 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017488 int maxcount;
17489 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017490 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017491 listitem_T *li;
17492 int need_capital = FALSE;
17493#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017494
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017495 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017496 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017497
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017498#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017499 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017500 {
17501 str = get_tv_string(&argvars[0]);
17502 if (argvars[1].v_type != VAR_UNKNOWN)
17503 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017504 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017505 if (maxcount <= 0)
17506 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017507 if (argvars[2].v_type != VAR_UNKNOWN)
17508 {
17509 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17510 if (typeerr)
17511 return;
17512 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017513 }
17514 else
17515 maxcount = 25;
17516
Bram Moolenaar4770d092006-01-12 23:22:24 +000017517 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017518
17519 for (i = 0; i < ga.ga_len; ++i)
17520 {
17521 str = ((char_u **)ga.ga_data)[i];
17522
17523 li = listitem_alloc();
17524 if (li == NULL)
17525 vim_free(str);
17526 else
17527 {
17528 li->li_tv.v_type = VAR_STRING;
17529 li->li_tv.v_lock = 0;
17530 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017531 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017532 }
17533 }
17534 ga_clear(&ga);
17535 }
17536#endif
17537}
17538
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017540f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017541 typval_T *argvars;
17542 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017543{
17544 char_u *str;
17545 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017546 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547 regmatch_T regmatch;
17548 char_u patbuf[NUMBUFLEN];
17549 char_u *save_cpo;
17550 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017552 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017553 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554
17555 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17556 save_cpo = p_cpo;
17557 p_cpo = (char_u *)"";
17558
17559 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017560 if (argvars[1].v_type != VAR_UNKNOWN)
17561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017562 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17563 if (pat == NULL)
17564 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017565 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017566 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017567 }
17568 if (pat == NULL || *pat == NUL)
17569 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017571 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017573 if (typeerr)
17574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17577 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017580 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017581 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017582 if (*str == NUL)
17583 match = FALSE; /* empty item at the end */
17584 else
17585 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586 if (match)
17587 end = regmatch.startp[0];
17588 else
17589 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017590 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17591 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017592 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017593 if (list_append_string(rettv->vval.v_list, str,
17594 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596 }
17597 if (!match)
17598 break;
17599 /* Advance to just after the match. */
17600 if (regmatch.endp[0] > str)
17601 col = 0;
17602 else
17603 {
17604 /* Don't get stuck at the same match. */
17605#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017606 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017607#else
17608 col = 1;
17609#endif
17610 }
17611 str = regmatch.endp[0];
17612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613
Bram Moolenaar473de612013-06-08 18:19:48 +020017614 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618}
17619
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017620#ifdef FEAT_FLOAT
17621/*
17622 * "sqrt()" function
17623 */
17624 static void
17625f_sqrt(argvars, rettv)
17626 typval_T *argvars;
17627 typval_T *rettv;
17628{
17629 float_T f;
17630
17631 rettv->v_type = VAR_FLOAT;
17632 if (get_float_arg(argvars, &f) == OK)
17633 rettv->vval.v_float = sqrt(f);
17634 else
17635 rettv->vval.v_float = 0.0;
17636}
17637
17638/*
17639 * "str2float()" function
17640 */
17641 static void
17642f_str2float(argvars, rettv)
17643 typval_T *argvars;
17644 typval_T *rettv;
17645{
17646 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17647
17648 if (*p == '+')
17649 p = skipwhite(p + 1);
17650 (void)string2float(p, &rettv->vval.v_float);
17651 rettv->v_type = VAR_FLOAT;
17652}
17653#endif
17654
Bram Moolenaar2c932302006-03-18 21:42:09 +000017655/*
17656 * "str2nr()" function
17657 */
17658 static void
17659f_str2nr(argvars, rettv)
17660 typval_T *argvars;
17661 typval_T *rettv;
17662{
17663 int base = 10;
17664 char_u *p;
17665 long n;
17666
17667 if (argvars[1].v_type != VAR_UNKNOWN)
17668 {
17669 base = get_tv_number(&argvars[1]);
17670 if (base != 8 && base != 10 && base != 16)
17671 {
17672 EMSG(_(e_invarg));
17673 return;
17674 }
17675 }
17676
17677 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017678 if (*p == '+')
17679 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017680 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17681 rettv->vval.v_number = n;
17682}
17683
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684#ifdef HAVE_STRFTIME
17685/*
17686 * "strftime({format}[, {time}])" function
17687 */
17688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017689f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017690 typval_T *argvars;
17691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692{
17693 char_u result_buf[256];
17694 struct tm *curtime;
17695 time_t seconds;
17696 char_u *p;
17697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017698 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017701 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 seconds = time(NULL);
17703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 curtime = localtime(&seconds);
17706 /* MSVC returns NULL for an invalid value of seconds. */
17707 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017708 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 else
17710 {
17711# ifdef FEAT_MBYTE
17712 vimconv_T conv;
17713 char_u *enc;
17714
17715 conv.vc_type = CONV_NONE;
17716 enc = enc_locale();
17717 convert_setup(&conv, p_enc, enc);
17718 if (conv.vc_type != CONV_NONE)
17719 p = string_convert(&conv, p, NULL);
17720# endif
17721 if (p != NULL)
17722 (void)strftime((char *)result_buf, sizeof(result_buf),
17723 (char *)p, curtime);
17724 else
17725 result_buf[0] = NUL;
17726
17727# ifdef FEAT_MBYTE
17728 if (conv.vc_type != CONV_NONE)
17729 vim_free(p);
17730 convert_setup(&conv, enc, p_enc);
17731 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017732 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 else
17734# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017735 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736
17737# ifdef FEAT_MBYTE
17738 /* Release conversion descriptors */
17739 convert_setup(&conv, NULL, NULL);
17740 vim_free(enc);
17741# endif
17742 }
17743}
17744#endif
17745
17746/*
17747 * "stridx()" function
17748 */
17749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017751 typval_T *argvars;
17752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753{
17754 char_u buf[NUMBUFLEN];
17755 char_u *needle;
17756 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017757 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017761 needle = get_tv_string_chk(&argvars[1]);
17762 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017764 if (needle == NULL || haystack == NULL)
17765 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 if (argvars[2].v_type != VAR_UNKNOWN)
17768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017769 int error = FALSE;
17770
17771 start_idx = get_tv_number_chk(&argvars[2], &error);
17772 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017773 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017774 if (start_idx >= 0)
17775 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017776 }
17777
17778 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17779 if (pos != NULL)
17780 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781}
17782
17783/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017784 * "string()" function
17785 */
17786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017787f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017788 typval_T *argvars;
17789 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017790{
17791 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017792 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017794 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017795 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017796 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017797 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017798 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799}
17800
17801/*
17802 * "strlen()" function
17803 */
17804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017806 typval_T *argvars;
17807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017809 rettv->vval.v_number = (varnumber_T)(STRLEN(
17810 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811}
17812
17813/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017814 * "strchars()" function
17815 */
17816 static void
17817f_strchars(argvars, rettv)
17818 typval_T *argvars;
17819 typval_T *rettv;
17820{
17821 char_u *s = get_tv_string(&argvars[0]);
17822#ifdef FEAT_MBYTE
17823 varnumber_T len = 0;
17824
17825 while (*s != NUL)
17826 {
17827 mb_cptr2char_adv(&s);
17828 ++len;
17829 }
17830 rettv->vval.v_number = len;
17831#else
17832 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17833#endif
17834}
17835
17836/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017837 * "strdisplaywidth()" function
17838 */
17839 static void
17840f_strdisplaywidth(argvars, rettv)
17841 typval_T *argvars;
17842 typval_T *rettv;
17843{
17844 char_u *s = get_tv_string(&argvars[0]);
17845 int col = 0;
17846
17847 if (argvars[1].v_type != VAR_UNKNOWN)
17848 col = get_tv_number(&argvars[1]);
17849
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017850 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017851}
17852
17853/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017854 * "strwidth()" function
17855 */
17856 static void
17857f_strwidth(argvars, rettv)
17858 typval_T *argvars;
17859 typval_T *rettv;
17860{
17861 char_u *s = get_tv_string(&argvars[0]);
17862
17863 rettv->vval.v_number = (varnumber_T)(
17864#ifdef FEAT_MBYTE
17865 mb_string2cells(s, -1)
17866#else
17867 STRLEN(s)
17868#endif
17869 );
17870}
17871
17872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 * "strpart()" function
17874 */
17875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017876f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017877 typval_T *argvars;
17878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879{
17880 char_u *p;
17881 int n;
17882 int len;
17883 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017884 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017886 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017887 slen = (int)STRLEN(p);
17888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017889 n = get_tv_number_chk(&argvars[1], &error);
17890 if (error)
17891 len = 0;
17892 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017894 else
17895 len = slen - n; /* default len: all bytes that are available. */
17896
17897 /*
17898 * Only return the overlap between the specified part and the actual
17899 * string.
17900 */
17901 if (n < 0)
17902 {
17903 len += n;
17904 n = 0;
17905 }
17906 else if (n > slen)
17907 n = slen;
17908 if (len < 0)
17909 len = 0;
17910 else if (n + len > slen)
17911 len = slen - n;
17912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017913 rettv->v_type = VAR_STRING;
17914 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915}
17916
17917/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017918 * "strridx()" function
17919 */
17920 static void
17921f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 typval_T *argvars;
17923 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017924{
17925 char_u buf[NUMBUFLEN];
17926 char_u *needle;
17927 char_u *haystack;
17928 char_u *rest;
17929 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017930 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017932 needle = get_tv_string_chk(&argvars[1]);
17933 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934
17935 rettv->vval.v_number = -1;
17936 if (needle == NULL || haystack == NULL)
17937 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017938
17939 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017940 if (argvars[2].v_type != VAR_UNKNOWN)
17941 {
17942 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017943 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017944 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017945 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017946 }
17947 else
17948 end_idx = haystack_len;
17949
Bram Moolenaar0d660222005-01-07 21:51:51 +000017950 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017951 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017952 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017953 lastmatch = haystack + end_idx;
17954 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017955 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017956 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017957 for (rest = haystack; *rest != '\0'; ++rest)
17958 {
17959 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017960 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017961 break;
17962 lastmatch = rest;
17963 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017964 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017965
17966 if (lastmatch == NULL)
17967 rettv->vval.v_number = -1;
17968 else
17969 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17970}
17971
17972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 * "strtrans()" function
17974 */
17975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017977 typval_T *argvars;
17978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017980 rettv->v_type = VAR_STRING;
17981 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982}
17983
17984/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017985 * "submatch()" function
17986 */
17987 static void
17988f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017989 typval_T *argvars;
17990 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017991{
Bram Moolenaar41571762014-04-02 19:00:58 +020017992 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020017993 int no;
17994 int retList = 0;
17995
17996 no = (int)get_tv_number_chk(&argvars[0], &error);
17997 if (error)
17998 return;
17999 error = FALSE;
18000 if (argvars[1].v_type != VAR_UNKNOWN)
18001 retList = get_tv_number_chk(&argvars[1], &error);
18002 if (error)
18003 return;
18004
18005 if (retList == 0)
18006 {
18007 rettv->v_type = VAR_STRING;
18008 rettv->vval.v_string = reg_submatch(no);
18009 }
18010 else
18011 {
18012 rettv->v_type = VAR_LIST;
18013 rettv->vval.v_list = reg_submatch_list(no);
18014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018015}
18016
18017/*
18018 * "substitute()" function
18019 */
18020 static void
18021f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 typval_T *argvars;
18023 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018024{
18025 char_u patbuf[NUMBUFLEN];
18026 char_u subbuf[NUMBUFLEN];
18027 char_u flagsbuf[NUMBUFLEN];
18028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018029 char_u *str = get_tv_string_chk(&argvars[0]);
18030 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18031 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18032 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18033
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018035 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18036 rettv->vval.v_string = NULL;
18037 else
18038 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039}
18040
18041/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018042 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018045f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048{
18049 int id = 0;
18050#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018051 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 long col;
18053 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018054 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018056 lnum = get_tv_lnum(argvars); /* -1 on type error */
18057 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18058 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018060 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018061 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018062 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063#endif
18064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018065 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066}
18067
18068/*
18069 * "synIDattr(id, what [, mode])" function
18070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018072f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018073 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075{
18076 char_u *p = NULL;
18077#ifdef FEAT_SYN_HL
18078 int id;
18079 char_u *what;
18080 char_u *mode;
18081 char_u modebuf[NUMBUFLEN];
18082 int modec;
18083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018084 id = get_tv_number(&argvars[0]);
18085 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018090 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 modec = 0; /* replace invalid with current */
18092 }
18093 else
18094 {
18095#ifdef FEAT_GUI
18096 if (gui.in_use)
18097 modec = 'g';
18098 else
18099#endif
18100 if (t_colors > 1)
18101 modec = 'c';
18102 else
18103 modec = 't';
18104 }
18105
18106
18107 switch (TOLOWER_ASC(what[0]))
18108 {
18109 case 'b':
18110 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18111 p = highlight_color(id, what, modec);
18112 else /* bold */
18113 p = highlight_has_attr(id, HL_BOLD, modec);
18114 break;
18115
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018116 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 p = highlight_color(id, what, modec);
18118 break;
18119
18120 case 'i':
18121 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18122 p = highlight_has_attr(id, HL_INVERSE, modec);
18123 else /* italic */
18124 p = highlight_has_attr(id, HL_ITALIC, modec);
18125 break;
18126
18127 case 'n': /* name */
18128 p = get_highlight_name(NULL, id - 1);
18129 break;
18130
18131 case 'r': /* reverse */
18132 p = highlight_has_attr(id, HL_INVERSE, modec);
18133 break;
18134
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018135 case 's':
18136 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18137 p = highlight_color(id, what, modec);
18138 else /* standout */
18139 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 break;
18141
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018142 case 'u':
18143 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18144 /* underline */
18145 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18146 else
18147 /* undercurl */
18148 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 break;
18150 }
18151
18152 if (p != NULL)
18153 p = vim_strsave(p);
18154#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155 rettv->v_type = VAR_STRING;
18156 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157}
18158
18159/*
18160 * "synIDtrans(id)" function
18161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018163f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166{
18167 int id;
18168
18169#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171
18172 if (id > 0)
18173 id = syn_get_final_id(id);
18174 else
18175#endif
18176 id = 0;
18177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179}
18180
18181/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018182 * "synconcealed(lnum, col)" function
18183 */
18184 static void
18185f_synconcealed(argvars, rettv)
18186 typval_T *argvars UNUSED;
18187 typval_T *rettv;
18188{
18189#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18190 long lnum;
18191 long col;
18192 int syntax_flags = 0;
18193 int cchar;
18194 int matchid = 0;
18195 char_u str[NUMBUFLEN];
18196#endif
18197
18198 rettv->v_type = VAR_LIST;
18199 rettv->vval.v_list = NULL;
18200
18201#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18202 lnum = get_tv_lnum(argvars); /* -1 on type error */
18203 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18204
18205 vim_memset(str, NUL, sizeof(str));
18206
18207 if (rettv_list_alloc(rettv) != FAIL)
18208 {
18209 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18210 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18211 && curwin->w_p_cole > 0)
18212 {
18213 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18214 syntax_flags = get_syntax_info(&matchid);
18215
18216 /* get the conceal character */
18217 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18218 {
18219 cchar = syn_get_sub_char();
18220 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18221 cchar = lcs_conceal;
18222 if (cchar != NUL)
18223 {
18224# ifdef FEAT_MBYTE
18225 if (has_mbyte)
18226 (*mb_char2bytes)(cchar, str);
18227 else
18228# endif
18229 str[0] = cchar;
18230 }
18231 }
18232 }
18233
18234 list_append_number(rettv->vval.v_list,
18235 (syntax_flags & HL_CONCEAL) != 0);
18236 /* -1 to auto-determine strlen */
18237 list_append_string(rettv->vval.v_list, str, -1);
18238 list_append_number(rettv->vval.v_list, matchid);
18239 }
18240#endif
18241}
18242
18243/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018244 * "synstack(lnum, col)" function
18245 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018246 static void
18247f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018248 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018249 typval_T *rettv;
18250{
18251#ifdef FEAT_SYN_HL
18252 long lnum;
18253 long col;
18254 int i;
18255 int id;
18256#endif
18257
18258 rettv->v_type = VAR_LIST;
18259 rettv->vval.v_list = NULL;
18260
18261#ifdef FEAT_SYN_HL
18262 lnum = get_tv_lnum(argvars); /* -1 on type error */
18263 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18264
18265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018266 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018267 && rettv_list_alloc(rettv) != FAIL)
18268 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018269 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018270 for (i = 0; ; ++i)
18271 {
18272 id = syn_get_stack_item(i);
18273 if (id < 0)
18274 break;
18275 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18276 break;
18277 }
18278 }
18279#endif
18280}
18281
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018283get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018284 typval_T *argvars;
18285 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018286 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018288 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018290 char_u *infile = NULL;
18291 char_u buf[NUMBUFLEN];
18292 int err = FALSE;
18293 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018294 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018296 rettv->v_type = VAR_STRING;
18297 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018298 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018299 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018300
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018301 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018302 {
18303 /*
18304 * Write the string to a temp file, to be used for input of the shell
18305 * command.
18306 */
18307 if ((infile = vim_tempname('i')) == NULL)
18308 {
18309 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018310 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018311 }
18312
18313 fd = mch_fopen((char *)infile, WRITEBIN);
18314 if (fd == NULL)
18315 {
18316 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018317 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018318 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018319 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018320 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018321 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18322 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018323 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018324 else
18325 {
18326 p = get_tv_string_buf_chk(&argvars[1], buf);
18327 if (p == NULL)
18328 {
18329 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018330 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018331 }
18332 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18333 err = TRUE;
18334 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018335 if (fclose(fd) != 0)
18336 err = TRUE;
18337 if (err)
18338 {
18339 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018340 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018341 }
18342 }
18343
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018344 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018346 int len;
18347 listitem_T *li;
18348 char_u *s = NULL;
18349 char_u *start;
18350 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018351 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018353 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18354 SHELL_SILENT | SHELL_COOKED, &len);
18355 if (res == NULL)
18356 goto errret;
18357
18358 list = list_alloc();
18359 if (list == NULL)
18360 goto errret;
18361
18362 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018364 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018365 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018366 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018367 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018368
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018369 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018370 if (s == NULL)
18371 goto errret;
18372
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018373 for (p = s; start < end; ++p, ++start)
18374 *p = *start == NUL ? NL : *start;
18375 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018376
18377 li = listitem_alloc();
18378 if (li == NULL)
18379 {
18380 vim_free(s);
18381 goto errret;
18382 }
18383 li->li_tv.v_type = VAR_STRING;
18384 li->li_tv.vval.v_string = s;
18385 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018387
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018388 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018389 rettv->v_type = VAR_LIST;
18390 rettv->vval.v_list = list;
18391 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018393 else
18394 {
18395 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18396 SHELL_SILENT | SHELL_COOKED, NULL);
18397#ifdef USE_CR
18398 /* translate <CR> into <NL> */
18399 if (res != NULL)
18400 {
18401 char_u *s;
18402
18403 for (s = res; *s; ++s)
18404 {
18405 if (*s == CAR)
18406 *s = NL;
18407 }
18408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409#else
18410# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018411 /* translate <CR><NL> into <NL> */
18412 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018414 char_u *s, *d;
18415
18416 d = res;
18417 for (s = res; *s; ++s)
18418 {
18419 if (s[0] == CAR && s[1] == NL)
18420 ++s;
18421 *d++ = *s;
18422 }
18423 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425# endif
18426#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018427 rettv->vval.v_string = res;
18428 res = NULL;
18429 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018430
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018431errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018432 if (infile != NULL)
18433 {
18434 mch_remove(infile);
18435 vim_free(infile);
18436 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018437 if (res != NULL)
18438 vim_free(res);
18439 if (list != NULL)
18440 list_free(list, TRUE);
18441}
18442
18443/*
18444 * "system()" function
18445 */
18446 static void
18447f_system(argvars, rettv)
18448 typval_T *argvars;
18449 typval_T *rettv;
18450{
18451 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18452}
18453
18454/*
18455 * "systemlist()" function
18456 */
18457 static void
18458f_systemlist(argvars, rettv)
18459 typval_T *argvars;
18460 typval_T *rettv;
18461{
18462 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463}
18464
18465/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018466 * "tabpagebuflist()" function
18467 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018468 static void
18469f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018470 typval_T *argvars UNUSED;
18471 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018472{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018473#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018474 tabpage_T *tp;
18475 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018476
18477 if (argvars[0].v_type == VAR_UNKNOWN)
18478 wp = firstwin;
18479 else
18480 {
18481 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18482 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018483 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018484 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018485 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018486 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018487 for (; wp != NULL; wp = wp->w_next)
18488 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018489 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018490 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018491 }
18492#endif
18493}
18494
18495
18496/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018497 * "tabpagenr()" function
18498 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018499 static void
18500f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018501 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018502 typval_T *rettv;
18503{
18504 int nr = 1;
18505#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018506 char_u *arg;
18507
18508 if (argvars[0].v_type != VAR_UNKNOWN)
18509 {
18510 arg = get_tv_string_chk(&argvars[0]);
18511 nr = 0;
18512 if (arg != NULL)
18513 {
18514 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018515 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018516 else
18517 EMSG2(_(e_invexpr2), arg);
18518 }
18519 }
18520 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018521 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018522#endif
18523 rettv->vval.v_number = nr;
18524}
18525
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018526
18527#ifdef FEAT_WINDOWS
18528static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18529
18530/*
18531 * Common code for tabpagewinnr() and winnr().
18532 */
18533 static int
18534get_winnr(tp, argvar)
18535 tabpage_T *tp;
18536 typval_T *argvar;
18537{
18538 win_T *twin;
18539 int nr = 1;
18540 win_T *wp;
18541 char_u *arg;
18542
18543 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18544 if (argvar->v_type != VAR_UNKNOWN)
18545 {
18546 arg = get_tv_string_chk(argvar);
18547 if (arg == NULL)
18548 nr = 0; /* type error; errmsg already given */
18549 else if (STRCMP(arg, "$") == 0)
18550 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18551 else if (STRCMP(arg, "#") == 0)
18552 {
18553 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18554 if (twin == NULL)
18555 nr = 0;
18556 }
18557 else
18558 {
18559 EMSG2(_(e_invexpr2), arg);
18560 nr = 0;
18561 }
18562 }
18563
18564 if (nr > 0)
18565 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18566 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018567 {
18568 if (wp == NULL)
18569 {
18570 /* didn't find it in this tabpage */
18571 nr = 0;
18572 break;
18573 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018574 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018575 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018576 return nr;
18577}
18578#endif
18579
18580/*
18581 * "tabpagewinnr()" function
18582 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018583 static void
18584f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018585 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018586 typval_T *rettv;
18587{
18588 int nr = 1;
18589#ifdef FEAT_WINDOWS
18590 tabpage_T *tp;
18591
18592 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18593 if (tp == NULL)
18594 nr = 0;
18595 else
18596 nr = get_winnr(tp, &argvars[1]);
18597#endif
18598 rettv->vval.v_number = nr;
18599}
18600
18601
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018602/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018603 * "tagfiles()" function
18604 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018605 static void
18606f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018607 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018608 typval_T *rettv;
18609{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018610 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018611 tagname_T tn;
18612 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018614 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018615 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018616 fname = alloc(MAXPATHL);
18617 if (fname == NULL)
18618 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018619
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018620 for (first = TRUE; ; first = FALSE)
18621 if (get_tagfname(&tn, first, fname) == FAIL
18622 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018623 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018624 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018625 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018626}
18627
18628/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018629 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018630 */
18631 static void
18632f_taglist(argvars, rettv)
18633 typval_T *argvars;
18634 typval_T *rettv;
18635{
18636 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018637
18638 tag_pattern = get_tv_string(&argvars[0]);
18639
18640 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018641 if (*tag_pattern == NUL)
18642 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018644 if (rettv_list_alloc(rettv) == OK)
18645 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018646}
18647
18648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649 * "tempname()" function
18650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655{
18656 static int x = 'A';
18657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 rettv->v_type = VAR_STRING;
18659 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660
18661 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18662 * names. Skip 'I' and 'O', they are used for shell redirection. */
18663 do
18664 {
18665 if (x == 'Z')
18666 x = '0';
18667 else if (x == '9')
18668 x = 'A';
18669 else
18670 {
18671#ifdef EBCDIC
18672 if (x == 'I')
18673 x = 'J';
18674 else if (x == 'R')
18675 x = 'S';
18676 else
18677#endif
18678 ++x;
18679 }
18680 } while (x == 'I' || x == 'O');
18681}
18682
18683/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018684 * "test(list)" function: Just checking the walls...
18685 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018686 static void
18687f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018688 typval_T *argvars UNUSED;
18689 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018690{
18691 /* Used for unit testing. Change the code below to your liking. */
18692#if 0
18693 listitem_T *li;
18694 list_T *l;
18695 char_u *bad, *good;
18696
18697 if (argvars[0].v_type != VAR_LIST)
18698 return;
18699 l = argvars[0].vval.v_list;
18700 if (l == NULL)
18701 return;
18702 li = l->lv_first;
18703 if (li == NULL)
18704 return;
18705 bad = get_tv_string(&li->li_tv);
18706 li = li->li_next;
18707 if (li == NULL)
18708 return;
18709 good = get_tv_string(&li->li_tv);
18710 rettv->vval.v_number = test_edit_score(bad, good);
18711#endif
18712}
18713
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018714#ifdef FEAT_FLOAT
18715/*
18716 * "tan()" function
18717 */
18718 static void
18719f_tan(argvars, rettv)
18720 typval_T *argvars;
18721 typval_T *rettv;
18722{
18723 float_T f;
18724
18725 rettv->v_type = VAR_FLOAT;
18726 if (get_float_arg(argvars, &f) == OK)
18727 rettv->vval.v_float = tan(f);
18728 else
18729 rettv->vval.v_float = 0.0;
18730}
18731
18732/*
18733 * "tanh()" function
18734 */
18735 static void
18736f_tanh(argvars, rettv)
18737 typval_T *argvars;
18738 typval_T *rettv;
18739{
18740 float_T f;
18741
18742 rettv->v_type = VAR_FLOAT;
18743 if (get_float_arg(argvars, &f) == OK)
18744 rettv->vval.v_float = tanh(f);
18745 else
18746 rettv->vval.v_float = 0.0;
18747}
18748#endif
18749
Bram Moolenaard52d9742005-08-21 22:20:28 +000018750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 * "tolower(string)" function
18752 */
18753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018754f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018755 typval_T *argvars;
18756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757{
18758 char_u *p;
18759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018760 p = vim_strsave(get_tv_string(&argvars[0]));
18761 rettv->v_type = VAR_STRING;
18762 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763
18764 if (p != NULL)
18765 while (*p != NUL)
18766 {
18767#ifdef FEAT_MBYTE
18768 int l;
18769
18770 if (enc_utf8)
18771 {
18772 int c, lc;
18773
18774 c = utf_ptr2char(p);
18775 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018776 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 /* TODO: reallocate string when byte count changes. */
18778 if (utf_char2len(lc) == l)
18779 utf_char2bytes(lc, p);
18780 p += l;
18781 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018782 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 p += l; /* skip multi-byte character */
18784 else
18785#endif
18786 {
18787 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18788 ++p;
18789 }
18790 }
18791}
18792
18793/*
18794 * "toupper(string)" function
18795 */
18796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 typval_T *argvars;
18799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018802 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803}
18804
18805/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018806 * "tr(string, fromstr, tostr)" function
18807 */
18808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018809f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 typval_T *argvars;
18811 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018812{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018813 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018814 char_u *fromstr;
18815 char_u *tostr;
18816 char_u *p;
18817#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018818 int inlen;
18819 int fromlen;
18820 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018821 int idx;
18822 char_u *cpstr;
18823 int cplen;
18824 int first = TRUE;
18825#endif
18826 char_u buf[NUMBUFLEN];
18827 char_u buf2[NUMBUFLEN];
18828 garray_T ga;
18829
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018830 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018831 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18832 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018833
18834 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018835 rettv->v_type = VAR_STRING;
18836 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018837 if (fromstr == NULL || tostr == NULL)
18838 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018839 ga_init2(&ga, (int)sizeof(char), 80);
18840
18841#ifdef FEAT_MBYTE
18842 if (!has_mbyte)
18843#endif
18844 /* not multi-byte: fromstr and tostr must be the same length */
18845 if (STRLEN(fromstr) != STRLEN(tostr))
18846 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018847#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018848error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018849#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018850 EMSG2(_(e_invarg2), fromstr);
18851 ga_clear(&ga);
18852 return;
18853 }
18854
18855 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018856 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018857 {
18858#ifdef FEAT_MBYTE
18859 if (has_mbyte)
18860 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018861 inlen = (*mb_ptr2len)(in_str);
18862 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018863 cplen = inlen;
18864 idx = 0;
18865 for (p = fromstr; *p != NUL; p += fromlen)
18866 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018867 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018868 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018869 {
18870 for (p = tostr; *p != NUL; p += tolen)
18871 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018872 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018873 if (idx-- == 0)
18874 {
18875 cplen = tolen;
18876 cpstr = p;
18877 break;
18878 }
18879 }
18880 if (*p == NUL) /* tostr is shorter than fromstr */
18881 goto error;
18882 break;
18883 }
18884 ++idx;
18885 }
18886
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018887 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018888 {
18889 /* Check that fromstr and tostr have the same number of
18890 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018891 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018892 first = FALSE;
18893 for (p = tostr; *p != NUL; p += tolen)
18894 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018895 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018896 --idx;
18897 }
18898 if (idx != 0)
18899 goto error;
18900 }
18901
18902 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018903 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018904 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018905
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018906 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018907 }
18908 else
18909#endif
18910 {
18911 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018912 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018913 if (p != NULL)
18914 ga_append(&ga, tostr[p - fromstr]);
18915 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018916 ga_append(&ga, *in_str);
18917 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018918 }
18919 }
18920
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018921 /* add a terminating NUL */
18922 ga_grow(&ga, 1);
18923 ga_append(&ga, NUL);
18924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018925 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018926}
18927
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018928#ifdef FEAT_FLOAT
18929/*
18930 * "trunc({float})" function
18931 */
18932 static void
18933f_trunc(argvars, rettv)
18934 typval_T *argvars;
18935 typval_T *rettv;
18936{
18937 float_T f;
18938
18939 rettv->v_type = VAR_FLOAT;
18940 if (get_float_arg(argvars, &f) == OK)
18941 /* trunc() is not in C90, use floor() or ceil() instead. */
18942 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18943 else
18944 rettv->vval.v_float = 0.0;
18945}
18946#endif
18947
Bram Moolenaar8299df92004-07-10 09:47:34 +000018948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 * "type(expr)" function
18950 */
18951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018952f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018953 typval_T *argvars;
18954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018956 int n;
18957
18958 switch (argvars[0].v_type)
18959 {
18960 case VAR_NUMBER: n = 0; break;
18961 case VAR_STRING: n = 1; break;
18962 case VAR_FUNC: n = 2; break;
18963 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018964 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018965#ifdef FEAT_FLOAT
18966 case VAR_FLOAT: n = 5; break;
18967#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018968 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18969 }
18970 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971}
18972
18973/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018974 * "undofile(name)" function
18975 */
18976 static void
18977f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018978 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018979 typval_T *rettv;
18980{
18981 rettv->v_type = VAR_STRING;
18982#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018983 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018984 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018985
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018986 if (*fname == NUL)
18987 {
18988 /* If there is no file name there will be no undo file. */
18989 rettv->vval.v_string = NULL;
18990 }
18991 else
18992 {
18993 char_u *ffname = FullName_save(fname, FALSE);
18994
18995 if (ffname != NULL)
18996 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18997 vim_free(ffname);
18998 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018999 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019000#else
19001 rettv->vval.v_string = NULL;
19002#endif
19003}
19004
19005/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019006 * "undotree()" function
19007 */
19008 static void
19009f_undotree(argvars, rettv)
19010 typval_T *argvars UNUSED;
19011 typval_T *rettv;
19012{
19013 if (rettv_dict_alloc(rettv) == OK)
19014 {
19015 dict_T *dict = rettv->vval.v_dict;
19016 list_T *list;
19017
Bram Moolenaar730cde92010-06-27 05:18:54 +020019018 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019019 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019020 dict_add_nr_str(dict, "save_last",
19021 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019022 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19023 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019024 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019025
19026 list = list_alloc();
19027 if (list != NULL)
19028 {
19029 u_eval_tree(curbuf->b_u_oldhead, list);
19030 dict_add_list(dict, "entries", list);
19031 }
19032 }
19033}
19034
19035/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019036 * "values(dict)" function
19037 */
19038 static void
19039f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019040 typval_T *argvars;
19041 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019042{
19043 dict_list(argvars, rettv, 1);
19044}
19045
19046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 * "virtcol(string)" function
19048 */
19049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019050f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019051 typval_T *argvars;
19052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053{
19054 colnr_T vcol = 0;
19055 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019056 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019058 fp = var2fpos(&argvars[0], FALSE, &fnum);
19059 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19060 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061 {
19062 getvvcol(curwin, fp, NULL, NULL, &vcol);
19063 ++vcol;
19064 }
19065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019066 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067}
19068
19069/*
19070 * "visualmode()" function
19071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019073f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019074 typval_T *argvars UNUSED;
19075 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 char_u str[2];
19078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 str[0] = curbuf->b_visual_mode_eval;
19081 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019082 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083
19084 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019085 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087}
19088
19089/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019090 * "wildmenumode()" function
19091 */
19092 static void
19093f_wildmenumode(argvars, rettv)
19094 typval_T *argvars UNUSED;
19095 typval_T *rettv UNUSED;
19096{
19097#ifdef FEAT_WILDMENU
19098 if (wild_menu_showing)
19099 rettv->vval.v_number = 1;
19100#endif
19101}
19102
19103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 * "winbufnr(nr)" function
19105 */
19106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019108 typval_T *argvars;
19109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110{
19111 win_T *wp;
19112
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019113 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019117 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118}
19119
19120/*
19121 * "wincol()" function
19122 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019125 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019126 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127{
19128 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130}
19131
19132/*
19133 * "winheight(nr)" function
19134 */
19135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 typval_T *argvars;
19138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139{
19140 win_T *wp;
19141
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019142 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147}
19148
19149/*
19150 * "winline()" function
19151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156{
19157 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019158 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159}
19160
19161/*
19162 * "winnr()" function
19163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168{
19169 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019170
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019172 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175}
19176
19177/*
19178 * "winrestcmd()" function
19179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184{
19185#ifdef FEAT_WINDOWS
19186 win_T *wp;
19187 int winnr = 1;
19188 garray_T ga;
19189 char_u buf[50];
19190
19191 ga_init2(&ga, (int)sizeof(char), 70);
19192 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19193 {
19194 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19195 ga_concat(&ga, buf);
19196# ifdef FEAT_VERTSPLIT
19197 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19198 ga_concat(&ga, buf);
19199# endif
19200 ++winnr;
19201 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019202 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019204 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019208 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209}
19210
19211/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019212 * "winrestview()" function
19213 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019214 static void
19215f_winrestview(argvars, rettv)
19216 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019217 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019218{
19219 dict_T *dict;
19220
19221 if (argvars[0].v_type != VAR_DICT
19222 || (dict = argvars[0].vval.v_dict) == NULL)
19223 EMSG(_(e_invarg));
19224 else
19225 {
19226 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19227 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19228#ifdef FEAT_VIRTUALEDIT
19229 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19230#endif
19231 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019232 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019233
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019234 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019235#ifdef FEAT_DIFF
19236 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19237#endif
19238 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19239 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19240
19241 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019242 win_new_height(curwin, curwin->w_height);
19243# ifdef FEAT_VERTSPLIT
19244 win_new_width(curwin, W_WIDTH(curwin));
19245# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019246 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019247
19248 if (curwin->w_topline == 0)
19249 curwin->w_topline = 1;
19250 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19251 curwin->w_topline = curbuf->b_ml.ml_line_count;
19252#ifdef FEAT_DIFF
19253 check_topfill(curwin, TRUE);
19254#endif
19255 }
19256}
19257
19258/*
19259 * "winsaveview()" function
19260 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019261 static void
19262f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019263 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019264 typval_T *rettv;
19265{
19266 dict_T *dict;
19267
Bram Moolenaara800b422010-06-27 01:15:55 +020019268 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019269 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019270 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019271
19272 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19273 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19274#ifdef FEAT_VIRTUALEDIT
19275 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19276#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019277 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019278 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19279
19280 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19281#ifdef FEAT_DIFF
19282 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19283#endif
19284 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19285 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19286}
19287
19288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 * "winwidth(nr)" function
19290 */
19291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019292f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019293 typval_T *argvars;
19294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295{
19296 win_T *wp;
19297
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019298 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 else
19302#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019303 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019305 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306#endif
19307}
19308
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019310 * Write list of strings to file
19311 */
19312 static int
19313write_list(fd, list, binary)
19314 FILE *fd;
19315 list_T *list;
19316 int binary;
19317{
19318 listitem_T *li;
19319 int c;
19320 int ret = OK;
19321 char_u *s;
19322
19323 for (li = list->lv_first; li != NULL; li = li->li_next)
19324 {
19325 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19326 {
19327 if (*s == '\n')
19328 c = putc(NUL, fd);
19329 else
19330 c = putc(*s, fd);
19331 if (c == EOF)
19332 {
19333 ret = FAIL;
19334 break;
19335 }
19336 }
19337 if (!binary || li->li_next != NULL)
19338 if (putc('\n', fd) == EOF)
19339 {
19340 ret = FAIL;
19341 break;
19342 }
19343 if (ret == FAIL)
19344 {
19345 EMSG(_(e_write));
19346 break;
19347 }
19348 }
19349 return ret;
19350}
19351
19352/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019353 * "writefile()" function
19354 */
19355 static void
19356f_writefile(argvars, rettv)
19357 typval_T *argvars;
19358 typval_T *rettv;
19359{
19360 int binary = FALSE;
19361 char_u *fname;
19362 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019363 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019364
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019365 if (check_restricted() || check_secure())
19366 return;
19367
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019368 if (argvars[0].v_type != VAR_LIST)
19369 {
19370 EMSG2(_(e_listarg), "writefile()");
19371 return;
19372 }
19373 if (argvars[0].vval.v_list == NULL)
19374 return;
19375
19376 if (argvars[2].v_type != VAR_UNKNOWN
19377 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19378 binary = TRUE;
19379
19380 /* Always open the file in binary mode, library functions have a mind of
19381 * their own about CR-LF conversion. */
19382 fname = get_tv_string(&argvars[1]);
19383 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19384 {
19385 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19386 ret = -1;
19387 }
19388 else
19389 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019390 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19391 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019392 fclose(fd);
19393 }
19394
19395 rettv->vval.v_number = ret;
19396}
19397
19398/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019399 * "xor(expr, expr)" function
19400 */
19401 static void
19402f_xor(argvars, rettv)
19403 typval_T *argvars;
19404 typval_T *rettv;
19405{
19406 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19407 ^ get_tv_number_chk(&argvars[1], NULL);
19408}
19409
19410
19411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019413 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414 */
19415 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019416var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019417 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019418 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019419 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019421 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019423 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424
Bram Moolenaara5525202006-03-02 22:52:09 +000019425 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019426 if (varp->v_type == VAR_LIST)
19427 {
19428 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019429 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019430 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019431 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019432
19433 l = varp->vval.v_list;
19434 if (l == NULL)
19435 return NULL;
19436
19437 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019438 pos.lnum = list_find_nr(l, 0L, &error);
19439 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019440 return NULL; /* invalid line number */
19441
19442 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019443 pos.col = list_find_nr(l, 1L, &error);
19444 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019445 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019446 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019447
19448 /* We accept "$" for the column number: last column. */
19449 li = list_find(l, 1L);
19450 if (li != NULL && li->li_tv.v_type == VAR_STRING
19451 && li->li_tv.vval.v_string != NULL
19452 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19453 pos.col = len + 1;
19454
Bram Moolenaara5525202006-03-02 22:52:09 +000019455 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019456 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019457 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019458 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019459
Bram Moolenaara5525202006-03-02 22:52:09 +000019460#ifdef FEAT_VIRTUALEDIT
19461 /* Get the virtual offset. Defaults to zero. */
19462 pos.coladd = list_find_nr(l, 2L, &error);
19463 if (error)
19464 pos.coladd = 0;
19465#endif
19466
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019467 return &pos;
19468 }
19469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019470 name = get_tv_string_chk(varp);
19471 if (name == NULL)
19472 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019473 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019475 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19476 {
19477 if (VIsual_active)
19478 return &VIsual;
19479 return &curwin->w_cursor;
19480 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019481 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019483 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19485 return NULL;
19486 return pp;
19487 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019488
19489#ifdef FEAT_VIRTUALEDIT
19490 pos.coladd = 0;
19491#endif
19492
Bram Moolenaar477933c2007-07-17 14:32:23 +000019493 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019494 {
19495 pos.col = 0;
19496 if (name[1] == '0') /* "w0": first visible line */
19497 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019498 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019499 pos.lnum = curwin->w_topline;
19500 return &pos;
19501 }
19502 else if (name[1] == '$') /* "w$": last visible line */
19503 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019504 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019505 pos.lnum = curwin->w_botline - 1;
19506 return &pos;
19507 }
19508 }
19509 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019511 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 {
19513 pos.lnum = curbuf->b_ml.ml_line_count;
19514 pos.col = 0;
19515 }
19516 else
19517 {
19518 pos.lnum = curwin->w_cursor.lnum;
19519 pos.col = (colnr_T)STRLEN(ml_get_curline());
19520 }
19521 return &pos;
19522 }
19523 return NULL;
19524}
19525
19526/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019527 * Convert list in "arg" into a position and optional file number.
19528 * When "fnump" is NULL there is no file number, only 3 items.
19529 * Note that the column is passed on as-is, the caller may want to decrement
19530 * it to use 1 for the first column.
19531 * Return FAIL when conversion is not possible, doesn't check the position for
19532 * validity.
19533 */
19534 static int
19535list2fpos(arg, posp, fnump)
19536 typval_T *arg;
19537 pos_T *posp;
19538 int *fnump;
19539{
19540 list_T *l = arg->vval.v_list;
19541 long i = 0;
19542 long n;
19543
Bram Moolenaarbde35262006-07-23 20:12:24 +000019544 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19545 * when "fnump" isn't NULL and "coladd" is optional. */
19546 if (arg->v_type != VAR_LIST
19547 || l == NULL
19548 || l->lv_len < (fnump == NULL ? 2 : 3)
19549 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019550 return FAIL;
19551
19552 if (fnump != NULL)
19553 {
19554 n = list_find_nr(l, i++, NULL); /* fnum */
19555 if (n < 0)
19556 return FAIL;
19557 if (n == 0)
19558 n = curbuf->b_fnum; /* current buffer */
19559 *fnump = n;
19560 }
19561
19562 n = list_find_nr(l, i++, NULL); /* lnum */
19563 if (n < 0)
19564 return FAIL;
19565 posp->lnum = n;
19566
19567 n = list_find_nr(l, i++, NULL); /* col */
19568 if (n < 0)
19569 return FAIL;
19570 posp->col = n;
19571
19572#ifdef FEAT_VIRTUALEDIT
19573 n = list_find_nr(l, i, NULL);
19574 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019575 posp->coladd = 0;
19576 else
19577 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019578#endif
19579
19580 return OK;
19581}
19582
19583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 * Get the length of an environment variable name.
19585 * Advance "arg" to the first character after the name.
19586 * Return 0 for error.
19587 */
19588 static int
19589get_env_len(arg)
19590 char_u **arg;
19591{
19592 char_u *p;
19593 int len;
19594
19595 for (p = *arg; vim_isIDc(*p); ++p)
19596 ;
19597 if (p == *arg) /* no name found */
19598 return 0;
19599
19600 len = (int)(p - *arg);
19601 *arg = p;
19602 return len;
19603}
19604
19605/*
19606 * Get the length of the name of a function or internal variable.
19607 * "arg" is advanced to the first non-white character after the name.
19608 * Return 0 if something is wrong.
19609 */
19610 static int
19611get_id_len(arg)
19612 char_u **arg;
19613{
19614 char_u *p;
19615 int len;
19616
19617 /* Find the end of the name. */
19618 for (p = *arg; eval_isnamec(*p); ++p)
19619 ;
19620 if (p == *arg) /* no name found */
19621 return 0;
19622
19623 len = (int)(p - *arg);
19624 *arg = skipwhite(p);
19625
19626 return len;
19627}
19628
19629/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019630 * Get the length of the name of a variable or function.
19631 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019633 * Return -1 if curly braces expansion failed.
19634 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 * If the name contains 'magic' {}'s, expand them and return the
19636 * expanded name in an allocated string via 'alias' - caller must free.
19637 */
19638 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019639get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 char_u **arg;
19641 char_u **alias;
19642 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019643 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644{
19645 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 char_u *p;
19647 char_u *expr_start;
19648 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649
19650 *alias = NULL; /* default to no alias */
19651
19652 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19653 && (*arg)[2] == (int)KE_SNR)
19654 {
19655 /* hard coded <SNR>, already translated */
19656 *arg += 3;
19657 return get_id_len(arg) + 3;
19658 }
19659 len = eval_fname_script(*arg);
19660 if (len > 0)
19661 {
19662 /* literal "<SID>", "s:" or "<SNR>" */
19663 *arg += len;
19664 }
19665
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019667 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019669 p = find_name_end(*arg, &expr_start, &expr_end,
19670 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 if (expr_start != NULL)
19672 {
19673 char_u *temp_string;
19674
19675 if (!evaluate)
19676 {
19677 len += (int)(p - *arg);
19678 *arg = skipwhite(p);
19679 return len;
19680 }
19681
19682 /*
19683 * Include any <SID> etc in the expanded string:
19684 * Thus the -len here.
19685 */
19686 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19687 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019688 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 *alias = temp_string;
19690 *arg = skipwhite(p);
19691 return (int)STRLEN(temp_string);
19692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693
19694 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019695 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 EMSG2(_(e_invexpr2), *arg);
19697
19698 return len;
19699}
19700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019701/*
19702 * Find the end of a variable or function name, taking care of magic braces.
19703 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19704 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019705 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019706 * Return a pointer to just after the name. Equal to "arg" if there is no
19707 * valid name.
19708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019710find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711 char_u *arg;
19712 char_u **expr_start;
19713 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019714 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019716 int mb_nest = 0;
19717 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 char_u *p;
19719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 *expr_start = NULL;
19723 *expr_end = NULL;
19724 }
19725
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019726 /* Quick check for valid starting character. */
19727 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19728 return arg;
19729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 for (p = arg; *p != NUL
19731 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019732 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019733 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019735 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019737 if (*p == '\'')
19738 {
19739 /* skip over 'string' to avoid counting [ and ] inside it. */
19740 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19741 ;
19742 if (*p == NUL)
19743 break;
19744 }
19745 else if (*p == '"')
19746 {
19747 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19748 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19749 if (*p == '\\' && p[1] != NUL)
19750 ++p;
19751 if (*p == NUL)
19752 break;
19753 }
19754
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 if (*p == '[')
19758 ++br_nest;
19759 else if (*p == ']')
19760 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019763 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 if (*p == '{')
19766 {
19767 mb_nest++;
19768 if (expr_start != NULL && *expr_start == NULL)
19769 *expr_start = p;
19770 }
19771 else if (*p == '}')
19772 {
19773 mb_nest--;
19774 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19775 *expr_end = p;
19776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 }
19779
19780 return p;
19781}
19782
19783/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019784 * Expands out the 'magic' {}'s in a variable/function name.
19785 * Note that this can call itself recursively, to deal with
19786 * constructs like foo{bar}{baz}{bam}
19787 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19788 * "in_start" ^
19789 * "expr_start" ^
19790 * "expr_end" ^
19791 * "in_end" ^
19792 *
19793 * Returns a new allocated string, which the caller must free.
19794 * Returns NULL for failure.
19795 */
19796 static char_u *
19797make_expanded_name(in_start, expr_start, expr_end, in_end)
19798 char_u *in_start;
19799 char_u *expr_start;
19800 char_u *expr_end;
19801 char_u *in_end;
19802{
19803 char_u c1;
19804 char_u *retval = NULL;
19805 char_u *temp_result;
19806 char_u *nextcmd = NULL;
19807
19808 if (expr_end == NULL || in_end == NULL)
19809 return NULL;
19810 *expr_start = NUL;
19811 *expr_end = NUL;
19812 c1 = *in_end;
19813 *in_end = NUL;
19814
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019815 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019816 if (temp_result != NULL && nextcmd == NULL)
19817 {
19818 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19819 + (in_end - expr_end) + 1));
19820 if (retval != NULL)
19821 {
19822 STRCPY(retval, in_start);
19823 STRCAT(retval, temp_result);
19824 STRCAT(retval, expr_end + 1);
19825 }
19826 }
19827 vim_free(temp_result);
19828
19829 *in_end = c1; /* put char back for error messages */
19830 *expr_start = '{';
19831 *expr_end = '}';
19832
19833 if (retval != NULL)
19834 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019835 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019836 if (expr_start != NULL)
19837 {
19838 /* Further expansion! */
19839 temp_result = make_expanded_name(retval, expr_start,
19840 expr_end, temp_result);
19841 vim_free(retval);
19842 retval = temp_result;
19843 }
19844 }
19845
19846 return retval;
19847}
19848
19849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019851 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 */
19853 static int
19854eval_isnamec(c)
19855 int c;
19856{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019857 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19858}
19859
19860/*
19861 * Return TRUE if character "c" can be used as the first character in a
19862 * variable or function name (excluding '{' and '}').
19863 */
19864 static int
19865eval_isnamec1(c)
19866 int c;
19867{
19868 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869}
19870
19871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 * Set number v: variable to "val".
19873 */
19874 void
19875set_vim_var_nr(idx, val)
19876 int idx;
19877 long val;
19878{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019879 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880}
19881
19882/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019883 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 */
19885 long
19886get_vim_var_nr(idx)
19887 int idx;
19888{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019889 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890}
19891
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019892/*
19893 * Get string v: variable value. Uses a static buffer, can only be used once.
19894 */
19895 char_u *
19896get_vim_var_str(idx)
19897 int idx;
19898{
19899 return get_tv_string(&vimvars[idx].vv_tv);
19900}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019901
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019903 * Get List v: variable value. Caller must take care of reference count when
19904 * needed.
19905 */
19906 list_T *
19907get_vim_var_list(idx)
19908 int idx;
19909{
19910 return vimvars[idx].vv_list;
19911}
19912
19913/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019914 * Set v:char to character "c".
19915 */
19916 void
19917set_vim_var_char(c)
19918 int c;
19919{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019920 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019921
19922#ifdef FEAT_MBYTE
19923 if (has_mbyte)
19924 buf[(*mb_char2bytes)(c, buf)] = NUL;
19925 else
19926#endif
19927 {
19928 buf[0] = c;
19929 buf[1] = NUL;
19930 }
19931 set_vim_var_string(VV_CHAR, buf, -1);
19932}
19933
19934/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019935 * Set v:count to "count" and v:count1 to "count1".
19936 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 */
19938 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019939set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940 long count;
19941 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019942 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019944 if (set_prevcount)
19945 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019946 vimvars[VV_COUNT].vv_nr = count;
19947 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948}
19949
19950/*
19951 * Set string v: variable to a copy of "val".
19952 */
19953 void
19954set_vim_var_string(idx, val, len)
19955 int idx;
19956 char_u *val;
19957 int len; /* length of "val" to use or -1 (whole string) */
19958{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019959 /* Need to do this (at least) once, since we can't initialize a union.
19960 * Will always be invoked when "v:progname" is set. */
19961 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19962
Bram Moolenaare9a41262005-01-15 22:18:47 +000019963 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019965 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019967 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019969 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
19972/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019973 * Set List v: variable to "val".
19974 */
19975 void
19976set_vim_var_list(idx, val)
19977 int idx;
19978 list_T *val;
19979{
19980 list_unref(vimvars[idx].vv_list);
19981 vimvars[idx].vv_list = val;
19982 if (val != NULL)
19983 ++val->lv_refcount;
19984}
19985
19986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 * Set v:register if needed.
19988 */
19989 void
19990set_reg_var(c)
19991 int c;
19992{
19993 char_u regname;
19994
19995 if (c == 0 || c == ' ')
19996 regname = '"';
19997 else
19998 regname = c;
19999 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020000 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 set_vim_var_string(VV_REG, &regname, 1);
20002}
20003
20004/*
20005 * Get or set v:exception. If "oldval" == NULL, return the current value.
20006 * Otherwise, restore the value to "oldval" and return NULL.
20007 * Must always be called in pairs to save and restore v:exception! Does not
20008 * take care of memory allocations.
20009 */
20010 char_u *
20011v_exception(oldval)
20012 char_u *oldval;
20013{
20014 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020015 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016
Bram Moolenaare9a41262005-01-15 22:18:47 +000020017 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 return NULL;
20019}
20020
20021/*
20022 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20023 * Otherwise, restore the value to "oldval" and return NULL.
20024 * Must always be called in pairs to save and restore v:throwpoint! Does not
20025 * take care of memory allocations.
20026 */
20027 char_u *
20028v_throwpoint(oldval)
20029 char_u *oldval;
20030{
20031 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020032 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033
Bram Moolenaare9a41262005-01-15 22:18:47 +000020034 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035 return NULL;
20036}
20037
20038#if defined(FEAT_AUTOCMD) || defined(PROTO)
20039/*
20040 * Set v:cmdarg.
20041 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20042 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20043 * Must always be called in pairs!
20044 */
20045 char_u *
20046set_cmdarg(eap, oldarg)
20047 exarg_T *eap;
20048 char_u *oldarg;
20049{
20050 char_u *oldval;
20051 char_u *newval;
20052 unsigned len;
20053
Bram Moolenaare9a41262005-01-15 22:18:47 +000020054 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020055 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020057 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020058 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020059 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 }
20061
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020062 if (eap->force_bin == FORCE_BIN)
20063 len = 6;
20064 else if (eap->force_bin == FORCE_NOBIN)
20065 len = 8;
20066 else
20067 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020068
20069 if (eap->read_edit)
20070 len += 7;
20071
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020072 if (eap->force_ff != 0)
20073 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20074# ifdef FEAT_MBYTE
20075 if (eap->force_enc != 0)
20076 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020077 if (eap->bad_char != 0)
20078 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020079# endif
20080
20081 newval = alloc(len + 1);
20082 if (newval == NULL)
20083 return NULL;
20084
20085 if (eap->force_bin == FORCE_BIN)
20086 sprintf((char *)newval, " ++bin");
20087 else if (eap->force_bin == FORCE_NOBIN)
20088 sprintf((char *)newval, " ++nobin");
20089 else
20090 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020091
20092 if (eap->read_edit)
20093 STRCAT(newval, " ++edit");
20094
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020095 if (eap->force_ff != 0)
20096 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20097 eap->cmd + eap->force_ff);
20098# ifdef FEAT_MBYTE
20099 if (eap->force_enc != 0)
20100 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20101 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020102 if (eap->bad_char == BAD_KEEP)
20103 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20104 else if (eap->bad_char == BAD_DROP)
20105 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20106 else if (eap->bad_char != 0)
20107 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020108# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020109 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020110 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111}
20112#endif
20113
20114/*
20115 * Get the value of internal variable "name".
20116 * Return OK or FAIL.
20117 */
20118 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020119get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 char_u *name;
20121 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020122 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020123 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020124 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125{
20126 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 typval_T *tv = NULL;
20128 typval_T atv;
20129 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020131
20132 /* truncate the name, so that we can use strcmp() */
20133 cc = name[len];
20134 name[len] = NUL;
20135
20136 /*
20137 * Check for "b:changedtick".
20138 */
20139 if (STRCMP(name, "b:changedtick") == 0)
20140 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020141 atv.v_type = VAR_NUMBER;
20142 atv.vval.v_number = curbuf->b_changedtick;
20143 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 }
20145
20146 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 * Check for user-defined variables.
20148 */
20149 else
20150 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020151 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020153 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 }
20155
Bram Moolenaare9a41262005-01-15 22:18:47 +000020156 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020158 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020159 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 ret = FAIL;
20161 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020163 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164
20165 name[len] = cc;
20166
20167 return ret;
20168}
20169
20170/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020171 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20172 * Also handle function call with Funcref variable: func(expr)
20173 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20174 */
20175 static int
20176handle_subscript(arg, rettv, evaluate, verbose)
20177 char_u **arg;
20178 typval_T *rettv;
20179 int evaluate; /* do more than finding the end */
20180 int verbose; /* give error messages */
20181{
20182 int ret = OK;
20183 dict_T *selfdict = NULL;
20184 char_u *s;
20185 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020186 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020187
20188 while (ret == OK
20189 && (**arg == '['
20190 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020191 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020192 && !vim_iswhite(*(*arg - 1)))
20193 {
20194 if (**arg == '(')
20195 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020196 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020197 if (evaluate)
20198 {
20199 functv = *rettv;
20200 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020201
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020202 /* Invoke the function. Recursive! */
20203 s = functv.vval.v_string;
20204 }
20205 else
20206 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020207 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020208 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20209 &len, evaluate, selfdict);
20210
20211 /* Clear the funcref afterwards, so that deleting it while
20212 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020213 if (evaluate)
20214 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020215
20216 /* Stop the expression evaluation when immediately aborting on
20217 * error, or when an interrupt occurred or an exception was thrown
20218 * but not caught. */
20219 if (aborting())
20220 {
20221 if (ret == OK)
20222 clear_tv(rettv);
20223 ret = FAIL;
20224 }
20225 dict_unref(selfdict);
20226 selfdict = NULL;
20227 }
20228 else /* **arg == '[' || **arg == '.' */
20229 {
20230 dict_unref(selfdict);
20231 if (rettv->v_type == VAR_DICT)
20232 {
20233 selfdict = rettv->vval.v_dict;
20234 if (selfdict != NULL)
20235 ++selfdict->dv_refcount;
20236 }
20237 else
20238 selfdict = NULL;
20239 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20240 {
20241 clear_tv(rettv);
20242 ret = FAIL;
20243 }
20244 }
20245 }
20246 dict_unref(selfdict);
20247 return ret;
20248}
20249
20250/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020251 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020252 * value).
20253 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020256{
Bram Moolenaar33570922005-01-25 22:26:29 +000020257 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020258}
20259
20260/*
20261 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 * The string "s" must have been allocated, it is consumed.
20263 * Return NULL for out of memory, the variable otherwise.
20264 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020266alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 char_u *s;
20268{
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020271 rettv = alloc_tv();
20272 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274 rettv->v_type = VAR_STRING;
20275 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 }
20277 else
20278 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020279 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280}
20281
20282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020283 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020285 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020286free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020287 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288{
20289 if (varp != NULL)
20290 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020291 switch (varp->v_type)
20292 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020293 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020294 func_unref(varp->vval.v_string);
20295 /*FALLTHROUGH*/
20296 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020297 vim_free(varp->vval.v_string);
20298 break;
20299 case VAR_LIST:
20300 list_unref(varp->vval.v_list);
20301 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020302 case VAR_DICT:
20303 dict_unref(varp->vval.v_dict);
20304 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020305 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020306#ifdef FEAT_FLOAT
20307 case VAR_FLOAT:
20308#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020309 case VAR_UNKNOWN:
20310 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020311 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020312 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020313 break;
20314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 vim_free(varp);
20316 }
20317}
20318
20319/*
20320 * Free the memory for a variable value and set the value to NULL or 0.
20321 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020322 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020323clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325{
20326 if (varp != NULL)
20327 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020328 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020330 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020331 func_unref(varp->vval.v_string);
20332 /*FALLTHROUGH*/
20333 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020334 vim_free(varp->vval.v_string);
20335 varp->vval.v_string = NULL;
20336 break;
20337 case VAR_LIST:
20338 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020339 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020340 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020341 case VAR_DICT:
20342 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020343 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020344 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020345 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020346 varp->vval.v_number = 0;
20347 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020348#ifdef FEAT_FLOAT
20349 case VAR_FLOAT:
20350 varp->vval.v_float = 0.0;
20351 break;
20352#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020353 case VAR_UNKNOWN:
20354 break;
20355 default:
20356 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020358 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 }
20360}
20361
20362/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020363 * Set the value of a variable to NULL without freeing items.
20364 */
20365 static void
20366init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020367 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368{
20369 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020370 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020371}
20372
20373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 * Get the number value of a variable.
20375 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020376 * For incompatible types, return 0.
20377 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20378 * caller of incompatible types: it sets *denote to TRUE if "denote"
20379 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 */
20381 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020385 int error = FALSE;
20386
20387 return get_tv_number_chk(varp, &error); /* return 0L on error */
20388}
20389
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020390 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020391get_tv_number_chk(varp, denote)
20392 typval_T *varp;
20393 int *denote;
20394{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020395 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020400 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020401#ifdef FEAT_FLOAT
20402 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020403 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020404 break;
20405#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020406 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020407 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020408 break;
20409 case VAR_STRING:
20410 if (varp->vval.v_string != NULL)
20411 vim_str2nr(varp->vval.v_string, NULL, NULL,
20412 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020413 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020414 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020415 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020416 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020417 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020418 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020419 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020420 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020421 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020422 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020424 if (denote == NULL) /* useful for values that must be unsigned */
20425 n = -1;
20426 else
20427 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020428 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429}
20430
20431/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020432 * Get the lnum from the first argument.
20433 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020434 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 */
20436 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020437get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020438 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439{
Bram Moolenaar33570922005-01-25 22:26:29 +000020440 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 linenr_T lnum;
20442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020443 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 if (lnum == 0) /* no valid number, try using line() */
20445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020446 rettv.v_type = VAR_NUMBER;
20447 f_line(argvars, &rettv);
20448 lnum = rettv.vval.v_number;
20449 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 }
20451 return lnum;
20452}
20453
20454/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020455 * Get the lnum from the first argument.
20456 * Also accepts "$", then "buf" is used.
20457 * Returns 0 on error.
20458 */
20459 static linenr_T
20460get_tv_lnum_buf(argvars, buf)
20461 typval_T *argvars;
20462 buf_T *buf;
20463{
20464 if (argvars[0].v_type == VAR_STRING
20465 && argvars[0].vval.v_string != NULL
20466 && argvars[0].vval.v_string[0] == '$'
20467 && buf != NULL)
20468 return buf->b_ml.ml_line_count;
20469 return get_tv_number_chk(&argvars[0], NULL);
20470}
20471
20472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 * Get the string value of a variable.
20474 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020475 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20476 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 * If the String variable has never been set, return an empty string.
20478 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020479 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20480 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 */
20482 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020483get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485{
20486 static char_u mybuf[NUMBUFLEN];
20487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020488 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489}
20490
20491 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020492get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020496 char_u *res = get_tv_string_buf_chk(varp, buf);
20497
20498 return res != NULL ? res : (char_u *)"";
20499}
20500
Bram Moolenaar7d647822014-04-05 21:28:56 +020020501/*
20502 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20503 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020504 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020505get_tv_string_chk(varp)
20506 typval_T *varp;
20507{
20508 static char_u mybuf[NUMBUFLEN];
20509
20510 return get_tv_string_buf_chk(varp, mybuf);
20511}
20512
20513 static char_u *
20514get_tv_string_buf_chk(varp, buf)
20515 typval_T *varp;
20516 char_u *buf;
20517{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020518 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020520 case VAR_NUMBER:
20521 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20522 return buf;
20523 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020524 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020525 break;
20526 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020527 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020528 break;
20529 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020530 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020531 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020532#ifdef FEAT_FLOAT
20533 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020534 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020535 break;
20536#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020537 case VAR_STRING:
20538 if (varp->vval.v_string != NULL)
20539 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020540 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020541 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020545 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546}
20547
20548/*
20549 * Find variable "name" in the list of variables.
20550 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020551 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020552 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020555 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020556find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020558 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020559 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020562 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563
Bram Moolenaara7043832005-01-21 11:56:39 +000020564 ht = find_var_ht(name, &varname);
20565 if (htp != NULL)
20566 *htp = ht;
20567 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020569 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570}
20571
20572/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020573 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020574 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020576 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020577find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020579 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020580 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020581 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020582{
Bram Moolenaar33570922005-01-25 22:26:29 +000020583 hashitem_T *hi;
20584
20585 if (*varname == NUL)
20586 {
20587 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020588 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020589 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020590 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 case 'g': return &globvars_var;
20592 case 'v': return &vimvars_var;
20593 case 'b': return &curbuf->b_bufvar;
20594 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020595#ifdef FEAT_WINDOWS
20596 case 't': return &curtab->tp_winvar;
20597#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020598 case 'l': return current_funccal == NULL
20599 ? NULL : &current_funccal->l_vars_var;
20600 case 'a': return current_funccal == NULL
20601 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020602 }
20603 return NULL;
20604 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020605
20606 hi = hash_find(ht, varname);
20607 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020608 {
20609 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020610 * worked find the variable again. Don't auto-load a script if it was
20611 * loaded already, otherwise it would be loaded every time when
20612 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020613 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020614 {
20615 /* Note: script_autoload() may make "hi" invalid. It must either
20616 * be obtained again or not used. */
20617 if (!script_autoload(varname, FALSE) || aborting())
20618 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020619 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020620 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020621 if (HASHITEM_EMPTY(hi))
20622 return NULL;
20623 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020624 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020625}
20626
20627/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020628 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020629 * Set "varname" to the start of name without ':'.
20630 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020631 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020632find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 char_u *name;
20634 char_u **varname;
20635{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020636 hashitem_T *hi;
20637
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 if (name[1] != ':')
20639 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020640 /* The name must not start with a colon or #. */
20641 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 return NULL;
20643 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020644
20645 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020646 hi = hash_find(&compat_hashtab, name);
20647 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020648 return &compat_hashtab;
20649
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020651 return &globvarht; /* global variable */
20652 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 }
20654 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020655 if (*name == 'g') /* global variable */
20656 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020657 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20658 */
20659 if (vim_strchr(name + 2, ':') != NULL
20660 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020661 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020663 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020665 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020666#ifdef FEAT_WINDOWS
20667 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020668 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020669#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020670 if (*name == 'v') /* v: variable */
20671 return &vimvarht;
20672 if (*name == 'a' && current_funccal != NULL) /* function argument */
20673 return &current_funccal->l_avars.dv_hashtab;
20674 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20675 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 if (*name == 's' /* script variable */
20677 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20678 return &SCRIPT_VARS(current_SID);
20679 return NULL;
20680}
20681
20682/*
20683 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020684 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 * Returns NULL when it doesn't exist.
20686 */
20687 char_u *
20688get_var_value(name)
20689 char_u *name;
20690{
Bram Moolenaar33570922005-01-25 22:26:29 +000020691 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020693 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 if (v == NULL)
20695 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020696 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697}
20698
20699/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020700 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 * sourcing this script and when executing functions defined in the script.
20702 */
20703 void
20704new_script_vars(id)
20705 scid_T id;
20706{
Bram Moolenaara7043832005-01-21 11:56:39 +000020707 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020708 hashtab_T *ht;
20709 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020710
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20712 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020713 /* Re-allocating ga_data means that an ht_array pointing to
20714 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020715 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020716 for (i = 1; i <= ga_scripts.ga_len; ++i)
20717 {
20718 ht = &SCRIPT_VARS(i);
20719 if (ht->ht_mask == HT_INIT_SIZE - 1)
20720 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020721 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020722 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020723 }
20724
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 while (ga_scripts.ga_len < id)
20726 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020727 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020728 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020729 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 }
20732 }
20733}
20734
20735/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020736 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20737 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 */
20739 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020740init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020741 dict_T *dict;
20742 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020743 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744{
Bram Moolenaar33570922005-01-25 22:26:29 +000020745 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020746 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020747 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020748 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020749 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 dict_var->di_tv.vval.v_dict = dict;
20751 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020752 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020753 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20754 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755}
20756
20757/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020758 * Unreference a dictionary initialized by init_var_dict().
20759 */
20760 void
20761unref_var_dict(dict)
20762 dict_T *dict;
20763{
20764 /* Now the dict needs to be freed if no one else is using it, go back to
20765 * normal reference counting. */
20766 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20767 dict_unref(dict);
20768}
20769
20770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 * Frees all allocated variables and the value they contain.
20773 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 */
20775 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020776vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020777 hashtab_T *ht;
20778{
20779 vars_clear_ext(ht, TRUE);
20780}
20781
20782/*
20783 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20784 */
20785 static void
20786vars_clear_ext(ht, free_val)
20787 hashtab_T *ht;
20788 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789{
Bram Moolenaara7043832005-01-21 11:56:39 +000020790 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020791 hashitem_T *hi;
20792 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793
Bram Moolenaar33570922005-01-25 22:26:29 +000020794 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020795 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020796 for (hi = ht->ht_array; todo > 0; ++hi)
20797 {
20798 if (!HASHITEM_EMPTY(hi))
20799 {
20800 --todo;
20801
Bram Moolenaar33570922005-01-25 22:26:29 +000020802 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020803 * ht_array might change then. hash_clear() takes care of it
20804 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020805 v = HI2DI(hi);
20806 if (free_val)
20807 clear_tv(&v->di_tv);
20808 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20809 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020810 }
20811 }
20812 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020813 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814}
20815
Bram Moolenaara7043832005-01-21 11:56:39 +000020816/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020817 * Delete a variable from hashtab "ht" at item "hi".
20818 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020821delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020822 hashtab_T *ht;
20823 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824{
Bram Moolenaar33570922005-01-25 22:26:29 +000020825 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020826
20827 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020828 clear_tv(&di->di_tv);
20829 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830}
20831
20832/*
20833 * List the value of one internal variable.
20834 */
20835 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020836list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020837 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020839 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020841 char_u *tofree;
20842 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020843 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020844
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020845 current_copyID += COPYID_INC;
20846 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020847 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020848 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020849 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850}
20851
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020853list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 char_u *prefix;
20855 char_u *name;
20856 int type;
20857 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020858 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859{
Bram Moolenaar31859182007-08-14 20:41:13 +000020860 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20861 msg_start();
20862 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020863 if (name != NULL) /* "a:" vars don't have a name stored */
20864 msg_puts(name);
20865 msg_putchar(' ');
20866 msg_advance(22);
20867 if (type == VAR_NUMBER)
20868 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020869 else if (type == VAR_FUNC)
20870 msg_putchar('*');
20871 else if (type == VAR_LIST)
20872 {
20873 msg_putchar('[');
20874 if (*string == '[')
20875 ++string;
20876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020877 else if (type == VAR_DICT)
20878 {
20879 msg_putchar('{');
20880 if (*string == '{')
20881 ++string;
20882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 else
20884 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020885
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020887
20888 if (type == VAR_FUNC)
20889 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020890 if (*first)
20891 {
20892 msg_clr_eos();
20893 *first = FALSE;
20894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895}
20896
20897/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 * If the variable already exists, the value is updated.
20900 * Otherwise the variable is created.
20901 */
20902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020903set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020905 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907{
Bram Moolenaar33570922005-01-25 22:26:29 +000020908 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020910 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020912 ht = find_var_ht(name, &varname);
20913 if (ht == NULL || *varname == NUL)
20914 {
20915 EMSG2(_(e_illvar), name);
20916 return;
20917 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020918 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020919
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020920 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20921 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020922
Bram Moolenaar33570922005-01-25 22:26:29 +000020923 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020925 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020926 if (var_check_ro(v->di_flags, name)
20927 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020928 return;
20929 if (v->di_tv.v_type != tv->v_type
20930 && !((v->di_tv.v_type == VAR_STRING
20931 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020932 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020933 || tv->v_type == VAR_NUMBER))
20934#ifdef FEAT_FLOAT
20935 && !((v->di_tv.v_type == VAR_NUMBER
20936 || v->di_tv.v_type == VAR_FLOAT)
20937 && (tv->v_type == VAR_NUMBER
20938 || tv->v_type == VAR_FLOAT))
20939#endif
20940 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020941 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020942 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020943 return;
20944 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020945
20946 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020947 * Handle setting internal v: variables separately: we don't change
20948 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020949 */
20950 if (ht == &vimvarht)
20951 {
20952 if (v->di_tv.v_type == VAR_STRING)
20953 {
20954 vim_free(v->di_tv.vval.v_string);
20955 if (copy || tv->v_type != VAR_STRING)
20956 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20957 else
20958 {
20959 /* Take over the string to avoid an extra alloc/free. */
20960 v->di_tv.vval.v_string = tv->vval.v_string;
20961 tv->vval.v_string = NULL;
20962 }
20963 }
20964 else if (v->di_tv.v_type != VAR_NUMBER)
20965 EMSG2(_(e_intern2), "set_var()");
20966 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020967 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020968 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020969 if (STRCMP(varname, "searchforward") == 0)
20970 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020971#ifdef FEAT_SEARCH_EXTRA
20972 else if (STRCMP(varname, "hlsearch") == 0)
20973 {
20974 no_hlsearch = !v->di_tv.vval.v_number;
20975 redraw_all_later(SOME_VALID);
20976 }
20977#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020978 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 return;
20980 }
20981
20982 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 }
20984 else /* add a new variable */
20985 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020986 /* Can't add "v:" variable. */
20987 if (ht == &vimvarht)
20988 {
20989 EMSG2(_(e_illvar), name);
20990 return;
20991 }
20992
Bram Moolenaar92124a32005-06-17 22:03:40 +000020993 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020994 if (!valid_varname(varname))
20995 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020996
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020997 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20998 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020999 if (v == NULL)
21000 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021001 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021004 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 return;
21006 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021007 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021009
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021010 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021011 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021012 else
21013 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021014 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021015 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018}
21019
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021020/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021021 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021022 * Also give an error message.
21023 */
21024 static int
21025var_check_ro(flags, name)
21026 int flags;
21027 char_u *name;
21028{
21029 if (flags & DI_FLAGS_RO)
21030 {
21031 EMSG2(_(e_readonlyvar), name);
21032 return TRUE;
21033 }
21034 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21035 {
21036 EMSG2(_(e_readonlysbx), name);
21037 return TRUE;
21038 }
21039 return FALSE;
21040}
21041
21042/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021043 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21044 * Also give an error message.
21045 */
21046 static int
21047var_check_fixed(flags, name)
21048 int flags;
21049 char_u *name;
21050{
21051 if (flags & DI_FLAGS_FIX)
21052 {
21053 EMSG2(_("E795: Cannot delete variable %s"), name);
21054 return TRUE;
21055 }
21056 return FALSE;
21057}
21058
21059/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021060 * Check if a funcref is assigned to a valid variable name.
21061 * Return TRUE and give an error if not.
21062 */
21063 static int
21064var_check_func_name(name, new_var)
21065 char_u *name; /* points to start of variable name */
21066 int new_var; /* TRUE when creating the variable */
21067{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021068 /* Allow for w: b: s: and t:. */
21069 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021070 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21071 ? name[2] : name[0]))
21072 {
21073 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21074 name);
21075 return TRUE;
21076 }
21077 /* Don't allow hiding a function. When "v" is not NULL we might be
21078 * assigning another function to the same var, the type is checked
21079 * below. */
21080 if (new_var && function_exists(name))
21081 {
21082 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21083 name);
21084 return TRUE;
21085 }
21086 return FALSE;
21087}
21088
21089/*
21090 * Check if a variable name is valid.
21091 * Return FALSE and give an error if not.
21092 */
21093 static int
21094valid_varname(varname)
21095 char_u *varname;
21096{
21097 char_u *p;
21098
21099 for (p = varname; *p != NUL; ++p)
21100 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21101 && *p != AUTOLOAD_CHAR)
21102 {
21103 EMSG2(_(e_illvar), varname);
21104 return FALSE;
21105 }
21106 return TRUE;
21107}
21108
21109/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021110 * Return TRUE if typeval "tv" is set to be locked (immutable).
21111 * Also give an error message, using "name".
21112 */
21113 static int
21114tv_check_lock(lock, name)
21115 int lock;
21116 char_u *name;
21117{
21118 if (lock & VAR_LOCKED)
21119 {
21120 EMSG2(_("E741: Value is locked: %s"),
21121 name == NULL ? (char_u *)_("Unknown") : name);
21122 return TRUE;
21123 }
21124 if (lock & VAR_FIXED)
21125 {
21126 EMSG2(_("E742: Cannot change value of %s"),
21127 name == NULL ? (char_u *)_("Unknown") : name);
21128 return TRUE;
21129 }
21130 return FALSE;
21131}
21132
21133/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021134 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021135 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021136 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021137 * It is OK for "from" and "to" to point to the same item. This is used to
21138 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021139 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021140 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021141copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 typval_T *from;
21143 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021145 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021146 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021147 switch (from->v_type)
21148 {
21149 case VAR_NUMBER:
21150 to->vval.v_number = from->vval.v_number;
21151 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021152#ifdef FEAT_FLOAT
21153 case VAR_FLOAT:
21154 to->vval.v_float = from->vval.v_float;
21155 break;
21156#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021157 case VAR_STRING:
21158 case VAR_FUNC:
21159 if (from->vval.v_string == NULL)
21160 to->vval.v_string = NULL;
21161 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021162 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021163 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021164 if (from->v_type == VAR_FUNC)
21165 func_ref(to->vval.v_string);
21166 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021167 break;
21168 case VAR_LIST:
21169 if (from->vval.v_list == NULL)
21170 to->vval.v_list = NULL;
21171 else
21172 {
21173 to->vval.v_list = from->vval.v_list;
21174 ++to->vval.v_list->lv_refcount;
21175 }
21176 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021177 case VAR_DICT:
21178 if (from->vval.v_dict == NULL)
21179 to->vval.v_dict = NULL;
21180 else
21181 {
21182 to->vval.v_dict = from->vval.v_dict;
21183 ++to->vval.v_dict->dv_refcount;
21184 }
21185 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021186 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021188 break;
21189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190}
21191
21192/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021193 * Make a copy of an item.
21194 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021195 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21196 * reference to an already copied list/dict can be used.
21197 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021198 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021199 static int
21200item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 typval_T *from;
21202 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021203 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021204 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021205{
21206 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021207 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021208
Bram Moolenaar33570922005-01-25 22:26:29 +000021209 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021210 {
21211 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021212 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021213 }
21214 ++recurse;
21215
21216 switch (from->v_type)
21217 {
21218 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021219#ifdef FEAT_FLOAT
21220 case VAR_FLOAT:
21221#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021222 case VAR_STRING:
21223 case VAR_FUNC:
21224 copy_tv(from, to);
21225 break;
21226 case VAR_LIST:
21227 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021228 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021229 if (from->vval.v_list == NULL)
21230 to->vval.v_list = NULL;
21231 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21232 {
21233 /* use the copy made earlier */
21234 to->vval.v_list = from->vval.v_list->lv_copylist;
21235 ++to->vval.v_list->lv_refcount;
21236 }
21237 else
21238 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21239 if (to->vval.v_list == NULL)
21240 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021241 break;
21242 case VAR_DICT:
21243 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021244 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021245 if (from->vval.v_dict == NULL)
21246 to->vval.v_dict = NULL;
21247 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21248 {
21249 /* use the copy made earlier */
21250 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21251 ++to->vval.v_dict->dv_refcount;
21252 }
21253 else
21254 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21255 if (to->vval.v_dict == NULL)
21256 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021257 break;
21258 default:
21259 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021260 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021261 }
21262 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021263 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021264}
21265
21266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 * ":echo expr1 ..." print each argument separated with a space, add a
21268 * newline at the end.
21269 * ":echon expr1 ..." print each argument plain.
21270 */
21271 void
21272ex_echo(eap)
21273 exarg_T *eap;
21274{
21275 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021277 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 char_u *p;
21279 int needclr = TRUE;
21280 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021281 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282
21283 if (eap->skip)
21284 ++emsg_skip;
21285 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21286 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021287 /* If eval1() causes an error message the text from the command may
21288 * still need to be cleared. E.g., "echo 22,44". */
21289 need_clr_eos = needclr;
21290
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021292 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 {
21294 /*
21295 * Report the invalid expression unless the expression evaluation
21296 * has been cancelled due to an aborting error, an interrupt, or an
21297 * exception.
21298 */
21299 if (!aborting())
21300 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021301 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 break;
21303 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021304 need_clr_eos = FALSE;
21305
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 if (!eap->skip)
21307 {
21308 if (atstart)
21309 {
21310 atstart = FALSE;
21311 /* Call msg_start() after eval1(), evaluating the expression
21312 * may cause a message to appear. */
21313 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021314 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021315 /* Mark the saved text as finishing the line, so that what
21316 * follows is displayed on a new line when scrolling back
21317 * at the more prompt. */
21318 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321 }
21322 else if (eap->cmdidx == CMD_echo)
21323 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021324 current_copyID += COPYID_INC;
21325 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021326 if (p != NULL)
21327 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021329 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021331 if (*p != TAB && needclr)
21332 {
21333 /* remove any text still there from the command */
21334 msg_clr_eos();
21335 needclr = FALSE;
21336 }
21337 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 }
21339 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021340 {
21341#ifdef FEAT_MBYTE
21342 if (has_mbyte)
21343 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021344 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021345
21346 (void)msg_outtrans_len_attr(p, i, echo_attr);
21347 p += i - 1;
21348 }
21349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021351 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021354 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021356 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 arg = skipwhite(arg);
21358 }
21359 eap->nextcmd = check_nextcmd(arg);
21360
21361 if (eap->skip)
21362 --emsg_skip;
21363 else
21364 {
21365 /* remove text that may still be there from the command */
21366 if (needclr)
21367 msg_clr_eos();
21368 if (eap->cmdidx == CMD_echo)
21369 msg_end();
21370 }
21371}
21372
21373/*
21374 * ":echohl {name}".
21375 */
21376 void
21377ex_echohl(eap)
21378 exarg_T *eap;
21379{
21380 int id;
21381
21382 id = syn_name2id(eap->arg);
21383 if (id == 0)
21384 echo_attr = 0;
21385 else
21386 echo_attr = syn_id2attr(id);
21387}
21388
21389/*
21390 * ":execute expr1 ..." execute the result of an expression.
21391 * ":echomsg expr1 ..." Print a message
21392 * ":echoerr expr1 ..." Print an error
21393 * Each gets spaces around each argument and a newline at the end for
21394 * echo commands
21395 */
21396 void
21397ex_execute(eap)
21398 exarg_T *eap;
21399{
21400 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021401 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 int ret = OK;
21403 char_u *p;
21404 garray_T ga;
21405 int len;
21406 int save_did_emsg;
21407
21408 ga_init2(&ga, 1, 80);
21409
21410 if (eap->skip)
21411 ++emsg_skip;
21412 while (*arg != NUL && *arg != '|' && *arg != '\n')
21413 {
21414 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021415 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 {
21417 /*
21418 * Report the invalid expression unless the expression evaluation
21419 * has been cancelled due to an aborting error, an interrupt, or an
21420 * exception.
21421 */
21422 if (!aborting())
21423 EMSG2(_(e_invexpr2), p);
21424 ret = FAIL;
21425 break;
21426 }
21427
21428 if (!eap->skip)
21429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 len = (int)STRLEN(p);
21432 if (ga_grow(&ga, len + 2) == FAIL)
21433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 ret = FAIL;
21436 break;
21437 }
21438 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 ga.ga_len += len;
21442 }
21443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445 arg = skipwhite(arg);
21446 }
21447
21448 if (ret != FAIL && ga.ga_data != NULL)
21449 {
21450 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021451 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021453 out_flush();
21454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 else if (eap->cmdidx == CMD_echoerr)
21456 {
21457 /* We don't want to abort following commands, restore did_emsg. */
21458 save_did_emsg = did_emsg;
21459 EMSG((char_u *)ga.ga_data);
21460 if (!force_abort)
21461 did_emsg = save_did_emsg;
21462 }
21463 else if (eap->cmdidx == CMD_execute)
21464 do_cmdline((char_u *)ga.ga_data,
21465 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21466 }
21467
21468 ga_clear(&ga);
21469
21470 if (eap->skip)
21471 --emsg_skip;
21472
21473 eap->nextcmd = check_nextcmd(arg);
21474}
21475
21476/*
21477 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21478 * "arg" points to the "&" or '+' when called, to "option" when returning.
21479 * Returns NULL when no option name found. Otherwise pointer to the char
21480 * after the option name.
21481 */
21482 static char_u *
21483find_option_end(arg, opt_flags)
21484 char_u **arg;
21485 int *opt_flags;
21486{
21487 char_u *p = *arg;
21488
21489 ++p;
21490 if (*p == 'g' && p[1] == ':')
21491 {
21492 *opt_flags = OPT_GLOBAL;
21493 p += 2;
21494 }
21495 else if (*p == 'l' && p[1] == ':')
21496 {
21497 *opt_flags = OPT_LOCAL;
21498 p += 2;
21499 }
21500 else
21501 *opt_flags = 0;
21502
21503 if (!ASCII_ISALPHA(*p))
21504 return NULL;
21505 *arg = p;
21506
21507 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21508 p += 4; /* termcap option */
21509 else
21510 while (ASCII_ISALPHA(*p))
21511 ++p;
21512 return p;
21513}
21514
21515/*
21516 * ":function"
21517 */
21518 void
21519ex_function(eap)
21520 exarg_T *eap;
21521{
21522 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021523 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 int j;
21525 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021527 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 char_u *name = NULL;
21529 char_u *p;
21530 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021531 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 garray_T newargs;
21533 garray_T newlines;
21534 int varargs = FALSE;
21535 int mustend = FALSE;
21536 int flags = 0;
21537 ufunc_T *fp;
21538 int indent;
21539 int nesting;
21540 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021541 dictitem_T *v;
21542 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021543 static int func_nr = 0; /* number for nameless function */
21544 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021545 hashtab_T *ht;
21546 int todo;
21547 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021548 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549
21550 /*
21551 * ":function" without argument: list functions.
21552 */
21553 if (ends_excmd(*eap->arg))
21554 {
21555 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021556 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021557 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021558 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021559 {
21560 if (!HASHITEM_EMPTY(hi))
21561 {
21562 --todo;
21563 fp = HI2UF(hi);
21564 if (!isdigit(*fp->uf_name))
21565 list_func_head(fp, FALSE);
21566 }
21567 }
21568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 eap->nextcmd = check_nextcmd(eap->arg);
21570 return;
21571 }
21572
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021573 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021574 * ":function /pat": list functions matching pattern.
21575 */
21576 if (*eap->arg == '/')
21577 {
21578 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21579 if (!eap->skip)
21580 {
21581 regmatch_T regmatch;
21582
21583 c = *p;
21584 *p = NUL;
21585 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21586 *p = c;
21587 if (regmatch.regprog != NULL)
21588 {
21589 regmatch.rm_ic = p_ic;
21590
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021591 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021592 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21593 {
21594 if (!HASHITEM_EMPTY(hi))
21595 {
21596 --todo;
21597 fp = HI2UF(hi);
21598 if (!isdigit(*fp->uf_name)
21599 && vim_regexec(&regmatch, fp->uf_name, 0))
21600 list_func_head(fp, FALSE);
21601 }
21602 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021603 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021604 }
21605 }
21606 if (*p == '/')
21607 ++p;
21608 eap->nextcmd = check_nextcmd(p);
21609 return;
21610 }
21611
21612 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021613 * Get the function name. There are these situations:
21614 * func normal function name
21615 * "name" == func, "fudi.fd_dict" == NULL
21616 * dict.func new dictionary entry
21617 * "name" == NULL, "fudi.fd_dict" set,
21618 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21619 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021620 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021621 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21622 * dict.func existing dict entry that's not a Funcref
21623 * "name" == NULL, "fudi.fd_dict" set,
21624 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021625 * s:func script-local function name
21626 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629 name = trans_function_name(&p, eap->skip, 0, &fudi);
21630 paren = (vim_strchr(p, '(') != NULL);
21631 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 {
21633 /*
21634 * Return on an invalid expression in braces, unless the expression
21635 * evaluation has been cancelled due to an aborting error, an
21636 * interrupt, or an exception.
21637 */
21638 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021640 if (!eap->skip && fudi.fd_newkey != NULL)
21641 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 else
21646 eap->skip = TRUE;
21647 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021648
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 /* An error in a function call during evaluation of an expression in magic
21650 * braces should not cause the function not to be defined. */
21651 saved_did_emsg = did_emsg;
21652 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653
21654 /*
21655 * ":function func" with only function name: list function.
21656 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021657 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 {
21659 if (!ends_excmd(*skipwhite(p)))
21660 {
21661 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021662 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 }
21664 eap->nextcmd = check_nextcmd(p);
21665 if (eap->nextcmd != NULL)
21666 *p = NUL;
21667 if (!eap->skip && !got_int)
21668 {
21669 fp = find_func(name);
21670 if (fp != NULL)
21671 {
21672 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021673 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021675 if (FUNCLINE(fp, j) == NULL)
21676 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 msg_putchar('\n');
21678 msg_outnum((long)(j + 1));
21679 if (j < 9)
21680 msg_putchar(' ');
21681 if (j < 99)
21682 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021683 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 out_flush(); /* show a line at a time */
21685 ui_breakcheck();
21686 }
21687 if (!got_int)
21688 {
21689 msg_putchar('\n');
21690 msg_puts((char_u *)" endfunction");
21691 }
21692 }
21693 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021694 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021696 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 }
21698
21699 /*
21700 * ":function name(arg1, arg2)" Define function.
21701 */
21702 p = skipwhite(p);
21703 if (*p != '(')
21704 {
21705 if (!eap->skip)
21706 {
21707 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 }
21710 /* attempt to continue by skipping some text */
21711 if (vim_strchr(p, '(') != NULL)
21712 p = vim_strchr(p, '(');
21713 }
21714 p = skipwhite(p + 1);
21715
21716 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21717 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21718
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021719 if (!eap->skip)
21720 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021721 /* Check the name of the function. Unless it's a dictionary function
21722 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021723 if (name != NULL)
21724 arg = name;
21725 else
21726 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021727 if (arg != NULL && (fudi.fd_di == NULL
21728 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021729 {
21730 if (*arg == K_SPECIAL)
21731 j = 3;
21732 else
21733 j = 0;
21734 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21735 : eval_isnamec(arg[j])))
21736 ++j;
21737 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021738 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021739 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021740 /* Disallow using the g: dict. */
21741 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21742 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021743 }
21744
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 /*
21746 * Isolate the arguments: "arg1, arg2, ...)"
21747 */
21748 while (*p != ')')
21749 {
21750 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21751 {
21752 varargs = TRUE;
21753 p += 3;
21754 mustend = TRUE;
21755 }
21756 else
21757 {
21758 arg = p;
21759 while (ASCII_ISALNUM(*p) || *p == '_')
21760 ++p;
21761 if (arg == p || isdigit(*arg)
21762 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21763 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21764 {
21765 if (!eap->skip)
21766 EMSG2(_("E125: Illegal argument: %s"), arg);
21767 break;
21768 }
21769 if (ga_grow(&newargs, 1) == FAIL)
21770 goto erret;
21771 c = *p;
21772 *p = NUL;
21773 arg = vim_strsave(arg);
21774 if (arg == NULL)
21775 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021776
21777 /* Check for duplicate argument name. */
21778 for (i = 0; i < newargs.ga_len; ++i)
21779 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21780 {
21781 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021782 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021783 goto erret;
21784 }
21785
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21787 *p = c;
21788 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 if (*p == ',')
21790 ++p;
21791 else
21792 mustend = TRUE;
21793 }
21794 p = skipwhite(p);
21795 if (mustend && *p != ')')
21796 {
21797 if (!eap->skip)
21798 EMSG2(_(e_invarg2), eap->arg);
21799 break;
21800 }
21801 }
21802 ++p; /* skip the ')' */
21803
Bram Moolenaare9a41262005-01-15 22:18:47 +000021804 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 for (;;)
21806 {
21807 p = skipwhite(p);
21808 if (STRNCMP(p, "range", 5) == 0)
21809 {
21810 flags |= FC_RANGE;
21811 p += 5;
21812 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021813 else if (STRNCMP(p, "dict", 4) == 0)
21814 {
21815 flags |= FC_DICT;
21816 p += 4;
21817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 else if (STRNCMP(p, "abort", 5) == 0)
21819 {
21820 flags |= FC_ABORT;
21821 p += 5;
21822 }
21823 else
21824 break;
21825 }
21826
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021827 /* When there is a line break use what follows for the function body.
21828 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21829 if (*p == '\n')
21830 line_arg = p + 1;
21831 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 EMSG(_(e_trailing));
21833
21834 /*
21835 * Read the body of the function, until ":endfunction" is found.
21836 */
21837 if (KeyTyped)
21838 {
21839 /* Check if the function already exists, don't let the user type the
21840 * whole function before telling him it doesn't work! For a script we
21841 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021842 if (!eap->skip && !eap->forceit)
21843 {
21844 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21845 EMSG(_(e_funcdict));
21846 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021847 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021850 if (!eap->skip && did_emsg)
21851 goto erret;
21852
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 msg_putchar('\n'); /* don't overwrite the function name */
21854 cmdline_row = msg_row;
21855 }
21856
21857 indent = 2;
21858 nesting = 0;
21859 for (;;)
21860 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021861 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021862 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021863 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021864 saved_wait_return = FALSE;
21865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021867 sourcing_lnum_off = sourcing_lnum;
21868
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021869 if (line_arg != NULL)
21870 {
21871 /* Use eap->arg, split up in parts by line breaks. */
21872 theline = line_arg;
21873 p = vim_strchr(theline, '\n');
21874 if (p == NULL)
21875 line_arg += STRLEN(line_arg);
21876 else
21877 {
21878 *p = NUL;
21879 line_arg = p + 1;
21880 }
21881 }
21882 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 theline = getcmdline(':', 0L, indent);
21884 else
21885 theline = eap->getline(':', eap->cookie, indent);
21886 if (KeyTyped)
21887 lines_left = Rows - 1;
21888 if (theline == NULL)
21889 {
21890 EMSG(_("E126: Missing :endfunction"));
21891 goto erret;
21892 }
21893
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021894 /* Detect line continuation: sourcing_lnum increased more than one. */
21895 if (sourcing_lnum > sourcing_lnum_off + 1)
21896 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21897 else
21898 sourcing_lnum_off = 0;
21899
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 if (skip_until != NULL)
21901 {
21902 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21903 * don't check for ":endfunc". */
21904 if (STRCMP(theline, skip_until) == 0)
21905 {
21906 vim_free(skip_until);
21907 skip_until = NULL;
21908 }
21909 }
21910 else
21911 {
21912 /* skip ':' and blanks*/
21913 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21914 ;
21915
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021916 /* Check for "endfunction". */
21917 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021919 if (line_arg == NULL)
21920 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 break;
21922 }
21923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021924 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 * at "end". */
21926 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21927 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021928 else if (STRNCMP(p, "if", 2) == 0
21929 || STRNCMP(p, "wh", 2) == 0
21930 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 || STRNCMP(p, "try", 3) == 0)
21932 indent += 2;
21933
21934 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021935 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021937 if (*p == '!')
21938 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 p += eval_fname_script(p);
21940 if (ASCII_ISALPHA(*p))
21941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021942 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 if (*skipwhite(p) == '(')
21944 {
21945 ++nesting;
21946 indent += 2;
21947 }
21948 }
21949 }
21950
21951 /* Check for ":append" or ":insert". */
21952 p = skip_range(p, NULL);
21953 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21954 || (p[0] == 'i'
21955 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21956 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21957 skip_until = vim_strsave((char_u *)".");
21958
21959 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21960 arg = skipwhite(skiptowhite(p));
21961 if (arg[0] == '<' && arg[1] =='<'
21962 && ((p[0] == 'p' && p[1] == 'y'
21963 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21964 || (p[0] == 'p' && p[1] == 'e'
21965 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21966 || (p[0] == 't' && p[1] == 'c'
21967 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021968 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21969 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21971 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021972 || (p[0] == 'm' && p[1] == 'z'
21973 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 ))
21975 {
21976 /* ":python <<" continues until a dot, like ":append" */
21977 p = skipwhite(arg + 2);
21978 if (*p == NUL)
21979 skip_until = vim_strsave((char_u *)".");
21980 else
21981 skip_until = vim_strsave(p);
21982 }
21983 }
21984
21985 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021986 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021987 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021988 if (line_arg == NULL)
21989 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021991 }
21992
21993 /* Copy the line to newly allocated memory. get_one_sourceline()
21994 * allocates 250 bytes per line, this saves 80% on average. The cost
21995 * is an extra alloc/free. */
21996 p = vim_strsave(theline);
21997 if (p != NULL)
21998 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021999 if (line_arg == NULL)
22000 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022001 theline = p;
22002 }
22003
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022004 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22005
22006 /* Add NULL lines for continuation lines, so that the line count is
22007 * equal to the index in the growarray. */
22008 while (sourcing_lnum_off-- > 0)
22009 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022010
22011 /* Check for end of eap->arg. */
22012 if (line_arg != NULL && *line_arg == NUL)
22013 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 }
22015
22016 /* Don't define the function when skipping commands or when an error was
22017 * detected. */
22018 if (eap->skip || did_emsg)
22019 goto erret;
22020
22021 /*
22022 * If there are no errors, add the function
22023 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022024 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022025 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022026 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022028 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022029 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022030 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022031 goto erret;
22032 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022034 fp = find_func(name);
22035 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022037 if (!eap->forceit)
22038 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022039 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022040 goto erret;
22041 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022042 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022043 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022044 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022045 name);
22046 goto erret;
22047 }
22048 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022049 ga_clear_strings(&(fp->uf_args));
22050 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022051 vim_free(name);
22052 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 }
22055 else
22056 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022057 char numbuf[20];
22058
22059 fp = NULL;
22060 if (fudi.fd_newkey == NULL && !eap->forceit)
22061 {
22062 EMSG(_(e_funcdict));
22063 goto erret;
22064 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022065 if (fudi.fd_di == NULL)
22066 {
22067 /* Can't add a function to a locked dictionary */
22068 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22069 goto erret;
22070 }
22071 /* Can't change an existing function if it is locked */
22072 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22073 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022074
22075 /* Give the function a sequential number. Can only be used with a
22076 * Funcref! */
22077 vim_free(name);
22078 sprintf(numbuf, "%d", ++func_nr);
22079 name = vim_strsave((char_u *)numbuf);
22080 if (name == NULL)
22081 goto erret;
22082 }
22083
22084 if (fp == NULL)
22085 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022086 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022087 {
22088 int slen, plen;
22089 char_u *scriptname;
22090
22091 /* Check that the autoload name matches the script name. */
22092 j = FAIL;
22093 if (sourcing_name != NULL)
22094 {
22095 scriptname = autoload_name(name);
22096 if (scriptname != NULL)
22097 {
22098 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022099 plen = (int)STRLEN(p);
22100 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022101 if (slen > plen && fnamecmp(p,
22102 sourcing_name + slen - plen) == 0)
22103 j = OK;
22104 vim_free(scriptname);
22105 }
22106 }
22107 if (j == FAIL)
22108 {
22109 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22110 goto erret;
22111 }
22112 }
22113
22114 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 if (fp == NULL)
22116 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022117
22118 if (fudi.fd_dict != NULL)
22119 {
22120 if (fudi.fd_di == NULL)
22121 {
22122 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022123 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022124 if (fudi.fd_di == NULL)
22125 {
22126 vim_free(fp);
22127 goto erret;
22128 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022129 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22130 {
22131 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022132 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022133 goto erret;
22134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022135 }
22136 else
22137 /* overwrite existing dict entry */
22138 clear_tv(&fudi.fd_di->di_tv);
22139 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022140 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022141 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022142 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022143
22144 /* behave like "dict" was used */
22145 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022146 }
22147
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022149 STRCPY(fp->uf_name, name);
22150 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022152 fp->uf_args = newargs;
22153 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022154#ifdef FEAT_PROFILE
22155 fp->uf_tml_count = NULL;
22156 fp->uf_tml_total = NULL;
22157 fp->uf_tml_self = NULL;
22158 fp->uf_profiling = FALSE;
22159 if (prof_def_func())
22160 func_do_profile(fp);
22161#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022162 fp->uf_varargs = varargs;
22163 fp->uf_flags = flags;
22164 fp->uf_calls = 0;
22165 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022166 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167
22168erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 ga_clear_strings(&newargs);
22170 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022171ret_free:
22172 vim_free(skip_until);
22173 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022176 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177}
22178
22179/*
22180 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022181 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022183 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022184 * TFN_INT: internal function name OK
22185 * TFN_QUIET: be quiet
22186 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 * Advances "pp" to just after the function name (if no error).
22188 */
22189 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022190trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 char_u **pp;
22192 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022193 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022194 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022196 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 char_u *start;
22198 char_u *end;
22199 int lead;
22200 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022202 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022203
22204 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022205 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022207
22208 /* Check for hard coded <SNR>: already translated function ID (from a user
22209 * command). */
22210 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22211 && (*pp)[2] == (int)KE_SNR)
22212 {
22213 *pp += 3;
22214 len = get_id_len(pp) + 3;
22215 return vim_strnsave(start, len);
22216 }
22217
22218 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22219 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022221 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022223
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022224 /* Note that TFN_ flags use the same values as GLV_ flags. */
22225 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022226 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022227 if (end == start)
22228 {
22229 if (!skip)
22230 EMSG(_("E129: Function name required"));
22231 goto theend;
22232 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022233 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022234 {
22235 /*
22236 * Report an invalid expression in braces, unless the expression
22237 * evaluation has been cancelled due to an aborting error, an
22238 * interrupt, or an exception.
22239 */
22240 if (!aborting())
22241 {
22242 if (end != NULL)
22243 EMSG2(_(e_invarg2), start);
22244 }
22245 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022246 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022247 goto theend;
22248 }
22249
22250 if (lv.ll_tv != NULL)
22251 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022252 if (fdp != NULL)
22253 {
22254 fdp->fd_dict = lv.ll_dict;
22255 fdp->fd_newkey = lv.ll_newkey;
22256 lv.ll_newkey = NULL;
22257 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022258 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022259 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22260 {
22261 name = vim_strsave(lv.ll_tv->vval.v_string);
22262 *pp = end;
22263 }
22264 else
22265 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022266 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22267 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022268 EMSG(_(e_funcref));
22269 else
22270 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022271 name = NULL;
22272 }
22273 goto theend;
22274 }
22275
22276 if (lv.ll_name == NULL)
22277 {
22278 /* Error found, but continue after the function name. */
22279 *pp = end;
22280 goto theend;
22281 }
22282
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022283 /* Check if the name is a Funcref. If so, use the value. */
22284 if (lv.ll_exp_name != NULL)
22285 {
22286 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022287 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022288 if (name == lv.ll_exp_name)
22289 name = NULL;
22290 }
22291 else
22292 {
22293 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022294 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022295 if (name == *pp)
22296 name = NULL;
22297 }
22298 if (name != NULL)
22299 {
22300 name = vim_strsave(name);
22301 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022302 if (STRNCMP(name, "<SNR>", 5) == 0)
22303 {
22304 /* Change "<SNR>" to the byte sequence. */
22305 name[0] = K_SPECIAL;
22306 name[1] = KS_EXTRA;
22307 name[2] = (int)KE_SNR;
22308 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22309 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022310 goto theend;
22311 }
22312
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022313 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022314 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022315 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022316 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22317 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22318 {
22319 /* When there was "s:" already or the name expanded to get a
22320 * leading "s:" then remove it. */
22321 lv.ll_name += 2;
22322 len -= 2;
22323 lead = 2;
22324 }
22325 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022326 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022327 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022328 /* skip over "s:" and "g:" */
22329 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022330 lv.ll_name += 2;
22331 len = (int)(end - lv.ll_name);
22332 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022333
22334 /*
22335 * Copy the function name to allocated memory.
22336 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22337 * Accept <SNR>123_name() outside a script.
22338 */
22339 if (skip)
22340 lead = 0; /* do nothing */
22341 else if (lead > 0)
22342 {
22343 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022344 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22345 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022346 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022347 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022348 if (current_SID <= 0)
22349 {
22350 EMSG(_(e_usingsid));
22351 goto theend;
22352 }
22353 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22354 lead += (int)STRLEN(sid_buf);
22355 }
22356 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022357 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022358 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022359 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022360 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022361 goto theend;
22362 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022363 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022364 {
22365 char_u *cp = vim_strchr(lv.ll_name, ':');
22366
22367 if (cp != NULL && cp < end)
22368 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022369 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022370 goto theend;
22371 }
22372 }
22373
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022374 name = alloc((unsigned)(len + lead + 1));
22375 if (name != NULL)
22376 {
22377 if (lead > 0)
22378 {
22379 name[0] = K_SPECIAL;
22380 name[1] = KS_EXTRA;
22381 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022382 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022383 STRCPY(name + 3, sid_buf);
22384 }
22385 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022386 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022387 }
22388 *pp = end;
22389
22390theend:
22391 clear_lval(&lv);
22392 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393}
22394
22395/*
22396 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22397 * Return 2 if "p" starts with "s:".
22398 * Return 0 otherwise.
22399 */
22400 static int
22401eval_fname_script(p)
22402 char_u *p;
22403{
22404 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22405 || STRNICMP(p + 1, "SNR>", 4) == 0))
22406 return 5;
22407 if (p[0] == 's' && p[1] == ':')
22408 return 2;
22409 return 0;
22410}
22411
22412/*
22413 * Return TRUE if "p" starts with "<SID>" or "s:".
22414 * Only works if eval_fname_script() returned non-zero for "p"!
22415 */
22416 static int
22417eval_fname_sid(p)
22418 char_u *p;
22419{
22420 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22421}
22422
22423/*
22424 * List the head of the function: "name(arg1, arg2)".
22425 */
22426 static void
22427list_func_head(fp, indent)
22428 ufunc_T *fp;
22429 int indent;
22430{
22431 int j;
22432
22433 msg_start();
22434 if (indent)
22435 MSG_PUTS(" ");
22436 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022437 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 {
22439 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022440 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441 }
22442 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022445 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 {
22447 if (j)
22448 MSG_PUTS(", ");
22449 msg_puts(FUNCARG(fp, j));
22450 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022451 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 {
22453 if (j)
22454 MSG_PUTS(", ");
22455 MSG_PUTS("...");
22456 }
22457 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022458 if (fp->uf_flags & FC_ABORT)
22459 MSG_PUTS(" abort");
22460 if (fp->uf_flags & FC_RANGE)
22461 MSG_PUTS(" range");
22462 if (fp->uf_flags & FC_DICT)
22463 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022464 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022465 if (p_verbose > 0)
22466 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467}
22468
22469/*
22470 * Find a function by name, return pointer to it in ufuncs.
22471 * Return NULL for unknown function.
22472 */
22473 static ufunc_T *
22474find_func(name)
22475 char_u *name;
22476{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022477 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022479 hi = hash_find(&func_hashtab, name);
22480 if (!HASHITEM_EMPTY(hi))
22481 return HI2UF(hi);
22482 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483}
22484
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022485#if defined(EXITFREE) || defined(PROTO)
22486 void
22487free_all_functions()
22488{
22489 hashitem_T *hi;
22490
22491 /* Need to start all over every time, because func_free() may change the
22492 * hash table. */
22493 while (func_hashtab.ht_used > 0)
22494 for (hi = func_hashtab.ht_array; ; ++hi)
22495 if (!HASHITEM_EMPTY(hi))
22496 {
22497 func_free(HI2UF(hi));
22498 break;
22499 }
22500}
22501#endif
22502
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022503 int
22504translated_function_exists(name)
22505 char_u *name;
22506{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022507 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022508 return find_internal_func(name) >= 0;
22509 return find_func(name) != NULL;
22510}
22511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022512/*
22513 * Return TRUE if a function "name" exists.
22514 */
22515 static int
22516function_exists(name)
22517 char_u *name;
22518{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022519 char_u *nm = name;
22520 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022521 int n = FALSE;
22522
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022523 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22524 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022525 nm = skipwhite(nm);
22526
22527 /* Only accept "funcname", "funcname ", "funcname (..." and
22528 * "funcname(...", not "funcname!...". */
22529 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022530 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022531 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022532 return n;
22533}
22534
Bram Moolenaara1544c02013-05-30 12:35:52 +020022535 char_u *
22536get_expanded_name(name, check)
22537 char_u *name;
22538 int check;
22539{
22540 char_u *nm = name;
22541 char_u *p;
22542
22543 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22544
22545 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022546 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022547 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022548
Bram Moolenaara1544c02013-05-30 12:35:52 +020022549 vim_free(p);
22550 return NULL;
22551}
22552
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022553/*
22554 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022555 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22556 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022557 */
22558 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022559builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022560 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022561 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022562{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022563 char_u *p;
22564
22565 if (!ASCII_ISLOWER(name[0]))
22566 return FALSE;
22567 p = vim_strchr(name, AUTOLOAD_CHAR);
22568 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022569}
22570
Bram Moolenaar05159a02005-02-26 23:04:13 +000022571#if defined(FEAT_PROFILE) || defined(PROTO)
22572/*
22573 * Start profiling function "fp".
22574 */
22575 static void
22576func_do_profile(fp)
22577 ufunc_T *fp;
22578{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022579 int len = fp->uf_lines.ga_len;
22580
22581 if (len == 0)
22582 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022583 fp->uf_tm_count = 0;
22584 profile_zero(&fp->uf_tm_self);
22585 profile_zero(&fp->uf_tm_total);
22586 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022587 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022588 if (fp->uf_tml_total == NULL)
22589 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022590 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022591 if (fp->uf_tml_self == NULL)
22592 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022593 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022594 fp->uf_tml_idx = -1;
22595 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22596 || fp->uf_tml_self == NULL)
22597 return; /* out of memory */
22598
22599 fp->uf_profiling = TRUE;
22600}
22601
22602/*
22603 * Dump the profiling results for all functions in file "fd".
22604 */
22605 void
22606func_dump_profile(fd)
22607 FILE *fd;
22608{
22609 hashitem_T *hi;
22610 int todo;
22611 ufunc_T *fp;
22612 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022613 ufunc_T **sorttab;
22614 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022615
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022616 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022617 if (todo == 0)
22618 return; /* nothing to dump */
22619
Bram Moolenaar73830342005-02-28 22:48:19 +000022620 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22621
Bram Moolenaar05159a02005-02-26 23:04:13 +000022622 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22623 {
22624 if (!HASHITEM_EMPTY(hi))
22625 {
22626 --todo;
22627 fp = HI2UF(hi);
22628 if (fp->uf_profiling)
22629 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022630 if (sorttab != NULL)
22631 sorttab[st_len++] = fp;
22632
Bram Moolenaar05159a02005-02-26 23:04:13 +000022633 if (fp->uf_name[0] == K_SPECIAL)
22634 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22635 else
22636 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22637 if (fp->uf_tm_count == 1)
22638 fprintf(fd, "Called 1 time\n");
22639 else
22640 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22641 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22642 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22643 fprintf(fd, "\n");
22644 fprintf(fd, "count total (s) self (s)\n");
22645
22646 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22647 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022648 if (FUNCLINE(fp, i) == NULL)
22649 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022650 prof_func_line(fd, fp->uf_tml_count[i],
22651 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022652 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22653 }
22654 fprintf(fd, "\n");
22655 }
22656 }
22657 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022658
22659 if (sorttab != NULL && st_len > 0)
22660 {
22661 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22662 prof_total_cmp);
22663 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22664 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22665 prof_self_cmp);
22666 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22667 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022668
22669 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022670}
Bram Moolenaar73830342005-02-28 22:48:19 +000022671
22672 static void
22673prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22674 FILE *fd;
22675 ufunc_T **sorttab;
22676 int st_len;
22677 char *title;
22678 int prefer_self; /* when equal print only self time */
22679{
22680 int i;
22681 ufunc_T *fp;
22682
22683 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22684 fprintf(fd, "count total (s) self (s) function\n");
22685 for (i = 0; i < 20 && i < st_len; ++i)
22686 {
22687 fp = sorttab[i];
22688 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22689 prefer_self);
22690 if (fp->uf_name[0] == K_SPECIAL)
22691 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22692 else
22693 fprintf(fd, " %s()\n", fp->uf_name);
22694 }
22695 fprintf(fd, "\n");
22696}
22697
22698/*
22699 * Print the count and times for one function or function line.
22700 */
22701 static void
22702prof_func_line(fd, count, total, self, prefer_self)
22703 FILE *fd;
22704 int count;
22705 proftime_T *total;
22706 proftime_T *self;
22707 int prefer_self; /* when equal print only self time */
22708{
22709 if (count > 0)
22710 {
22711 fprintf(fd, "%5d ", count);
22712 if (prefer_self && profile_equal(total, self))
22713 fprintf(fd, " ");
22714 else
22715 fprintf(fd, "%s ", profile_msg(total));
22716 if (!prefer_self && profile_equal(total, self))
22717 fprintf(fd, " ");
22718 else
22719 fprintf(fd, "%s ", profile_msg(self));
22720 }
22721 else
22722 fprintf(fd, " ");
22723}
22724
22725/*
22726 * Compare function for total time sorting.
22727 */
22728 static int
22729#ifdef __BORLANDC__
22730_RTLENTRYF
22731#endif
22732prof_total_cmp(s1, s2)
22733 const void *s1;
22734 const void *s2;
22735{
22736 ufunc_T *p1, *p2;
22737
22738 p1 = *(ufunc_T **)s1;
22739 p2 = *(ufunc_T **)s2;
22740 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22741}
22742
22743/*
22744 * Compare function for self time sorting.
22745 */
22746 static int
22747#ifdef __BORLANDC__
22748_RTLENTRYF
22749#endif
22750prof_self_cmp(s1, s2)
22751 const void *s1;
22752 const void *s2;
22753{
22754 ufunc_T *p1, *p2;
22755
22756 p1 = *(ufunc_T **)s1;
22757 p2 = *(ufunc_T **)s2;
22758 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22759}
22760
Bram Moolenaar05159a02005-02-26 23:04:13 +000022761#endif
22762
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022763/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022764 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022765 * Return TRUE if a package was loaded.
22766 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022767 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022768script_autoload(name, reload)
22769 char_u *name;
22770 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022771{
22772 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022773 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022774 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022775 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022776
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022777 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022778 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022779 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022780 return FALSE;
22781
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022782 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022783
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022784 /* Find the name in the list of previously loaded package names. Skip
22785 * "autoload/", it's always the same. */
22786 for (i = 0; i < ga_loaded.ga_len; ++i)
22787 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22788 break;
22789 if (!reload && i < ga_loaded.ga_len)
22790 ret = FALSE; /* was loaded already */
22791 else
22792 {
22793 /* Remember the name if it wasn't loaded already. */
22794 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22795 {
22796 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22797 tofree = NULL;
22798 }
22799
22800 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022801 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022802 ret = TRUE;
22803 }
22804
22805 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022806 return ret;
22807}
22808
22809/*
22810 * Return the autoload script name for a function or variable name.
22811 * Returns NULL when out of memory.
22812 */
22813 static char_u *
22814autoload_name(name)
22815 char_u *name;
22816{
22817 char_u *p;
22818 char_u *scriptname;
22819
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022820 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022821 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22822 if (scriptname == NULL)
22823 return FALSE;
22824 STRCPY(scriptname, "autoload/");
22825 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022826 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022827 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022828 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022829 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022830 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022831}
22832
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22834
22835/*
22836 * Function given to ExpandGeneric() to obtain the list of user defined
22837 * function names.
22838 */
22839 char_u *
22840get_user_func_name(xp, idx)
22841 expand_T *xp;
22842 int idx;
22843{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022844 static long_u done;
22845 static hashitem_T *hi;
22846 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847
22848 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022850 done = 0;
22851 hi = func_hashtab.ht_array;
22852 }
22853 if (done < func_hashtab.ht_used)
22854 {
22855 if (done++ > 0)
22856 ++hi;
22857 while (HASHITEM_EMPTY(hi))
22858 ++hi;
22859 fp = HI2UF(hi);
22860
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022861 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022862 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022863
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022864 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22865 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866
22867 cat_func_name(IObuff, fp);
22868 if (xp->xp_context != EXPAND_USER_FUNC)
22869 {
22870 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022871 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 STRCAT(IObuff, ")");
22873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 return IObuff;
22875 }
22876 return NULL;
22877}
22878
22879#endif /* FEAT_CMDL_COMPL */
22880
22881/*
22882 * Copy the function name of "fp" to buffer "buf".
22883 * "buf" must be able to hold the function name plus three bytes.
22884 * Takes care of script-local function names.
22885 */
22886 static void
22887cat_func_name(buf, fp)
22888 char_u *buf;
22889 ufunc_T *fp;
22890{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022891 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 {
22893 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022894 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 }
22896 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022897 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898}
22899
22900/*
22901 * ":delfunction {name}"
22902 */
22903 void
22904ex_delfunction(eap)
22905 exarg_T *eap;
22906{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022907 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 char_u *p;
22909 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911
22912 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022913 name = trans_function_name(&p, eap->skip, 0, &fudi);
22914 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022916 {
22917 if (fudi.fd_dict != NULL && !eap->skip)
22918 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 if (!ends_excmd(*skipwhite(p)))
22922 {
22923 vim_free(name);
22924 EMSG(_(e_trailing));
22925 return;
22926 }
22927 eap->nextcmd = check_nextcmd(p);
22928 if (eap->nextcmd != NULL)
22929 *p = NUL;
22930
22931 if (!eap->skip)
22932 fp = find_func(name);
22933 vim_free(name);
22934
22935 if (!eap->skip)
22936 {
22937 if (fp == NULL)
22938 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022939 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 return;
22941 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022942 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 {
22944 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22945 return;
22946 }
22947
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022948 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022950 /* Delete the dict item that refers to the function, it will
22951 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022952 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022954 else
22955 func_free(fp);
22956 }
22957}
22958
22959/*
22960 * Free a function and remove it from the list of functions.
22961 */
22962 static void
22963func_free(fp)
22964 ufunc_T *fp;
22965{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022966 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022967
22968 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022969 ga_clear_strings(&(fp->uf_args));
22970 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022971#ifdef FEAT_PROFILE
22972 vim_free(fp->uf_tml_count);
22973 vim_free(fp->uf_tml_total);
22974 vim_free(fp->uf_tml_self);
22975#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022976
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022977 /* remove the function from the function hashtable */
22978 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22979 if (HASHITEM_EMPTY(hi))
22980 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022981 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022982 hash_remove(&func_hashtab, hi);
22983
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022984 vim_free(fp);
22985}
22986
22987/*
22988 * Unreference a Function: decrement the reference count and free it when it
22989 * becomes zero. Only for numbered functions.
22990 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022991 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022992func_unref(name)
22993 char_u *name;
22994{
22995 ufunc_T *fp;
22996
22997 if (name != NULL && isdigit(*name))
22998 {
22999 fp = find_func(name);
23000 if (fp == NULL)
23001 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023002 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023003 {
23004 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023005 * when "uf_calls" becomes zero. */
23006 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023007 func_free(fp);
23008 }
23009 }
23010}
23011
23012/*
23013 * Count a reference to a Function.
23014 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023015 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023016func_ref(name)
23017 char_u *name;
23018{
23019 ufunc_T *fp;
23020
23021 if (name != NULL && isdigit(*name))
23022 {
23023 fp = find_func(name);
23024 if (fp == NULL)
23025 EMSG2(_(e_intern2), "func_ref()");
23026 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023027 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 }
23029}
23030
23031/*
23032 * Call a user function.
23033 */
23034 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023035call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036 ufunc_T *fp; /* pointer to function */
23037 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023038 typval_T *argvars; /* arguments */
23039 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 linenr_T firstline; /* first line of range */
23041 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043{
Bram Moolenaar33570922005-01-25 22:26:29 +000023044 char_u *save_sourcing_name;
23045 linenr_T save_sourcing_lnum;
23046 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023047 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023048 int save_did_emsg;
23049 static int depth = 0;
23050 dictitem_T *v;
23051 int fixvar_idx = 0; /* index in fixvar[] */
23052 int i;
23053 int ai;
23054 char_u numbuf[NUMBUFLEN];
23055 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023056#ifdef FEAT_PROFILE
23057 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023058 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060
23061 /* If depth of calling is getting too high, don't execute the function */
23062 if (depth >= p_mfd)
23063 {
23064 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023065 rettv->v_type = VAR_NUMBER;
23066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 return;
23068 }
23069 ++depth;
23070
23071 line_breakcheck(); /* check for CTRL-C hit */
23072
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023073 fc = (funccall_T *)alloc(sizeof(funccall_T));
23074 fc->caller = current_funccal;
23075 current_funccal = fc;
23076 fc->func = fp;
23077 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023078 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023079 fc->linenr = 0;
23080 fc->returned = FALSE;
23081 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023083 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23084 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085
Bram Moolenaar33570922005-01-25 22:26:29 +000023086 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023087 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023088 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23089 * each argument variable and saves a lot of time.
23090 */
23091 /*
23092 * Init l: variables.
23093 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023094 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023095 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023096 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023097 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23098 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023099 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023100 name = v->di_key;
23101 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023102 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023103 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023104 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023105 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023106 v->di_tv.vval.v_dict = selfdict;
23107 ++selfdict->dv_refcount;
23108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023109
Bram Moolenaar33570922005-01-25 22:26:29 +000023110 /*
23111 * Init a: variables.
23112 * Set a:0 to "argcount".
23113 * Set a:000 to a list with room for the "..." arguments.
23114 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023115 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023116 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023117 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023118 /* Use "name" to avoid a warning from some compiler that checks the
23119 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023120 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023121 name = v->di_key;
23122 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023124 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023125 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023126 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023127 v->di_tv.vval.v_list = &fc->l_varlist;
23128 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23129 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23130 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023131
23132 /*
23133 * Set a:firstline to "firstline" and a:lastline to "lastline".
23134 * Set a:name to named arguments.
23135 * Set a:N to the "..." arguments.
23136 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023137 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023139 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023140 (varnumber_T)lastline);
23141 for (i = 0; i < argcount; ++i)
23142 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023143 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023144 if (ai < 0)
23145 /* named argument a:name */
23146 name = FUNCARG(fp, i);
23147 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023148 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023149 /* "..." argument a:1, a:2, etc. */
23150 sprintf((char *)numbuf, "%d", ai + 1);
23151 name = numbuf;
23152 }
23153 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23154 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023155 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23157 }
23158 else
23159 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023160 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23161 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 if (v == NULL)
23163 break;
23164 v->di_flags = DI_FLAGS_RO;
23165 }
23166 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023167 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023168
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023169 /* Note: the values are copied directly to avoid alloc/free.
23170 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023171 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023172 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023173
23174 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23175 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023176 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23177 fc->l_listitems[ai].li_tv = argvars[i];
23178 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023179 }
23180 }
23181
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 /* Don't redraw while executing the function. */
23183 ++RedrawingDisabled;
23184 save_sourcing_name = sourcing_name;
23185 save_sourcing_lnum = sourcing_lnum;
23186 sourcing_lnum = 1;
23187 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023188 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 if (sourcing_name != NULL)
23190 {
23191 if (save_sourcing_name != NULL
23192 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23193 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23194 else
23195 STRCPY(sourcing_name, "function ");
23196 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23197
23198 if (p_verbose >= 12)
23199 {
23200 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023201 verbose_enter_scroll();
23202
Bram Moolenaar555b2802005-05-19 21:08:39 +000023203 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 if (p_verbose >= 14)
23205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023207 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023208 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023209 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210
23211 msg_puts((char_u *)"(");
23212 for (i = 0; i < argcount; ++i)
23213 {
23214 if (i > 0)
23215 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023216 if (argvars[i].v_type == VAR_NUMBER)
23217 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 else
23219 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023220 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23221 if (s != NULL)
23222 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023223 if (vim_strsize(s) > MSG_BUF_CLEN)
23224 {
23225 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23226 s = buf;
23227 }
23228 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023229 vim_free(tofree);
23230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 }
23232 }
23233 msg_puts((char_u *)")");
23234 }
23235 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023236
23237 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 --no_wait_return;
23239 }
23240 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023241#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023242 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023243 {
23244 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23245 func_do_profile(fp);
23246 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023247 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023248 {
23249 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023250 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023251 profile_zero(&fp->uf_tm_children);
23252 }
23253 script_prof_save(&wait_start);
23254 }
23255#endif
23256
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023258 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259 save_did_emsg = did_emsg;
23260 did_emsg = FALSE;
23261
23262 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023263 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23265
23266 --RedrawingDisabled;
23267
23268 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023269 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023271 clear_tv(rettv);
23272 rettv->v_type = VAR_NUMBER;
23273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 }
23275
Bram Moolenaar05159a02005-02-26 23:04:13 +000023276#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023277 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023278 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023280 profile_end(&call_start);
23281 profile_sub_wait(&wait_start, &call_start);
23282 profile_add(&fp->uf_tm_total, &call_start);
23283 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023284 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023285 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023286 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23287 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023288 }
23289 }
23290#endif
23291
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 /* when being verbose, mention the return value */
23293 if (p_verbose >= 12)
23294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023296 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023299 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023300 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023301 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023302 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023305 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023306 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023307 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023308 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023309
Bram Moolenaar555b2802005-05-19 21:08:39 +000023310 /* The value may be very long. Skip the middle part, so that we
23311 * have some idea how it starts and ends. smsg() would always
23312 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023313 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023314 if (s != NULL)
23315 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023316 if (vim_strsize(s) > MSG_BUF_CLEN)
23317 {
23318 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23319 s = buf;
23320 }
23321 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023322 vim_free(tofree);
23323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 }
23325 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023326
23327 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 --no_wait_return;
23329 }
23330
23331 vim_free(sourcing_name);
23332 sourcing_name = save_sourcing_name;
23333 sourcing_lnum = save_sourcing_lnum;
23334 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023335#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023336 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023337 script_prof_restore(&wait_start);
23338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339
23340 if (p_verbose >= 12 && sourcing_name != NULL)
23341 {
23342 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023343 verbose_enter_scroll();
23344
Bram Moolenaar555b2802005-05-19 21:08:39 +000023345 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023347
23348 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 --no_wait_return;
23350 }
23351
23352 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023353 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023355
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023356 /* If the a:000 list and the l: and a: dicts are not referenced we can
23357 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023358 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23359 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23360 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23361 {
23362 free_funccal(fc, FALSE);
23363 }
23364 else
23365 {
23366 hashitem_T *hi;
23367 listitem_T *li;
23368 int todo;
23369
23370 /* "fc" is still in use. This can happen when returning "a:000" or
23371 * assigning "l:" to a global variable.
23372 * Link "fc" in the list for garbage collection later. */
23373 fc->caller = previous_funccal;
23374 previous_funccal = fc;
23375
23376 /* Make a copy of the a: variables, since we didn't do that above. */
23377 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23378 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23379 {
23380 if (!HASHITEM_EMPTY(hi))
23381 {
23382 --todo;
23383 v = HI2DI(hi);
23384 copy_tv(&v->di_tv, &v->di_tv);
23385 }
23386 }
23387
23388 /* Make a copy of the a:000 items, since we didn't do that above. */
23389 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23390 copy_tv(&li->li_tv, &li->li_tv);
23391 }
23392}
23393
23394/*
23395 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023396 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023397 */
23398 static int
23399can_free_funccal(fc, copyID)
23400 funccall_T *fc;
23401 int copyID;
23402{
23403 return (fc->l_varlist.lv_copyID != copyID
23404 && fc->l_vars.dv_copyID != copyID
23405 && fc->l_avars.dv_copyID != copyID);
23406}
23407
23408/*
23409 * Free "fc" and what it contains.
23410 */
23411 static void
23412free_funccal(fc, free_val)
23413 funccall_T *fc;
23414 int free_val; /* a: vars were allocated */
23415{
23416 listitem_T *li;
23417
23418 /* The a: variables typevals may not have been allocated, only free the
23419 * allocated variables. */
23420 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23421
23422 /* free all l: variables */
23423 vars_clear(&fc->l_vars.dv_hashtab);
23424
23425 /* Free the a:000 variables if they were allocated. */
23426 if (free_val)
23427 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23428 clear_tv(&li->li_tv);
23429
23430 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431}
23432
23433/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 * Add a number variable "name" to dict "dp" with value "nr".
23435 */
23436 static void
23437add_nr_var(dp, v, name, nr)
23438 dict_T *dp;
23439 dictitem_T *v;
23440 char *name;
23441 varnumber_T nr;
23442{
23443 STRCPY(v->di_key, name);
23444 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23445 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23446 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023447 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023448 v->di_tv.vval.v_number = nr;
23449}
23450
23451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 * ":return [expr]"
23453 */
23454 void
23455ex_return(eap)
23456 exarg_T *eap;
23457{
23458 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023459 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 int returning = FALSE;
23461
23462 if (current_funccal == NULL)
23463 {
23464 EMSG(_("E133: :return not inside a function"));
23465 return;
23466 }
23467
23468 if (eap->skip)
23469 ++emsg_skip;
23470
23471 eap->nextcmd = NULL;
23472 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023473 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 {
23475 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023476 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023478 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 }
23480 /* It's safer to return also on error. */
23481 else if (!eap->skip)
23482 {
23483 /*
23484 * Return unless the expression evaluation has been cancelled due to an
23485 * aborting error, an interrupt, or an exception.
23486 */
23487 if (!aborting())
23488 returning = do_return(eap, FALSE, TRUE, NULL);
23489 }
23490
23491 /* When skipping or the return gets pending, advance to the next command
23492 * in this line (!returning). Otherwise, ignore the rest of the line.
23493 * Following lines will be ignored by get_func_line(). */
23494 if (returning)
23495 eap->nextcmd = NULL;
23496 else if (eap->nextcmd == NULL) /* no argument */
23497 eap->nextcmd = check_nextcmd(arg);
23498
23499 if (eap->skip)
23500 --emsg_skip;
23501}
23502
23503/*
23504 * Return from a function. Possibly makes the return pending. Also called
23505 * for a pending return at the ":endtry" or after returning from an extra
23506 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023508 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 * FALSE when the return gets pending.
23510 */
23511 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023512do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 exarg_T *eap;
23514 int reanimate;
23515 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023516 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517{
23518 int idx;
23519 struct condstack *cstack = eap->cstack;
23520
23521 if (reanimate)
23522 /* Undo the return. */
23523 current_funccal->returned = FALSE;
23524
23525 /*
23526 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23527 * not in its finally clause (which then is to be executed next) is found.
23528 * In this case, make the ":return" pending for execution at the ":endtry".
23529 * Otherwise, return normally.
23530 */
23531 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23532 if (idx >= 0)
23533 {
23534 cstack->cs_pending[idx] = CSTP_RETURN;
23535
23536 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023537 /* A pending return again gets pending. "rettv" points to an
23538 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023540 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 else
23542 {
23543 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023544 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023546 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023548 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 {
23550 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023551 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023552 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 else
23554 EMSG(_(e_outofmem));
23555 }
23556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023557 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558
23559 if (reanimate)
23560 {
23561 /* The pending return value could be overwritten by a ":return"
23562 * without argument in a finally clause; reset the default
23563 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023564 current_funccal->rettv->v_type = VAR_NUMBER;
23565 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 }
23567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023568 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 }
23570 else
23571 {
23572 current_funccal->returned = TRUE;
23573
23574 /* If the return is carried out now, store the return value. For
23575 * a return immediately after reanimation, the value is already
23576 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023577 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023579 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023580 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023582 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 }
23584 }
23585
23586 return idx < 0;
23587}
23588
23589/*
23590 * Free the variable with a pending return value.
23591 */
23592 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023593discard_pending_return(rettv)
23594 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595{
Bram Moolenaar33570922005-01-25 22:26:29 +000023596 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597}
23598
23599/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023600 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 * is an allocated string. Used by report_pending() for verbose messages.
23602 */
23603 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023604get_return_cmd(rettv)
23605 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023607 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023608 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023609 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023611 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023612 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023613 if (s == NULL)
23614 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023615
23616 STRCPY(IObuff, ":return ");
23617 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23618 if (STRLEN(s) + 8 >= IOSIZE)
23619 STRCPY(IObuff + IOSIZE - 4, "...");
23620 vim_free(tofree);
23621 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622}
23623
23624/*
23625 * Get next function line.
23626 * Called by do_cmdline() to get the next line.
23627 * Returns allocated string, or NULL for end of function.
23628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 char_u *
23630get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023631 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023633 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634{
Bram Moolenaar33570922005-01-25 22:26:29 +000023635 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023636 ufunc_T *fp = fcp->func;
23637 char_u *retval;
23638 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639
23640 /* If breakpoints have been added/deleted need to check for it. */
23641 if (fcp->dbg_tick != debug_tick)
23642 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023643 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 sourcing_lnum);
23645 fcp->dbg_tick = debug_tick;
23646 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023648 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023649 func_line_end(cookie);
23650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651
Bram Moolenaar05159a02005-02-26 23:04:13 +000023652 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023653 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23654 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 retval = NULL;
23656 else
23657 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023658 /* Skip NULL lines (continuation lines). */
23659 while (fcp->linenr < gap->ga_len
23660 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23661 ++fcp->linenr;
23662 if (fcp->linenr >= gap->ga_len)
23663 retval = NULL;
23664 else
23665 {
23666 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23667 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023668#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023669 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023670 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023671#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 }
23674
23675 /* Did we encounter a breakpoint? */
23676 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23677 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023678 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023680 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 sourcing_lnum);
23682 fcp->dbg_tick = debug_tick;
23683 }
23684
23685 return retval;
23686}
23687
Bram Moolenaar05159a02005-02-26 23:04:13 +000023688#if defined(FEAT_PROFILE) || defined(PROTO)
23689/*
23690 * Called when starting to read a function line.
23691 * "sourcing_lnum" must be correct!
23692 * When skipping lines it may not actually be executed, but we won't find out
23693 * until later and we need to store the time now.
23694 */
23695 void
23696func_line_start(cookie)
23697 void *cookie;
23698{
23699 funccall_T *fcp = (funccall_T *)cookie;
23700 ufunc_T *fp = fcp->func;
23701
23702 if (fp->uf_profiling && sourcing_lnum >= 1
23703 && sourcing_lnum <= fp->uf_lines.ga_len)
23704 {
23705 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023706 /* Skip continuation lines. */
23707 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23708 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023709 fp->uf_tml_execed = FALSE;
23710 profile_start(&fp->uf_tml_start);
23711 profile_zero(&fp->uf_tml_children);
23712 profile_get_wait(&fp->uf_tml_wait);
23713 }
23714}
23715
23716/*
23717 * Called when actually executing a function line.
23718 */
23719 void
23720func_line_exec(cookie)
23721 void *cookie;
23722{
23723 funccall_T *fcp = (funccall_T *)cookie;
23724 ufunc_T *fp = fcp->func;
23725
23726 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23727 fp->uf_tml_execed = TRUE;
23728}
23729
23730/*
23731 * Called when done with a function line.
23732 */
23733 void
23734func_line_end(cookie)
23735 void *cookie;
23736{
23737 funccall_T *fcp = (funccall_T *)cookie;
23738 ufunc_T *fp = fcp->func;
23739
23740 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23741 {
23742 if (fp->uf_tml_execed)
23743 {
23744 ++fp->uf_tml_count[fp->uf_tml_idx];
23745 profile_end(&fp->uf_tml_start);
23746 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023747 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023748 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23749 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023750 }
23751 fp->uf_tml_idx = -1;
23752 }
23753}
23754#endif
23755
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756/*
23757 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023758 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 */
23760 int
23761func_has_ended(cookie)
23762 void *cookie;
23763{
Bram Moolenaar33570922005-01-25 22:26:29 +000023764 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765
23766 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23767 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023768 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 || fcp->returned);
23770}
23771
23772/*
23773 * return TRUE if cookie indicates a function which "abort"s on errors.
23774 */
23775 int
23776func_has_abort(cookie)
23777 void *cookie;
23778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023779 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780}
23781
23782#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23783typedef enum
23784{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023785 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23786 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23787 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788} var_flavour_T;
23789
23790static var_flavour_T var_flavour __ARGS((char_u *varname));
23791
23792 static var_flavour_T
23793var_flavour(varname)
23794 char_u *varname;
23795{
23796 char_u *p = varname;
23797
23798 if (ASCII_ISUPPER(*p))
23799 {
23800 while (*(++p))
23801 if (ASCII_ISLOWER(*p))
23802 return VAR_FLAVOUR_SESSION;
23803 return VAR_FLAVOUR_VIMINFO;
23804 }
23805 else
23806 return VAR_FLAVOUR_DEFAULT;
23807}
23808#endif
23809
23810#if defined(FEAT_VIMINFO) || defined(PROTO)
23811/*
23812 * Restore global vars that start with a capital from the viminfo file
23813 */
23814 int
23815read_viminfo_varlist(virp, writing)
23816 vir_T *virp;
23817 int writing;
23818{
23819 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023820 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023821 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822
23823 if (!writing && (find_viminfo_parameter('!') != NULL))
23824 {
23825 tab = vim_strchr(virp->vir_line + 1, '\t');
23826 if (tab != NULL)
23827 {
23828 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023829 switch (*tab)
23830 {
23831 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023832#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023833 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023834#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023835 case 'D': type = VAR_DICT; break;
23836 case 'L': type = VAR_LIST; break;
23837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838
23839 tab = vim_strchr(tab, '\t');
23840 if (tab != NULL)
23841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023842 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023843 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023844 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023846#ifdef FEAT_FLOAT
23847 else if (type == VAR_FLOAT)
23848 (void)string2float(tab + 1, &tv.vval.v_float);
23849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023851 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023852 if (type == VAR_DICT || type == VAR_LIST)
23853 {
23854 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23855
23856 if (etv == NULL)
23857 /* Failed to parse back the dict or list, use it as a
23858 * string. */
23859 tv.v_type = VAR_STRING;
23860 else
23861 {
23862 vim_free(tv.vval.v_string);
23863 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023864 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023865 }
23866 }
23867
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023868 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023869
23870 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023871 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023872 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23873 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 }
23875 }
23876 }
23877
23878 return viminfo_readline(virp);
23879}
23880
23881/*
23882 * Write global vars that start with a capital to the viminfo file
23883 */
23884 void
23885write_viminfo_varlist(fp)
23886 FILE *fp;
23887{
Bram Moolenaar33570922005-01-25 22:26:29 +000023888 hashitem_T *hi;
23889 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023890 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023891 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023892 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023893 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023894 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895
23896 if (find_viminfo_parameter('!') == NULL)
23897 return;
23898
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023899 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023900
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023901 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023902 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023904 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023906 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023907 this_var = HI2DI(hi);
23908 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023910 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023911 {
23912 case VAR_STRING: s = "STR"; break;
23913 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023914#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023915 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023916#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023917 case VAR_DICT: s = "DIC"; break;
23918 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023919 default: continue;
23920 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023921 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023922 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023923 if (p != NULL)
23924 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023925 vim_free(tofree);
23926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 }
23928 }
23929}
23930#endif
23931
23932#if defined(FEAT_SESSION) || defined(PROTO)
23933 int
23934store_session_globals(fd)
23935 FILE *fd;
23936{
Bram Moolenaar33570922005-01-25 22:26:29 +000023937 hashitem_T *hi;
23938 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 char_u *p, *t;
23941
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023943 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023945 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023948 this_var = HI2DI(hi);
23949 if ((this_var->di_tv.v_type == VAR_NUMBER
23950 || this_var->di_tv.v_type == VAR_STRING)
23951 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023952 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023953 /* Escape special characters with a backslash. Turn a LF and
23954 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023955 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023956 (char_u *)"\\\"\n\r");
23957 if (p == NULL) /* out of memory */
23958 break;
23959 for (t = p; *t != NUL; ++t)
23960 if (*t == '\n')
23961 *t = 'n';
23962 else if (*t == '\r')
23963 *t = 'r';
23964 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023965 this_var->di_key,
23966 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23967 : ' ',
23968 p,
23969 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23970 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023971 || put_eol(fd) == FAIL)
23972 {
23973 vim_free(p);
23974 return FAIL;
23975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 vim_free(p);
23977 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023978#ifdef FEAT_FLOAT
23979 else if (this_var->di_tv.v_type == VAR_FLOAT
23980 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23981 {
23982 float_T f = this_var->di_tv.vval.v_float;
23983 int sign = ' ';
23984
23985 if (f < 0)
23986 {
23987 f = -f;
23988 sign = '-';
23989 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023990 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023991 this_var->di_key, sign, f) < 0)
23992 || put_eol(fd) == FAIL)
23993 return FAIL;
23994 }
23995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 }
23997 }
23998 return OK;
23999}
24000#endif
24001
Bram Moolenaar661b1822005-07-28 22:36:45 +000024002/*
24003 * Display script name where an item was last set.
24004 * Should only be invoked when 'verbose' is non-zero.
24005 */
24006 void
24007last_set_msg(scriptID)
24008 scid_T scriptID;
24009{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024010 char_u *p;
24011
Bram Moolenaar661b1822005-07-28 22:36:45 +000024012 if (scriptID != 0)
24013 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024014 p = home_replace_save(NULL, get_scriptname(scriptID));
24015 if (p != NULL)
24016 {
24017 verbose_enter();
24018 MSG_PUTS(_("\n\tLast set from "));
24019 MSG_PUTS(p);
24020 vim_free(p);
24021 verbose_leave();
24022 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024023 }
24024}
24025
Bram Moolenaard812df62008-11-09 12:46:09 +000024026/*
24027 * List v:oldfiles in a nice way.
24028 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024029 void
24030ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024031 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024032{
24033 list_T *l = vimvars[VV_OLDFILES].vv_list;
24034 listitem_T *li;
24035 int nr = 0;
24036
24037 if (l == NULL)
24038 msg((char_u *)_("No old files"));
24039 else
24040 {
24041 msg_start();
24042 msg_scroll = TRUE;
24043 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24044 {
24045 msg_outnum((long)++nr);
24046 MSG_PUTS(": ");
24047 msg_outtrans(get_tv_string(&li->li_tv));
24048 msg_putchar('\n');
24049 out_flush(); /* output one line at a time */
24050 ui_breakcheck();
24051 }
24052 /* Assume "got_int" was set to truncate the listing. */
24053 got_int = FALSE;
24054
24055#ifdef FEAT_BROWSE_CMD
24056 if (cmdmod.browse)
24057 {
24058 quit_more = FALSE;
24059 nr = prompt_for_number(FALSE);
24060 msg_starthere();
24061 if (nr > 0)
24062 {
24063 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24064 (long)nr);
24065
24066 if (p != NULL)
24067 {
24068 p = expand_env_save(p);
24069 eap->arg = p;
24070 eap->cmdidx = CMD_edit;
24071 cmdmod.browse = FALSE;
24072 do_exedit(eap, NULL);
24073 vim_free(p);
24074 }
24075 }
24076 }
24077#endif
24078 }
24079}
24080
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081#endif /* FEAT_EVAL */
24082
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024084#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085
24086#ifdef WIN3264
24087/*
24088 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24089 */
24090static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24091static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24092static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24093
24094/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024095 * Get the short path (8.3) for the filename in "fnamep".
24096 * Only works for a valid file name.
24097 * When the path gets longer "fnamep" is changed and the allocated buffer
24098 * is put in "bufp".
24099 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24100 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 */
24102 static int
24103get_short_pathname(fnamep, bufp, fnamelen)
24104 char_u **fnamep;
24105 char_u **bufp;
24106 int *fnamelen;
24107{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024108 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 char_u *newbuf;
24110
24111 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 l = GetShortPathName(*fnamep, *fnamep, len);
24113 if (l > len - 1)
24114 {
24115 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024116 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 newbuf = vim_strnsave(*fnamep, l);
24118 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024119 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120
24121 vim_free(*bufp);
24122 *fnamep = *bufp = newbuf;
24123
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024124 /* Really should always succeed, as the buffer is big enough. */
24125 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 }
24127
24128 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024129 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130}
24131
24132/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024133 * Get the short path (8.3) for the filename in "fname". The converted
24134 * path is returned in "bufp".
24135 *
24136 * Some of the directories specified in "fname" may not exist. This function
24137 * will shorten the existing directories at the beginning of the path and then
24138 * append the remaining non-existing path.
24139 *
24140 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024141 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024142 * bufp - Pointer to an allocated buffer for the filename.
24143 * fnamelen - Length of the filename pointed to by fname
24144 *
24145 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 */
24147 static int
24148shortpath_for_invalid_fname(fname, bufp, fnamelen)
24149 char_u **fname;
24150 char_u **bufp;
24151 int *fnamelen;
24152{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024153 char_u *short_fname, *save_fname, *pbuf_unused;
24154 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024156 int old_len, len;
24157 int new_len, sfx_len;
24158 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159
24160 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024161 old_len = *fnamelen;
24162 save_fname = vim_strnsave(*fname, old_len);
24163 pbuf_unused = NULL;
24164 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024166 endp = save_fname + old_len - 1; /* Find the end of the copy */
24167 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024169 /*
24170 * Try shortening the supplied path till it succeeds by removing one
24171 * directory at a time from the tail of the path.
24172 */
24173 len = 0;
24174 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024176 /* go back one path-separator */
24177 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24178 --endp;
24179 if (endp <= save_fname)
24180 break; /* processed the complete path */
24181
24182 /*
24183 * Replace the path separator with a NUL and try to shorten the
24184 * resulting path.
24185 */
24186 ch = *endp;
24187 *endp = 0;
24188 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024189 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024190 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24191 {
24192 retval = FAIL;
24193 goto theend;
24194 }
24195 *endp = ch; /* preserve the string */
24196
24197 if (len > 0)
24198 break; /* successfully shortened the path */
24199
24200 /* failed to shorten the path. Skip the path separator */
24201 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202 }
24203
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024204 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024206 /*
24207 * Succeeded in shortening the path. Now concatenate the shortened
24208 * path with the remaining path at the tail.
24209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024211 /* Compute the length of the new path. */
24212 sfx_len = (int)(save_endp - endp) + 1;
24213 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024215 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024217 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024219 /* There is not enough space in the currently allocated string,
24220 * copy it to a buffer big enough. */
24221 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024223 {
24224 retval = FAIL;
24225 goto theend;
24226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 }
24228 else
24229 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024230 /* Transfer short_fname to the main buffer (it's big enough),
24231 * unless get_short_pathname() did its work in-place. */
24232 *fname = *bufp = save_fname;
24233 if (short_fname != save_fname)
24234 vim_strncpy(save_fname, short_fname, len);
24235 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024237
24238 /* concat the not-shortened part of the path */
24239 vim_strncpy(*fname + len, endp, sfx_len);
24240 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024242
24243theend:
24244 vim_free(pbuf_unused);
24245 vim_free(save_fname);
24246
24247 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248}
24249
24250/*
24251 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024252 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253 */
24254 static int
24255shortpath_for_partial(fnamep, bufp, fnamelen)
24256 char_u **fnamep;
24257 char_u **bufp;
24258 int *fnamelen;
24259{
24260 int sepcount, len, tflen;
24261 char_u *p;
24262 char_u *pbuf, *tfname;
24263 int hasTilde;
24264
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024265 /* Count up the path separators from the RHS.. so we know which part
24266 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024268 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 if (vim_ispathsep(*p))
24270 ++sepcount;
24271
24272 /* Need full path first (use expand_env() to remove a "~/") */
24273 hasTilde = (**fnamep == '~');
24274 if (hasTilde)
24275 pbuf = tfname = expand_env_save(*fnamep);
24276 else
24277 pbuf = tfname = FullName_save(*fnamep, FALSE);
24278
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024279 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024281 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283
24284 if (len == 0)
24285 {
24286 /* Don't have a valid filename, so shorten the rest of the
24287 * path if we can. This CAN give us invalid 8.3 filenames, but
24288 * there's not a lot of point in guessing what it might be.
24289 */
24290 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024291 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 }
24294
24295 /* Count the paths backward to find the beginning of the desired string. */
24296 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024297 {
24298#ifdef FEAT_MBYTE
24299 if (has_mbyte)
24300 p -= mb_head_off(tfname, p);
24301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302 if (vim_ispathsep(*p))
24303 {
24304 if (sepcount == 0 || (hasTilde && sepcount == 1))
24305 break;
24306 else
24307 sepcount --;
24308 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024310 if (hasTilde)
24311 {
24312 --p;
24313 if (p >= tfname)
24314 *p = '~';
24315 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 }
24318 else
24319 ++p;
24320
24321 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24322 vim_free(*bufp);
24323 *fnamelen = (int)STRLEN(p);
24324 *bufp = pbuf;
24325 *fnamep = p;
24326
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024327 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328}
24329#endif /* WIN3264 */
24330
24331/*
24332 * Adjust a filename, according to a string of modifiers.
24333 * *fnamep must be NUL terminated when called. When returning, the length is
24334 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024335 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 * When there is an error, *fnamep is set to NULL.
24337 */
24338 int
24339modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24340 char_u *src; /* string with modifiers */
24341 int *usedlen; /* characters after src that are used */
24342 char_u **fnamep; /* file name so far */
24343 char_u **bufp; /* buffer for allocated file name or NULL */
24344 int *fnamelen; /* length of fnamep */
24345{
24346 int valid = 0;
24347 char_u *tail;
24348 char_u *s, *p, *pbuf;
24349 char_u dirname[MAXPATHL];
24350 int c;
24351 int has_fullname = 0;
24352#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024353 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 int has_shortname = 0;
24355#endif
24356
24357repeat:
24358 /* ":p" - full path/file_name */
24359 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24360 {
24361 has_fullname = 1;
24362
24363 valid |= VALID_PATH;
24364 *usedlen += 2;
24365
24366 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24367 if ((*fnamep)[0] == '~'
24368#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24369 && ((*fnamep)[1] == '/'
24370# ifdef BACKSLASH_IN_FILENAME
24371 || (*fnamep)[1] == '\\'
24372# endif
24373 || (*fnamep)[1] == NUL)
24374
24375#endif
24376 )
24377 {
24378 *fnamep = expand_env_save(*fnamep);
24379 vim_free(*bufp); /* free any allocated file name */
24380 *bufp = *fnamep;
24381 if (*fnamep == NULL)
24382 return -1;
24383 }
24384
24385 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024386 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 {
24388 if (vim_ispathsep(*p)
24389 && p[1] == '.'
24390 && (p[2] == NUL
24391 || vim_ispathsep(p[2])
24392 || (p[2] == '.'
24393 && (p[3] == NUL || vim_ispathsep(p[3])))))
24394 break;
24395 }
24396
24397 /* FullName_save() is slow, don't use it when not needed. */
24398 if (*p != NUL || !vim_isAbsName(*fnamep))
24399 {
24400 *fnamep = FullName_save(*fnamep, *p != NUL);
24401 vim_free(*bufp); /* free any allocated file name */
24402 *bufp = *fnamep;
24403 if (*fnamep == NULL)
24404 return -1;
24405 }
24406
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024407#ifdef WIN3264
24408# if _WIN32_WINNT >= 0x0500
24409 if (vim_strchr(*fnamep, '~') != NULL)
24410 {
24411 /* Expand 8.3 filename to full path. Needed to make sure the same
24412 * file does not have two different names.
24413 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24414 p = alloc(_MAX_PATH + 1);
24415 if (p != NULL)
24416 {
24417 if (GetLongPathName(*fnamep, p, MAXPATHL))
24418 {
24419 vim_free(*bufp);
24420 *bufp = *fnamep = p;
24421 }
24422 else
24423 vim_free(p);
24424 }
24425 }
24426# endif
24427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 /* Append a path separator to a directory. */
24429 if (mch_isdir(*fnamep))
24430 {
24431 /* Make room for one or two extra characters. */
24432 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24433 vim_free(*bufp); /* free any allocated file name */
24434 *bufp = *fnamep;
24435 if (*fnamep == NULL)
24436 return -1;
24437 add_pathsep(*fnamep);
24438 }
24439 }
24440
24441 /* ":." - path relative to the current directory */
24442 /* ":~" - path relative to the home directory */
24443 /* ":8" - shortname path - postponed till after */
24444 while (src[*usedlen] == ':'
24445 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24446 {
24447 *usedlen += 2;
24448 if (c == '8')
24449 {
24450#ifdef WIN3264
24451 has_shortname = 1; /* Postpone this. */
24452#endif
24453 continue;
24454 }
24455 pbuf = NULL;
24456 /* Need full path first (use expand_env() to remove a "~/") */
24457 if (!has_fullname)
24458 {
24459 if (c == '.' && **fnamep == '~')
24460 p = pbuf = expand_env_save(*fnamep);
24461 else
24462 p = pbuf = FullName_save(*fnamep, FALSE);
24463 }
24464 else
24465 p = *fnamep;
24466
24467 has_fullname = 0;
24468
24469 if (p != NULL)
24470 {
24471 if (c == '.')
24472 {
24473 mch_dirname(dirname, MAXPATHL);
24474 s = shorten_fname(p, dirname);
24475 if (s != NULL)
24476 {
24477 *fnamep = s;
24478 if (pbuf != NULL)
24479 {
24480 vim_free(*bufp); /* free any allocated file name */
24481 *bufp = pbuf;
24482 pbuf = NULL;
24483 }
24484 }
24485 }
24486 else
24487 {
24488 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24489 /* Only replace it when it starts with '~' */
24490 if (*dirname == '~')
24491 {
24492 s = vim_strsave(dirname);
24493 if (s != NULL)
24494 {
24495 *fnamep = s;
24496 vim_free(*bufp);
24497 *bufp = s;
24498 }
24499 }
24500 }
24501 vim_free(pbuf);
24502 }
24503 }
24504
24505 tail = gettail(*fnamep);
24506 *fnamelen = (int)STRLEN(*fnamep);
24507
24508 /* ":h" - head, remove "/file_name", can be repeated */
24509 /* Don't remove the first "/" or "c:\" */
24510 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24511 {
24512 valid |= VALID_HEAD;
24513 *usedlen += 2;
24514 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024515 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024516 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517 *fnamelen = (int)(tail - *fnamep);
24518#ifdef VMS
24519 if (*fnamelen > 0)
24520 *fnamelen += 1; /* the path separator is part of the path */
24521#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024522 if (*fnamelen == 0)
24523 {
24524 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24525 p = vim_strsave((char_u *)".");
24526 if (p == NULL)
24527 return -1;
24528 vim_free(*bufp);
24529 *bufp = *fnamep = tail = p;
24530 *fnamelen = 1;
24531 }
24532 else
24533 {
24534 while (tail > s && !after_pathsep(s, tail))
24535 mb_ptr_back(*fnamep, tail);
24536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 }
24538
24539 /* ":8" - shortname */
24540 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24541 {
24542 *usedlen += 2;
24543#ifdef WIN3264
24544 has_shortname = 1;
24545#endif
24546 }
24547
24548#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024549 /*
24550 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551 */
24552 if (has_shortname)
24553 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024554 /* Copy the string if it is shortened by :h and when it wasn't copied
24555 * yet, because we are going to change it in place. Avoids changing
24556 * the buffer name for "%:8". */
24557 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 {
24559 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024560 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 return -1;
24562 vim_free(*bufp);
24563 *bufp = *fnamep = p;
24564 }
24565
24566 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024567 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568 if (!has_fullname && !vim_isAbsName(*fnamep))
24569 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024570 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 return -1;
24572 }
24573 else
24574 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024575 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576
Bram Moolenaardc935552011-08-17 15:23:23 +020024577 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024579 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 return -1;
24581
24582 if (l == 0)
24583 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024584 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024586 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024587 return -1;
24588 }
24589 *fnamelen = l;
24590 }
24591 }
24592#endif /* WIN3264 */
24593
24594 /* ":t" - tail, just the basename */
24595 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24596 {
24597 *usedlen += 2;
24598 *fnamelen -= (int)(tail - *fnamep);
24599 *fnamep = tail;
24600 }
24601
24602 /* ":e" - extension, can be repeated */
24603 /* ":r" - root, without extension, can be repeated */
24604 while (src[*usedlen] == ':'
24605 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24606 {
24607 /* find a '.' in the tail:
24608 * - for second :e: before the current fname
24609 * - otherwise: The last '.'
24610 */
24611 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24612 s = *fnamep - 2;
24613 else
24614 s = *fnamep + *fnamelen - 1;
24615 for ( ; s > tail; --s)
24616 if (s[0] == '.')
24617 break;
24618 if (src[*usedlen + 1] == 'e') /* :e */
24619 {
24620 if (s > tail)
24621 {
24622 *fnamelen += (int)(*fnamep - (s + 1));
24623 *fnamep = s + 1;
24624#ifdef VMS
24625 /* cut version from the extension */
24626 s = *fnamep + *fnamelen - 1;
24627 for ( ; s > *fnamep; --s)
24628 if (s[0] == ';')
24629 break;
24630 if (s > *fnamep)
24631 *fnamelen = s - *fnamep;
24632#endif
24633 }
24634 else if (*fnamep <= tail)
24635 *fnamelen = 0;
24636 }
24637 else /* :r */
24638 {
24639 if (s > tail) /* remove one extension */
24640 *fnamelen = (int)(s - *fnamep);
24641 }
24642 *usedlen += 2;
24643 }
24644
24645 /* ":s?pat?foo?" - substitute */
24646 /* ":gs?pat?foo?" - global substitute */
24647 if (src[*usedlen] == ':'
24648 && (src[*usedlen + 1] == 's'
24649 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24650 {
24651 char_u *str;
24652 char_u *pat;
24653 char_u *sub;
24654 int sep;
24655 char_u *flags;
24656 int didit = FALSE;
24657
24658 flags = (char_u *)"";
24659 s = src + *usedlen + 2;
24660 if (src[*usedlen + 1] == 'g')
24661 {
24662 flags = (char_u *)"g";
24663 ++s;
24664 }
24665
24666 sep = *s++;
24667 if (sep)
24668 {
24669 /* find end of pattern */
24670 p = vim_strchr(s, sep);
24671 if (p != NULL)
24672 {
24673 pat = vim_strnsave(s, (int)(p - s));
24674 if (pat != NULL)
24675 {
24676 s = p + 1;
24677 /* find end of substitution */
24678 p = vim_strchr(s, sep);
24679 if (p != NULL)
24680 {
24681 sub = vim_strnsave(s, (int)(p - s));
24682 str = vim_strnsave(*fnamep, *fnamelen);
24683 if (sub != NULL && str != NULL)
24684 {
24685 *usedlen = (int)(p + 1 - src);
24686 s = do_string_sub(str, pat, sub, flags);
24687 if (s != NULL)
24688 {
24689 *fnamep = s;
24690 *fnamelen = (int)STRLEN(s);
24691 vim_free(*bufp);
24692 *bufp = s;
24693 didit = TRUE;
24694 }
24695 }
24696 vim_free(sub);
24697 vim_free(str);
24698 }
24699 vim_free(pat);
24700 }
24701 }
24702 /* after using ":s", repeat all the modifiers */
24703 if (didit)
24704 goto repeat;
24705 }
24706 }
24707
Bram Moolenaar26df0922014-02-23 23:39:13 +010024708 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24709 {
24710 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24711 if (p == NULL)
24712 return -1;
24713 vim_free(*bufp);
24714 *bufp = *fnamep = p;
24715 *fnamelen = (int)STRLEN(p);
24716 *usedlen += 2;
24717 }
24718
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 return valid;
24720}
24721
24722/*
24723 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24724 * "flags" can be "g" to do a global substitute.
24725 * Returns an allocated string, NULL for error.
24726 */
24727 char_u *
24728do_string_sub(str, pat, sub, flags)
24729 char_u *str;
24730 char_u *pat;
24731 char_u *sub;
24732 char_u *flags;
24733{
24734 int sublen;
24735 regmatch_T regmatch;
24736 int i;
24737 int do_all;
24738 char_u *tail;
24739 garray_T ga;
24740 char_u *ret;
24741 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024742 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743
24744 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24745 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024746 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747
24748 ga_init2(&ga, 1, 200);
24749
24750 do_all = (flags[0] == 'g');
24751
24752 regmatch.rm_ic = p_ic;
24753 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24754 if (regmatch.regprog != NULL)
24755 {
24756 tail = str;
24757 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24758 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024759 /* Skip empty match except for first match. */
24760 if (regmatch.startp[0] == regmatch.endp[0])
24761 {
24762 if (zero_width == regmatch.startp[0])
24763 {
24764 /* avoid getting stuck on a match with an empty string */
24765 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24766 ++ga.ga_len;
24767 continue;
24768 }
24769 zero_width = regmatch.startp[0];
24770 }
24771
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772 /*
24773 * Get some space for a temporary buffer to do the substitution
24774 * into. It will contain:
24775 * - The text up to where the match is.
24776 * - The substituted text.
24777 * - The text after the match.
24778 */
24779 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24780 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24781 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24782 {
24783 ga_clear(&ga);
24784 break;
24785 }
24786
24787 /* copy the text up to where the match is */
24788 i = (int)(regmatch.startp[0] - tail);
24789 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24790 /* add the substituted text */
24791 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24792 + ga.ga_len + i, TRUE, TRUE, FALSE);
24793 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024794 tail = regmatch.endp[0];
24795 if (*tail == NUL)
24796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797 if (!do_all)
24798 break;
24799 }
24800
24801 if (ga.ga_data != NULL)
24802 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24803
Bram Moolenaar473de612013-06-08 18:19:48 +020024804 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805 }
24806
24807 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24808 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024809 if (p_cpo == empty_option)
24810 p_cpo = save_cpo;
24811 else
24812 /* Darn, evaluating {sub} expression changed the value. */
24813 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814
24815 return ret;
24816}
24817
24818#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */