blob: 0e9ec9ed0ae7df7655601eeabc42870a3b3c693d [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));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200466static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200471static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000473static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100482static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100484static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#ifdef FEAT_FLOAT
487static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
488#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000489static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000490static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000492static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000494#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000495static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000496static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
498#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#ifdef FEAT_FLOAT
502static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200503static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000505static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
508static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200518static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200520#ifdef FEAT_FLOAT
521static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
522#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000525static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531#ifdef FEAT_FLOAT
532static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200534static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000536static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000545static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000547static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000553static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000561static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000562static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000563static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000564static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200567static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000568static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000576static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000577static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000590static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100595static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000597static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000609#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200610static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000611static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200613#ifdef FEAT_LUA
614static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
615#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000620static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000621static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000622static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000624static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000628#ifdef vim_mkdir
629static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100632#ifdef FEAT_MZSCHEME
633static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
636static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100637static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000638static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000639#ifdef FEAT_FLOAT
640static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000643static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000644static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200645#ifdef FEAT_PYTHON3
646static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
648#ifdef FEAT_PYTHON
649static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
650#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000652static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000653static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000665#ifdef FEAT_FLOAT
666static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
667#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200668static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100670static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000672static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000673static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000675static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000682static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000683static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000684static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000685static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200687static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000688static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100690#ifdef FEAT_CRYPT
691static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
692#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000693static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200694static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#ifdef FEAT_FLOAT
697static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200698static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000699#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000700static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000701static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000702static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000704static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#ifdef FEAT_FLOAT
706static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
708#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000709static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200710static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711#ifdef HAVE_STRFTIME
712static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
713#endif
714static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200720static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200721static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000727static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200728static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200730static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000732static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000733static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000734static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000735static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000737static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200738#ifdef FEAT_FLOAT
739static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
741#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000745#ifdef FEAT_FLOAT
746static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200749static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200750static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100751static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100755static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000762static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100766static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767
Bram Moolenaar493c1782014-05-28 14:34:46 +0200768static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000769static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static int get_env_len __ARGS((char_u **arg));
771static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000773static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
774#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
775#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
776 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000777static 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 +0000778static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100780static 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 +0000781static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static typval_T *alloc_tv __ARGS((void));
783static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static void init_tv __ARGS((typval_T *varp));
785static long get_tv_number __ARGS((typval_T *varp));
786static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000787static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static char_u *get_tv_string __ARGS((typval_T *varp));
789static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000790static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100791static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
792static 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 +0000793static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
794static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
795static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000796static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
797static 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 +0000798static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
799static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000800static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200801static int var_check_func_name __ARGS((char_u *name, int new_var));
802static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000803static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000804static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
806static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
807static int eval_fname_script __ARGS((char_u *p));
808static int eval_fname_sid __ARGS((char_u *p));
809static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000810static ufunc_T *find_func __ARGS((char_u *name));
811static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200812static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813#ifdef FEAT_PROFILE
814static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000815static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
816static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_total_cmp __ARGS((const void *s1, const void *s2));
822static int
823# ifdef __BORLANDC__
824 _RTLENTRYF
825# endif
826 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000827#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200828static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000829static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000830static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000832static 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 +0000833static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
834static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000836static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
837static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000838static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000839static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000840static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200841static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200842static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000843
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200844
845#ifdef EBCDIC
846static int compare_func_name __ARGS((const void *s1, const void *s2));
847static void sortFunctions __ARGS(());
848#endif
849
Bram Moolenaar33570922005-01-25 22:26:29 +0000850/*
851 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000852 */
853 void
854eval_init()
855{
Bram Moolenaar33570922005-01-25 22:26:29 +0000856 int i;
857 struct vimvar *p;
858
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200859 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
860 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200861 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000862 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000863 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
865 for (i = 0; i < VV_LEN; ++i)
866 {
867 p = &vimvars[i];
868 STRCPY(p->vv_di.di_key, p->vv_name);
869 if (p->vv_flags & VV_RO)
870 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
871 else if (p->vv_flags & VV_RO_SBX)
872 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
873 else
874 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000875
876 /* add to v: scope dict, unless the value is not always available */
877 if (p->vv_type != VAR_UNKNOWN)
878 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000879 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000880 /* add to compat scope dict */
881 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000883 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100884 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200885 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886
887#ifdef EBCDIC
888 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100889 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200890 */
891 sortFunctions();
892#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000893}
894
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000895#if defined(EXITFREE) || defined(PROTO)
896 void
897eval_clear()
898{
899 int i;
900 struct vimvar *p;
901
902 for (i = 0; i < VV_LEN; ++i)
903 {
904 p = &vimvars[i];
905 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000907 vim_free(p->vv_str);
908 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000909 }
910 else if (p->vv_di.di_tv.v_type == VAR_LIST)
911 {
912 list_unref(p->vv_list);
913 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000915 }
916 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000917 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000918 hash_clear(&compat_hashtab);
919
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100921# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200922 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100923# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924
925 /* global variables */
926 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000927
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000928 /* autoloaded script names */
929 ga_clear_strings(&ga_loaded);
930
Bram Moolenaarcca74132013-09-25 21:00:28 +0200931 /* Script-local variables. First clear all the variables and in a second
932 * loop free the scriptvar_T, because a variable in one script might hold
933 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200935 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200936 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200937 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200938 ga_clear(&ga_scripts);
939
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000940 /* unreferenced lists and dicts */
941 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000942
943 /* functions */
944 free_all_functions();
945 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000946}
947#endif
948
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 * Return the name of the executed function.
951 */
952 char_u *
953func_name(cookie)
954 void *cookie;
955{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000956 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Return the address holding the next breakpoint line for a funccall cookie.
961 */
962 linenr_T *
963func_breakpoint(cookie)
964 void *cookie;
965{
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967}
968
969/*
970 * Return the address holding the debug tick for a funccall cookie.
971 */
972 int *
973func_dbg_tick(cookie)
974 void *cookie;
975{
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
980 * Return the nesting level for a funccall cookie.
981 */
982 int
983func_level(cookie)
984 void *cookie;
985{
Bram Moolenaar33570922005-01-25 22:26:29 +0000986 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988
989/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000990funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000992/* pointer to list of previously used funccal, still around because some
993 * item in it is still being used. */
994funccall_T *previous_funccal = NULL;
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/*
997 * Return TRUE when a function was ended by a ":return" command.
998 */
999 int
1000current_func_returned()
1001{
1002 return current_funccal->returned;
1003}
1004
1005
1006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 * Set an internal variable to a string value. Creates the variable if it does
1008 * not already exist.
1009 */
1010 void
1011set_internal_string_var(name, value)
1012 char_u *name;
1013 char_u *value;
1014{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001016 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 val = vim_strsave(value);
1019 if (val != NULL)
1020 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001022 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001024 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001025 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027 }
1028}
1029
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001031static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032static char_u *redir_endp = NULL;
1033static char_u *redir_varname = NULL;
1034
1035/*
1036 * Start recording command output to a variable
1037 * Returns OK if successfully completed the setup. FAIL otherwise.
1038 */
1039 int
1040var_redir_start(name, append)
1041 char_u *name;
1042 int append; /* append to an existing variable */
1043{
1044 int save_emsg;
1045 int err;
1046 typval_T tv;
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001049 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 {
1051 EMSG(_(e_invarg));
1052 return FAIL;
1053 }
1054
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001055 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 redir_varname = vim_strsave(name);
1057 if (redir_varname == NULL)
1058 return FAIL;
1059
1060 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1061 if (redir_lval == NULL)
1062 {
1063 var_redir_stop();
1064 return FAIL;
1065 }
1066
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001067 /* The output is stored in growarray "redir_ga" until redirection ends. */
1068 ga_init2(&redir_ga, (int)sizeof(char), 500);
1069
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001071 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001072 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1074 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001075 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 if (redir_endp != NULL && *redir_endp != NUL)
1077 /* Trailing characters are present after the variable name */
1078 EMSG(_(e_trailing));
1079 else
1080 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001081 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 var_redir_stop();
1083 return FAIL;
1084 }
1085
1086 /* check if we can write to the variable: set it to or append an empty
1087 * string */
1088 save_emsg = did_emsg;
1089 did_emsg = FALSE;
1090 tv.v_type = VAR_STRING;
1091 tv.vval.v_string = (char_u *)"";
1092 if (append)
1093 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1094 else
1095 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001096 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001098 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099 if (err)
1100 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001101 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 var_redir_stop();
1103 return FAIL;
1104 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105
1106 return OK;
1107}
1108
1109/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 * Append "value[value_len]" to the variable set by var_redir_start().
1111 * The actual appending is postponed until redirection ends, because the value
1112 * appended may in fact be the string we write to, changing it may cause freed
1113 * memory to be used:
1114 * :redir => foo
1115 * :let foo
1116 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 */
1118 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001119var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124
1125 if (redir_lval == NULL)
1126 return;
1127
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001131 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001133 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 {
1135 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001136 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 }
1138 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140}
1141
1142/*
1143 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001144 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 */
1146 void
1147var_redir_stop()
1148{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001149 typval_T tv;
1150
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 if (redir_lval != NULL)
1152 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 /* If there was no error: assign the text to the variable. */
1154 if (redir_endp != NULL)
1155 {
1156 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1157 tv.v_type = VAR_STRING;
1158 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 /* Call get_lval() again, if it's inside a Dict or List it may
1160 * have changed. */
1161 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001162 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001163 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1164 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1165 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001168 /* free the collected output */
1169 vim_free(redir_ga.ga_data);
1170 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 vim_free(redir_lval);
1173 redir_lval = NULL;
1174 }
1175 vim_free(redir_varname);
1176 redir_varname = NULL;
1177}
1178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179# if defined(FEAT_MBYTE) || defined(PROTO)
1180 int
1181eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1182 char_u *enc_from;
1183 char_u *enc_to;
1184 char_u *fname_from;
1185 char_u *fname_to;
1186{
1187 int err = FALSE;
1188
1189 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1190 set_vim_var_string(VV_CC_TO, enc_to, -1);
1191 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1192 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1193 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1194 err = TRUE;
1195 set_vim_var_string(VV_CC_FROM, NULL, -1);
1196 set_vim_var_string(VV_CC_TO, NULL, -1);
1197 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1198 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1199
1200 if (err)
1201 return FAIL;
1202 return OK;
1203}
1204# endif
1205
1206# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1207 int
1208eval_printexpr(fname, args)
1209 char_u *fname;
1210 char_u *args;
1211{
1212 int err = FALSE;
1213
1214 set_vim_var_string(VV_FNAME_IN, fname, -1);
1215 set_vim_var_string(VV_CMDARG, args, -1);
1216 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1217 err = TRUE;
1218 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1219 set_vim_var_string(VV_CMDARG, NULL, -1);
1220
1221 if (err)
1222 {
1223 mch_remove(fname);
1224 return FAIL;
1225 }
1226 return OK;
1227}
1228# endif
1229
1230# if defined(FEAT_DIFF) || defined(PROTO)
1231 void
1232eval_diff(origfile, newfile, outfile)
1233 char_u *origfile;
1234 char_u *newfile;
1235 char_u *outfile;
1236{
1237 int err = FALSE;
1238
1239 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1240 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1241 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1242 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1243 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1244 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1245 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1246}
1247
1248 void
1249eval_patch(origfile, difffile, outfile)
1250 char_u *origfile;
1251 char_u *difffile;
1252 char_u *outfile;
1253{
1254 int err;
1255
1256 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1257 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1258 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1259 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1260 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1261 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1262 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1263}
1264# endif
1265
1266/*
1267 * Top level evaluation function, returning a boolean.
1268 * Sets "error" to TRUE if there was an error.
1269 * Return TRUE or FALSE.
1270 */
1271 int
1272eval_to_bool(arg, error, nextcmd, skip)
1273 char_u *arg;
1274 int *error;
1275 char_u **nextcmd;
1276 int skip; /* only parse, don't execute */
1277{
Bram Moolenaar33570922005-01-25 22:26:29 +00001278 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 int retval = FALSE;
1280
1281 if (skip)
1282 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001283 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 else
1286 {
1287 *error = FALSE;
1288 if (!skip)
1289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001290 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001291 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 }
1294 if (skip)
1295 --emsg_skip;
1296
1297 return retval;
1298}
1299
1300/*
1301 * Top level evaluation function, returning a string. If "skip" is TRUE,
1302 * only parsing to "nextcmd" is done, without reporting errors. Return
1303 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1304 */
1305 char_u *
1306eval_to_string_skip(arg, nextcmd, skip)
1307 char_u *arg;
1308 char_u **nextcmd;
1309 int skip; /* only parse, don't execute */
1310{
Bram Moolenaar33570922005-01-25 22:26:29 +00001311 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 char_u *retval;
1313
1314 if (skip)
1315 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 retval = NULL;
1318 else
1319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001320 retval = vim_strsave(get_tv_string(&tv));
1321 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 if (skip)
1324 --emsg_skip;
1325
1326 return retval;
1327}
1328
1329/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330 * Skip over an expression at "*pp".
1331 * Return FAIL for an error, OK otherwise.
1332 */
1333 int
1334skip_expr(pp)
1335 char_u **pp;
1336{
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001338
1339 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001341}
1342
1343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 * When "convert" is TRUE convert a List into a sequence of lines and convert
1346 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 * Return pointer to allocated memory, or NULL for failure.
1348 */
1349 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001350eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 char_u *arg;
1352 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
Bram Moolenaar33570922005-01-25 22:26:29 +00001355 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001357 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001358#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001362 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 retval = NULL;
1364 else
1365 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001366 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001367 {
1368 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001369 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001370 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001371 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001372 if (tv.vval.v_list->lv_len > 0)
1373 ga_append(&ga, NL);
1374 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001375 ga_append(&ga, NUL);
1376 retval = (char_u *)ga.ga_data;
1377 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001378#ifdef FEAT_FLOAT
1379 else if (convert && tv.v_type == VAR_FLOAT)
1380 {
1381 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1382 retval = vim_strsave(numbuf);
1383 }
1384#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001385 else
1386 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389
1390 return retval;
1391}
1392
1393/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 * Call eval_to_string() without using current local variables and using
1395 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 */
1397 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 char_u *arg;
1400 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001401 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 char_u *retval;
1404 void *save_funccalp;
1405
1406 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 if (use_sandbox)
1408 ++sandbox;
1409 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001410 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001411 if (use_sandbox)
1412 --sandbox;
1413 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 restore_funccal(save_funccalp);
1415 return retval;
1416}
1417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418/*
1419 * Top level evaluation function, returning a number.
1420 * Evaluates "expr" silently.
1421 * Returns -1 for an error.
1422 */
1423 int
1424eval_to_number(expr)
1425 char_u *expr;
1426{
Bram Moolenaar33570922005-01-25 22:26:29 +00001427 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001429 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 ++emsg_off;
1432
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001433 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 retval = -1;
1435 else
1436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001437 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
1440 --emsg_off;
1441
1442 return retval;
1443}
1444
Bram Moolenaara40058a2005-07-11 22:42:07 +00001445/*
1446 * Prepare v: variable "idx" to be used.
1447 * Save the current typeval in "save_tv".
1448 * When not used yet add the variable to the v: hashtable.
1449 */
1450 static void
1451prepare_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 *save_tv = vimvars[idx].vv_tv;
1456 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1457 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1458}
1459
1460/*
1461 * Restore v: variable "idx" to typeval "save_tv".
1462 * When no longer defined, remove the variable from the v: hashtable.
1463 */
1464 static void
1465restore_vimvar(idx, save_tv)
1466 int idx;
1467 typval_T *save_tv;
1468{
1469 hashitem_T *hi;
1470
Bram Moolenaara40058a2005-07-11 22:42:07 +00001471 vimvars[idx].vv_tv = *save_tv;
1472 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1473 {
1474 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1475 if (HASHITEM_EMPTY(hi))
1476 EMSG2(_(e_intern2), "restore_vimvar()");
1477 else
1478 hash_remove(&vimvarht, hi);
1479 }
1480}
1481
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001482#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001483/*
1484 * Evaluate an expression to a list with suggestions.
1485 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001486 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001487 */
1488 list_T *
1489eval_spell_expr(badword, expr)
1490 char_u *badword;
1491 char_u *expr;
1492{
1493 typval_T save_val;
1494 typval_T rettv;
1495 list_T *list = NULL;
1496 char_u *p = skipwhite(expr);
1497
1498 /* Set "v:val" to the bad word. */
1499 prepare_vimvar(VV_VAL, &save_val);
1500 vimvars[VV_VAL].vv_type = VAR_STRING;
1501 vimvars[VV_VAL].vv_str = badword;
1502 if (p_verbose == 0)
1503 ++emsg_off;
1504
1505 if (eval1(&p, &rettv, TRUE) == OK)
1506 {
1507 if (rettv.v_type != VAR_LIST)
1508 clear_tv(&rettv);
1509 else
1510 list = rettv.vval.v_list;
1511 }
1512
1513 if (p_verbose == 0)
1514 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001515 restore_vimvar(VV_VAL, &save_val);
1516
1517 return list;
1518}
1519
1520/*
1521 * "list" is supposed to contain two items: a word and a number. Return the
1522 * word in "pp" and the number as the return value.
1523 * Return -1 if anything isn't right.
1524 * Used to get the good word and score from the eval_spell_expr() result.
1525 */
1526 int
1527get_spellword(list, pp)
1528 list_T *list;
1529 char_u **pp;
1530{
1531 listitem_T *li;
1532
1533 li = list->lv_first;
1534 if (li == NULL)
1535 return -1;
1536 *pp = get_tv_string(&li->li_tv);
1537
1538 li = li->li_next;
1539 if (li == NULL)
1540 return -1;
1541 return get_tv_number(&li->li_tv);
1542}
1543#endif
1544
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 * Top level evaluation function.
1547 * Returns an allocated typval_T with the result.
1548 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 */
1550 typval_T *
1551eval_expr(arg, nextcmd)
1552 char_u *arg;
1553 char_u **nextcmd;
1554{
1555 typval_T *tv;
1556
1557 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001558 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001559 {
1560 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001561 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001562 }
1563
1564 return tv;
1565}
1566
1567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001569 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570 * Uses argv[argc] for the function arguments. Only Number and String
1571 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001572 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001574 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001575call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 char_u *func;
1577 int argc;
1578 char_u **argv;
1579 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001580 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582{
Bram Moolenaar33570922005-01-25 22:26:29 +00001583 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 long n;
1585 int len;
1586 int i;
1587 int doesrange;
1588 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001591 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001593 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595 for (i = 0; i < argc; i++)
1596 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001597 /* Pass a NULL or empty argument as an empty string */
1598 if (argv[i] == NULL || *argv[i] == NUL)
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001602 continue;
1603 }
1604
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001605 if (str_arg_only)
1606 len = 0;
1607 else
1608 /* Recognize a number argument, the others must be strings. */
1609 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (len != 0 && len == (int)STRLEN(argv[i]))
1611 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001612 argvars[i].v_type = VAR_NUMBER;
1613 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 }
1615 else
1616 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001617 argvars[i].v_type = VAR_STRING;
1618 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 }
1621
1622 if (safe)
1623 {
1624 save_funccalp = save_funccal();
1625 ++sandbox;
1626 }
1627
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1629 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001631 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (safe)
1633 {
1634 --sandbox;
1635 restore_funccal(save_funccalp);
1636 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 vim_free(argvars);
1638
1639 if (ret == FAIL)
1640 clear_tv(rettv);
1641
1642 return ret;
1643}
1644
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001645/*
1646 * Call vimL function "func" and return the result as a number.
1647 * Returns -1 when calling the function fails.
1648 * Uses argv[argc] for the function arguments.
1649 */
1650 long
1651call_func_retnr(func, argc, argv, safe)
1652 char_u *func;
1653 int argc;
1654 char_u **argv;
1655 int safe; /* use the sandbox */
1656{
1657 typval_T rettv;
1658 long retval;
1659
1660 /* All arguments are passed as strings, no conversion to number. */
1661 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1662 return -1;
1663
1664 retval = get_tv_number_chk(&rettv, NULL);
1665 clear_tv(&rettv);
1666 return retval;
1667}
1668
1669#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1670 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1671
Bram Moolenaar4f688582007-07-24 12:34:30 +00001672# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001674 * Call vimL function "func" and return the result as a string.
1675 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 * Uses argv[argc] for the function arguments.
1677 */
1678 void *
1679call_func_retstr(func, argc, argv, safe)
1680 char_u *func;
1681 int argc;
1682 char_u **argv;
1683 int safe; /* use the sandbox */
1684{
1685 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001686 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001688 /* All arguments are passed as strings, no conversion to number. */
1689 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690 return NULL;
1691
1692 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 return retval;
1695}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001696# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001697
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001698/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001699 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001701 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 */
1703 void *
1704call_func_retlist(func, argc, argv, safe)
1705 char_u *func;
1706 int argc;
1707 char_u **argv;
1708 int safe; /* use the sandbox */
1709{
1710 typval_T rettv;
1711
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001712 /* All arguments are passed as strings, no conversion to number. */
1713 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 return NULL;
1715
1716 if (rettv.v_type != VAR_LIST)
1717 {
1718 clear_tv(&rettv);
1719 return NULL;
1720 }
1721
1722 return rettv.vval.v_list;
1723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
1725
1726/*
1727 * Save the current function call pointer, and set it to NULL.
1728 * Used when executing autocommands and for ":source".
1729 */
1730 void *
1731save_funccal()
1732{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 current_funccal = NULL;
1736 return (void *)fc;
1737}
1738
1739 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001740restore_funccal(vfc)
1741 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001743 funccall_T *fc = (funccall_T *)vfc;
1744
1745 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746}
1747
Bram Moolenaar05159a02005-02-26 23:04:13 +00001748#if defined(FEAT_PROFILE) || defined(PROTO)
1749/*
1750 * Prepare profiling for entering a child or something else that is not
1751 * counted for the script/function itself.
1752 * Should always be called in pair with prof_child_exit().
1753 */
1754 void
1755prof_child_enter(tm)
1756 proftime_T *tm; /* place to store waittime */
1757{
1758 funccall_T *fc = current_funccal;
1759
1760 if (fc != NULL && fc->func->uf_profiling)
1761 profile_start(&fc->prof_child);
1762 script_prof_save(tm);
1763}
1764
1765/*
1766 * Take care of time spent in a child.
1767 * Should always be called after prof_child_enter().
1768 */
1769 void
1770prof_child_exit(tm)
1771 proftime_T *tm; /* where waittime was stored */
1772{
1773 funccall_T *fc = current_funccal;
1774
1775 if (fc != NULL && fc->func->uf_profiling)
1776 {
1777 profile_end(&fc->prof_child);
1778 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1779 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1780 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1781 }
1782 script_prof_restore(tm);
1783}
1784#endif
1785
1786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#ifdef FEAT_FOLDING
1788/*
1789 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1790 * it in "*cp". Doesn't give error messages.
1791 */
1792 int
1793eval_foldexpr(arg, cp)
1794 char_u *arg;
1795 int *cp;
1796{
Bram Moolenaar33570922005-01-25 22:26:29 +00001797 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 int retval;
1799 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001800 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1801 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001804 if (use_sandbox)
1805 ++sandbox;
1806 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 if (tv.v_type == VAR_NUMBER)
1814 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001815 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 retval = 0;
1817 else
1818 {
1819 /* If the result is a string, check if there is a non-digit before
1820 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (!VIM_ISDIGIT(*s) && *s != '-')
1823 *cp = *s++;
1824 retval = atol((char *)s);
1825 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001826 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 }
1828 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001829 if (use_sandbox)
1830 --sandbox;
1831 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 return retval;
1834}
1835#endif
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 * ":let" list all variable values
1839 * ":let var1 var2" list variable values
1840 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001841 * ":let var += expr" assignment command.
1842 * ":let var -= expr" assignment command.
1843 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 */
1846 void
1847ex_let(eap)
1848 exarg_T *eap;
1849{
1850 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001852 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 int var_count = 0;
1855 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001856 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001857 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
Bram Moolenaardb552d602006-03-23 22:59:57 +00001860 argend = skip_var_list(arg, &var_count, &semicolon);
1861 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001862 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001863 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1864 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001865 expr = skipwhite(argend);
1866 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1867 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001869 /*
1870 * ":let" without "=": list variables
1871 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 if (*arg == '[')
1873 EMSG(_(e_invarg));
1874 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 list_glob_vars(&first);
1881 list_buf_vars(&first);
1882 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001883#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001884 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001885#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_script_vars(&first);
1887 list_func_vars(&first);
1888 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 eap->nextcmd = check_nextcmd(arg);
1891 }
1892 else
1893 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 op[0] = '=';
1895 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001896 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001898 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1899 op[0] = *expr; /* +=, -= or .= */
1900 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 else
1903 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (eap->skip)
1906 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (eap->skip)
1909 {
1910 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 --emsg_skip;
1913 }
1914 else if (i != FAIL)
1915 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001917 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 }
1921}
1922
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923/*
1924 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1925 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 * When "nextchars" is not NULL it points to a string with characters that
1927 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1928 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 * Returns OK or FAIL;
1930 */
1931 static int
1932ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1933 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001934 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 int copy; /* copy values from "tv", don't move */
1936 int semicolon; /* from skip_var_list() */
1937 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939{
1940 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001943 listitem_T *item;
1944 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945
1946 if (*arg != '[')
1947 {
1948 /*
1949 * ":let var = expr" or ":for var in list"
1950 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 return OK;
1954 }
1955
1956 /*
1957 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1958 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001959 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 {
1961 EMSG(_(e_listreq));
1962 return FAIL;
1963 }
1964
1965 i = list_len(l);
1966 if (semicolon == 0 && var_count < i)
1967 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001968 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969 return FAIL;
1970 }
1971 if (var_count - semicolon > i)
1972 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001973 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 return FAIL;
1975 }
1976
1977 item = l->lv_first;
1978 while (*arg != ']')
1979 {
1980 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982 item = item->li_next;
1983 if (arg == NULL)
1984 return FAIL;
1985
1986 arg = skipwhite(arg);
1987 if (*arg == ';')
1988 {
1989 /* Put the rest of the list (may be empty) in the var after ';'.
1990 * Create a new list for this. */
1991 l = list_alloc();
1992 if (l == NULL)
1993 return FAIL;
1994 while (item != NULL)
1995 {
1996 list_append_tv(l, &item->li_tv);
1997 item = item->li_next;
1998 }
1999
2000 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002001 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 ltv.vval.v_list = l;
2003 l->lv_refcount = 1;
2004
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2006 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 clear_tv(&ltv);
2008 if (arg == NULL)
2009 return FAIL;
2010 break;
2011 }
2012 else if (*arg != ',' && *arg != ']')
2013 {
2014 EMSG2(_(e_intern2), "ex_let_vars()");
2015 return FAIL;
2016 }
2017 }
2018
2019 return OK;
2020}
2021
2022/*
2023 * Skip over assignable variable "var" or list of variables "[var, var]".
2024 * Used for ":let varvar = expr" and ":for varvar in expr".
2025 * For "[var, var]" increment "*var_count" for each variable.
2026 * for "[var, var; var]" set "semicolon".
2027 * Return NULL for an error.
2028 */
2029 static char_u *
2030skip_var_list(arg, var_count, semicolon)
2031 char_u *arg;
2032 int *var_count;
2033 int *semicolon;
2034{
2035 char_u *p, *s;
2036
2037 if (*arg == '[')
2038 {
2039 /* "[var, var]": find the matching ']'. */
2040 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002041 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 {
2043 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2044 s = skip_var_one(p);
2045 if (s == p)
2046 {
2047 EMSG2(_(e_invarg2), p);
2048 return NULL;
2049 }
2050 ++*var_count;
2051
2052 p = skipwhite(s);
2053 if (*p == ']')
2054 break;
2055 else if (*p == ';')
2056 {
2057 if (*semicolon == 1)
2058 {
2059 EMSG(_("Double ; in list of variables"));
2060 return NULL;
2061 }
2062 *semicolon = 1;
2063 }
2064 else if (*p != ',')
2065 {
2066 EMSG2(_(e_invarg2), p);
2067 return NULL;
2068 }
2069 }
2070 return p + 1;
2071 }
2072 else
2073 return skip_var_one(arg);
2074}
2075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002076/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002077 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002078 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002079 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002080 static char_u *
2081skip_var_one(arg)
2082 char_u *arg;
2083{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 if (*arg == '@' && arg[1] != NUL)
2085 return arg + 2;
2086 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2087 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002088}
2089
Bram Moolenaara7043832005-01-21 11:56:39 +00002090/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 * List variables for hashtab "ht" with prefix "prefix".
2092 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002094 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002096 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002097 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002098 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002100{
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 hashitem_T *hi;
2102 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 int todo;
2104
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002105 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2107 {
2108 if (!HASHITEM_EMPTY(hi))
2109 {
2110 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 di = HI2DI(hi);
2112 if (empty || di->di_tv.v_type != VAR_STRING
2113 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002115 }
2116 }
2117}
2118
2119/*
2120 * List global variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_glob_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List buffer variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_buf_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136 char_u numbuf[NUMBUFLEN];
2137
Bram Moolenaar429fa852013-04-15 12:27:36 +02002138 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002140
2141 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2143 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002144}
2145
2146/*
2147 * List window variables.
2148 */
2149 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150list_win_vars(first)
2151 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002153 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002155}
2156
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157#ifdef FEAT_WINDOWS
2158/*
2159 * List tab page variables.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_tab_vars(first)
2163 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002165 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002167}
2168#endif
2169
Bram Moolenaara7043832005-01-21 11:56:39 +00002170/*
2171 * List Vim variables.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_vim_vars(first)
2175 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178}
2179
2180/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181 * List script-local variables, if there is a script.
2182 */
2183 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_script_vars(first)
2185 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186{
2187 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2189 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002190}
2191
2192/*
2193 * List function variables, if there is a function.
2194 */
2195 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196list_func_vars(first)
2197 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198{
2199 if (current_funccal != NULL)
2200 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002202}
2203
2204/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 * List variables in "arg".
2206 */
2207 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 exarg_T *eap;
2210 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212{
2213 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 char_u *name_start;
2217 char_u *arg_subsc;
2218 char_u *tofree;
2219 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220
2221 while (!ends_excmd(*arg) && !got_int)
2222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002225 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2227 {
2228 emsg_severe = TRUE;
2229 EMSG(_(e_trailing));
2230 break;
2231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* get_name_len() takes care of expanding curly braces */
2236 name_start = name = arg;
2237 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2238 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 /* This is mainly to keep test 49 working: when expanding
2241 * curly braces fails overrule the exception error message. */
2242 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 emsg_severe = TRUE;
2245 EMSG2(_(e_invarg2), arg);
2246 break;
2247 }
2248 error = TRUE;
2249 }
2250 else
2251 {
2252 if (tofree != NULL)
2253 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002254 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 else
2257 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 /* handle d.key, l[idx], f(expr) */
2259 arg_subsc = arg;
2260 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 'g': list_glob_vars(first); break;
2269 case 'b': list_buf_vars(first); break;
2270 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002271#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002273#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'v': list_vim_vars(first); break;
2275 case 's': list_script_vars(first); break;
2276 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 default:
2278 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002279 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002280 }
2281 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 {
2283 char_u numbuf[NUMBUFLEN];
2284 char_u *tf;
2285 int c;
2286 char_u *s;
2287
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002288 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 c = *arg;
2290 *arg = NUL;
2291 list_one_var_a((char_u *)"",
2292 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002293 tv.v_type,
2294 s == NULL ? (char_u *)"" : s,
2295 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 *arg = c;
2297 vim_free(tf);
2298 }
2299 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
2302 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303
2304 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306
2307 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 }
2309
2310 return arg;
2311}
2312
2313/*
2314 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2315 * Returns a pointer to the char just after the var name.
2316 * Returns NULL if there is an error.
2317 */
2318 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002319ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002321 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002323 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325{
2326 int c1;
2327 char_u *name;
2328 char_u *p;
2329 char_u *arg_end = NULL;
2330 int len;
2331 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333
2334 /*
2335 * ":let $VAR = expr": Set environment variable.
2336 */
2337 if (*arg == '$')
2338 {
2339 /* Find the end of the name. */
2340 ++arg;
2341 name = arg;
2342 len = get_env_len(&arg);
2343 if (len == 0)
2344 EMSG2(_(e_invarg2), name - 1);
2345 else
2346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 if (op != NULL && (*op == '+' || *op == '-'))
2348 EMSG2(_(e_letwrong), op);
2349 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002350 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002352 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 {
2354 c1 = name[len];
2355 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 p = get_tv_string_chk(tv);
2357 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 {
2359 int mustfree = FALSE;
2360 char_u *s = vim_getenv(name, &mustfree);
2361
2362 if (s != NULL)
2363 {
2364 p = tofree = concat_str(s, p);
2365 if (mustfree)
2366 vim_free(s);
2367 }
2368 }
2369 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002372 if (STRICMP(name, "HOME") == 0)
2373 init_homedir();
2374 else if (didset_vim && STRICMP(name, "VIM") == 0)
2375 didset_vim = FALSE;
2376 else if (didset_vimruntime
2377 && STRICMP(name, "VIMRUNTIME") == 0)
2378 didset_vimruntime = FALSE;
2379 arg_end = arg;
2380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002382 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 }
2384 }
2385 }
2386
2387 /*
2388 * ":let &option = expr": Set option value.
2389 * ":let &l:option = expr": Set local option value.
2390 * ":let &g:option = expr": Set global option value.
2391 */
2392 else if (*arg == '&')
2393 {
2394 /* Find the end of the name. */
2395 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002396 if (p == NULL || (endchars != NULL
2397 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002398 EMSG(_(e_letunexp));
2399 else
2400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 long n;
2402 int opt_type;
2403 long numval;
2404 char_u *stringval = NULL;
2405 char_u *s;
2406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002407 c1 = *p;
2408 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409
2410 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 s = get_tv_string_chk(tv); /* != NULL if number or string */
2412 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 {
2414 opt_type = get_option_value(arg, &numval,
2415 &stringval, opt_flags);
2416 if ((opt_type == 1 && *op == '.')
2417 || (opt_type == 0 && *op != '.'))
2418 EMSG2(_(e_letwrong), op);
2419 else
2420 {
2421 if (opt_type == 1) /* number */
2422 {
2423 if (*op == '+')
2424 n = numval + n;
2425 else
2426 n = numval - n;
2427 }
2428 else if (opt_type == 0 && stringval != NULL) /* string */
2429 {
2430 s = concat_str(stringval, s);
2431 vim_free(stringval);
2432 stringval = s;
2433 }
2434 }
2435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002436 if (s != NULL)
2437 {
2438 set_option_value(arg, n, s, opt_flags);
2439 arg_end = p;
2440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 }
2444 }
2445
2446 /*
2447 * ":let @r = expr": Set register contents.
2448 */
2449 else if (*arg == '@')
2450 {
2451 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 if (op != NULL && (*op == '+' || *op == '-'))
2453 EMSG2(_(e_letwrong), op);
2454 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 EMSG(_(e_letunexp));
2457 else
2458 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002459 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 char_u *s;
2461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002462 p = get_tv_string_chk(tv);
2463 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002465 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 if (s != NULL)
2467 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002468 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 vim_free(s);
2470 }
2471 }
2472 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 arg_end = arg + 1;
2476 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002477 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479 }
2480
2481 /*
2482 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002485 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002487 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002489 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2493 EMSG(_(e_letunexp));
2494 else
2495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 arg_end = p;
2498 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002499 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 }
2502
2503 else
2504 EMSG2(_(e_invarg2), arg);
2505
2506 return arg_end;
2507}
2508
2509/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002510 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2511 */
2512 static int
2513check_changedtick(arg)
2514 char_u *arg;
2515{
2516 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2517 {
2518 EMSG2(_(e_readonlyvar), arg);
2519 return TRUE;
2520 }
2521 return FALSE;
2522}
2523
2524/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 * Get an lval: variable, Dict item or List item that can be assigned a value
2526 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2527 * "name.key", "name.key[expr]" etc.
2528 * Indexing only works if "name" is an existing List or Dictionary.
2529 * "name" points to the start of the name.
2530 * If "rettv" is not NULL it points to the value to be assigned.
2531 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2532 * wrong; must end in space or cmd separator.
2533 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002534 * flags:
2535 * GLV_QUIET: do not give error messages
2536 * GLV_NO_AUTOLOAD: do not use script autoloading
2537 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002539 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 */
2542 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002543get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 typval_T *rettv;
2546 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 int unlet;
2548 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002551{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 char_u *expr_start, *expr_end;
2554 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 dictitem_T *v;
2556 typval_T var1;
2557 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002561 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002564
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002566 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567
2568 if (skip)
2569 {
2570 /* When skipping just find the end of the name. */
2571 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 }
2574
2575 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002576 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (expr_start != NULL)
2578 {
2579 /* Don't expand the name when we already know there is an error. */
2580 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2581 && *p != '[' && *p != '.')
2582 {
2583 EMSG(_(e_trailing));
2584 return NULL;
2585 }
2586
2587 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2588 if (lp->ll_exp_name == NULL)
2589 {
2590 /* Report an invalid expression in braces, unless the
2591 * expression evaluation has been cancelled due to an
2592 * aborting error, an interrupt, or an exception. */
2593 if (!aborting() && !quiet)
2594 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002595 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 EMSG2(_(e_invarg2), name);
2597 return NULL;
2598 }
2599 }
2600 lp->ll_name = lp->ll_exp_name;
2601 }
2602 else
2603 lp->ll_name = name;
2604
2605 /* Without [idx] or .key we are done. */
2606 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2607 return p;
2608
2609 cc = *p;
2610 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002611 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (v == NULL && !quiet)
2613 EMSG2(_(e_undefvar), lp->ll_name);
2614 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 if (v == NULL)
2616 return NULL;
2617
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 /*
2619 * Loop until no more [idx] or .key is following.
2620 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002621 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2625 && !(lp->ll_tv->v_type == VAR_DICT
2626 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (!quiet)
2629 EMSG(_("E689: Can only index a List or Dictionary"));
2630 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E708: [:] must come last"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 len = -1;
2640 if (*p == '.')
2641 {
2642 key = p + 1;
2643 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2644 ;
2645 if (len == 0)
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (!quiet)
2648 EMSG(_(e_emptykey));
2649 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002650 }
2651 p = key + len;
2652 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 else
2654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002656 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 if (*p == ':')
2658 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 empty1 = FALSE;
2662 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002664 if (get_tv_string_chk(&var1) == NULL)
2665 {
2666 /* not a number or string */
2667 clear_tv(&var1);
2668 return NULL;
2669 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671
2672 /* Optionally get the second index [ :expr]. */
2673 if (*p == ':')
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002678 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (rettv != NULL && (rettv->v_type != VAR_LIST
2684 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (!empty1)
2689 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
2692 p = skipwhite(p + 1);
2693 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 else
2696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704 if (get_tv_string_chk(&var2) == NULL)
2705 {
2706 /* not a number or string */
2707 if (!empty1)
2708 clear_tv(&var1);
2709 clear_tv(&var2);
2710 return NULL;
2711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (!quiet)
2721 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 if (!empty1)
2723 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
2728
2729 /* Skip to past ']'. */
2730 ++p;
2731 }
2732
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
2735 if (len == -1)
2736 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002738 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 if (*key == NUL)
2740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (!quiet)
2742 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 lp->ll_list = NULL;
2748 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002749 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002750
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002751 /* When assigning to a scope dictionary check that a function and
2752 * variable name is valid (only variable name unless it is l: or
2753 * g: dictionary). Disallow overwriting a builtin function. */
2754 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 int prevval;
2757 int wrong;
2758
2759 if (len != -1)
2760 {
2761 prevval = key[len];
2762 key[len] = NUL;
2763 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002764 else
2765 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002766 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2767 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002769 || !valid_varname(key);
2770 if (len != -1)
2771 key[len] = prevval;
2772 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002773 return NULL;
2774 }
2775
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002778 /* Can't add "v:" variable. */
2779 if (lp->ll_dict == &vimvardict)
2780 {
2781 EMSG2(_(e_illvar), name);
2782 return NULL;
2783 }
2784
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002785 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002789 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (len == -1)
2791 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 }
2794 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 p = NULL;
2802 break;
2803 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 /* existing variable, need to check if it can be changed */
2805 else if (var_check_ro(lp->ll_di->di_flags, name))
2806 return NULL;
2807
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 if (len == -1)
2809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 else
2813 {
2814 /*
2815 * Get the number and item for the only or first index of the List.
2816 */
2817 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 else
2820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002821 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 clear_tv(&var1);
2823 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002824 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_list = lp->ll_tv->vval.v_list;
2826 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2827 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002829 if (lp->ll_n1 < 0)
2830 {
2831 lp->ll_n1 = 0;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 }
2834 }
2835 if (lp->ll_li == NULL)
2836 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002839 if (!quiet)
2840 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843
2844 /*
2845 * May need to find the item or absolute index for the second
2846 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 * When no index given: "lp->ll_empty2" is TRUE.
2848 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002852 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 {
2859 if (!quiet)
2860 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002862 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 }
2865
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2867 if (lp->ll_n1 < 0)
2868 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2869 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 {
2871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002875 }
2876
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002879 }
2880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 return p;
2882}
2883
2884/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 */
2887 static void
2888clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002889 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890{
2891 vim_free(lp->ll_exp_name);
2892 vim_free(lp->ll_newkey);
2893}
2894
2895/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 */
2900 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002901set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002902 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002904 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907{
2908 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002909 listitem_T *ri;
2910 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911
2912 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002913 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 cc = *endp;
2917 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 if (op != NULL && *op != '=')
2919 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002922 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002923 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002924 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 {
2926 if (tv_op(&tv, rettv, op) == OK)
2927 set_var(lp->ll_name, &tv, FALSE);
2928 clear_tv(&tv);
2929 }
2930 }
2931 else
2932 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002934 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002936 else if (tv_check_lock(lp->ll_newkey == NULL
2937 ? lp->ll_tv->v_lock
2938 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2939 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 else if (lp->ll_range)
2941 {
2942 /*
2943 * Assign the List values to the list items.
2944 */
2945 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 if (op != NULL && *op != '=')
2948 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2949 else
2950 {
2951 clear_tv(&lp->ll_li->li_tv);
2952 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2953 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 ri = ri->li_next;
2955 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2956 break;
2957 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002959 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002960 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 ri = NULL;
2963 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002964 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 lp->ll_li = lp->ll_li->li_next;
2967 ++lp->ll_n1;
2968 }
2969 if (ri != NULL)
2970 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 else if (lp->ll_empty2
2972 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 : lp->ll_n1 != lp->ll_n2)
2974 EMSG(_("E711: List value has not enough items"));
2975 }
2976 else
2977 {
2978 /*
2979 * Assign to a List or Dictionary item.
2980 */
2981 if (lp->ll_newkey != NULL)
2982 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 {
2985 EMSG2(_(e_letwrong), op);
2986 return;
2987 }
2988
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002990 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 if (di == NULL)
2992 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002993 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2994 {
2995 vim_free(di);
2996 return;
2997 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 else if (op != NULL && *op != '=')
3001 {
3002 tv_op(lp->ll_tv, rettv, op);
3003 return;
3004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003005 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003007
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 /*
3009 * Assign the value to the variable or list item.
3010 */
3011 if (copy)
3012 copy_tv(rettv, lp->ll_tv);
3013 else
3014 {
3015 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003016 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003018 }
3019 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003020}
3021
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003022/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3024 * Returns OK or FAIL.
3025 */
3026 static int
3027tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003028 typval_T *tv1;
3029 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 char_u *op;
3031{
3032 long n;
3033 char_u numbuf[NUMBUFLEN];
3034 char_u *s;
3035
3036 /* Can't do anything with a Funcref or a Dict on the right. */
3037 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3038 {
3039 switch (tv1->v_type)
3040 {
3041 case VAR_DICT:
3042 case VAR_FUNC:
3043 break;
3044
3045 case VAR_LIST:
3046 if (*op != '+' || tv2->v_type != VAR_LIST)
3047 break;
3048 /* List += List */
3049 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3050 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3051 return OK;
3052
3053 case VAR_NUMBER:
3054 case VAR_STRING:
3055 if (tv2->v_type == VAR_LIST)
3056 break;
3057 if (*op == '+' || *op == '-')
3058 {
3059 /* nr += nr or nr -= nr*/
3060 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003061#ifdef FEAT_FLOAT
3062 if (tv2->v_type == VAR_FLOAT)
3063 {
3064 float_T f = n;
3065
3066 if (*op == '+')
3067 f += tv2->vval.v_float;
3068 else
3069 f -= tv2->vval.v_float;
3070 clear_tv(tv1);
3071 tv1->v_type = VAR_FLOAT;
3072 tv1->vval.v_float = f;
3073 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003075#endif
3076 {
3077 if (*op == '+')
3078 n += get_tv_number(tv2);
3079 else
3080 n -= get_tv_number(tv2);
3081 clear_tv(tv1);
3082 tv1->v_type = VAR_NUMBER;
3083 tv1->vval.v_number = n;
3084 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003085 }
3086 else
3087 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003088 if (tv2->v_type == VAR_FLOAT)
3089 break;
3090
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 /* str .= str */
3092 s = get_tv_string(tv1);
3093 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3094 clear_tv(tv1);
3095 tv1->v_type = VAR_STRING;
3096 tv1->vval.v_string = s;
3097 }
3098 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003099
3100#ifdef FEAT_FLOAT
3101 case VAR_FLOAT:
3102 {
3103 float_T f;
3104
3105 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3106 && tv2->v_type != VAR_NUMBER
3107 && tv2->v_type != VAR_STRING))
3108 break;
3109 if (tv2->v_type == VAR_FLOAT)
3110 f = tv2->vval.v_float;
3111 else
3112 f = get_tv_number(tv2);
3113 if (*op == '+')
3114 tv1->vval.v_float += f;
3115 else
3116 tv1->vval.v_float -= f;
3117 }
3118 return OK;
3119#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003120 }
3121 }
3122
3123 EMSG2(_(e_letwrong), op);
3124 return FAIL;
3125}
3126
3127/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128 * Add a watcher to a list.
3129 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003130 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003132 list_T *l;
3133 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134{
3135 lw->lw_next = l->lv_watch;
3136 l->lv_watch = lw;
3137}
3138
3139/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003140 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141 * No warning when it isn't found...
3142 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003143 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003144list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 list_T *l;
3146 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147{
Bram Moolenaar33570922005-01-25 22:26:29 +00003148 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149
3150 lwp = &l->lv_watch;
3151 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3152 {
3153 if (lw == lwrem)
3154 {
3155 *lwp = lw->lw_next;
3156 break;
3157 }
3158 lwp = &lw->lw_next;
3159 }
3160}
3161
3162/*
3163 * Just before removing an item from a list: advance watchers to the next
3164 * item.
3165 */
3166 static void
3167list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172
3173 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3174 if (lw->lw_item == item)
3175 lw->lw_item = item->li_next;
3176}
3177
3178/*
3179 * Evaluate the expression used in a ":for var in expr" command.
3180 * "arg" points to "var".
3181 * Set "*errp" to TRUE for an error, FALSE otherwise;
3182 * Return a pointer that holds the info. Null when there is an error.
3183 */
3184 void *
3185eval_for_line(arg, errp, nextcmdp, skip)
3186 char_u *arg;
3187 int *errp;
3188 char_u **nextcmdp;
3189 int skip;
3190{
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003193 typval_T tv;
3194 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 *errp = TRUE; /* default: there is an error */
3197
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 if (fi == NULL)
3200 return NULL;
3201
3202 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3203 if (expr == NULL)
3204 return fi;
3205
3206 expr = skipwhite(expr);
3207 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3208 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003209 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210 return fi;
3211 }
3212
3213 if (skip)
3214 ++emsg_skip;
3215 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3216 {
3217 *errp = FALSE;
3218 if (!skip)
3219 {
3220 l = tv.vval.v_list;
3221 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003222 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003224 clear_tv(&tv);
3225 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 else
3227 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003228 /* No need to increment the refcount, it's already set for the
3229 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 fi->fi_list = l;
3231 list_add_watch(l, &fi->fi_lw);
3232 fi->fi_lw.lw_item = l->lv_first;
3233 }
3234 }
3235 }
3236 if (skip)
3237 --emsg_skip;
3238
3239 return fi;
3240}
3241
3242/*
3243 * Use the first item in a ":for" list. Advance to the next.
3244 * Assign the values to the variable (list). "arg" points to the first one.
3245 * Return TRUE when a valid item was found, FALSE when at end of list or
3246 * something wrong.
3247 */
3248 int
3249next_for_item(fi_void, arg)
3250 void *fi_void;
3251 char_u *arg;
3252{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003253 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
3257 item = fi->fi_lw.lw_item;
3258 if (item == NULL)
3259 result = FALSE;
3260 else
3261 {
3262 fi->fi_lw.lw_item = item->li_next;
3263 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3264 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3265 }
3266 return result;
3267}
3268
3269/*
3270 * Free the structure used to store info used by ":for".
3271 */
3272 void
3273free_for_info(fi_void)
3274 void *fi_void;
3275{
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003278 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003281 list_unref(fi->fi_list);
3282 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 vim_free(fi);
3284}
3285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3287
3288 void
3289set_context_for_expression(xp, arg, cmdidx)
3290 expand_T *xp;
3291 char_u *arg;
3292 cmdidx_T cmdidx;
3293{
3294 int got_eq = FALSE;
3295 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 if (cmdidx == CMD_let)
3299 {
3300 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003301 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 {
3303 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003304 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 {
3306 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 if (vim_iswhite(*p))
3309 break;
3310 }
3311 return;
3312 }
3313 }
3314 else
3315 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3316 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 while ((xp->xp_pattern = vim_strpbrk(arg,
3318 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3319 {
3320 c = *xp->xp_pattern;
3321 if (c == '&')
3322 {
3323 c = xp->xp_pattern[1];
3324 if (c == '&')
3325 {
3326 ++xp->xp_pattern;
3327 xp->xp_context = cmdidx != CMD_let || got_eq
3328 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3329 }
3330 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003333 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3334 xp->xp_pattern += 2;
3335
3336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 else if (c == '$')
3339 {
3340 /* environment variable */
3341 xp->xp_context = EXPAND_ENV_VARS;
3342 }
3343 else if (c == '=')
3344 {
3345 got_eq = TRUE;
3346 xp->xp_context = EXPAND_EXPRESSION;
3347 }
3348 else if (c == '<'
3349 && xp->xp_context == EXPAND_FUNCTIONS
3350 && vim_strchr(xp->xp_pattern, '(') == NULL)
3351 {
3352 /* Function name can start with "<SNR>" */
3353 break;
3354 }
3355 else if (cmdidx != CMD_let || got_eq)
3356 {
3357 if (c == '"') /* string */
3358 {
3359 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3360 if (c == '\\' && xp->xp_pattern[1] != NUL)
3361 ++xp->xp_pattern;
3362 xp->xp_context = EXPAND_NOTHING;
3363 }
3364 else if (c == '\'') /* literal string */
3365 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003366 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3368 /* skip */ ;
3369 xp->xp_context = EXPAND_NOTHING;
3370 }
3371 else if (c == '|')
3372 {
3373 if (xp->xp_pattern[1] == '|')
3374 {
3375 ++xp->xp_pattern;
3376 xp->xp_context = EXPAND_EXPRESSION;
3377 }
3378 else
3379 xp->xp_context = EXPAND_COMMANDS;
3380 }
3381 else
3382 xp->xp_context = EXPAND_EXPRESSION;
3383 }
3384 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003385 /* Doesn't look like something valid, expand as an expression
3386 * anyway. */
3387 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 arg = xp->xp_pattern;
3389 if (*arg != NUL)
3390 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3391 /* skip */ ;
3392 }
3393 xp->xp_pattern = arg;
3394}
3395
3396#endif /* FEAT_CMDL_COMPL */
3397
3398/*
3399 * ":1,25call func(arg1, arg2)" function call.
3400 */
3401 void
3402ex_call(eap)
3403 exarg_T *eap;
3404{
3405 char_u *arg = eap->arg;
3406 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003408 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 linenr_T lnum;
3412 int doesrange;
3413 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003414 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003416 if (eap->skip)
3417 {
3418 /* trans_function_name() doesn't work well when skipping, use eval0()
3419 * instead to skip to any following command, e.g. for:
3420 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003421 ++emsg_skip;
3422 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3423 clear_tv(&rettv);
3424 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003425 return;
3426 }
3427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003429 if (fudi.fd_newkey != NULL)
3430 {
3431 /* Still need to give an error message for missing key. */
3432 EMSG2(_(e_dictkey), fudi.fd_newkey);
3433 vim_free(fudi.fd_newkey);
3434 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 if (tofree == NULL)
3436 return;
3437
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003438 /* Increase refcount on dictionary, it could get deleted when evaluating
3439 * the arguments. */
3440 if (fudi.fd_dict != NULL)
3441 ++fudi.fd_dict->dv_refcount;
3442
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003443 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003444 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003445 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaar532c7802005-01-27 14:44:31 +00003447 /* Skip white space to allow ":call func ()". Not good, but required for
3448 * backward compatibility. */
3449 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
3452 if (*startarg != '(')
3453 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003454 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 goto end;
3456 }
3457
3458 /*
3459 * When skipping, evaluate the function once, to find the end of the
3460 * arguments.
3461 * When the function takes a range, this is discovered after the first
3462 * call, and the loop is broken.
3463 */
3464 if (eap->skip)
3465 {
3466 ++emsg_skip;
3467 lnum = eap->line2; /* do it once, also with an invalid range */
3468 }
3469 else
3470 lnum = eap->line1;
3471 for ( ; lnum <= eap->line2; ++lnum)
3472 {
3473 if (!eap->skip && eap->addr_count > 0)
3474 {
3475 curwin->w_cursor.lnum = lnum;
3476 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003477#ifdef FEAT_VIRTUALEDIT
3478 curwin->w_cursor.coladd = 0;
3479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003482 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 eap->line1, eap->line2, &doesrange,
3484 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
3486 failed = TRUE;
3487 break;
3488 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003489
3490 /* Handle a function returning a Funcref, Dictionary or List. */
3491 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3492 {
3493 failed = TRUE;
3494 break;
3495 }
3496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (doesrange || eap->skip)
3499 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003502 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003504 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (aborting())
3506 break;
3507 }
3508 if (eap->skip)
3509 --emsg_skip;
3510
3511 if (!failed)
3512 {
3513 /* Check for trailing illegal characters and a following command. */
3514 if (!ends_excmd(*arg))
3515 {
3516 emsg_severe = TRUE;
3517 EMSG(_(e_trailing));
3518 }
3519 else
3520 eap->nextcmd = check_nextcmd(arg);
3521 }
3522
3523end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003524 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003525 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526}
3527
3528/*
3529 * ":unlet[!] var1 ... " command.
3530 */
3531 void
3532ex_unlet(eap)
3533 exarg_T *eap;
3534{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003535 ex_unletlock(eap, eap->arg, 0);
3536}
3537
3538/*
3539 * ":lockvar" and ":unlockvar" commands
3540 */
3541 void
3542ex_lockvar(eap)
3543 exarg_T *eap;
3544{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003546 int deep = 2;
3547
3548 if (eap->forceit)
3549 deep = -1;
3550 else if (vim_isdigit(*arg))
3551 {
3552 deep = getdigits(&arg);
3553 arg = skipwhite(arg);
3554 }
3555
3556 ex_unletlock(eap, arg, deep);
3557}
3558
3559/*
3560 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3561 */
3562 static void
3563ex_unletlock(eap, argstart, deep)
3564 exarg_T *eap;
3565 char_u *argstart;
3566 int deep;
3567{
3568 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572
3573 do
3574 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003575 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003576 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003577 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003578 if (lv.ll_name == NULL)
3579 error = TRUE; /* error but continue parsing */
3580 if (name_end == NULL || (!vim_iswhite(*name_end)
3581 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003583 if (name_end != NULL)
3584 {
3585 emsg_severe = TRUE;
3586 EMSG(_(e_trailing));
3587 }
3588 if (!(eap->skip || error))
3589 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 break;
3591 }
3592
3593 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 {
3595 if (eap->cmdidx == CMD_unlet)
3596 {
3597 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3598 error = TRUE;
3599 }
3600 else
3601 {
3602 if (do_lock_var(&lv, name_end, deep,
3603 eap->cmdidx == CMD_lockvar) == FAIL)
3604 error = TRUE;
3605 }
3606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608 if (!eap->skip)
3609 clear_lval(&lv);
3610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 arg = skipwhite(name_end);
3612 } while (!ends_excmd(*arg));
3613
3614 eap->nextcmd = check_nextcmd(arg);
3615}
3616
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003619 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 int forceit;
3622{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 int ret = OK;
3624 int cc;
3625
3626 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003628 cc = *name_end;
3629 *name_end = NUL;
3630
3631 /* Normal name or expanded name. */
3632 if (check_changedtick(lp->ll_name))
3633 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003637 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3639 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 else if (lp->ll_range)
3641 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643
3644 /* Delete a range of List items. */
3645 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3646 {
3647 li = lp->ll_li->li_next;
3648 listitem_remove(lp->ll_list, lp->ll_li);
3649 lp->ll_li = li;
3650 ++lp->ll_n1;
3651 }
3652 }
3653 else
3654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 /* unlet a List item. */
3657 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003660 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 }
3662
3663 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003664}
3665
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666/*
3667 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 */
3670 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
Bram Moolenaar33570922005-01-25 22:26:29 +00003675 hashtab_T *ht;
3676 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003677 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003678 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar33570922005-01-25 22:26:29 +00003680 ht = find_var_ht(name, &varname);
3681 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 hi = hash_find(ht, varname);
3684 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003685 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003686 di = HI2DI(hi);
3687 if (var_check_fixed(di->di_flags, name)
3688 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689 return FAIL;
3690 delete_var(ht, hi);
3691 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003694 if (forceit)
3695 return OK;
3696 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return FAIL;
3698}
3699
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003700/*
3701 * Lock or unlock variable indicated by "lp".
3702 * "deep" is the levels to go (-1 for unlimited);
3703 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3704 */
3705 static int
3706do_lock_var(lp, name_end, deep, lock)
3707 lval_T *lp;
3708 char_u *name_end;
3709 int deep;
3710 int lock;
3711{
3712 int ret = OK;
3713 int cc;
3714 dictitem_T *di;
3715
3716 if (deep == 0) /* nothing to do */
3717 return OK;
3718
3719 if (lp->ll_tv == NULL)
3720 {
3721 cc = *name_end;
3722 *name_end = NUL;
3723
3724 /* Normal name or expanded name. */
3725 if (check_changedtick(lp->ll_name))
3726 ret = FAIL;
3727 else
3728 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003729 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003730 if (di == NULL)
3731 ret = FAIL;
3732 else
3733 {
3734 if (lock)
3735 di->di_flags |= DI_FLAGS_LOCK;
3736 else
3737 di->di_flags &= ~DI_FLAGS_LOCK;
3738 item_lock(&di->di_tv, deep, lock);
3739 }
3740 }
3741 *name_end = cc;
3742 }
3743 else if (lp->ll_range)
3744 {
3745 listitem_T *li = lp->ll_li;
3746
3747 /* (un)lock a range of List items. */
3748 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3749 {
3750 item_lock(&li->li_tv, deep, lock);
3751 li = li->li_next;
3752 ++lp->ll_n1;
3753 }
3754 }
3755 else if (lp->ll_list != NULL)
3756 /* (un)lock a List item. */
3757 item_lock(&lp->ll_li->li_tv, deep, lock);
3758 else
3759 /* un(lock) a Dictionary item. */
3760 item_lock(&lp->ll_di->di_tv, deep, lock);
3761
3762 return ret;
3763}
3764
3765/*
3766 * Lock or unlock an item. "deep" is nr of levels to go.
3767 */
3768 static void
3769item_lock(tv, deep, lock)
3770 typval_T *tv;
3771 int deep;
3772 int lock;
3773{
3774 static int recurse = 0;
3775 list_T *l;
3776 listitem_T *li;
3777 dict_T *d;
3778 hashitem_T *hi;
3779 int todo;
3780
3781 if (recurse >= DICT_MAXNEST)
3782 {
3783 EMSG(_("E743: variable nested too deep for (un)lock"));
3784 return;
3785 }
3786 if (deep == 0)
3787 return;
3788 ++recurse;
3789
3790 /* lock/unlock the item itself */
3791 if (lock)
3792 tv->v_lock |= VAR_LOCKED;
3793 else
3794 tv->v_lock &= ~VAR_LOCKED;
3795
3796 switch (tv->v_type)
3797 {
3798 case VAR_LIST:
3799 if ((l = tv->vval.v_list) != NULL)
3800 {
3801 if (lock)
3802 l->lv_lock |= VAR_LOCKED;
3803 else
3804 l->lv_lock &= ~VAR_LOCKED;
3805 if (deep < 0 || deep > 1)
3806 /* recursive: lock/unlock the items the List contains */
3807 for (li = l->lv_first; li != NULL; li = li->li_next)
3808 item_lock(&li->li_tv, deep - 1, lock);
3809 }
3810 break;
3811 case VAR_DICT:
3812 if ((d = tv->vval.v_dict) != NULL)
3813 {
3814 if (lock)
3815 d->dv_lock |= VAR_LOCKED;
3816 else
3817 d->dv_lock &= ~VAR_LOCKED;
3818 if (deep < 0 || deep > 1)
3819 {
3820 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003821 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3823 {
3824 if (!HASHITEM_EMPTY(hi))
3825 {
3826 --todo;
3827 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3828 }
3829 }
3830 }
3831 }
3832 }
3833 --recurse;
3834}
3835
Bram Moolenaara40058a2005-07-11 22:42:07 +00003836/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003837 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3838 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003839 */
3840 static int
3841tv_islocked(tv)
3842 typval_T *tv;
3843{
3844 return (tv->v_lock & VAR_LOCKED)
3845 || (tv->v_type == VAR_LIST
3846 && tv->vval.v_list != NULL
3847 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3848 || (tv->v_type == VAR_DICT
3849 && tv->vval.v_dict != NULL
3850 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3851}
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3854/*
3855 * Delete all "menutrans_" variables.
3856 */
3857 void
3858del_menutrans_vars()
3859{
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003861 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862
Bram Moolenaar33570922005-01-25 22:26:29 +00003863 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003864 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003865 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003866 {
3867 if (!HASHITEM_EMPTY(hi))
3868 {
3869 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3871 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003872 }
3873 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003874 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875}
3876#endif
3877
3878#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3879
3880/*
3881 * Local string buffer for the next two functions to store a variable name
3882 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3883 * get_user_var_name().
3884 */
3885
3886static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3887
3888static char_u *varnamebuf = NULL;
3889static int varnamebuflen = 0;
3890
3891/*
3892 * Function to concatenate a prefix and a variable name.
3893 */
3894 static char_u *
3895cat_prefix_varname(prefix, name)
3896 int prefix;
3897 char_u *name;
3898{
3899 int len;
3900
3901 len = (int)STRLEN(name) + 3;
3902 if (len > varnamebuflen)
3903 {
3904 vim_free(varnamebuf);
3905 len += 10; /* some additional space */
3906 varnamebuf = alloc(len);
3907 if (varnamebuf == NULL)
3908 {
3909 varnamebuflen = 0;
3910 return NULL;
3911 }
3912 varnamebuflen = len;
3913 }
3914 *varnamebuf = prefix;
3915 varnamebuf[1] = ':';
3916 STRCPY(varnamebuf + 2, name);
3917 return varnamebuf;
3918}
3919
3920/*
3921 * Function given to ExpandGeneric() to obtain the list of user defined
3922 * (global/buffer/window/built-in) variable names.
3923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 char_u *
3925get_user_var_name(xp, idx)
3926 expand_T *xp;
3927 int idx;
3928{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003929 static long_u gdone;
3930 static long_u bdone;
3931 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932#ifdef FEAT_WINDOWS
3933 static long_u tdone;
3934#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003935 static int vidx;
3936 static hashitem_T *hi;
3937 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003940 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003941 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003942#ifdef FEAT_WINDOWS
3943 tdone = 0;
3944#endif
3945 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003946
3947 /* Global variables */
3948 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003952 else
3953 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 while (HASHITEM_EMPTY(hi))
3955 ++hi;
3956 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3957 return cat_prefix_varname('g', hi->hi_key);
3958 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003960
3961 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003962 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003967 else
3968 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003969 while (HASHITEM_EMPTY(hi))
3970 ++hi;
3971 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 return (char_u *)"b:changedtick";
3977 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003978
3979 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003980 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003981 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003983 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003985 else
3986 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003987 while (HASHITEM_EMPTY(hi))
3988 ++hi;
3989 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003991
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003992#ifdef FEAT_WINDOWS
3993 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003994 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003995 if (tdone < ht->ht_used)
3996 {
3997 if (tdone++ == 0)
3998 hi = ht->ht_array;
3999 else
4000 ++hi;
4001 while (HASHITEM_EMPTY(hi))
4002 ++hi;
4003 return cat_prefix_varname('t', hi->hi_key);
4004 }
4005#endif
4006
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 /* v: variables */
4008 if (vidx < VV_LEN)
4009 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 vim_free(varnamebuf);
4012 varnamebuf = NULL;
4013 varnamebuflen = 0;
4014 return NULL;
4015}
4016
4017#endif /* FEAT_CMDL_COMPL */
4018
4019/*
4020 * types for expressions.
4021 */
4022typedef enum
4023{
4024 TYPE_UNKNOWN = 0
4025 , TYPE_EQUAL /* == */
4026 , TYPE_NEQUAL /* != */
4027 , TYPE_GREATER /* > */
4028 , TYPE_GEQUAL /* >= */
4029 , TYPE_SMALLER /* < */
4030 , TYPE_SEQUAL /* <= */
4031 , TYPE_MATCH /* =~ */
4032 , TYPE_NOMATCH /* !~ */
4033} exptype_T;
4034
4035/*
4036 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4039 */
4040
4041/*
4042 * Handle zero level expression.
4043 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004045 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 * Return OK or FAIL.
4047 */
4048 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 char_u **nextcmd;
4053 int evaluate;
4054{
4055 int ret;
4056 char_u *p;
4057
4058 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (ret == FAIL || !ends_excmd(*p))
4061 {
4062 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 /*
4065 * Report the invalid expression unless the expression evaluation has
4066 * been cancelled due to an aborting error, an interrupt, or an
4067 * exception.
4068 */
4069 if (!aborting())
4070 EMSG2(_(e_invexpr2), arg);
4071 ret = FAIL;
4072 }
4073 if (nextcmd != NULL)
4074 *nextcmd = check_nextcmd(p);
4075
4076 return ret;
4077}
4078
4079/*
4080 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004081 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 *
4083 * "arg" must point to the first non-white of the expression.
4084 * "arg" is advanced to the next non-white after the recognized expression.
4085 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004086 * Note: "rettv.v_lock" is not set.
4087 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 * Return OK or FAIL.
4089 */
4090 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 int evaluate;
4095{
4096 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099 /*
4100 * Get the first variable.
4101 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return FAIL;
4104
4105 if ((*arg)[0] == '?')
4106 {
4107 result = FALSE;
4108 if (evaluate)
4109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004110 int error = FALSE;
4111
4112 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004115 if (error)
4116 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118
4119 /*
4120 * Get the second variable.
4121 */
4122 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125
4126 /*
4127 * Check for the ":".
4128 */
4129 if ((*arg)[0] != ':')
4130 {
4131 EMSG(_("E109: Missing ':' after '?'"));
4132 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 return FAIL;
4135 }
4136
4137 /*
4138 * Get the third variable.
4139 */
4140 *arg = skipwhite(*arg + 1);
4141 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4142 {
4143 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return FAIL;
4146 }
4147 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150
4151 return OK;
4152}
4153
4154/*
4155 * Handle first level expression:
4156 * expr2 || expr2 || expr2 logical OR
4157 *
4158 * "arg" must point to the first non-white of the expression.
4159 * "arg" is advanced to the next non-white after the recognized expression.
4160 *
4161 * Return OK or FAIL.
4162 */
4163 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int evaluate;
4168{
Bram Moolenaar33570922005-01-25 22:26:29 +00004169 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 long result;
4171 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /*
4175 * Get the first variable.
4176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 /*
4181 * Repeat until there is no following "||".
4182 */
4183 first = TRUE;
4184 result = FALSE;
4185 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4186 {
4187 if (evaluate && first)
4188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (error)
4193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 first = FALSE;
4195 }
4196
4197 /*
4198 * Get the second variable.
4199 */
4200 *arg = skipwhite(*arg + 2);
4201 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4202 return FAIL;
4203
4204 /*
4205 * Compute the result.
4206 */
4207 if (evaluate && !result)
4208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004209 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 if (error)
4213 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 if (evaluate)
4216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 rettv->v_type = VAR_NUMBER;
4218 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * Handle second level expression:
4227 * expr3 && expr3 && expr3 logical AND
4228 *
4229 * "arg" must point to the first non-white of the expression.
4230 * "arg" is advanced to the next non-white after the recognized expression.
4231 *
4232 * Return OK or FAIL.
4233 */
4234 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004235eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 int evaluate;
4239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "&&".
4253 */
4254 first = TRUE;
4255 result = TRUE;
4256 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval4(arg, &var2, evaluate && result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle third level expression:
4298 * var1 == var2
4299 * var1 =~ var2
4300 * var1 != var2
4301 * var1 !~ var2
4302 * var1 > var2
4303 * var1 >= var2
4304 * var1 < var2
4305 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004306 * var1 is var2
4307 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 *
4309 * "arg" must point to the first non-white of the expression.
4310 * "arg" is advanced to the next non-white after the recognized expression.
4311 *
4312 * Return OK or FAIL.
4313 */
4314 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 int evaluate;
4319{
Bram Moolenaar33570922005-01-25 22:26:29 +00004320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 char_u *p;
4322 int i;
4323 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004324 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 int len = 2;
4326 long n1, n2;
4327 char_u *s1, *s2;
4328 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4329 regmatch_T regmatch;
4330 int ic;
4331 char_u *save_cpo;
4332
4333 /*
4334 * Get the first variable.
4335 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return FAIL;
4338
4339 p = *arg;
4340 switch (p[0])
4341 {
4342 case '=': if (p[1] == '=')
4343 type = TYPE_EQUAL;
4344 else if (p[1] == '~')
4345 type = TYPE_MATCH;
4346 break;
4347 case '!': if (p[1] == '=')
4348 type = TYPE_NEQUAL;
4349 else if (p[1] == '~')
4350 type = TYPE_NOMATCH;
4351 break;
4352 case '>': if (p[1] != '=')
4353 {
4354 type = TYPE_GREATER;
4355 len = 1;
4356 }
4357 else
4358 type = TYPE_GEQUAL;
4359 break;
4360 case '<': if (p[1] != '=')
4361 {
4362 type = TYPE_SMALLER;
4363 len = 1;
4364 }
4365 else
4366 type = TYPE_SEQUAL;
4367 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004368 case 'i': if (p[1] == 's')
4369 {
4370 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4371 len = 5;
4372 if (!vim_isIDc(p[len]))
4373 {
4374 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4375 type_is = TRUE;
4376 }
4377 }
4378 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380
4381 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004382 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 */
4384 if (type != TYPE_UNKNOWN)
4385 {
4386 /* extra question mark appended: ignore case */
4387 if (p[len] == '?')
4388 {
4389 ic = TRUE;
4390 ++len;
4391 }
4392 /* extra '#' appended: match case */
4393 else if (p[len] == '#')
4394 {
4395 ic = FALSE;
4396 ++len;
4397 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004398 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 else
4400 ic = p_ic;
4401
4402 /*
4403 * Get the second variable.
4404 */
4405 *arg = skipwhite(p + len);
4406 if (eval5(arg, &var2, evaluate) == FAIL)
4407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return FAIL;
4410 }
4411
4412 if (evaluate)
4413 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 if (type_is && rettv->v_type != var2.v_type)
4415 {
4416 /* For "is" a different type always means FALSE, for "notis"
4417 * it means TRUE. */
4418 n1 = (type == TYPE_NEQUAL);
4419 }
4420 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4421 {
4422 if (type_is)
4423 {
4424 n1 = (rettv->v_type == var2.v_type
4425 && rettv->vval.v_list == var2.vval.v_list);
4426 if (type == TYPE_NEQUAL)
4427 n1 = !n1;
4428 }
4429 else if (rettv->v_type != var2.v_type
4430 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4431 {
4432 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004433 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004435 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 clear_tv(rettv);
4437 clear_tv(&var2);
4438 return FAIL;
4439 }
4440 else
4441 {
4442 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004443 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4444 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 if (type == TYPE_NEQUAL)
4446 n1 = !n1;
4447 }
4448 }
4449
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004450 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4451 {
4452 if (type_is)
4453 {
4454 n1 = (rettv->v_type == var2.v_type
4455 && rettv->vval.v_dict == var2.vval.v_dict);
4456 if (type == TYPE_NEQUAL)
4457 n1 = !n1;
4458 }
4459 else if (rettv->v_type != var2.v_type
4460 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4461 {
4462 if (rettv->v_type != var2.v_type)
4463 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4464 else
4465 EMSG(_("E736: Invalid operation for Dictionary"));
4466 clear_tv(rettv);
4467 clear_tv(&var2);
4468 return FAIL;
4469 }
4470 else
4471 {
4472 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004473 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4474 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004475 if (type == TYPE_NEQUAL)
4476 n1 = !n1;
4477 }
4478 }
4479
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004480 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4481 {
4482 if (rettv->v_type != var2.v_type
4483 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4484 {
4485 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004486 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004488 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 clear_tv(rettv);
4490 clear_tv(&var2);
4491 return FAIL;
4492 }
4493 else
4494 {
4495 /* Compare two Funcrefs for being equal or unequal. */
4496 if (rettv->vval.v_string == NULL
4497 || var2.vval.v_string == NULL)
4498 n1 = FALSE;
4499 else
4500 n1 = STRCMP(rettv->vval.v_string,
4501 var2.vval.v_string) == 0;
4502 if (type == TYPE_NEQUAL)
4503 n1 = !n1;
4504 }
4505 }
4506
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004507#ifdef FEAT_FLOAT
4508 /*
4509 * If one of the two variables is a float, compare as a float.
4510 * When using "=~" or "!~", always compare as string.
4511 */
4512 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4513 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4514 {
4515 float_T f1, f2;
4516
4517 if (rettv->v_type == VAR_FLOAT)
4518 f1 = rettv->vval.v_float;
4519 else
4520 f1 = get_tv_number(rettv);
4521 if (var2.v_type == VAR_FLOAT)
4522 f2 = var2.vval.v_float;
4523 else
4524 f2 = get_tv_number(&var2);
4525 n1 = FALSE;
4526 switch (type)
4527 {
4528 case TYPE_EQUAL: n1 = (f1 == f2); break;
4529 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4530 case TYPE_GREATER: n1 = (f1 > f2); break;
4531 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4532 case TYPE_SMALLER: n1 = (f1 < f2); break;
4533 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4534 case TYPE_UNKNOWN:
4535 case TYPE_MATCH:
4536 case TYPE_NOMATCH: break; /* avoid gcc warning */
4537 }
4538 }
4539#endif
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 /*
4542 * If one of the two variables is a number, compare as a number.
4543 * When using "=~" or "!~", always compare as string.
4544 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004545 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 n1 = get_tv_number(rettv);
4549 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 switch (type)
4551 {
4552 case TYPE_EQUAL: n1 = (n1 == n2); break;
4553 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4554 case TYPE_GREATER: n1 = (n1 > n2); break;
4555 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4556 case TYPE_SMALLER: n1 = (n1 < n2); break;
4557 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4558 case TYPE_UNKNOWN:
4559 case TYPE_MATCH:
4560 case TYPE_NOMATCH: break; /* avoid gcc warning */
4561 }
4562 }
4563 else
4564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004565 s1 = get_tv_string_buf(rettv, buf1);
4566 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4568 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4569 else
4570 i = 0;
4571 n1 = FALSE;
4572 switch (type)
4573 {
4574 case TYPE_EQUAL: n1 = (i == 0); break;
4575 case TYPE_NEQUAL: n1 = (i != 0); break;
4576 case TYPE_GREATER: n1 = (i > 0); break;
4577 case TYPE_GEQUAL: n1 = (i >= 0); break;
4578 case TYPE_SMALLER: n1 = (i < 0); break;
4579 case TYPE_SEQUAL: n1 = (i <= 0); break;
4580
4581 case TYPE_MATCH:
4582 case TYPE_NOMATCH:
4583 /* avoid 'l' flag in 'cpoptions' */
4584 save_cpo = p_cpo;
4585 p_cpo = (char_u *)"";
4586 regmatch.regprog = vim_regcomp(s2,
4587 RE_MAGIC + RE_STRING);
4588 regmatch.rm_ic = ic;
4589 if (regmatch.regprog != NULL)
4590 {
4591 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004592 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (type == TYPE_NOMATCH)
4594 n1 = !n1;
4595 }
4596 p_cpo = save_cpo;
4597 break;
4598
4599 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4600 }
4601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602 clear_tv(rettv);
4603 clear_tv(&var2);
4604 rettv->v_type = VAR_NUMBER;
4605 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607 }
4608
4609 return OK;
4610}
4611
4612/*
4613 * Handle fourth level expression:
4614 * + number addition
4615 * - number subtraction
4616 * . string concatenation
4617 *
4618 * "arg" must point to the first non-white of the expression.
4619 * "arg" is advanced to the next non-white after the recognized expression.
4620 *
4621 * Return OK or FAIL.
4622 */
4623 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004624eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 int evaluate;
4628{
Bram Moolenaar33570922005-01-25 22:26:29 +00004629 typval_T var2;
4630 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 int op;
4632 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633#ifdef FEAT_FLOAT
4634 float_T f1 = 0, f2 = 0;
4635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 char_u *s1, *s2;
4637 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4638 char_u *p;
4639
4640 /*
4641 * Get the first variable.
4642 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004643 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 return FAIL;
4645
4646 /*
4647 * Repeat computing, until no '+', '-' or '.' is following.
4648 */
4649 for (;;)
4650 {
4651 op = **arg;
4652 if (op != '+' && op != '-' && op != '.')
4653 break;
4654
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004655 if ((op != '+' || rettv->v_type != VAR_LIST)
4656#ifdef FEAT_FLOAT
4657 && (op == '.' || rettv->v_type != VAR_FLOAT)
4658#endif
4659 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004660 {
4661 /* For "list + ...", an illegal use of the first operand as
4662 * a number cannot be determined before evaluating the 2nd
4663 * operand: if this is also a list, all is ok.
4664 * For "something . ...", "something - ..." or "non-list + ...",
4665 * we know that the first operand needs to be a string or number
4666 * without evaluating the 2nd operand. So check before to avoid
4667 * side effects after an error. */
4668 if (evaluate && get_tv_string_chk(rettv) == NULL)
4669 {
4670 clear_tv(rettv);
4671 return FAIL;
4672 }
4673 }
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 /*
4676 * Get the second variable.
4677 */
4678 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004679 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 return FAIL;
4683 }
4684
4685 if (evaluate)
4686 {
4687 /*
4688 * Compute the result.
4689 */
4690 if (op == '.')
4691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4693 s2 = get_tv_string_buf_chk(&var2, buf2);
4694 if (s2 == NULL) /* type error ? */
4695 {
4696 clear_tv(rettv);
4697 clear_tv(&var2);
4698 return FAIL;
4699 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004700 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 clear_tv(rettv);
4702 rettv->v_type = VAR_STRING;
4703 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004705 else if (op == '+' && rettv->v_type == VAR_LIST
4706 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004707 {
4708 /* concatenate Lists */
4709 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4710 &var3) == FAIL)
4711 {
4712 clear_tv(rettv);
4713 clear_tv(&var2);
4714 return FAIL;
4715 }
4716 clear_tv(rettv);
4717 *rettv = var3;
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 else
4720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 int error = FALSE;
4722
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723#ifdef FEAT_FLOAT
4724 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004725 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004726 f1 = rettv->vval.v_float;
4727 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004728 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004729 else
4730#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732 n1 = get_tv_number_chk(rettv, &error);
4733 if (error)
4734 {
4735 /* This can only happen for "list + non-list". For
4736 * "non-list + ..." or "something - ...", we returned
4737 * before evaluating the 2nd operand. */
4738 clear_tv(rettv);
4739 return FAIL;
4740 }
4741#ifdef FEAT_FLOAT
4742 if (var2.v_type == VAR_FLOAT)
4743 f1 = n1;
4744#endif
4745 }
4746#ifdef FEAT_FLOAT
4747 if (var2.v_type == VAR_FLOAT)
4748 {
4749 f2 = var2.vval.v_float;
4750 n2 = 0;
4751 }
4752 else
4753#endif
4754 {
4755 n2 = get_tv_number_chk(&var2, &error);
4756 if (error)
4757 {
4758 clear_tv(rettv);
4759 clear_tv(&var2);
4760 return FAIL;
4761 }
4762#ifdef FEAT_FLOAT
4763 if (rettv->v_type == VAR_FLOAT)
4764 f2 = n2;
4765#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004767 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768
4769#ifdef FEAT_FLOAT
4770 /* If there is a float on either side the result is a float. */
4771 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4772 {
4773 if (op == '+')
4774 f1 = f1 + f2;
4775 else
4776 f1 = f1 - f2;
4777 rettv->v_type = VAR_FLOAT;
4778 rettv->vval.v_float = f1;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781#endif
4782 {
4783 if (op == '+')
4784 n1 = n1 + n2;
4785 else
4786 n1 = n1 - n2;
4787 rettv->v_type = VAR_NUMBER;
4788 rettv->vval.v_number = n1;
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004791 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
4793 }
4794 return OK;
4795}
4796
4797/*
4798 * Handle fifth level expression:
4799 * * number multiplication
4800 * / number division
4801 * % number modulo
4802 *
4803 * "arg" must point to the first non-white of the expression.
4804 * "arg" is advanced to the next non-white after the recognized expression.
4805 *
4806 * Return OK or FAIL.
4807 */
4808 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004809eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004813 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar33570922005-01-25 22:26:29 +00004815 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 int op;
4817 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818#ifdef FEAT_FLOAT
4819 int use_float = FALSE;
4820 float_T f1 = 0, f2;
4821#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
4824 /*
4825 * Get the first variable.
4826 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004827 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 return FAIL;
4829
4830 /*
4831 * Repeat computing, until no '*', '/' or '%' is following.
4832 */
4833 for (;;)
4834 {
4835 op = **arg;
4836 if (op != '*' && op != '/' && op != '%')
4837 break;
4838
4839 if (evaluate)
4840 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#ifdef FEAT_FLOAT
4842 if (rettv->v_type == VAR_FLOAT)
4843 {
4844 f1 = rettv->vval.v_float;
4845 use_float = TRUE;
4846 n1 = 0;
4847 }
4848 else
4849#endif
4850 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004852 if (error)
4853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 else
4856 n1 = 0;
4857
4858 /*
4859 * Get the second variable.
4860 */
4861 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004862 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 return FAIL;
4864
4865 if (evaluate)
4866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867#ifdef FEAT_FLOAT
4868 if (var2.v_type == VAR_FLOAT)
4869 {
4870 if (!use_float)
4871 {
4872 f1 = n1;
4873 use_float = TRUE;
4874 }
4875 f2 = var2.vval.v_float;
4876 n2 = 0;
4877 }
4878 else
4879#endif
4880 {
4881 n2 = get_tv_number_chk(&var2, &error);
4882 clear_tv(&var2);
4883 if (error)
4884 return FAIL;
4885#ifdef FEAT_FLOAT
4886 if (use_float)
4887 f2 = n2;
4888#endif
4889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 /*
4892 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#ifdef FEAT_FLOAT
4896 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 if (op == '*')
4899 f1 = f1 * f2;
4900 else if (op == '/')
4901 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902# ifdef VMS
4903 /* VMS crashes on divide by zero, work around it */
4904 if (f2 == 0.0)
4905 {
4906 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004907 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004909 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004910 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004911 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004912 }
4913 else
4914 f1 = f1 / f2;
4915# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 /* We rely on the floating point library to handle divide
4917 * by zero to result in "inf" and not a crash. */
4918 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004919# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004923 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004924 return FAIL;
4925 }
4926 rettv->v_type = VAR_FLOAT;
4927 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 if (op == '*')
4933 n1 = n1 * n2;
4934 else if (op == '/')
4935 {
4936 if (n2 == 0) /* give an error message? */
4937 {
4938 if (n1 == 0)
4939 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4940 else if (n1 < 0)
4941 n1 = -0x7fffffffL;
4942 else
4943 n1 = 0x7fffffffL;
4944 }
4945 else
4946 n1 = n1 / n2;
4947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949 {
4950 if (n2 == 0) /* give an error message? */
4951 n1 = 0;
4952 else
4953 n1 = n1 % n2;
4954 }
4955 rettv->v_type = VAR_NUMBER;
4956 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 }
4960
4961 return OK;
4962}
4963
4964/*
4965 * Handle sixth level expression:
4966 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004967 * "string" string constant
4968 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 * &option-name option value
4970 * @r register contents
4971 * identifier variable value
4972 * function() function call
4973 * $VAR environment variable
4974 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004975 * [expr, expr] List
4976 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 *
4978 * Also handle:
4979 * ! in front logical NOT
4980 * - in front unary minus
4981 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 * trailing [] subscript in String or List
4983 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 *
4985 * "arg" must point to the first non-white of the expression.
4986 * "arg" is advanced to the next non-white after the recognized expression.
4987 *
4988 * Return OK or FAIL.
4989 */
4990 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004991eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004995 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 long n;
4998 int len;
4999 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 char_u *start_leader, *end_leader;
5001 int ret = OK;
5002 char_u *alias;
5003
5004 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005006 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
5010 /*
5011 * Skip '!' and '-' characters. They are handled later.
5012 */
5013 start_leader = *arg;
5014 while (**arg == '!' || **arg == '-' || **arg == '+')
5015 *arg = skipwhite(*arg + 1);
5016 end_leader = *arg;
5017
5018 switch (**arg)
5019 {
5020 /*
5021 * Number constant.
5022 */
5023 case '0':
5024 case '1':
5025 case '2':
5026 case '3':
5027 case '4':
5028 case '5':
5029 case '6':
5030 case '7':
5031 case '8':
5032 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 {
5034#ifdef FEAT_FLOAT
5035 char_u *p = skipdigits(*arg + 1);
5036 int get_float = FALSE;
5037
5038 /* We accept a float when the format matches
5039 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005040 * strict to avoid backwards compatibility problems.
5041 * Don't look for a float after the "." operator, so that
5042 * ":let vers = 1.2.3" doesn't fail. */
5043 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 get_float = TRUE;
5046 p = skipdigits(p + 2);
5047 if (*p == 'e' || *p == 'E')
5048 {
5049 ++p;
5050 if (*p == '-' || *p == '+')
5051 ++p;
5052 if (!vim_isdigit(*p))
5053 get_float = FALSE;
5054 else
5055 p = skipdigits(p + 1);
5056 }
5057 if (ASCII_ISALPHA(*p) || *p == '.')
5058 get_float = FALSE;
5059 }
5060 if (get_float)
5061 {
5062 float_T f;
5063
5064 *arg += string2float(*arg, &f);
5065 if (evaluate)
5066 {
5067 rettv->v_type = VAR_FLOAT;
5068 rettv->vval.v_float = f;
5069 }
5070 }
5071 else
5072#endif
5073 {
5074 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5075 *arg += len;
5076 if (evaluate)
5077 {
5078 rettv->v_type = VAR_NUMBER;
5079 rettv->vval.v_number = n;
5080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 /*
5086 * String constant: "string".
5087 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005088 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090
5091 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005092 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005095 break;
5096
5097 /*
5098 * List: [expr, expr]
5099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005100 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 break;
5102
5103 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 * Dictionary: {key: val, key: val}
5105 */
5106 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5107 break;
5108
5109 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005110 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005112 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
5114
5115 /*
5116 * Environment variable: $VAR.
5117 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 break;
5120
5121 /*
5122 * Register contents: @r.
5123 */
5124 case '@': ++*arg;
5125 if (evaluate)
5126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005128 rettv->vval.v_string = get_reg_contents(**arg,
5129 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 if (**arg != NUL)
5132 ++*arg;
5133 break;
5134
5135 /*
5136 * nested expression: (expression).
5137 */
5138 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (**arg == ')')
5141 ++*arg;
5142 else if (ret == OK)
5143 {
5144 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005145 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 ret = FAIL;
5147 }
5148 break;
5149
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
5152 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153
5154 if (ret == NOTDONE)
5155 {
5156 /*
5157 * Must be a variable or function name.
5158 * Can also be a curly-braces kind of name: {expr}.
5159 */
5160 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005161 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 if (alias != NULL)
5163 s = alias;
5164
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005165 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 ret = FAIL;
5167 else
5168 {
5169 if (**arg == '(') /* recursive! */
5170 {
5171 /* If "s" is the name of a variable of type VAR_FUNC
5172 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005173 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174
5175 /* Invoke the function. */
5176 ret = get_func_tv(s, len, rettv, arg,
5177 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005178 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005179
5180 /* If evaluate is FALSE rettv->v_type was not set in
5181 * get_func_tv, but it's needed in handle_subscript() to parse
5182 * what follows. So set it here. */
5183 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5184 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005185 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005186 rettv->v_type = VAR_FUNC;
5187 }
5188
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 /* Stop the expression evaluation when immediately
5190 * aborting on error, or when an interrupt occurred or
5191 * an exception was thrown but not caught. */
5192 if (aborting())
5193 {
5194 if (ret == OK)
5195 clear_tv(rettv);
5196 ret = FAIL;
5197 }
5198 }
5199 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005200 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005201 else
5202 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005204 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 }
5206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 *arg = skipwhite(*arg);
5208
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005209 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5210 * expr(expr). */
5211 if (ret == OK)
5212 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
5214 /*
5215 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5216 */
5217 if (ret == OK && evaluate && end_leader > start_leader)
5218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005220 int val = 0;
5221#ifdef FEAT_FLOAT
5222 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005224 if (rettv->v_type == VAR_FLOAT)
5225 f = rettv->vval.v_float;
5226 else
5227#endif
5228 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 clear_tv(rettv);
5232 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 else
5235 {
5236 while (end_leader > start_leader)
5237 {
5238 --end_leader;
5239 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005240 {
5241#ifdef FEAT_FLOAT
5242 if (rettv->v_type == VAR_FLOAT)
5243 f = !f;
5244 else
5245#endif
5246 val = !val;
5247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005249 {
5250#ifdef FEAT_FLOAT
5251 if (rettv->v_type == VAR_FLOAT)
5252 f = -f;
5253 else
5254#endif
5255 val = -val;
5256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005258#ifdef FEAT_FLOAT
5259 if (rettv->v_type == VAR_FLOAT)
5260 {
5261 clear_tv(rettv);
5262 rettv->vval.v_float = f;
5263 }
5264 else
5265#endif
5266 {
5267 clear_tv(rettv);
5268 rettv->v_type = VAR_NUMBER;
5269 rettv->vval.v_number = val;
5270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273
5274 return ret;
5275}
5276
5277/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005278 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5279 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5281 */
5282 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005287 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288{
5289 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005290 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 long len = -1;
5293 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005297 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 if (verbose)
5300 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 return FAIL;
5302 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005303#ifdef FEAT_FLOAT
5304 else if (rettv->v_type == VAR_FLOAT)
5305 {
5306 if (verbose)
5307 EMSG(_(e_float_as_string));
5308 return FAIL;
5309 }
5310#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005311
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005313 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314 /*
5315 * dict.name
5316 */
5317 key = *arg + 1;
5318 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5319 ;
5320 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005321 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005323 }
5324 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 /*
5327 * something[idx]
5328 *
5329 * Get the (first) variable from inside the [].
5330 */
5331 *arg = skipwhite(*arg + 1);
5332 if (**arg == ':')
5333 empty1 = TRUE;
5334 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5335 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5337 {
5338 /* not a number or string */
5339 clear_tv(&var1);
5340 return FAIL;
5341 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342
5343 /*
5344 * Get the second variable from inside the [:].
5345 */
5346 if (**arg == ':')
5347 {
5348 range = TRUE;
5349 *arg = skipwhite(*arg + 1);
5350 if (**arg == ']')
5351 empty2 = TRUE;
5352 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354 if (!empty1)
5355 clear_tv(&var1);
5356 return FAIL;
5357 }
5358 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5359 {
5360 /* not a number or string */
5361 if (!empty1)
5362 clear_tv(&var1);
5363 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 return FAIL;
5365 }
5366 }
5367
5368 /* Check for the ']'. */
5369 if (**arg != ']')
5370 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 if (verbose)
5372 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 clear_tv(&var1);
5374 if (range)
5375 clear_tv(&var2);
5376 return FAIL;
5377 }
5378 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 }
5380
5381 if (evaluate)
5382 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005383 n1 = 0;
5384 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005386 n1 = get_tv_number(&var1);
5387 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 }
5389 if (range)
5390 {
5391 if (empty2)
5392 n2 = -1;
5393 else
5394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395 n2 = get_tv_number(&var2);
5396 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 }
5398 }
5399
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005400 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 {
5402 case VAR_NUMBER:
5403 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005404 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 len = (long)STRLEN(s);
5406 if (range)
5407 {
5408 /* The resulting variable is a substring. If the indexes
5409 * are out of range the result is empty. */
5410 if (n1 < 0)
5411 {
5412 n1 = len + n1;
5413 if (n1 < 0)
5414 n1 = 0;
5415 }
5416 if (n2 < 0)
5417 n2 = len + n2;
5418 else if (n2 >= len)
5419 n2 = len;
5420 if (n1 >= len || n2 < 0 || n1 > n2)
5421 s = NULL;
5422 else
5423 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5424 }
5425 else
5426 {
5427 /* The resulting variable is a string of a single
5428 * character. If the index is too big or negative the
5429 * result is empty. */
5430 if (n1 >= len || n1 < 0)
5431 s = NULL;
5432 else
5433 s = vim_strnsave(s + n1, 1);
5434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 clear_tv(rettv);
5436 rettv->v_type = VAR_STRING;
5437 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 break;
5439
5440 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 if (n1 < 0)
5443 n1 = len + n1;
5444 if (!empty1 && (n1 < 0 || n1 >= len))
5445 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005446 /* For a range we allow invalid values and return an empty
5447 * list. A list index out of range is an error. */
5448 if (!range)
5449 {
5450 if (verbose)
5451 EMSGN(_(e_listidx), n1);
5452 return FAIL;
5453 }
5454 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 }
5456 if (range)
5457 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005458 list_T *l;
5459 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460
5461 if (n2 < 0)
5462 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005463 else if (n2 >= len)
5464 n2 = len - 1;
5465 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005466 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 l = list_alloc();
5468 if (l == NULL)
5469 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 n1 <= n2; ++n1)
5472 {
5473 if (list_append_tv(l, &item->li_tv) == FAIL)
5474 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005475 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 return FAIL;
5477 }
5478 item = item->li_next;
5479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 clear_tv(rettv);
5481 rettv->v_type = VAR_LIST;
5482 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005483 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485 else
5486 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005487 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488 clear_tv(rettv);
5489 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 }
5491 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492
5493 case VAR_DICT:
5494 if (range)
5495 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 if (verbose)
5497 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 if (len == -1)
5499 clear_tv(&var1);
5500 return FAIL;
5501 }
5502 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005503 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005504
5505 if (len == -1)
5506 {
5507 key = get_tv_string(&var1);
5508 if (*key == NUL)
5509 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005510 if (verbose)
5511 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005512 clear_tv(&var1);
5513 return FAIL;
5514 }
5515 }
5516
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005517 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005519 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005520 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005521 if (len == -1)
5522 clear_tv(&var1);
5523 if (item == NULL)
5524 return FAIL;
5525
5526 copy_tv(&item->di_tv, &var1);
5527 clear_tv(rettv);
5528 *rettv = var1;
5529 }
5530 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 }
5532 }
5533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 return OK;
5535}
5536
5537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 * Get an option value.
5539 * "arg" points to the '&' or '+' before the option name.
5540 * "arg" is advanced to character after the option name.
5541 * Return OK or FAIL.
5542 */
5543 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 int evaluate;
5548{
5549 char_u *option_end;
5550 long numval;
5551 char_u *stringval;
5552 int opt_type;
5553 int c;
5554 int working = (**arg == '+'); /* has("+option") */
5555 int ret = OK;
5556 int opt_flags;
5557
5558 /*
5559 * Isolate the option name and find its value.
5560 */
5561 option_end = find_option_end(arg, &opt_flags);
5562 if (option_end == NULL)
5563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 EMSG2(_("E112: Option name missing: %s"), *arg);
5566 return FAIL;
5567 }
5568
5569 if (!evaluate)
5570 {
5571 *arg = option_end;
5572 return OK;
5573 }
5574
5575 c = *option_end;
5576 *option_end = NUL;
5577 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 if (opt_type == -3) /* invalid name */
5581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 EMSG2(_("E113: Unknown option: %s"), *arg);
5584 ret = FAIL;
5585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005586 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 if (opt_type == -2) /* hidden string option */
5589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 rettv->v_type = VAR_STRING;
5591 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593 else if (opt_type == -1) /* hidden number option */
5594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 rettv->v_type = VAR_NUMBER;
5596 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598 else if (opt_type == 1) /* number option */
5599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 rettv->v_type = VAR_NUMBER;
5601 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 else /* string option */
5604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 rettv->v_type = VAR_STRING;
5606 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
5608 }
5609 else if (working && (opt_type == -2 || opt_type == -1))
5610 ret = FAIL;
5611
5612 *option_end = c; /* put back for error messages */
5613 *arg = option_end;
5614
5615 return ret;
5616}
5617
5618/*
5619 * Allocate a variable for a string constant.
5620 * Return OK or FAIL.
5621 */
5622 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 int evaluate;
5627{
5628 char_u *p;
5629 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 int extra = 0;
5631
5632 /*
5633 * Find the end of the string, skipping backslashed characters.
5634 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637 if (*p == '\\' && p[1] != NUL)
5638 {
5639 ++p;
5640 /* A "\<x>" form occupies at least 4 characters, and produces up
5641 * to 6 characters: reserve space for 2 extra */
5642 if (*p == '<')
5643 extra += 2;
5644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646
5647 if (*p != '"')
5648 {
5649 EMSG2(_("E114: Missing quote: %s"), *arg);
5650 return FAIL;
5651 }
5652
5653 /* If only parsing, set *arg and return here */
5654 if (!evaluate)
5655 {
5656 *arg = p + 1;
5657 return OK;
5658 }
5659
5660 /*
5661 * Copy the string into allocated memory, handling backslashed
5662 * characters.
5663 */
5664 name = alloc((unsigned)(p - *arg + extra));
5665 if (name == NULL)
5666 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 rettv->v_type = VAR_STRING;
5668 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
5672 if (*p == '\\')
5673 {
5674 switch (*++p)
5675 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 case 'b': *name++ = BS; ++p; break;
5677 case 'e': *name++ = ESC; ++p; break;
5678 case 'f': *name++ = FF; ++p; break;
5679 case 'n': *name++ = NL; ++p; break;
5680 case 'r': *name++ = CAR; ++p; break;
5681 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 case 'X': /* hex: "\x1", "\x12" */
5684 case 'x':
5685 case 'u': /* Unicode: "\u0023" */
5686 case 'U':
5687 if (vim_isxdigit(p[1]))
5688 {
5689 int n, nr;
5690 int c = toupper(*p);
5691
5692 if (c == 'X')
5693 n = 2;
5694 else
5695 n = 4;
5696 nr = 0;
5697 while (--n >= 0 && vim_isxdigit(p[1]))
5698 {
5699 ++p;
5700 nr = (nr << 4) + hex2nr(*p);
5701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703#ifdef FEAT_MBYTE
5704 /* For "\u" store the number according to
5705 * 'encoding'. */
5706 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 else
5709#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 break;
5713
5714 /* octal: "\1", "\12", "\123" */
5715 case '0':
5716 case '1':
5717 case '2':
5718 case '3':
5719 case '4':
5720 case '5':
5721 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 case '7': *name = *p++ - '0';
5723 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 *name = (*name << 3) + *p++ - '0';
5726 if (*p >= '0' && *p <= '7')
5727 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 break;
5731
5732 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 if (extra != 0)
5735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 break;
5738 }
5739 /* FALLTHROUGH */
5740
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 break;
5743 }
5744 }
5745 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 *arg = p + 1;
5751
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 return OK;
5753}
5754
5755/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 * Return OK or FAIL.
5758 */
5759 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005760get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 int evaluate;
5764{
5765 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 int reduce = 0;
5768
5769 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005771 */
5772 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 break;
5778 ++reduce;
5779 ++p;
5780 }
5781 }
5782
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 return FAIL;
5787 }
5788
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 if (!evaluate)
5791 {
5792 *arg = p + 1;
5793 return OK;
5794 }
5795
5796 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 */
5799 str = alloc((unsigned)((p - *arg) - reduce));
5800 if (str == NULL)
5801 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 rettv->v_type = VAR_STRING;
5803 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005804
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810 break;
5811 ++p;
5812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005816 *arg = p + 1;
5817
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 return OK;
5819}
5820
5821/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 * Allocate a variable for a List and fill it from "*arg".
5823 * Return OK or FAIL.
5824 */
5825 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005826get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 int evaluate;
5830{
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 list_T *l = NULL;
5832 typval_T tv;
5833 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834
5835 if (evaluate)
5836 {
5837 l = list_alloc();
5838 if (l == NULL)
5839 return FAIL;
5840 }
5841
5842 *arg = skipwhite(*arg + 1);
5843 while (**arg != ']' && **arg != NUL)
5844 {
5845 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5846 goto failret;
5847 if (evaluate)
5848 {
5849 item = listitem_alloc();
5850 if (item != NULL)
5851 {
5852 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005853 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 list_append(l, item);
5855 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005856 else
5857 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 }
5859
5860 if (**arg == ']')
5861 break;
5862 if (**arg != ',')
5863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 goto failret;
5866 }
5867 *arg = skipwhite(*arg + 1);
5868 }
5869
5870 if (**arg != ']')
5871 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873failret:
5874 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005875 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876 return FAIL;
5877 }
5878
5879 *arg = skipwhite(*arg + 1);
5880 if (evaluate)
5881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005882 rettv->v_type = VAR_LIST;
5883 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884 ++l->lv_refcount;
5885 }
5886
5887 return OK;
5888}
5889
5890/*
5891 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005892 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005893 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005894 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005895list_alloc()
5896{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005897 list_T *l;
5898
5899 l = (list_T *)alloc_clear(sizeof(list_T));
5900 if (l != NULL)
5901 {
5902 /* Prepend the list to the list of lists for garbage collection. */
5903 if (first_list != NULL)
5904 first_list->lv_used_prev = l;
5905 l->lv_used_prev = NULL;
5906 l->lv_used_next = first_list;
5907 first_list = l;
5908 }
5909 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910}
5911
5912/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005913 * Allocate an empty list for a return value.
5914 * Returns OK or FAIL.
5915 */
5916 static int
5917rettv_list_alloc(rettv)
5918 typval_T *rettv;
5919{
5920 list_T *l = list_alloc();
5921
5922 if (l == NULL)
5923 return FAIL;
5924
5925 rettv->vval.v_list = l;
5926 rettv->v_type = VAR_LIST;
5927 ++l->lv_refcount;
5928 return OK;
5929}
5930
5931/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 * Unreference a list: decrement the reference count and free it when it
5933 * becomes zero.
5934 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005935 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005939 if (l != NULL && --l->lv_refcount <= 0)
5940 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941}
5942
5943/*
5944 * Free a list, including all items it points to.
5945 * Ignores the reference count.
5946 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005947 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005948list_free(l, recurse)
5949 list_T *l;
5950 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951{
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005954 /* Remove the list from the list of lists for garbage collection. */
5955 if (l->lv_used_prev == NULL)
5956 first_list = l->lv_used_next;
5957 else
5958 l->lv_used_prev->lv_used_next = l->lv_used_next;
5959 if (l->lv_used_next != NULL)
5960 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5961
Bram Moolenaard9fba312005-06-26 22:34:35 +00005962 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005964 /* Remove the item before deleting it. */
5965 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005966 if (recurse || (item->li_tv.v_type != VAR_LIST
5967 && item->li_tv.v_type != VAR_DICT))
5968 clear_tv(&item->li_tv);
5969 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 }
5971 vim_free(l);
5972}
5973
5974/*
5975 * Allocate a list item.
5976 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005977 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978listitem_alloc()
5979{
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981}
5982
5983/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005984 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005986 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005990 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 vim_free(item);
5992}
5993
5994/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005995 * Remove a list item from a List and free it. Also clears the value.
5996 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005997 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005998listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 list_T *l;
6000 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006001{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006002 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006003 listitem_free(item);
6004}
6005
6006/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Get the number of items in a list.
6008 */
6009 static long
6010list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 if (l == NULL)
6014 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006015 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016}
6017
6018/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006019 * Return TRUE when two lists have exactly the same values.
6020 */
6021 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006022list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 list_T *l1;
6024 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006025 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006026 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006029
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006030 if (l1 == NULL || l2 == NULL)
6031 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006032 if (l1 == l2)
6033 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006034 if (list_len(l1) != list_len(l2))
6035 return FALSE;
6036
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006037 for (item1 = l1->lv_first, item2 = l2->lv_first;
6038 item1 != NULL && item2 != NULL;
6039 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006041 return FALSE;
6042 return item1 == NULL && item2 == NULL;
6043}
6044
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006045#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6046 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006047/*
6048 * Return the dictitem that an entry in a hashtable points to.
6049 */
6050 dictitem_T *
6051dict_lookup(hi)
6052 hashitem_T *hi;
6053{
6054 return HI2DI(hi);
6055}
6056#endif
6057
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006058/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 * Return TRUE when two dictionaries have exactly the same key/values.
6060 */
6061 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006062dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 dict_T *d1;
6064 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006066 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006067{
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 hashitem_T *hi;
6069 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006070 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006072 if (d1 == NULL || d2 == NULL)
6073 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006074 if (d1 == d2)
6075 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006076 if (dict_len(d1) != dict_len(d2))
6077 return FALSE;
6078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006079 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006082 if (!HASHITEM_EMPTY(hi))
6083 {
6084 item2 = dict_find(d2, hi->hi_key, -1);
6085 if (item2 == NULL)
6086 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006087 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006088 return FALSE;
6089 --todo;
6090 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006091 }
6092 return TRUE;
6093}
6094
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095static int tv_equal_recurse_limit;
6096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006097/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006100 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101 */
6102 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 typval_T *tv1;
6105 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 int ic; /* ignore case */
6107 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108{
6109 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006110 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006112 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006114 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 * recursiveness to a limit. We guess they are equal then.
6119 * A fixed limit has the problem of still taking an awful long time.
6120 * Reduce the limit every time running into it. That should work fine for
6121 * deeply linked structures that are not recursively linked and catch
6122 * recursiveness quickly. */
6123 if (!recursive)
6124 tv_equal_recurse_limit = 1000;
6125 if (recursive_cnt >= tv_equal_recurse_limit)
6126 {
6127 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006128 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006130
6131 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006134 ++recursive_cnt;
6135 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6136 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006137 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006138
6139 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 ++recursive_cnt;
6141 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6142 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006143 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006144
6145 case VAR_FUNC:
6146 return (tv1->vval.v_string != NULL
6147 && tv2->vval.v_string != NULL
6148 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6149
6150 case VAR_NUMBER:
6151 return tv1->vval.v_number == tv2->vval.v_number;
6152
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006153#ifdef FEAT_FLOAT
6154 case VAR_FLOAT:
6155 return tv1->vval.v_float == tv2->vval.v_float;
6156#endif
6157
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006158 case VAR_STRING:
6159 s1 = get_tv_string_buf(tv1, buf1);
6160 s2 = get_tv_string_buf(tv2, buf2);
6161 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006163
6164 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006165 return TRUE;
6166}
6167
6168/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 * Locate item with index "n" in list "l" and return it.
6170 * A negative index is counted from the end; -1 is the last item.
6171 * Returns NULL when "n" is out of range.
6172 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006173 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 long n;
6177{
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 long idx;
6180
6181 if (l == NULL)
6182 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006183
6184 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006186 n = l->lv_len + n;
6187
6188 /* Check for index out of range. */
6189 if (n < 0 || n >= l->lv_len)
6190 return NULL;
6191
6192 /* When there is a cached index may start search from there. */
6193 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006195 if (n < l->lv_idx / 2)
6196 {
6197 /* closest to the start of the list */
6198 item = l->lv_first;
6199 idx = 0;
6200 }
6201 else if (n > (l->lv_idx + l->lv_len) / 2)
6202 {
6203 /* closest to the end of the list */
6204 item = l->lv_last;
6205 idx = l->lv_len - 1;
6206 }
6207 else
6208 {
6209 /* closest to the cached index */
6210 item = l->lv_idx_item;
6211 idx = l->lv_idx;
6212 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 }
6214 else
6215 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006216 if (n < l->lv_len / 2)
6217 {
6218 /* closest to the start of the list */
6219 item = l->lv_first;
6220 idx = 0;
6221 }
6222 else
6223 {
6224 /* closest to the end of the list */
6225 item = l->lv_last;
6226 idx = l->lv_len - 1;
6227 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006229
6230 while (n > idx)
6231 {
6232 /* search forward */
6233 item = item->li_next;
6234 ++idx;
6235 }
6236 while (n < idx)
6237 {
6238 /* search backward */
6239 item = item->li_prev;
6240 --idx;
6241 }
6242
6243 /* cache the used index */
6244 l->lv_idx = idx;
6245 l->lv_idx_item = item;
6246
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 return item;
6248}
6249
6250/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006251 * Get list item "l[idx]" as a number.
6252 */
6253 static long
6254list_find_nr(l, idx, errorp)
6255 list_T *l;
6256 long idx;
6257 int *errorp; /* set to TRUE when something wrong */
6258{
6259 listitem_T *li;
6260
6261 li = list_find(l, idx);
6262 if (li == NULL)
6263 {
6264 if (errorp != NULL)
6265 *errorp = TRUE;
6266 return -1L;
6267 }
6268 return get_tv_number_chk(&li->li_tv, errorp);
6269}
6270
6271/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006272 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6273 */
6274 char_u *
6275list_find_str(l, idx)
6276 list_T *l;
6277 long idx;
6278{
6279 listitem_T *li;
6280
6281 li = list_find(l, idx - 1);
6282 if (li == NULL)
6283 {
6284 EMSGN(_(e_listidx), idx);
6285 return NULL;
6286 }
6287 return get_tv_string(&li->li_tv);
6288}
6289
6290/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006291 * Locate "item" list "l" and return its index.
6292 * Returns -1 when "item" is not in the list.
6293 */
6294 static long
6295list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 list_T *l;
6297 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006298{
6299 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006301
6302 if (l == NULL)
6303 return -1;
6304 idx = 0;
6305 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6306 ++idx;
6307 if (li == NULL)
6308 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006309 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006310}
6311
6312/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 * Append item "item" to the end of list "l".
6314 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006315 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 list_T *l;
6318 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319{
6320 if (l->lv_last == NULL)
6321 {
6322 /* empty list */
6323 l->lv_first = item;
6324 l->lv_last = item;
6325 item->li_prev = NULL;
6326 }
6327 else
6328 {
6329 l->lv_last->li_next = item;
6330 item->li_prev = l->lv_last;
6331 l->lv_last = item;
6332 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006333 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 item->li_next = NULL;
6335}
6336
6337/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006341 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006343 list_T *l;
6344 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347
Bram Moolenaar05159a02005-02-26 23:04:13 +00006348 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006350 copy_tv(tv, &li->li_tv);
6351 list_append(l, li);
6352 return OK;
6353}
6354
6355/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006356 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006357 * Return FAIL when out of memory.
6358 */
6359 int
6360list_append_dict(list, dict)
6361 list_T *list;
6362 dict_T *dict;
6363{
6364 listitem_T *li = listitem_alloc();
6365
6366 if (li == NULL)
6367 return FAIL;
6368 li->li_tv.v_type = VAR_DICT;
6369 li->li_tv.v_lock = 0;
6370 li->li_tv.vval.v_dict = dict;
6371 list_append(list, li);
6372 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 return OK;
6374}
6375
6376/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006377 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006378 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006379 * Returns FAIL when out of memory.
6380 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006381 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006382list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006383 list_T *l;
6384 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006385 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006386{
6387 listitem_T *li = listitem_alloc();
6388
6389 if (li == NULL)
6390 return FAIL;
6391 list_append(l, li);
6392 li->li_tv.v_type = VAR_STRING;
6393 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006394 if (str == NULL)
6395 li->li_tv.vval.v_string = NULL;
6396 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006397 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006398 return FAIL;
6399 return OK;
6400}
6401
6402/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006403 * Append "n" to list "l".
6404 * Returns FAIL when out of memory.
6405 */
6406 static int
6407list_append_number(l, n)
6408 list_T *l;
6409 varnumber_T n;
6410{
6411 listitem_T *li;
6412
6413 li = listitem_alloc();
6414 if (li == NULL)
6415 return FAIL;
6416 li->li_tv.v_type = VAR_NUMBER;
6417 li->li_tv.v_lock = 0;
6418 li->li_tv.vval.v_number = n;
6419 list_append(l, li);
6420 return OK;
6421}
6422
6423/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 * If "item" is NULL append at the end.
6426 * Return FAIL when out of memory.
6427 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006428 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 list_T *l;
6431 typval_T *tv;
6432 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433{
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435
6436 if (ni == NULL)
6437 return FAIL;
6438 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006439 list_insert(l, ni, item);
6440 return OK;
6441}
6442
6443 void
6444list_insert(l, ni, item)
6445 list_T *l;
6446 listitem_T *ni;
6447 listitem_T *item;
6448{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449 if (item == NULL)
6450 /* Append new item at end of list. */
6451 list_append(l, ni);
6452 else
6453 {
6454 /* Insert new item before existing item. */
6455 ni->li_prev = item->li_prev;
6456 ni->li_next = item;
6457 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006460 ++l->lv_idx;
6461 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006465 l->lv_idx_item = NULL;
6466 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006468 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470}
6471
6472/*
6473 * Extend "l1" with "l2".
6474 * If "bef" is NULL append at the end, otherwise insert before this item.
6475 * Returns FAIL when out of memory.
6476 */
6477 static int
6478list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 list_T *l1;
6480 list_T *l2;
6481 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482{
Bram Moolenaar33570922005-01-25 22:26:29 +00006483 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006484 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006485
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006486 /* We also quit the loop when we have inserted the original item count of
6487 * the list, avoid a hang when we extend a list with itself. */
6488 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6490 return FAIL;
6491 return OK;
6492}
6493
6494/*
6495 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6496 * Return FAIL when out of memory.
6497 */
6498 static int
6499list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 list_T *l1;
6501 list_T *l2;
6502 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503{
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006506 if (l1 == NULL || l2 == NULL)
6507 return FAIL;
6508
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 if (l == NULL)
6512 return FAIL;
6513 tv->v_type = VAR_LIST;
6514 tv->vval.v_list = l;
6515
6516 /* append all items from the second list */
6517 return list_extend(l, l2, NULL);
6518}
6519
6520/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006523 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 * Returns NULL when out of memory.
6525 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006526 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006528 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006529 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006530 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531{
Bram Moolenaar33570922005-01-25 22:26:29 +00006532 list_T *copy;
6533 listitem_T *item;
6534 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535
6536 if (orig == NULL)
6537 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538
6539 copy = list_alloc();
6540 if (copy != NULL)
6541 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 if (copyID != 0)
6543 {
6544 /* Do this before adding the items, because one of the items may
6545 * refer back to this list. */
6546 orig->lv_copyID = copyID;
6547 orig->lv_copylist = copy;
6548 }
6549 for (item = orig->lv_first; item != NULL && !got_int;
6550 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 {
6552 ni = listitem_alloc();
6553 if (ni == NULL)
6554 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006556 {
6557 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6558 {
6559 vim_free(ni);
6560 break;
6561 }
6562 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006564 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 list_append(copy, ni);
6566 }
6567 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 if (item != NULL)
6569 {
6570 list_unref(copy);
6571 copy = NULL;
6572 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573 }
6574
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 return copy;
6576}
6577
6578/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006580 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006581 * This used to be called list_remove, but that conflicts with a Sun header
6582 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006584 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006585vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 list_T *l;
6587 listitem_T *item;
6588 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589{
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592 /* notify watchers */
6593 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006595 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 list_fix_watch(l, ip);
6597 if (ip == item2)
6598 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600
6601 if (item2->li_next == NULL)
6602 l->lv_last = item->li_prev;
6603 else
6604 item2->li_next->li_prev = item->li_prev;
6605 if (item->li_prev == NULL)
6606 l->lv_first = item2->li_next;
6607 else
6608 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006609 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610}
6611
6612/*
6613 * Return an allocated string with the string representation of a list.
6614 * May return NULL.
6615 */
6616 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006617list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006618 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006619 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620{
6621 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 if (tv->vval.v_list == NULL)
6624 return NULL;
6625 ga_init2(&ga, (int)sizeof(char), 80);
6626 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006627 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 {
6629 vim_free(ga.ga_data);
6630 return NULL;
6631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 ga_append(&ga, ']');
6633 ga_append(&ga, NUL);
6634 return (char_u *)ga.ga_data;
6635}
6636
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006637typedef struct join_S {
6638 char_u *s;
6639 char_u *tofree;
6640} join_T;
6641
6642 static int
6643list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6644 garray_T *gap; /* to store the result in */
6645 list_T *l;
6646 char_u *sep;
6647 int echo_style;
6648 int copyID;
6649 garray_T *join_gap; /* to keep each list item string */
6650{
6651 int i;
6652 join_T *p;
6653 int len;
6654 int sumlen = 0;
6655 int first = TRUE;
6656 char_u *tofree;
6657 char_u numbuf[NUMBUFLEN];
6658 listitem_T *item;
6659 char_u *s;
6660
6661 /* Stringify each item in the list. */
6662 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6663 {
6664 if (echo_style)
6665 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6666 else
6667 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6668 if (s == NULL)
6669 return FAIL;
6670
6671 len = (int)STRLEN(s);
6672 sumlen += len;
6673
6674 ga_grow(join_gap, 1);
6675 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6676 if (tofree != NULL || s != numbuf)
6677 {
6678 p->s = s;
6679 p->tofree = tofree;
6680 }
6681 else
6682 {
6683 p->s = vim_strnsave(s, len);
6684 p->tofree = p->s;
6685 }
6686
6687 line_breakcheck();
6688 }
6689
6690 /* Allocate result buffer with its total size, avoid re-allocation and
6691 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6692 if (join_gap->ga_len >= 2)
6693 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6694 if (ga_grow(gap, sumlen + 2) == FAIL)
6695 return FAIL;
6696
6697 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6698 {
6699 if (first)
6700 first = FALSE;
6701 else
6702 ga_concat(gap, sep);
6703 p = ((join_T *)join_gap->ga_data) + i;
6704
6705 if (p->s != NULL)
6706 ga_concat(gap, p->s);
6707 line_breakcheck();
6708 }
6709
6710 return OK;
6711}
6712
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006713/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006715 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006716 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006718 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006719list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006720 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006721 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006722 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006723 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006724 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006725{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006726 garray_T join_ga;
6727 int retval;
6728 join_T *p;
6729 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006730
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006731 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6732 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6733
6734 /* Dispose each item in join_ga. */
6735 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006736 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737 p = (join_T *)join_ga.ga_data;
6738 for (i = 0; i < join_ga.ga_len; ++i)
6739 {
6740 vim_free(p->tofree);
6741 ++p;
6742 }
6743 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006745
6746 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006747}
6748
6749/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 * Garbage collection for lists and dictionaries.
6751 *
6752 * We use reference counts to be able to free most items right away when they
6753 * are no longer used. But for composite items it's possible that it becomes
6754 * unused while the reference count is > 0: When there is a recursive
6755 * reference. Example:
6756 * :let l = [1, 2, 3]
6757 * :let d = {9: l}
6758 * :let l[1] = d
6759 *
6760 * Since this is quite unusual we handle this with garbage collection: every
6761 * once in a while find out which lists and dicts are not referenced from any
6762 * variable.
6763 *
6764 * Here is a good reference text about garbage collection (refers to Python
6765 * but it applies to all reference-counting mechanisms):
6766 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006767 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006768
6769/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 * Do garbage collection for lists and dicts.
6771 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006772 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773 int
6774garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006775{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006776 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 buf_T *buf;
6778 win_T *wp;
6779 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006780 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006781 int did_free;
6782 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006783#ifdef FEAT_WINDOWS
6784 tabpage_T *tp;
6785#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006787 /* Only do this once. */
6788 want_garbage_collect = FALSE;
6789 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006790 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006791
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006792 /* We advance by two because we add one for items referenced through
6793 * previous_funccal. */
6794 current_copyID += COPYID_INC;
6795 copyID = current_copyID;
6796
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006797 /*
6798 * 1. Go through all accessible variables and mark all lists and dicts
6799 * with copyID.
6800 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006801
6802 /* Don't free variables in the previous_funccal list unless they are only
6803 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006804 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006805 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6806 {
6807 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6808 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6809 }
6810
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 /* script-local variables */
6812 for (i = 1; i <= ga_scripts.ga_len; ++i)
6813 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6814
6815 /* buffer-local variables */
6816 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006817 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006818
6819 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006821 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006822#ifdef FEAT_AUTOCMD
6823 if (aucmd_win != NULL)
6824 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6825#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006827#ifdef FEAT_WINDOWS
6828 /* tabpage-local variables */
6829 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006830 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006831#endif
6832
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 /* global variables */
6834 set_ref_in_ht(&globvarht, copyID);
6835
6836 /* function-local variables */
6837 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6838 {
6839 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6840 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6841 }
6842
Bram Moolenaard812df62008-11-09 12:46:09 +00006843 /* v: vars */
6844 set_ref_in_ht(&vimvarht, copyID);
6845
Bram Moolenaar1dced572012-04-05 16:54:08 +02006846#ifdef FEAT_LUA
6847 set_ref_in_lua(copyID);
6848#endif
6849
Bram Moolenaardb913952012-06-29 12:54:53 +02006850#ifdef FEAT_PYTHON
6851 set_ref_in_python(copyID);
6852#endif
6853
6854#ifdef FEAT_PYTHON3
6855 set_ref_in_python3(copyID);
6856#endif
6857
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006858 /*
6859 * 2. Free lists and dictionaries that are not referenced.
6860 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 did_free = free_unref_items(copyID);
6862
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006863 /*
6864 * 3. Check if any funccal can be freed now.
6865 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 for (pfc = &previous_funccal; *pfc != NULL; )
6867 {
6868 if (can_free_funccal(*pfc, copyID))
6869 {
6870 fc = *pfc;
6871 *pfc = fc->caller;
6872 free_funccal(fc, TRUE);
6873 did_free = TRUE;
6874 did_free_funccal = TRUE;
6875 }
6876 else
6877 pfc = &(*pfc)->caller;
6878 }
6879 if (did_free_funccal)
6880 /* When a funccal was freed some more items might be garbage
6881 * collected, so run again. */
6882 (void)garbage_collect();
6883
6884 return did_free;
6885}
6886
6887/*
6888 * Free lists and dictionaries that are no longer referenced.
6889 */
6890 static int
6891free_unref_items(copyID)
6892 int copyID;
6893{
6894 dict_T *dd;
6895 list_T *ll;
6896 int did_free = FALSE;
6897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 */
6901 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006904 /* Free the Dictionary and ordinary items it contains, but don't
6905 * recurse into Lists and Dictionaries, they will be in the list
6906 * of dicts or list of lists. */
6907 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 did_free = TRUE;
6909
6910 /* restart, next dict may also have been freed */
6911 dd = first_dict;
6912 }
6913 else
6914 dd = dd->dv_used_next;
6915
6916 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006917 * Go through the list of lists and free items without the copyID.
6918 * But don't free a list that has a watcher (used in a for loop), these
6919 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920 */
6921 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006922 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6923 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006925 /* Free the List and ordinary items it contains, but don't recurse
6926 * into Lists and Dictionaries, they will be in the list of dicts
6927 * or list of lists. */
6928 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 did_free = TRUE;
6930
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006931 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006932 ll = first_list;
6933 }
6934 else
6935 ll = ll->lv_used_next;
6936
6937 return did_free;
6938}
6939
6940/*
6941 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6942 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006943 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944set_ref_in_ht(ht, copyID)
6945 hashtab_T *ht;
6946 int copyID;
6947{
6948 int todo;
6949 hashitem_T *hi;
6950
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006951 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 for (hi = ht->ht_array; todo > 0; ++hi)
6953 if (!HASHITEM_EMPTY(hi))
6954 {
6955 --todo;
6956 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6957 }
6958}
6959
6960/*
6961 * Mark all lists and dicts referenced through list "l" with "copyID".
6962 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006963 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964set_ref_in_list(l, copyID)
6965 list_T *l;
6966 int copyID;
6967{
6968 listitem_T *li;
6969
6970 for (li = l->lv_first; li != NULL; li = li->li_next)
6971 set_ref_in_item(&li->li_tv, copyID);
6972}
6973
6974/*
6975 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6976 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006977 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978set_ref_in_item(tv, copyID)
6979 typval_T *tv;
6980 int copyID;
6981{
6982 dict_T *dd;
6983 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006984
6985 switch (tv->v_type)
6986 {
6987 case VAR_DICT:
6988 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006989 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006990 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 /* Didn't see this dict yet. */
6992 dd->dv_copyID = copyID;
6993 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006994 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006996
6997 case VAR_LIST:
6998 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006999 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007000 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001 /* Didn't see this list yet. */
7002 ll->lv_copyID = copyID;
7003 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007004 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007006 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007008}
7009
7010/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007011 * Allocate an empty header for a dictionary.
7012 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007013 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007014dict_alloc()
7015{
Bram Moolenaar33570922005-01-25 22:26:29 +00007016 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007017
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007019 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007020 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007021 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 if (first_dict != NULL)
7023 first_dict->dv_used_prev = d;
7024 d->dv_used_next = first_dict;
7025 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007026 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027
Bram Moolenaar33570922005-01-25 22:26:29 +00007028 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007029 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007030 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007031 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007032 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007033 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007034 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035}
7036
7037/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007038 * Allocate an empty dict for a return value.
7039 * Returns OK or FAIL.
7040 */
7041 static int
7042rettv_dict_alloc(rettv)
7043 typval_T *rettv;
7044{
7045 dict_T *d = dict_alloc();
7046
7047 if (d == NULL)
7048 return FAIL;
7049
7050 rettv->vval.v_dict = d;
7051 rettv->v_type = VAR_DICT;
7052 ++d->dv_refcount;
7053 return OK;
7054}
7055
7056
7057/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007058 * Unreference a Dictionary: decrement the reference count and free it when it
7059 * becomes zero.
7060 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007061 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007065 if (d != NULL && --d->dv_refcount <= 0)
7066 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007067}
7068
7069/*
7070 * Free a Dictionary, including all items it contains.
7071 * Ignores the reference count.
7072 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007073 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007074dict_free(d, recurse)
7075 dict_T *d;
7076 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007077{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007079 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007080 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* Remove the dict from the list of dicts for garbage collection. */
7083 if (d->dv_used_prev == NULL)
7084 first_dict = d->dv_used_next;
7085 else
7086 d->dv_used_prev->dv_used_next = d->dv_used_next;
7087 if (d->dv_used_next != NULL)
7088 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7089
7090 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007091 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007092 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007095 if (!HASHITEM_EMPTY(hi))
7096 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007097 /* Remove the item before deleting it, just in case there is
7098 * something recursive causing trouble. */
7099 di = HI2DI(hi);
7100 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007101 if (recurse || (di->di_tv.v_type != VAR_LIST
7102 && di->di_tv.v_type != VAR_DICT))
7103 clear_tv(&di->di_tv);
7104 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007105 --todo;
7106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007107 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109 vim_free(d);
7110}
7111
7112/*
7113 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 * The "key" is copied to the new item.
7115 * Note that the value of the item "di_tv" still needs to be initialized!
7116 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007118 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119dictitem_alloc(key)
7120 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007121{
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007124 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007126 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007128 di->di_flags = 0;
7129 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131}
7132
7133/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134 * Make a copy of a Dictionary item.
7135 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007138 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007139{
Bram Moolenaar33570922005-01-25 22:26:29 +00007140 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007142 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7143 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 if (di != NULL)
7145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007147 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148 copy_tv(&org->di_tv, &di->di_tv);
7149 }
7150 return di;
7151}
7152
7153/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007154 * Remove item "item" from Dictionary "dict" and free it.
7155 */
7156 static void
7157dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 dict_T *dict;
7159 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160{
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007164 if (HASHITEM_EMPTY(hi))
7165 EMSG2(_(e_intern2), "dictitem_remove()");
7166 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007167 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168 dictitem_free(item);
7169}
7170
7171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007172 * Free a dict item. Also clears the value.
7173 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007174 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178 clear_tv(&item->di_tv);
7179 vim_free(item);
7180}
7181
7182/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7184 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007185 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186 * Returns NULL when out of memory.
7187 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007193{
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 dict_T *copy;
7195 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198
7199 if (orig == NULL)
7200 return NULL;
7201
7202 copy = dict_alloc();
7203 if (copy != NULL)
7204 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007205 if (copyID != 0)
7206 {
7207 orig->dv_copyID = copyID;
7208 orig->dv_copydict = copy;
7209 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007210 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007211 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007215 --todo;
7216
7217 di = dictitem_alloc(hi->hi_key);
7218 if (di == NULL)
7219 break;
7220 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 {
7222 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7223 copyID) == FAIL)
7224 {
7225 vim_free(di);
7226 break;
7227 }
7228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 else
7230 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7231 if (dict_add(copy, di) == FAIL)
7232 {
7233 dictitem_free(di);
7234 break;
7235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007237 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238
Bram Moolenaare9a41262005-01-15 22:18:47 +00007239 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007240 if (todo > 0)
7241 {
7242 dict_unref(copy);
7243 copy = NULL;
7244 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007245 }
7246
7247 return copy;
7248}
7249
7250/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007252 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007254 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 dict_T *d;
7257 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258{
Bram Moolenaar33570922005-01-25 22:26:29 +00007259 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260}
7261
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007263 * Add a number or string entry to dictionary "d".
7264 * When "str" is NULL use number "nr", otherwise use "str".
7265 * Returns FAIL when out of memory and when key already exists.
7266 */
7267 int
7268dict_add_nr_str(d, key, nr, str)
7269 dict_T *d;
7270 char *key;
7271 long nr;
7272 char_u *str;
7273{
7274 dictitem_T *item;
7275
7276 item = dictitem_alloc((char_u *)key);
7277 if (item == NULL)
7278 return FAIL;
7279 item->di_tv.v_lock = 0;
7280 if (str == NULL)
7281 {
7282 item->di_tv.v_type = VAR_NUMBER;
7283 item->di_tv.vval.v_number = nr;
7284 }
7285 else
7286 {
7287 item->di_tv.v_type = VAR_STRING;
7288 item->di_tv.vval.v_string = vim_strsave(str);
7289 }
7290 if (dict_add(d, item) == FAIL)
7291 {
7292 dictitem_free(item);
7293 return FAIL;
7294 }
7295 return OK;
7296}
7297
7298/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007299 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007300 * Returns FAIL when out of memory and when key already exists.
7301 */
7302 int
7303dict_add_list(d, key, list)
7304 dict_T *d;
7305 char *key;
7306 list_T *list;
7307{
7308 dictitem_T *item;
7309
7310 item = dictitem_alloc((char_u *)key);
7311 if (item == NULL)
7312 return FAIL;
7313 item->di_tv.v_lock = 0;
7314 item->di_tv.v_type = VAR_LIST;
7315 item->di_tv.vval.v_list = list;
7316 if (dict_add(d, item) == FAIL)
7317 {
7318 dictitem_free(item);
7319 return FAIL;
7320 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007321 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007322 return OK;
7323}
7324
7325/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 * Get the number of items in a Dictionary.
7327 */
7328 static long
7329dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007331{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332 if (d == NULL)
7333 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007334 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335}
7336
7337/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 * Find item "key[len]" in Dictionary "d".
7339 * If "len" is negative use strlen(key).
7340 * Returns NULL when not found.
7341 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007342 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 char_u *key;
7346 int len;
7347{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348#define AKEYLEN 200
7349 char_u buf[AKEYLEN];
7350 char_u *akey;
7351 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354 if (len < 0)
7355 akey = key;
7356 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007357 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 tofree = akey = vim_strnsave(key, len);
7359 if (akey == NULL)
7360 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007361 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 else
7363 {
7364 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007365 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 akey = buf;
7367 }
7368
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 vim_free(tofree);
7371 if (HASHITEM_EMPTY(hi))
7372 return NULL;
7373 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374}
7375
7376/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007377 * Get a string item from a dictionary.
7378 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007379 * Returns NULL if the entry doesn't exist or out of memory.
7380 */
7381 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007382get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007383 dict_T *d;
7384 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007385 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007386{
7387 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007388 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007389
7390 di = dict_find(d, key, -1);
7391 if (di == NULL)
7392 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007393 s = get_tv_string(&di->di_tv);
7394 if (save && s != NULL)
7395 s = vim_strsave(s);
7396 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007397}
7398
7399/*
7400 * Get a number item from a dictionary.
7401 * Returns 0 if the entry doesn't exist or out of memory.
7402 */
7403 long
7404get_dict_number(d, key)
7405 dict_T *d;
7406 char_u *key;
7407{
7408 dictitem_T *di;
7409
7410 di = dict_find(d, key, -1);
7411 if (di == NULL)
7412 return 0;
7413 return get_tv_number(&di->di_tv);
7414}
7415
7416/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 * Return an allocated string with the string representation of a Dictionary.
7418 * May return NULL.
7419 */
7420 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007421dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007423 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424{
7425 garray_T ga;
7426 int first = TRUE;
7427 char_u *tofree;
7428 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007432 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007434 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007435 return NULL;
7436 ga_init2(&ga, (int)sizeof(char), 80);
7437 ga_append(&ga, '{');
7438
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007439 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007440 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007442 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 --todo;
7445
7446 if (first)
7447 first = FALSE;
7448 else
7449 ga_concat(&ga, (char_u *)", ");
7450
7451 tofree = string_quote(hi->hi_key, FALSE);
7452 if (tofree != NULL)
7453 {
7454 ga_concat(&ga, tofree);
7455 vim_free(tofree);
7456 }
7457 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007458 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 if (s != NULL)
7460 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007462 if (s == NULL)
7463 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007466 if (todo > 0)
7467 {
7468 vim_free(ga.ga_data);
7469 return NULL;
7470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471
7472 ga_append(&ga, '}');
7473 ga_append(&ga, NUL);
7474 return (char_u *)ga.ga_data;
7475}
7476
7477/*
7478 * Allocate a variable for a Dictionary and fill it from "*arg".
7479 * Return OK or FAIL. Returns NOTDONE for {expr}.
7480 */
7481 static int
7482get_dict_tv(arg, rettv, evaluate)
7483 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 int evaluate;
7486{
Bram Moolenaar33570922005-01-25 22:26:29 +00007487 dict_T *d = NULL;
7488 typval_T tvkey;
7489 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007490 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494
7495 /*
7496 * First check if it's not a curly-braces thing: {expr}.
7497 * Must do this without evaluating, otherwise a function may be called
7498 * twice. Unfortunately this means we need to call eval1() twice for the
7499 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007500 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502 if (*start != '}')
7503 {
7504 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7505 return FAIL;
7506 if (*start == '}')
7507 return NOTDONE;
7508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509
7510 if (evaluate)
7511 {
7512 d = dict_alloc();
7513 if (d == NULL)
7514 return FAIL;
7515 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 tvkey.v_type = VAR_UNKNOWN;
7517 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518
7519 *arg = skipwhite(*arg + 1);
7520 while (**arg != '}' && **arg != NUL)
7521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 goto failret;
7524 if (**arg != ':')
7525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007526 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 goto failret;
7529 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007530 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007532 key = get_tv_string_buf_chk(&tvkey, buf);
7533 if (key == NULL || *key == NUL)
7534 {
7535 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7536 if (key != NULL)
7537 EMSG(_(e_emptykey));
7538 clear_tv(&tvkey);
7539 goto failret;
7540 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542
7543 *arg = skipwhite(*arg + 1);
7544 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7545 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007546 if (evaluate)
7547 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548 goto failret;
7549 }
7550 if (evaluate)
7551 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 if (item != NULL)
7554 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007555 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 clear_tv(&tv);
7558 goto failret;
7559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 item = dictitem_alloc(key);
7561 clear_tv(&tvkey);
7562 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007565 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 if (dict_add(d, item) == FAIL)
7567 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 }
7569 }
7570
7571 if (**arg == '}')
7572 break;
7573 if (**arg != ',')
7574 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007575 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 goto failret;
7577 }
7578 *arg = skipwhite(*arg + 1);
7579 }
7580
7581 if (**arg != '}')
7582 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007583 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584failret:
7585 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007586 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 return FAIL;
7588 }
7589
7590 *arg = skipwhite(*arg + 1);
7591 if (evaluate)
7592 {
7593 rettv->v_type = VAR_DICT;
7594 rettv->vval.v_dict = d;
7595 ++d->dv_refcount;
7596 }
7597
7598 return OK;
7599}
7600
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602 * Return a string with the string representation of a variable.
7603 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007604 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007605 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007607 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608 */
7609 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007610echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007611 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007612 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007613 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007614 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007615{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 static int recurse = 0;
7617 char_u *r = NULL;
7618
Bram Moolenaar33570922005-01-25 22:26:29 +00007619 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007621 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007622 *tofree = NULL;
7623 return NULL;
7624 }
7625 ++recurse;
7626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007627 switch (tv->v_type)
7628 {
7629 case VAR_FUNC:
7630 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007631 r = tv->vval.v_string;
7632 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007633
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007634 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007635 if (tv->vval.v_list == NULL)
7636 {
7637 *tofree = NULL;
7638 r = NULL;
7639 }
7640 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7641 {
7642 *tofree = NULL;
7643 r = (char_u *)"[...]";
7644 }
7645 else
7646 {
7647 tv->vval.v_list->lv_copyID = copyID;
7648 *tofree = list2string(tv, copyID);
7649 r = *tofree;
7650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007652
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 if (tv->vval.v_dict == NULL)
7655 {
7656 *tofree = NULL;
7657 r = NULL;
7658 }
7659 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7660 {
7661 *tofree = NULL;
7662 r = (char_u *)"{...}";
7663 }
7664 else
7665 {
7666 tv->vval.v_dict->dv_copyID = copyID;
7667 *tofree = dict2string(tv, copyID);
7668 r = *tofree;
7669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007671
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672 case VAR_STRING:
7673 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674 *tofree = NULL;
7675 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007676 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007677
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007678#ifdef FEAT_FLOAT
7679 case VAR_FLOAT:
7680 *tofree = NULL;
7681 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7682 r = numbuf;
7683 break;
7684#endif
7685
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007687 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007688 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690
7691 --recurse;
7692 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693}
7694
7695/*
7696 * Return a string with the string representation of a variable.
7697 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7698 * "numbuf" is used for a number.
7699 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007700 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701 */
7702 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007703tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 char_u **tofree;
7706 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007707 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007708{
7709 switch (tv->v_type)
7710 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 case VAR_FUNC:
7712 *tofree = string_quote(tv->vval.v_string, TRUE);
7713 return *tofree;
7714 case VAR_STRING:
7715 *tofree = string_quote(tv->vval.v_string, FALSE);
7716 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007717#ifdef FEAT_FLOAT
7718 case VAR_FLOAT:
7719 *tofree = NULL;
7720 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7721 return numbuf;
7722#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007728 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007729 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007730 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007731}
7732
7733/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007734 * Return string "str" in ' quotes, doubling ' characters.
7735 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007737 */
7738 static char_u *
7739string_quote(str, function)
7740 char_u *str;
7741 int function;
7742{
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007744 char_u *p, *r, *s;
7745
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 len = (function ? 13 : 3);
7747 if (str != NULL)
7748 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007749 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 for (p = str; *p != NUL; mb_ptr_adv(p))
7751 if (*p == '\'')
7752 ++len;
7753 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007754 s = r = alloc(len);
7755 if (r != NULL)
7756 {
7757 if (function)
7758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007760 r += 10;
7761 }
7762 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 if (str != NULL)
7765 for (p = str; *p != NUL; )
7766 {
7767 if (*p == '\'')
7768 *r++ = '\'';
7769 MB_COPY_CHAR(p, r);
7770 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007772 if (function)
7773 *r++ = ')';
7774 *r++ = NUL;
7775 }
7776 return s;
7777}
7778
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007779#ifdef FEAT_FLOAT
7780/*
7781 * Convert the string "text" to a floating point number.
7782 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7783 * this always uses a decimal point.
7784 * Returns the length of the text that was consumed.
7785 */
7786 static int
7787string2float(text, value)
7788 char_u *text;
7789 float_T *value; /* result stored here */
7790{
7791 char *s = (char *)text;
7792 float_T f;
7793
7794 f = strtod(s, &s);
7795 *value = f;
7796 return (int)((char_u *)s - text);
7797}
7798#endif
7799
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 * Get the value of an environment variable.
7802 * "arg" is pointing to the '$'. It is advanced to after the name.
7803 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007804 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 */
7806 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007807get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 int evaluate;
7811{
7812 char_u *string = NULL;
7813 int len;
7814 int cc;
7815 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007816 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
7818 ++*arg;
7819 name = *arg;
7820 len = get_env_len(arg);
7821 if (evaluate)
7822 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007823 if (len == 0)
7824 return FAIL; /* can't be an environment variable */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007825
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007826 cc = name[len];
7827 name[len] = NUL;
7828 /* first try vim_getenv(), fast for normal environment vars */
7829 string = vim_getenv(name, &mustfree);
7830 if (string != NULL && *string != NUL)
7831 {
7832 if (!mustfree)
7833 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007835 else
7836 {
7837 if (mustfree)
7838 vim_free(string);
7839
7840 /* next try expanding things like $VIM and ${HOME} */
7841 string = expand_env_save(name - 1);
7842 if (string != NULL && *string == '$')
7843 {
7844 vim_free(string);
7845 string = NULL;
7846 }
7847 }
7848 name[len] = cc;
7849
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->v_type = VAR_STRING;
7851 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
7853
7854 return OK;
7855}
7856
7857/*
7858 * Array with names and number of arguments of all internal functions
7859 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7860 */
7861static struct fst
7862{
7863 char *f_name; /* function name */
7864 char f_min_argc; /* minimal number of arguments */
7865 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007867 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868} functions[] =
7869{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#ifdef FEAT_FLOAT
7871 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007872 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007874 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007875 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"append", 2, 2, f_append},
7877 {"argc", 0, 0, f_argc},
7878 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007879 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007880 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007881#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007882 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007884 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007887 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"bufexists", 1, 1, f_bufexists},
7889 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7890 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7891 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7892 {"buflisted", 1, 1, f_buflisted},
7893 {"bufloaded", 1, 1, f_bufloaded},
7894 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007895 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"bufwinnr", 1, 1, f_bufwinnr},
7897 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007898 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007899 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"ceil", 1, 1, f_ceil},
7903#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007904 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007905 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007907 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007909#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007910 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007911 {"complete_add", 1, 1, f_complete_add},
7912 {"complete_check", 0, 0, f_complete_check},
7913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#ifdef FEAT_FLOAT
7917 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007918 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007922 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007923 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"delete", 1, 1, f_delete},
7925 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007926 {"diff_filler", 1, 1, f_diff_filler},
7927 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007928 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007930 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"eventhandler", 0, 0, f_eventhandler},
7932 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007933 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007935#ifdef FEAT_FLOAT
7936 {"exp", 1, 1, f_exp},
7937#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007938 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007939 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007940 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7942 {"filereadable", 1, 1, f_filereadable},
7943 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007945 {"finddir", 1, 3, f_finddir},
7946 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947#ifdef FEAT_FLOAT
7948 {"float2nr", 1, 1, f_float2nr},
7949 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007950 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007951#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007952 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"fnamemodify", 2, 2, f_fnamemodify},
7954 {"foldclosed", 1, 1, f_foldclosed},
7955 {"foldclosedend", 1, 1, f_foldclosedend},
7956 {"foldlevel", 1, 1, f_foldlevel},
7957 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007958 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007960 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007961 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007962 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007963 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007964 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"getchar", 0, 1, f_getchar},
7966 {"getcharmod", 0, 0, f_getcharmod},
7967 {"getcmdline", 0, 0, f_getcmdline},
7968 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007969 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007971 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007972 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"getfsize", 1, 1, f_getfsize},
7974 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007975 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007976 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007977 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007978 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007979 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007980 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007981 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02007982 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007984 {"gettabvar", 2, 3, f_gettabvar},
7985 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"getwinposx", 0, 0, f_getwinposx},
7987 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007988 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007989 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02007990 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007992 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007993 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007994 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7996 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7997 {"histadd", 2, 2, f_histadd},
7998 {"histdel", 1, 2, f_histdel},
7999 {"histget", 1, 2, f_histget},
8000 {"histnr", 1, 1, f_histnr},
8001 {"hlID", 1, 1, f_hlID},
8002 {"hlexists", 1, 1, f_hlexists},
8003 {"hostname", 0, 0, f_hostname},
8004 {"iconv", 3, 3, f_iconv},
8005 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008007 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008009 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {"inputrestore", 0, 0, f_inputrestore},
8011 {"inputsave", 0, 0, f_inputsave},
8012 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008013 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008014 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008016 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008021 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"libcall", 3, 3, f_libcall},
8023 {"libcallnr", 3, 3, f_libcallnr},
8024 {"line", 1, 1, f_line},
8025 {"line2byte", 1, 1, f_line2byte},
8026 {"lispindent", 1, 1, f_lispindent},
8027 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008028#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008029 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008030 {"log10", 1, 1, f_log10},
8031#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008032#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008033 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008034#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008035 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008036 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008037 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008038 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008039 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008040 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008041 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008042 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008043 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008044 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008045 {"max", 1, 1, f_max},
8046 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008047#ifdef vim_mkdir
8048 {"mkdir", 1, 3, f_mkdir},
8049#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008051#ifdef FEAT_MZSCHEME
8052 {"mzeval", 1, 1, f_mzeval},
8053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008055 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008056 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008057 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008058#ifdef FEAT_FLOAT
8059 {"pow", 2, 2, f_pow},
8060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008062 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008063 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008064#ifdef FEAT_PYTHON3
8065 {"py3eval", 1, 1, f_py3eval},
8066#endif
8067#ifdef FEAT_PYTHON
8068 {"pyeval", 1, 1, f_pyeval},
8069#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008070 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008071 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008072 {"reltime", 0, 2, f_reltime},
8073 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"remote_expr", 2, 3, f_remote_expr},
8075 {"remote_foreground", 1, 1, f_remote_foreground},
8076 {"remote_peek", 1, 2, f_remote_peek},
8077 {"remote_read", 1, 1, f_remote_read},
8078 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008079 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008081 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008083 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008084#ifdef FEAT_FLOAT
8085 {"round", 1, 1, f_round},
8086#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008087 {"screenattr", 2, 2, f_screenattr},
8088 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008089 {"screencol", 0, 0, f_screencol},
8090 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008091 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008092 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008093 {"searchpair", 3, 7, f_searchpair},
8094 {"searchpairpos", 3, 7, f_searchpairpos},
8095 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"server2client", 2, 2, f_server2client},
8097 {"serverlist", 0, 0, f_serverlist},
8098 {"setbufvar", 3, 3, f_setbufvar},
8099 {"setcmdpos", 1, 1, f_setcmdpos},
8100 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008101 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008102 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008103 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008104 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008106 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008107 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008109#ifdef FEAT_CRYPT
8110 {"sha256", 1, 1, f_sha256},
8111#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008112 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008113 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008117 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008118#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008119 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008120 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008121 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008122 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008123 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008124#ifdef FEAT_FLOAT
8125 {"sqrt", 1, 1, f_sqrt},
8126 {"str2float", 1, 1, f_str2float},
8127#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008128 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008129 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008130 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131#ifdef HAVE_STRFTIME
8132 {"strftime", 1, 2, f_strftime},
8133#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008134 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008135 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"strlen", 1, 1, f_strlen},
8137 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008138 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008140 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008141 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"substitute", 4, 4, f_substitute},
8143 {"synID", 3, 3, f_synID},
8144 {"synIDattr", 2, 3, f_synIDattr},
8145 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008146 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008147 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008148 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008149 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008150 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008151 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008152 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008153 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008154 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008155#ifdef FEAT_FLOAT
8156 {"tan", 1, 1, f_tan},
8157 {"tanh", 1, 1, f_tanh},
8158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008160 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"tolower", 1, 1, f_tolower},
8162 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008163 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#ifdef FEAT_FLOAT
8165 {"trunc", 1, 1, f_trunc},
8166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008168 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008169 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008170 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008171 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"virtcol", 1, 1, f_virtcol},
8173 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008174 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"winbufnr", 1, 1, f_winbufnr},
8176 {"wincol", 0, 0, f_wincol},
8177 {"winheight", 1, 1, f_winheight},
8178 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008179 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008181 {"winrestview", 1, 1, f_winrestview},
8182 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008184 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008185 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186};
8187
8188#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8189
8190/*
8191 * Function given to ExpandGeneric() to obtain the list of internal
8192 * or user defined function names.
8193 */
8194 char_u *
8195get_function_name(xp, idx)
8196 expand_T *xp;
8197 int idx;
8198{
8199 static int intidx = -1;
8200 char_u *name;
8201
8202 if (idx == 0)
8203 intidx = -1;
8204 if (intidx < 0)
8205 {
8206 name = get_user_func_name(xp, idx);
8207 if (name != NULL)
8208 return name;
8209 }
8210 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8211 {
8212 STRCPY(IObuff, functions[intidx].f_name);
8213 STRCAT(IObuff, "(");
8214 if (functions[intidx].f_max_argc == 0)
8215 STRCAT(IObuff, ")");
8216 return IObuff;
8217 }
8218
8219 return NULL;
8220}
8221
8222/*
8223 * Function given to ExpandGeneric() to obtain the list of internal or
8224 * user defined variable or function names.
8225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 char_u *
8227get_expr_name(xp, idx)
8228 expand_T *xp;
8229 int idx;
8230{
8231 static int intidx = -1;
8232 char_u *name;
8233
8234 if (idx == 0)
8235 intidx = -1;
8236 if (intidx < 0)
8237 {
8238 name = get_function_name(xp, idx);
8239 if (name != NULL)
8240 return name;
8241 }
8242 return get_user_var_name(xp, ++intidx);
8243}
8244
8245#endif /* FEAT_CMDL_COMPL */
8246
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008247#if defined(EBCDIC) || defined(PROTO)
8248/*
8249 * Compare struct fst by function name.
8250 */
8251 static int
8252compare_func_name(s1, s2)
8253 const void *s1;
8254 const void *s2;
8255{
8256 struct fst *p1 = (struct fst *)s1;
8257 struct fst *p2 = (struct fst *)s2;
8258
8259 return STRCMP(p1->f_name, p2->f_name);
8260}
8261
8262/*
8263 * Sort the function table by function name.
8264 * The sorting of the table above is ASCII dependant.
8265 * On machines using EBCDIC we have to sort it.
8266 */
8267 static void
8268sortFunctions()
8269{
8270 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8271
8272 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8273}
8274#endif
8275
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277/*
8278 * Find internal function in table above.
8279 * Return index, or -1 if not found
8280 */
8281 static int
8282find_internal_func(name)
8283 char_u *name; /* name of the function */
8284{
8285 int first = 0;
8286 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8287 int cmp;
8288 int x;
8289
8290 /*
8291 * Find the function name in the table. Binary search.
8292 */
8293 while (first <= last)
8294 {
8295 x = first + ((unsigned)(last - first) >> 1);
8296 cmp = STRCMP(name, functions[x].f_name);
8297 if (cmp < 0)
8298 last = x - 1;
8299 else if (cmp > 0)
8300 first = x + 1;
8301 else
8302 return x;
8303 }
8304 return -1;
8305}
8306
8307/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8309 * name it contains, otherwise return "name".
8310 */
8311 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008312deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 char_u *name;
8314 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008315 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008316{
Bram Moolenaar33570922005-01-25 22:26:29 +00008317 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008318 int cc;
8319
8320 cc = name[*lenp];
8321 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008322 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008325 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008326 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008327 {
8328 *lenp = 0;
8329 return (char_u *)""; /* just in case */
8330 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008331 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008333 }
8334
8335 return name;
8336}
8337
8338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 * Allocate a variable for the result of a function.
8340 * Return OK or FAIL.
8341 */
8342 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008343get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8344 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 char_u *name; /* name of the function */
8346 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 char_u **arg; /* argument, pointing to the '(' */
8349 linenr_T firstline; /* first line of range */
8350 linenr_T lastline; /* last line of range */
8351 int *doesrange; /* return: function handled range */
8352 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354{
8355 char_u *argp;
8356 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008357 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 int argcount = 0; /* number of arguments found */
8359
8360 /*
8361 * Get the arguments.
8362 */
8363 argp = *arg;
8364 while (argcount < MAX_FUNC_ARGS)
8365 {
8366 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8367 if (*argp == ')' || *argp == ',' || *argp == NUL)
8368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8370 {
8371 ret = FAIL;
8372 break;
8373 }
8374 ++argcount;
8375 if (*argp != ',')
8376 break;
8377 }
8378 if (*argp == ')')
8379 ++argp;
8380 else
8381 ret = FAIL;
8382
8383 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008385 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 {
8388 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008389 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008391 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393
8394 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
8397 *arg = skipwhite(argp);
8398 return ret;
8399}
8400
8401
8402/*
8403 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008404 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008405 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 */
8407 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008408call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008410 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008412 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008414 typval_T *argvars; /* vars for arguments, must have "argcount"
8415 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 linenr_T firstline; /* first line of range */
8417 linenr_T lastline; /* last line of range */
8418 int *doesrange; /* return: function handled range */
8419 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008420 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421{
8422 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423#define ERROR_UNKNOWN 0
8424#define ERROR_TOOMANY 1
8425#define ERROR_TOOFEW 2
8426#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008427#define ERROR_DICT 4
8428#define ERROR_NONE 5
8429#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 int error = ERROR_NONE;
8431 int i;
8432 int llen;
8433 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434#define FLEN_FIXED 40
8435 char_u fname_buf[FLEN_FIXED + 1];
8436 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008437 char_u *name;
8438
8439 /* Make a copy of the name, if it comes from a funcref variable it could
8440 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008441 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008442 if (name == NULL)
8443 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444
8445 /*
8446 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8447 * Change <SNR>123_name() to K_SNR 123_name().
8448 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8449 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 llen = eval_fname_script(name);
8451 if (llen > 0)
8452 {
8453 fname_buf[0] = K_SPECIAL;
8454 fname_buf[1] = KS_EXTRA;
8455 fname_buf[2] = (int)KE_SNR;
8456 i = 3;
8457 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8458 {
8459 if (current_SID <= 0)
8460 error = ERROR_SCRIPT;
8461 else
8462 {
8463 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8464 i = (int)STRLEN(fname_buf);
8465 }
8466 }
8467 if (i + STRLEN(name + llen) < FLEN_FIXED)
8468 {
8469 STRCPY(fname_buf + i, name + llen);
8470 fname = fname_buf;
8471 }
8472 else
8473 {
8474 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8475 if (fname == NULL)
8476 error = ERROR_OTHER;
8477 else
8478 {
8479 mch_memmove(fname, fname_buf, (size_t)i);
8480 STRCPY(fname + i, name + llen);
8481 }
8482 }
8483 }
8484 else
8485 fname = name;
8486
8487 *doesrange = FALSE;
8488
8489
8490 /* execute the function if no errors detected and executing */
8491 if (evaluate && error == ERROR_NONE)
8492 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008493 char_u *rfname = fname;
8494
8495 /* Ignore "g:" before a function name. */
8496 if (fname[0] == 'g' && fname[1] == ':')
8497 rfname = fname + 2;
8498
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008499 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8500 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 error = ERROR_UNKNOWN;
8502
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008503 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {
8505 /*
8506 * User defined function.
8507 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008508 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008511 /* Trigger FuncUndefined event, may load the function. */
8512 if (fp == NULL
8513 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008514 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008515 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008517 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008518 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 }
8520#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008521 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008522 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008523 {
8524 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008525 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008526 }
8527
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (fp != NULL)
8529 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008530 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008532 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008534 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008536 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 else
8539 {
8540 /*
8541 * Call the user function.
8542 * Save and restore search patterns, script variables and
8543 * redo buffer.
8544 */
8545 save_search_patterns();
8546 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008547 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008548 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008549 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008550 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8551 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8552 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008553 /* Function was unreferenced while being used, free it
8554 * now. */
8555 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 restoreRedobuff();
8557 restore_search_patterns();
8558 error = ERROR_NONE;
8559 }
8560 }
8561 }
8562 else
8563 {
8564 /*
8565 * Find the function name in the table, call its implementation.
8566 */
8567 i = find_internal_func(fname);
8568 if (i >= 0)
8569 {
8570 if (argcount < functions[i].f_min_argc)
8571 error = ERROR_TOOFEW;
8572 else if (argcount > functions[i].f_max_argc)
8573 error = ERROR_TOOMANY;
8574 else
8575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008577 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 error = ERROR_NONE;
8579 }
8580 }
8581 }
8582 /*
8583 * The function call (or "FuncUndefined" autocommand sequence) might
8584 * have been aborted by an error, an interrupt, or an explicitly thrown
8585 * exception that has not been caught so far. This situation can be
8586 * tested for by calling aborting(). For an error in an internal
8587 * function or for the "E132" error in call_user_func(), however, the
8588 * throw point at which the "force_abort" flag (temporarily reset by
8589 * emsg()) is normally updated has not been reached yet. We need to
8590 * update that flag first to make aborting() reliable.
8591 */
8592 update_force_abort();
8593 }
8594 if (error == ERROR_NONE)
8595 ret = OK;
8596
8597 /*
8598 * Report an error unless the argument evaluation or function call has been
8599 * cancelled due to an aborting error, an interrupt, or an exception.
8600 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008601 if (!aborting())
8602 {
8603 switch (error)
8604 {
8605 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008606 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008607 break;
8608 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008609 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008610 break;
8611 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008612 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008613 name);
8614 break;
8615 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008616 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008617 name);
8618 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008620 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 name);
8622 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008623 }
8624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 if (fname != name && fname != fname_buf)
8627 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008628 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
8630 return ret;
8631}
8632
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008633/*
8634 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008635 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008636 */
8637 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008638emsg_funcname(ermsg, name)
8639 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008640 char_u *name;
8641{
8642 char_u *p;
8643
8644 if (*name == K_SPECIAL)
8645 p = concat_str((char_u *)"<SNR>", name + 3);
8646 else
8647 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008648 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008649 if (p != name)
8650 vim_free(p);
8651}
8652
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008653/*
8654 * Return TRUE for a non-zero Number and a non-empty String.
8655 */
8656 static int
8657non_zero_arg(argvars)
8658 typval_T *argvars;
8659{
8660 return ((argvars[0].v_type == VAR_NUMBER
8661 && argvars[0].vval.v_number != 0)
8662 || (argvars[0].v_type == VAR_STRING
8663 && argvars[0].vval.v_string != NULL
8664 && *argvars[0].vval.v_string != NUL));
8665}
8666
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667/*********************************************
8668 * Implementation of the built-in functions
8669 */
8670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008671#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008672static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8673
8674/*
8675 * Get the float value of "argvars[0]" into "f".
8676 * Returns FAIL when the argument is not a Number or Float.
8677 */
8678 static int
8679get_float_arg(argvars, f)
8680 typval_T *argvars;
8681 float_T *f;
8682{
8683 if (argvars[0].v_type == VAR_FLOAT)
8684 {
8685 *f = argvars[0].vval.v_float;
8686 return OK;
8687 }
8688 if (argvars[0].v_type == VAR_NUMBER)
8689 {
8690 *f = (float_T)argvars[0].vval.v_number;
8691 return OK;
8692 }
8693 EMSG(_("E808: Number or Float required"));
8694 return FAIL;
8695}
8696
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008697/*
8698 * "abs(expr)" function
8699 */
8700 static void
8701f_abs(argvars, rettv)
8702 typval_T *argvars;
8703 typval_T *rettv;
8704{
8705 if (argvars[0].v_type == VAR_FLOAT)
8706 {
8707 rettv->v_type = VAR_FLOAT;
8708 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8709 }
8710 else
8711 {
8712 varnumber_T n;
8713 int error = FALSE;
8714
8715 n = get_tv_number_chk(&argvars[0], &error);
8716 if (error)
8717 rettv->vval.v_number = -1;
8718 else if (n > 0)
8719 rettv->vval.v_number = n;
8720 else
8721 rettv->vval.v_number = -n;
8722 }
8723}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008724
8725/*
8726 * "acos()" function
8727 */
8728 static void
8729f_acos(argvars, rettv)
8730 typval_T *argvars;
8731 typval_T *rettv;
8732{
8733 float_T f;
8734
8735 rettv->v_type = VAR_FLOAT;
8736 if (get_float_arg(argvars, &f) == OK)
8737 rettv->vval.v_float = acos(f);
8738 else
8739 rettv->vval.v_float = 0.0;
8740}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008741#endif
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 */
8746 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008748 typval_T *argvars;
8749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar33570922005-01-25 22:26:29 +00008751 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008754 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008756 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008757 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008758 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008759 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008760 }
8761 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762 EMSG(_(e_listreq));
8763}
8764
8765/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008766 * "and(expr, expr)" function
8767 */
8768 static void
8769f_and(argvars, rettv)
8770 typval_T *argvars;
8771 typval_T *rettv;
8772{
8773 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8774 & get_tv_number_chk(&argvars[1], NULL);
8775}
8776
8777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008778 * "append(lnum, string/list)" function
8779 */
8780 static void
8781f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008782 typval_T *argvars;
8783 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008784{
8785 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008786 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 list_T *l = NULL;
8788 listitem_T *li = NULL;
8789 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008790 long added = 0;
8791
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008792 /* When coming here from Insert mode, sync undo, so that this can be
8793 * undone separately from what was previously inserted. */
8794 if (u_sync_once == 2)
8795 {
8796 u_sync_once = 1; /* notify that u_sync() was called */
8797 u_sync(TRUE);
8798 }
8799
Bram Moolenaar0d660222005-01-07 21:51:51 +00008800 lnum = get_tv_lnum(argvars);
8801 if (lnum >= 0
8802 && lnum <= curbuf->b_ml.ml_line_count
8803 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008805 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008806 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008807 l = argvars[1].vval.v_list;
8808 if (l == NULL)
8809 return;
8810 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008811 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008812 for (;;)
8813 {
8814 if (l == NULL)
8815 tv = &argvars[1]; /* append a string */
8816 else if (li == NULL)
8817 break; /* end of list */
8818 else
8819 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 line = get_tv_string_chk(tv);
8821 if (line == NULL) /* type error */
8822 {
8823 rettv->vval.v_number = 1; /* Failed */
8824 break;
8825 }
8826 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008827 ++added;
8828 if (l == NULL)
8829 break;
8830 li = li->li_next;
8831 }
8832
8833 appended_lines_mark(lnum, added);
8834 if (curwin->w_cursor.lnum > lnum)
8835 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008837 else
8838 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841/*
8842 * "argc()" function
8843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008849 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850}
8851
8852/*
8853 * "argidx()" function
8854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
8863/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008864 * "arglistid()" function
8865 */
8866 static void
8867f_arglistid(argvars, rettv)
8868 typval_T *argvars UNUSED;
8869 typval_T *rettv;
8870{
8871 win_T *wp;
8872 tabpage_T *tp = NULL;
8873 long n;
8874
8875 rettv->vval.v_number = -1;
8876 if (argvars[0].v_type != VAR_UNKNOWN)
8877 {
8878 if (argvars[1].v_type != VAR_UNKNOWN)
8879 {
8880 n = get_tv_number(&argvars[1]);
8881 if (n >= 0)
8882 tp = find_tabpage(n);
8883 }
8884 else
8885 tp = curtab;
8886
8887 if (tp != NULL)
8888 {
8889 wp = find_win_by_nr(&argvars[0], tp);
8890 if (wp != NULL)
8891 rettv->vval.v_number = wp->w_alist->id;
8892 }
8893 }
8894 else
8895 rettv->vval.v_number = curwin->w_alist->id;
8896}
8897
8898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 * "argv(nr)" function
8900 */
8901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008902f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008903 typval_T *argvars;
8904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906 int idx;
8907
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008908 if (argvars[0].v_type != VAR_UNKNOWN)
8909 {
8910 idx = get_tv_number_chk(&argvars[0], NULL);
8911 if (idx >= 0 && idx < ARGCOUNT)
8912 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8913 else
8914 rettv->vval.v_string = NULL;
8915 rettv->v_type = VAR_STRING;
8916 }
8917 else if (rettv_list_alloc(rettv) == OK)
8918 for (idx = 0; idx < ARGCOUNT; ++idx)
8919 list_append_string(rettv->vval.v_list,
8920 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921}
8922
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008923#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008925 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008926 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008927 static void
8928f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008929 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008930 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008931{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008932 float_T f;
8933
8934 rettv->v_type = VAR_FLOAT;
8935 if (get_float_arg(argvars, &f) == OK)
8936 rettv->vval.v_float = asin(f);
8937 else
8938 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008939}
8940
8941/*
8942 * "atan()" function
8943 */
8944 static void
8945f_atan(argvars, rettv)
8946 typval_T *argvars;
8947 typval_T *rettv;
8948{
8949 float_T f;
8950
8951 rettv->v_type = VAR_FLOAT;
8952 if (get_float_arg(argvars, &f) == OK)
8953 rettv->vval.v_float = atan(f);
8954 else
8955 rettv->vval.v_float = 0.0;
8956}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008957
8958/*
8959 * "atan2()" function
8960 */
8961 static void
8962f_atan2(argvars, rettv)
8963 typval_T *argvars;
8964 typval_T *rettv;
8965{
8966 float_T fx, fy;
8967
8968 rettv->v_type = VAR_FLOAT;
8969 if (get_float_arg(argvars, &fx) == OK
8970 && get_float_arg(&argvars[1], &fy) == OK)
8971 rettv->vval.v_float = atan2(fx, fy);
8972 else
8973 rettv->vval.v_float = 0.0;
8974}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008975#endif
8976
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977/*
8978 * "browse(save, title, initdir, default)" function
8979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008982 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985#ifdef FEAT_BROWSE
8986 int save;
8987 char_u *title;
8988 char_u *initdir;
8989 char_u *defname;
8990 char_u buf[NUMBUFLEN];
8991 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008992 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 save = get_tv_number_chk(&argvars[0], &error);
8995 title = get_tv_string_chk(&argvars[1]);
8996 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8997 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 if (error || title == NULL || initdir == NULL || defname == NULL)
9000 rettv->vval.v_string = NULL;
9001 else
9002 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009003 do_browse(save ? BROWSE_SAVE : 0,
9004 title, defname, NULL, initdir, NULL, curbuf);
9005#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009007#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009008 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009009}
9010
9011/*
9012 * "browsedir(title, initdir)" function
9013 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009015f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009016 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009018{
9019#ifdef FEAT_BROWSE
9020 char_u *title;
9021 char_u *initdir;
9022 char_u buf[NUMBUFLEN];
9023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009024 title = get_tv_string_chk(&argvars[0]);
9025 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009027 if (title == NULL || initdir == NULL)
9028 rettv->vval.v_string = NULL;
9029 else
9030 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009031 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009033 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
Bram Moolenaar33570922005-01-25 22:26:29 +00009038static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009039
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040/*
9041 * Find a buffer by number or exact name.
9042 */
9043 static buf_T *
9044find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009045 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046{
9047 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009049 if (avar->v_type == VAR_NUMBER)
9050 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009051 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009053 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009054 if (buf == NULL)
9055 {
9056 /* No full path name match, try a match with a URL or a "nofile"
9057 * buffer, these don't use the full path. */
9058 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9059 if (buf->b_fname != NULL
9060 && (path_with_url(buf->b_fname)
9061#ifdef FEAT_QUICKFIX
9062 || bt_nofile(buf)
9063#endif
9064 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009065 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009066 break;
9067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 }
9069 return buf;
9070}
9071
9072/*
9073 * "bufexists(expr)" function
9074 */
9075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081}
9082
9083/*
9084 * "buflisted(expr)" function
9085 */
9086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 typval_T *argvars;
9089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090{
9091 buf_T *buf;
9092
9093 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095}
9096
9097/*
9098 * "bufloaded(expr)" function
9099 */
9100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *argvars;
9103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 buf_T *buf;
9106
9107 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009111static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113/*
9114 * Get buffer by number or pattern.
9115 */
9116 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009117get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009119 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 int save_magic;
9123 char_u *save_cpo;
9124 buf_T *buf;
9125
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126 if (tv->v_type == VAR_NUMBER)
9127 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009128 if (tv->v_type != VAR_STRING)
9129 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 if (name == NULL || *name == NUL)
9131 return curbuf;
9132 if (name[0] == '$' && name[1] == NUL)
9133 return lastbuf;
9134
9135 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9136 save_magic = p_magic;
9137 p_magic = TRUE;
9138 save_cpo = p_cpo;
9139 p_cpo = (char_u *)"";
9140
9141 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009142 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143
9144 p_magic = save_magic;
9145 p_cpo = save_cpo;
9146
9147 /* If not found, try expanding the name, like done for bufexists(). */
9148 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150
9151 return buf;
9152}
9153
9154/*
9155 * "bufname(expr)" function
9156 */
9157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009159 typval_T *argvars;
9160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 buf_T *buf;
9163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009166 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 --emsg_off;
9173}
9174
9175/*
9176 * "bufnr(expr)" function
9177 */
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
9183 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009184 int error = FALSE;
9185 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009187 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009189 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009190 --emsg_off;
9191
9192 /* If the buffer isn't found and the second argument is not zero create a
9193 * new buffer. */
9194 if (buf == NULL
9195 && argvars[1].v_type != VAR_UNKNOWN
9196 && get_tv_number_chk(&argvars[1], &error) != 0
9197 && !error
9198 && (name = get_tv_string_chk(&argvars[0])) != NULL
9199 && !error)
9200 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
9208/*
9209 * "bufwinnr(nr)" function
9210 */
9211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009212f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009213 typval_T *argvars;
9214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
9216#ifdef FEAT_WINDOWS
9217 win_T *wp;
9218 int winnr = 0;
9219#endif
9220 buf_T *buf;
9221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009222 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009224 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225#ifdef FEAT_WINDOWS
9226 for (wp = firstwin; wp; wp = wp->w_next)
9227 {
9228 ++winnr;
9229 if (wp->w_buffer == buf)
9230 break;
9231 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009232 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235#endif
9236 --emsg_off;
9237}
9238
9239/*
9240 * "byte2line(byte)" function
9241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009243f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009244 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
9247#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249#else
9250 long boff = 0;
9251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009252 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009256 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 (linenr_T)0, &boff);
9258#endif
9259}
9260
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009261 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009262byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009263 typval_T *argvars;
9264 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009265 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009266{
9267#ifdef FEAT_MBYTE
9268 char_u *t;
9269#endif
9270 char_u *str;
9271 long idx;
9272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 str = get_tv_string_chk(&argvars[0]);
9274 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009276 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009277 return;
9278
9279#ifdef FEAT_MBYTE
9280 t = str;
9281 for ( ; idx > 0; idx--)
9282 {
9283 if (*t == NUL) /* EOL reached */
9284 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009285 if (enc_utf8 && comp)
9286 t += utf_ptr2len(t);
9287 else
9288 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009290 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009291#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009294#endif
9295}
9296
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009297/*
9298 * "byteidx()" function
9299 */
9300 static void
9301f_byteidx(argvars, rettv)
9302 typval_T *argvars;
9303 typval_T *rettv;
9304{
9305 byteidx(argvars, rettv, FALSE);
9306}
9307
9308/*
9309 * "byteidxcomp()" function
9310 */
9311 static void
9312f_byteidxcomp(argvars, rettv)
9313 typval_T *argvars;
9314 typval_T *rettv;
9315{
9316 byteidx(argvars, rettv, TRUE);
9317}
9318
Bram Moolenaardb913952012-06-29 12:54:53 +02009319 int
9320func_call(name, args, selfdict, rettv)
9321 char_u *name;
9322 typval_T *args;
9323 dict_T *selfdict;
9324 typval_T *rettv;
9325{
9326 listitem_T *item;
9327 typval_T argv[MAX_FUNC_ARGS + 1];
9328 int argc = 0;
9329 int dummy;
9330 int r = 0;
9331
9332 for (item = args->vval.v_list->lv_first; item != NULL;
9333 item = item->li_next)
9334 {
9335 if (argc == MAX_FUNC_ARGS)
9336 {
9337 EMSG(_("E699: Too many arguments"));
9338 break;
9339 }
9340 /* Make a copy of each argument. This is needed to be able to set
9341 * v_lock to VAR_FIXED in the copy without changing the original list.
9342 */
9343 copy_tv(&item->li_tv, &argv[argc++]);
9344 }
9345
9346 if (item == NULL)
9347 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9348 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9349 &dummy, TRUE, selfdict);
9350
9351 /* Free the arguments. */
9352 while (argc > 0)
9353 clear_tv(&argv[--argc]);
9354
9355 return r;
9356}
9357
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009358/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009359 * "call(func, arglist)" function
9360 */
9361 static void
9362f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009363 typval_T *argvars;
9364 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009365{
9366 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009367 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009368
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009369 if (argvars[1].v_type != VAR_LIST)
9370 {
9371 EMSG(_(e_listreq));
9372 return;
9373 }
9374 if (argvars[1].vval.v_list == NULL)
9375 return;
9376
9377 if (argvars[0].v_type == VAR_FUNC)
9378 func = argvars[0].vval.v_string;
9379 else
9380 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 if (*func == NUL)
9382 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009383
Bram Moolenaare9a41262005-01-15 22:18:47 +00009384 if (argvars[2].v_type != VAR_UNKNOWN)
9385 {
9386 if (argvars[2].v_type != VAR_DICT)
9387 {
9388 EMSG(_(e_dictreq));
9389 return;
9390 }
9391 selfdict = argvars[2].vval.v_dict;
9392 }
9393
Bram Moolenaardb913952012-06-29 12:54:53 +02009394 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009395}
9396
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009397#ifdef FEAT_FLOAT
9398/*
9399 * "ceil({float})" function
9400 */
9401 static void
9402f_ceil(argvars, rettv)
9403 typval_T *argvars;
9404 typval_T *rettv;
9405{
9406 float_T f;
9407
9408 rettv->v_type = VAR_FLOAT;
9409 if (get_float_arg(argvars, &f) == OK)
9410 rettv->vval.v_float = ceil(f);
9411 else
9412 rettv->vval.v_float = 0.0;
9413}
9414#endif
9415
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009416/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009417 * "changenr()" function
9418 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009419 static void
9420f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009421 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009422 typval_T *rettv;
9423{
9424 rettv->vval.v_number = curbuf->b_u_seq_cur;
9425}
9426
9427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 * "char2nr(string)" function
9429 */
9430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 typval_T *argvars;
9433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
9435#ifdef FEAT_MBYTE
9436 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009437 {
9438 int utf8 = 0;
9439
9440 if (argvars[1].v_type != VAR_UNKNOWN)
9441 utf8 = get_tv_number_chk(&argvars[1], NULL);
9442
9443 if (utf8)
9444 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9445 else
9446 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 else
9449#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
9454 * "cindent(lnum)" function
9455 */
9456 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
9461#ifdef FEAT_CINDENT
9462 pos_T pos;
9463 linenr_T lnum;
9464
9465 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9468 {
9469 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009470 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 curwin->w_cursor = pos;
9472 }
9473 else
9474#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009479 * "clearmatches()" function
9480 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009481 static void
9482f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009483 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009484 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009485{
9486#ifdef FEAT_SEARCH_EXTRA
9487 clear_matches(curwin);
9488#endif
9489}
9490
9491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 * "col(string)" function
9493 */
9494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009496 typval_T *argvars;
9497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498{
9499 colnr_T col = 0;
9500 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009501 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009503 fp = var2fpos(&argvars[0], FALSE, &fnum);
9504 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 {
9506 if (fp->col == MAXCOL)
9507 {
9508 /* '> can be MAXCOL, get the length of the line then */
9509 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009510 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 else
9512 col = MAXCOL;
9513 }
9514 else
9515 {
9516 col = fp->col + 1;
9517#ifdef FEAT_VIRTUALEDIT
9518 /* col(".") when the cursor is on the NUL at the end of the line
9519 * because of "coladd" can be seen as an extra column. */
9520 if (virtual_active() && fp == &curwin->w_cursor)
9521 {
9522 char_u *p = ml_get_cursor();
9523
9524 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9525 curwin->w_virtcol - curwin->w_cursor.coladd))
9526 {
9527# ifdef FEAT_MBYTE
9528 int l;
9529
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009530 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 col += l;
9532# else
9533 if (*p != NUL && p[1] == NUL)
9534 ++col;
9535# endif
9536 }
9537 }
9538#endif
9539 }
9540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009541 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542}
9543
Bram Moolenaar572cb562005-08-05 21:35:02 +00009544#if defined(FEAT_INS_EXPAND)
9545/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009546 * "complete()" function
9547 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009548 static void
9549f_complete(argvars, rettv)
9550 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009551 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009552{
9553 int startcol;
9554
9555 if ((State & INSERT) == 0)
9556 {
9557 EMSG(_("E785: complete() can only be used in Insert mode"));
9558 return;
9559 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009560
9561 /* Check for undo allowed here, because if something was already inserted
9562 * the line was already saved for undo and this check isn't done. */
9563 if (!undo_allowed())
9564 return;
9565
Bram Moolenaarade00832006-03-10 21:46:58 +00009566 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9567 {
9568 EMSG(_(e_invarg));
9569 return;
9570 }
9571
9572 startcol = get_tv_number_chk(&argvars[0], NULL);
9573 if (startcol <= 0)
9574 return;
9575
9576 set_completion(startcol - 1, argvars[1].vval.v_list);
9577}
9578
9579/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009580 * "complete_add()" function
9581 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009582 static void
9583f_complete_add(argvars, rettv)
9584 typval_T *argvars;
9585 typval_T *rettv;
9586{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009587 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009588}
9589
9590/*
9591 * "complete_check()" function
9592 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009593 static void
9594f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009595 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009596 typval_T *rettv;
9597{
9598 int saved = RedrawingDisabled;
9599
9600 RedrawingDisabled = 0;
9601 ins_compl_check_keys(0);
9602 rettv->vval.v_number = compl_interrupted;
9603 RedrawingDisabled = saved;
9604}
9605#endif
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607/*
9608 * "confirm(message, buttons[, default [, type]])" function
9609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009612 typval_T *argvars UNUSED;
9613 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9616 char_u *message;
9617 char_u *buttons = NULL;
9618 char_u buf[NUMBUFLEN];
9619 char_u buf2[NUMBUFLEN];
9620 int def = 1;
9621 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 char_u *typestr;
9623 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 message = get_tv_string_chk(&argvars[0]);
9626 if (message == NULL)
9627 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009630 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9631 if (buttons == NULL)
9632 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009636 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9639 if (typestr == NULL)
9640 error = TRUE;
9641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 switch (TOUPPER_ASC(*typestr))
9644 {
9645 case 'E': type = VIM_ERROR; break;
9646 case 'Q': type = VIM_QUESTION; break;
9647 case 'I': type = VIM_INFO; break;
9648 case 'W': type = VIM_WARNING; break;
9649 case 'G': type = VIM_GENERIC; break;
9650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 }
9652 }
9653 }
9654 }
9655
9656 if (buttons == NULL || *buttons == NUL)
9657 buttons = (char_u *)_("&Ok");
9658
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009659 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009661 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662#endif
9663}
9664
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009665/*
9666 * "copy()" function
9667 */
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009672{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009673 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009676#ifdef FEAT_FLOAT
9677/*
9678 * "cos()" function
9679 */
9680 static void
9681f_cos(argvars, rettv)
9682 typval_T *argvars;
9683 typval_T *rettv;
9684{
9685 float_T f;
9686
9687 rettv->v_type = VAR_FLOAT;
9688 if (get_float_arg(argvars, &f) == OK)
9689 rettv->vval.v_float = cos(f);
9690 else
9691 rettv->vval.v_float = 0.0;
9692}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009693
9694/*
9695 * "cosh()" function
9696 */
9697 static void
9698f_cosh(argvars, rettv)
9699 typval_T *argvars;
9700 typval_T *rettv;
9701{
9702 float_T f;
9703
9704 rettv->v_type = VAR_FLOAT;
9705 if (get_float_arg(argvars, &f) == OK)
9706 rettv->vval.v_float = cosh(f);
9707 else
9708 rettv->vval.v_float = 0.0;
9709}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009710#endif
9711
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009713 * "count()" function
9714 */
9715 static void
9716f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 typval_T *argvars;
9718 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009719{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720 long n = 0;
9721 int ic = FALSE;
9722
Bram Moolenaare9a41262005-01-15 22:18:47 +00009723 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009724 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009725 listitem_T *li;
9726 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009727 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009728
Bram Moolenaare9a41262005-01-15 22:18:47 +00009729 if ((l = argvars[0].vval.v_list) != NULL)
9730 {
9731 li = l->lv_first;
9732 if (argvars[2].v_type != VAR_UNKNOWN)
9733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009734 int error = FALSE;
9735
9736 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009737 if (argvars[3].v_type != VAR_UNKNOWN)
9738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 idx = get_tv_number_chk(&argvars[3], &error);
9740 if (!error)
9741 {
9742 li = list_find(l, idx);
9743 if (li == NULL)
9744 EMSGN(_(e_listidx), idx);
9745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009746 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 if (error)
9748 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009749 }
9750
9751 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009752 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009753 ++n;
9754 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009755 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009756 else if (argvars[0].v_type == VAR_DICT)
9757 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009758 int todo;
9759 dict_T *d;
9760 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009761
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009762 if ((d = argvars[0].vval.v_dict) != NULL)
9763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009764 int error = FALSE;
9765
Bram Moolenaare9a41262005-01-15 22:18:47 +00009766 if (argvars[2].v_type != VAR_UNKNOWN)
9767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009768 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009769 if (argvars[3].v_type != VAR_UNKNOWN)
9770 EMSG(_(e_invarg));
9771 }
9772
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009773 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009774 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009775 {
9776 if (!HASHITEM_EMPTY(hi))
9777 {
9778 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009779 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009780 ++n;
9781 }
9782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009783 }
9784 }
9785 else
9786 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009787 rettv->vval.v_number = n;
9788}
9789
9790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9792 *
9793 * Checks the existence of a cscope connection.
9794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009797 typval_T *argvars UNUSED;
9798 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800#ifdef FEAT_CSCOPE
9801 int num = 0;
9802 char_u *dbpath = NULL;
9803 char_u *prepend = NULL;
9804 char_u buf[NUMBUFLEN];
9805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009806 if (argvars[0].v_type != VAR_UNKNOWN
9807 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 num = (int)get_tv_number(&argvars[0]);
9810 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816#endif
9817}
9818
9819/*
9820 * "cursor(lnum, col)" function
9821 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009822 * Moves the cursor to the specified line and column.
9823 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009827 typval_T *argvars;
9828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009831#ifdef FEAT_VIRTUALEDIT
9832 long coladd = 0;
9833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009835 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009836 if (argvars[1].v_type == VAR_UNKNOWN)
9837 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009838 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009839 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009840
Bram Moolenaar493c1782014-05-28 14:34:46 +02009841 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009842 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009843 line = pos.lnum;
9844 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009845#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009846 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009847#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009848 if (curswant >= 0)
9849 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009850 }
9851 else
9852 {
9853 line = get_tv_lnum(argvars);
9854 col = get_tv_number_chk(&argvars[1], NULL);
9855#ifdef FEAT_VIRTUALEDIT
9856 if (argvars[2].v_type != VAR_UNKNOWN)
9857 coladd = get_tv_number_chk(&argvars[2], NULL);
9858#endif
9859 }
9860 if (line < 0 || col < 0
9861#ifdef FEAT_VIRTUALEDIT
9862 || coladd < 0
9863#endif
9864 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009865 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 if (line > 0)
9867 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 if (col > 0)
9869 curwin->w_cursor.col = col - 1;
9870#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009871 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872#endif
9873
9874 /* Make sure the cursor is in a valid position. */
9875 check_cursor();
9876#ifdef FEAT_MBYTE
9877 /* Correct cursor for multi-byte character. */
9878 if (has_mbyte)
9879 mb_adjust_cursor();
9880#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009881
9882 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009883 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884}
9885
9886/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009887 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 */
9889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 typval_T *argvars;
9892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009894 int noref = 0;
9895
9896 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009898 if (noref < 0 || noref > 1)
9899 EMSG(_(e_invarg));
9900 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009901 {
9902 current_copyID += COPYID_INC;
9903 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905}
9906
9907/*
9908 * "delete()" function
9909 */
9910 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009912 typval_T *argvars;
9913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914{
9915 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919}
9920
9921/*
9922 * "did_filetype()" function
9923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009925f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009926 typval_T *argvars UNUSED;
9927 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009930 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#endif
9932}
9933
9934/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009935 * "diff_filler()" function
9936 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009937 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009939 typval_T *argvars UNUSED;
9940 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009941{
9942#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009944#endif
9945}
9946
9947/*
9948 * "diff_hlID()" function
9949 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009952 typval_T *argvars UNUSED;
9953 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009954{
9955#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009957 static linenr_T prev_lnum = 0;
9958 static int changedtick = 0;
9959 static int fnum = 0;
9960 static int change_start = 0;
9961 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009962 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009963 int filler_lines;
9964 int col;
9965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 if (lnum < 0) /* ignore type error in {lnum} arg */
9967 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009968 if (lnum != prev_lnum
9969 || changedtick != curbuf->b_changedtick
9970 || fnum != curbuf->b_fnum)
9971 {
9972 /* New line, buffer, change: need to get the values. */
9973 filler_lines = diff_check(curwin, lnum);
9974 if (filler_lines < 0)
9975 {
9976 if (filler_lines == -1)
9977 {
9978 change_start = MAXCOL;
9979 change_end = -1;
9980 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9981 hlID = HLF_ADD; /* added line */
9982 else
9983 hlID = HLF_CHD; /* changed line */
9984 }
9985 else
9986 hlID = HLF_ADD; /* added line */
9987 }
9988 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009989 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009990 prev_lnum = lnum;
9991 changedtick = curbuf->b_changedtick;
9992 fnum = curbuf->b_fnum;
9993 }
9994
9995 if (hlID == HLF_CHD || hlID == HLF_TXD)
9996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009997 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009998 if (col >= change_start && col <= change_end)
9999 hlID = HLF_TXD; /* changed text */
10000 else
10001 hlID = HLF_CHD; /* changed line */
10002 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010003 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010004#endif
10005}
10006
10007/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010008 * "empty({expr})" function
10009 */
10010 static void
10011f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 typval_T *argvars;
10013 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010014{
10015 int n;
10016
10017 switch (argvars[0].v_type)
10018 {
10019 case VAR_STRING:
10020 case VAR_FUNC:
10021 n = argvars[0].vval.v_string == NULL
10022 || *argvars[0].vval.v_string == NUL;
10023 break;
10024 case VAR_NUMBER:
10025 n = argvars[0].vval.v_number == 0;
10026 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010027#ifdef FEAT_FLOAT
10028 case VAR_FLOAT:
10029 n = argvars[0].vval.v_float == 0.0;
10030 break;
10031#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010032 case VAR_LIST:
10033 n = argvars[0].vval.v_list == NULL
10034 || argvars[0].vval.v_list->lv_first == NULL;
10035 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010036 case VAR_DICT:
10037 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010038 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010039 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010040 default:
10041 EMSG2(_(e_intern2), "f_empty()");
10042 n = 0;
10043 }
10044
10045 rettv->vval.v_number = n;
10046}
10047
10048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 * "escape({string}, {chars})" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010053 typval_T *argvars;
10054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
10056 char_u buf[NUMBUFLEN];
10057
Bram Moolenaar758711c2005-02-02 23:11:38 +000010058 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10059 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061}
10062
10063/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010064 * "eval()" function
10065 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010066 static void
10067f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010068 typval_T *argvars;
10069 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010070{
10071 char_u *s;
10072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010073 s = get_tv_string_chk(&argvars[0]);
10074 if (s != NULL)
10075 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10078 {
10079 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010080 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010082 else if (*s != NUL)
10083 EMSG(_(e_trailing));
10084}
10085
10086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 * "eventhandler()" function
10088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095}
10096
10097/*
10098 * "executable()" function
10099 */
10100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010102 typval_T *argvars;
10103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010105 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10106}
10107
10108/*
10109 * "exepath()" function
10110 */
10111 static void
10112f_exepath(argvars, rettv)
10113 typval_T *argvars;
10114 typval_T *rettv;
10115{
10116 char_u *p = NULL;
10117
10118 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10119 rettv->v_type = VAR_STRING;
10120 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121}
10122
10123/*
10124 * "exists()" function
10125 */
10126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010128 typval_T *argvars;
10129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130{
10131 char_u *p;
10132 char_u *name;
10133 int n = FALSE;
10134 int len = 0;
10135
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (*p == '$') /* environment variable */
10138 {
10139 /* first try "normal" environment variables (fast) */
10140 if (mch_getenv(p + 1) != NULL)
10141 n = TRUE;
10142 else
10143 {
10144 /* try expanding things like $VIM and ${HOME} */
10145 p = expand_env_save(p);
10146 if (p != NULL && *p != '$')
10147 n = TRUE;
10148 vim_free(p);
10149 }
10150 }
10151 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010153 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010154 if (*skipwhite(p) != NUL)
10155 n = FALSE; /* trailing garbage */
10156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 else if (*p == '*') /* internal or user defined function */
10158 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010159 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 }
10161 else if (*p == ':')
10162 {
10163 n = cmd_exists(p + 1);
10164 }
10165 else if (*p == '#')
10166 {
10167#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010168 if (p[1] == '#')
10169 n = autocmd_supported(p + 2);
10170 else
10171 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172#endif
10173 }
10174 else /* internal variable */
10175 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010176 char_u *tofree;
10177 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010179 /* get_name_len() takes care of expanding curly braces */
10180 name = p;
10181 len = get_name_len(&p, &tofree, TRUE, FALSE);
10182 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010184 if (tofree != NULL)
10185 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010186 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010187 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010189 /* handle d.key, l[idx], f(expr) */
10190 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10191 if (n)
10192 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 }
10194 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010195 if (*p != NUL)
10196 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010198 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 }
10200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010201 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202}
10203
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010204#ifdef FEAT_FLOAT
10205/*
10206 * "exp()" function
10207 */
10208 static void
10209f_exp(argvars, rettv)
10210 typval_T *argvars;
10211 typval_T *rettv;
10212{
10213 float_T f;
10214
10215 rettv->v_type = VAR_FLOAT;
10216 if (get_float_arg(argvars, &f) == OK)
10217 rettv->vval.v_float = exp(f);
10218 else
10219 rettv->vval.v_float = 0.0;
10220}
10221#endif
10222
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223/*
10224 * "expand()" function
10225 */
10226 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010227f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010228 typval_T *argvars;
10229 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230{
10231 char_u *s;
10232 int len;
10233 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010234 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010237 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010240 if (argvars[1].v_type != VAR_UNKNOWN
10241 && argvars[2].v_type != VAR_UNKNOWN
10242 && get_tv_number_chk(&argvars[2], &error)
10243 && !error)
10244 {
10245 rettv->v_type = VAR_LIST;
10246 rettv->vval.v_list = NULL;
10247 }
10248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 if (*s == '%' || *s == '#' || *s == '<')
10251 {
10252 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010253 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010255 if (rettv->v_type == VAR_LIST)
10256 {
10257 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10258 list_append_string(rettv->vval.v_list, result, -1);
10259 else
10260 vim_free(result);
10261 }
10262 else
10263 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 }
10265 else
10266 {
10267 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010268 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010269 if (argvars[1].v_type != VAR_UNKNOWN
10270 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010271 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 if (!error)
10273 {
10274 ExpandInit(&xpc);
10275 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010276 if (p_wic)
10277 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010278 if (rettv->v_type == VAR_STRING)
10279 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10280 options, WILD_ALL);
10281 else if (rettv_list_alloc(rettv) != FAIL)
10282 {
10283 int i;
10284
10285 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10286 for (i = 0; i < xpc.xp_numfiles; i++)
10287 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10288 ExpandCleanup(&xpc);
10289 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 }
10291 else
10292 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 }
10294}
10295
10296/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010297 * Go over all entries in "d2" and add them to "d1".
10298 * When "action" is "error" then a duplicate key is an error.
10299 * When "action" is "force" then a duplicate key is overwritten.
10300 * Otherwise duplicate keys are ignored ("action" is "keep").
10301 */
10302 void
10303dict_extend(d1, d2, action)
10304 dict_T *d1;
10305 dict_T *d2;
10306 char_u *action;
10307{
10308 dictitem_T *di1;
10309 hashitem_T *hi2;
10310 int todo;
10311
10312 todo = (int)d2->dv_hashtab.ht_used;
10313 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10314 {
10315 if (!HASHITEM_EMPTY(hi2))
10316 {
10317 --todo;
10318 di1 = dict_find(d1, hi2->hi_key, -1);
10319 if (d1->dv_scope != 0)
10320 {
10321 /* Disallow replacing a builtin function in l: and g:.
10322 * Check the key to be valid when adding to any
10323 * scope. */
10324 if (d1->dv_scope == VAR_DEF_SCOPE
10325 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10326 && var_check_func_name(hi2->hi_key,
10327 di1 == NULL))
10328 break;
10329 if (!valid_varname(hi2->hi_key))
10330 break;
10331 }
10332 if (di1 == NULL)
10333 {
10334 di1 = dictitem_copy(HI2DI(hi2));
10335 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10336 dictitem_free(di1);
10337 }
10338 else if (*action == 'e')
10339 {
10340 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10341 break;
10342 }
10343 else if (*action == 'f' && HI2DI(hi2) != di1)
10344 {
10345 clear_tv(&di1->di_tv);
10346 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10347 }
10348 }
10349 }
10350}
10351
10352/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010353 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010355 */
10356 static void
10357f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010358 typval_T *argvars;
10359 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010360{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010361 char *arg_errmsg = N_("extend() argument");
10362
Bram Moolenaare9a41262005-01-15 22:18:47 +000010363 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010364 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010365 list_T *l1, *l2;
10366 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010367 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010369
Bram Moolenaare9a41262005-01-15 22:18:47 +000010370 l1 = argvars[0].vval.v_list;
10371 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010372 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010373 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010374 {
10375 if (argvars[2].v_type != VAR_UNKNOWN)
10376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010377 before = get_tv_number_chk(&argvars[2], &error);
10378 if (error)
10379 return; /* type error; errmsg already given */
10380
Bram Moolenaar758711c2005-02-02 23:11:38 +000010381 if (before == l1->lv_len)
10382 item = NULL;
10383 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010384 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010385 item = list_find(l1, before);
10386 if (item == NULL)
10387 {
10388 EMSGN(_(e_listidx), before);
10389 return;
10390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010391 }
10392 }
10393 else
10394 item = NULL;
10395 list_extend(l1, l2, item);
10396
Bram Moolenaare9a41262005-01-15 22:18:47 +000010397 copy_tv(&argvars[0], rettv);
10398 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010400 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10401 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010402 dict_T *d1, *d2;
10403 char_u *action;
10404 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405
10406 d1 = argvars[0].vval.v_dict;
10407 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010408 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010409 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010410 {
10411 /* Check the third argument. */
10412 if (argvars[2].v_type != VAR_UNKNOWN)
10413 {
10414 static char *(av[]) = {"keep", "force", "error"};
10415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 action = get_tv_string_chk(&argvars[2]);
10417 if (action == NULL)
10418 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010419 for (i = 0; i < 3; ++i)
10420 if (STRCMP(action, av[i]) == 0)
10421 break;
10422 if (i == 3)
10423 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010424 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010425 return;
10426 }
10427 }
10428 else
10429 action = (char_u *)"force";
10430
Bram Moolenaara9922d62013-05-30 13:01:18 +020010431 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010432
Bram Moolenaare9a41262005-01-15 22:18:47 +000010433 copy_tv(&argvars[0], rettv);
10434 }
10435 }
10436 else
10437 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010438}
10439
10440/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010441 * "feedkeys()" function
10442 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010443 static void
10444f_feedkeys(argvars, rettv)
10445 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010446 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010447{
10448 int remap = TRUE;
10449 char_u *keys, *flags;
10450 char_u nbuf[NUMBUFLEN];
10451 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010452 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010453
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010454 /* This is not allowed in the sandbox. If the commands would still be
10455 * executed in the sandbox it would be OK, but it probably happens later,
10456 * when "sandbox" is no longer set. */
10457 if (check_secure())
10458 return;
10459
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010460 keys = get_tv_string(&argvars[0]);
10461 if (*keys != NUL)
10462 {
10463 if (argvars[1].v_type != VAR_UNKNOWN)
10464 {
10465 flags = get_tv_string_buf(&argvars[1], nbuf);
10466 for ( ; *flags != NUL; ++flags)
10467 {
10468 switch (*flags)
10469 {
10470 case 'n': remap = FALSE; break;
10471 case 'm': remap = TRUE; break;
10472 case 't': typed = TRUE; break;
10473 }
10474 }
10475 }
10476
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010477 /* Need to escape K_SPECIAL and CSI before putting the string in the
10478 * typeahead buffer. */
10479 keys_esc = vim_strsave_escape_csi(keys);
10480 if (keys_esc != NULL)
10481 {
10482 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010483 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010484 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010485 if (vgetc_busy)
10486 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010487 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010488 }
10489}
10490
10491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 * "filereadable()" function
10493 */
10494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010499 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 char_u *p;
10501 int n;
10502
Bram Moolenaarc236c162008-07-13 17:41:49 +000010503#ifndef O_NONBLOCK
10504# define O_NONBLOCK 0
10505#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010507 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10508 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 {
10510 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010511 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 }
10513 else
10514 n = FALSE;
10515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010516 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517}
10518
10519/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010520 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 * rights to write into.
10522 */
10523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010525 typval_T *argvars;
10526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010528 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010531static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010532
10533 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010534findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010535 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010537 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010538{
10539#ifdef FEAT_SEARCHPATH
10540 char_u *fname;
10541 char_u *fresult = NULL;
10542 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10543 char_u *p;
10544 char_u pathbuf[NUMBUFLEN];
10545 int count = 1;
10546 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010547 int error = FALSE;
10548#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010549
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010550 rettv->vval.v_string = NULL;
10551 rettv->v_type = VAR_STRING;
10552
10553#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010555
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10559 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010560 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 else
10562 {
10563 if (*p != NUL)
10564 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010567 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010569 }
10570
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010571 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10572 error = TRUE;
10573
10574 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 do
10577 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010578 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010579 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 fresult = find_file_in_path_option(first ? fname : NULL,
10581 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010582 0, first, path,
10583 find_what,
10584 curbuf->b_ffname,
10585 find_what == FINDFILE_DIR
10586 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010588
10589 if (fresult != NULL && rettv->v_type == VAR_LIST)
10590 list_append_string(rettv->vval.v_list, fresult, -1);
10591
10592 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010594
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010595 if (rettv->v_type == VAR_STRING)
10596 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010597#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010598}
10599
Bram Moolenaar33570922005-01-25 22:26:29 +000010600static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10601static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010602
10603/*
10604 * Implementation of map() and filter().
10605 */
10606 static void
10607filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010608 typval_T *argvars;
10609 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010610 int map;
10611{
10612 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010614 listitem_T *li, *nli;
10615 list_T *l = NULL;
10616 dictitem_T *di;
10617 hashtab_T *ht;
10618 hashitem_T *hi;
10619 dict_T *d = NULL;
10620 typval_T save_val;
10621 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010623 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010624 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10625 char *arg_errmsg = (map ? N_("map() argument")
10626 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010627 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010628 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010631 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010632 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010633 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 return;
10635 }
10636 else if (argvars[0].v_type == VAR_DICT)
10637 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010638 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010639 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010640 return;
10641 }
10642 else
10643 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010644 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010645 return;
10646 }
10647
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 expr = get_tv_string_buf_chk(&argvars[1], buf);
10649 /* On type errors, the preceding call has already displayed an error
10650 * message. Avoid a misleading error message for an empty string that
10651 * was not passed as argument. */
10652 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010654 prepare_vimvar(VV_VAL, &save_val);
10655 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010656
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010657 /* We reset "did_emsg" to be able to detect whether an error
10658 * occurred during evaluation of the expression. */
10659 save_did_emsg = did_emsg;
10660 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010661
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010662 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010663 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010665 vimvars[VV_KEY].vv_type = VAR_STRING;
10666
10667 ht = &d->dv_hashtab;
10668 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010669 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010672 if (!HASHITEM_EMPTY(hi))
10673 {
10674 --todo;
10675 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010676 if (tv_check_lock(di->di_tv.v_lock,
10677 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010678 break;
10679 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010680 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010681 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010682 break;
10683 if (!map && rem)
10684 dictitem_remove(d, di);
10685 clear_tv(&vimvars[VV_KEY].vv_tv);
10686 }
10687 }
10688 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010689 }
10690 else
10691 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010692 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 for (li = l->lv_first; li != NULL; li = nli)
10695 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010696 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010697 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010699 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010700 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010701 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010702 break;
10703 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010705 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010706 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010707 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010708
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010709 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010711
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010712 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010713 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010714
10715 copy_tv(&argvars[0], rettv);
10716}
10717
10718 static int
10719filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010721 char_u *expr;
10722 int map;
10723 int *remp;
10724{
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010726 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010727 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010728
Bram Moolenaar33570922005-01-25 22:26:29 +000010729 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010730 s = expr;
10731 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010732 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733 if (*s != NUL) /* check for trailing chars after expr */
10734 {
10735 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010736 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010737 }
10738 if (map)
10739 {
10740 /* map(): replace the list item value */
10741 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010742 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010743 *tv = rettv;
10744 }
10745 else
10746 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010747 int error = FALSE;
10748
Bram Moolenaare9a41262005-01-15 22:18:47 +000010749 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010751 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 /* On type error, nothing has been removed; return FAIL to stop the
10753 * loop. The error message was given by get_tv_number_chk(). */
10754 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010755 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010756 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010757 retval = OK;
10758theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010759 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010760 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010761}
10762
10763/*
10764 * "filter()" function
10765 */
10766 static void
10767f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *argvars;
10769 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010770{
10771 filter_map(argvars, rettv, FALSE);
10772}
10773
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010774/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010775 * "finddir({fname}[, {path}[, {count}]])" function
10776 */
10777 static void
10778f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010779 typval_T *argvars;
10780 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010781{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010782 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783}
10784
10785/*
10786 * "findfile({fname}[, {path}[, {count}]])" function
10787 */
10788 static void
10789f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *argvars;
10791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010793 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010794}
10795
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010796#ifdef FEAT_FLOAT
10797/*
10798 * "float2nr({float})" function
10799 */
10800 static void
10801f_float2nr(argvars, rettv)
10802 typval_T *argvars;
10803 typval_T *rettv;
10804{
10805 float_T f;
10806
10807 if (get_float_arg(argvars, &f) == OK)
10808 {
10809 if (f < -0x7fffffff)
10810 rettv->vval.v_number = -0x7fffffff;
10811 else if (f > 0x7fffffff)
10812 rettv->vval.v_number = 0x7fffffff;
10813 else
10814 rettv->vval.v_number = (varnumber_T)f;
10815 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010816}
10817
10818/*
10819 * "floor({float})" function
10820 */
10821 static void
10822f_floor(argvars, rettv)
10823 typval_T *argvars;
10824 typval_T *rettv;
10825{
10826 float_T f;
10827
10828 rettv->v_type = VAR_FLOAT;
10829 if (get_float_arg(argvars, &f) == OK)
10830 rettv->vval.v_float = floor(f);
10831 else
10832 rettv->vval.v_float = 0.0;
10833}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010834
10835/*
10836 * "fmod()" function
10837 */
10838 static void
10839f_fmod(argvars, rettv)
10840 typval_T *argvars;
10841 typval_T *rettv;
10842{
10843 float_T fx, fy;
10844
10845 rettv->v_type = VAR_FLOAT;
10846 if (get_float_arg(argvars, &fx) == OK
10847 && get_float_arg(&argvars[1], &fy) == OK)
10848 rettv->vval.v_float = fmod(fx, fy);
10849 else
10850 rettv->vval.v_float = 0.0;
10851}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010852#endif
10853
Bram Moolenaar0d660222005-01-07 21:51:51 +000010854/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010855 * "fnameescape({string})" function
10856 */
10857 static void
10858f_fnameescape(argvars, rettv)
10859 typval_T *argvars;
10860 typval_T *rettv;
10861{
10862 rettv->vval.v_string = vim_strsave_fnameescape(
10863 get_tv_string(&argvars[0]), FALSE);
10864 rettv->v_type = VAR_STRING;
10865}
10866
10867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 * "fnamemodify({fname}, {mods})" function
10869 */
10870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010872 typval_T *argvars;
10873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874{
10875 char_u *fname;
10876 char_u *mods;
10877 int usedlen = 0;
10878 int len;
10879 char_u *fbuf = NULL;
10880 char_u buf[NUMBUFLEN];
10881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 fname = get_tv_string_chk(&argvars[0]);
10883 mods = get_tv_string_buf_chk(&argvars[1], buf);
10884 if (fname == NULL || mods == NULL)
10885 fname = NULL;
10886 else
10887 {
10888 len = (int)STRLEN(fname);
10889 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 vim_free(fbuf);
10898}
10899
Bram Moolenaar33570922005-01-25 22:26:29 +000010900static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901
10902/*
10903 * "foldclosed()" function
10904 */
10905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010907 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010908 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010909 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910{
10911#ifdef FEAT_FOLDING
10912 linenr_T lnum;
10913 linenr_T first, last;
10914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10917 {
10918 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10919 {
10920 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 return;
10925 }
10926 }
10927#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929}
10930
10931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010932 * "foldclosed()" function
10933 */
10934 static void
10935f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 typval_T *argvars;
10937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010938{
10939 foldclosed_both(argvars, rettv, FALSE);
10940}
10941
10942/*
10943 * "foldclosedend()" function
10944 */
10945 static void
10946f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010947 typval_T *argvars;
10948 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010949{
10950 foldclosed_both(argvars, rettv, TRUE);
10951}
10952
10953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 * "foldlevel()" function
10955 */
10956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010958 typval_T *argvars UNUSED;
10959 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
10961#ifdef FEAT_FOLDING
10962 linenr_T lnum;
10963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010966 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968}
10969
10970/*
10971 * "foldtext()" function
10972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978#ifdef FEAT_FOLDING
10979 linenr_T lnum;
10980 char_u *s;
10981 char_u *r;
10982 int len;
10983 char *txt;
10984#endif
10985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 rettv->v_type = VAR_STRING;
10987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010989 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10990 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10991 <= curbuf->b_ml.ml_line_count
10992 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 {
10994 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10996 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 {
10998 if (!linewhite(lnum))
10999 break;
11000 ++lnum;
11001 }
11002
11003 /* Find interesting text in this line. */
11004 s = skipwhite(ml_get(lnum));
11005 /* skip C comment-start */
11006 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011009 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011010 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011011 {
11012 s = skipwhite(ml_get(lnum + 1));
11013 if (*s == '*')
11014 s = skipwhite(s + 1);
11015 }
11016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 txt = _("+-%s%3ld lines: ");
11018 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011019 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 + 20 /* for %3ld */
11021 + STRLEN(s))); /* concatenated */
11022 if (r != NULL)
11023 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011024 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11025 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11026 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 len = (int)STRLEN(r);
11028 STRCAT(r, s);
11029 /* remove 'foldmarker' and 'commentstring' */
11030 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011031 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033 }
11034#endif
11035}
11036
11037/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011038 * "foldtextresult(lnum)" function
11039 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011042 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011043 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011044{
11045#ifdef FEAT_FOLDING
11046 linenr_T lnum;
11047 char_u *text;
11048 char_u buf[51];
11049 foldinfo_T foldinfo;
11050 int fold_count;
11051#endif
11052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 rettv->v_type = VAR_STRING;
11054 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011055#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011056 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057 /* treat illegal types and illegal string values for {lnum} the same */
11058 if (lnum < 0)
11059 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011060 fold_count = foldedCount(curwin, lnum, &foldinfo);
11061 if (fold_count > 0)
11062 {
11063 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11064 &foldinfo, buf);
11065 if (text == buf)
11066 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011068 }
11069#endif
11070}
11071
11072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 * "foreground()" function
11074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011077 typval_T *argvars UNUSED;
11078 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080#ifdef FEAT_GUI
11081 if (gui.in_use)
11082 gui_mch_set_foreground();
11083#else
11084# ifdef WIN32
11085 win32_set_foreground();
11086# endif
11087#endif
11088}
11089
11090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011091 * "function()" function
11092 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011095 typval_T *argvars;
11096 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011097{
11098 char_u *s;
11099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011101 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011102 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011103 /* Don't check an autoload name for existence here. */
11104 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011105 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011106 else
11107 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011108 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011109 {
11110 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011111 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011112
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011113 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11114 * also be called from another script. Using trans_function_name()
11115 * would also work, but some plugins depend on the name being
11116 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011117 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011118 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011119 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011120 if (rettv->vval.v_string != NULL)
11121 {
11122 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011123 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011124 }
11125 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011126 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011127 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011129 }
11130}
11131
11132/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011133 * "garbagecollect()" function
11134 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011135 static void
11136f_garbagecollect(argvars, rettv)
11137 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011138 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011139{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011140 /* This is postponed until we are back at the toplevel, because we may be
11141 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11142 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011143
11144 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11145 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011146}
11147
11148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011149 * "get()" function
11150 */
11151 static void
11152f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011153 typval_T *argvars;
11154 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155{
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 listitem_T *li;
11157 list_T *l;
11158 dictitem_T *di;
11159 dict_T *d;
11160 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161
Bram Moolenaare9a41262005-01-15 22:18:47 +000011162 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011163 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 int error = FALSE;
11167
11168 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11169 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011172 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 else if (argvars[0].v_type == VAR_DICT)
11174 {
11175 if ((d = argvars[0].vval.v_dict) != NULL)
11176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011177 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011178 if (di != NULL)
11179 tv = &di->di_tv;
11180 }
11181 }
11182 else
11183 EMSG2(_(e_listdictarg), "get()");
11184
11185 if (tv == NULL)
11186 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011188 copy_tv(&argvars[2], rettv);
11189 }
11190 else
11191 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192}
11193
Bram Moolenaar342337a2005-07-21 21:11:17 +000011194static 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 +000011195
11196/*
11197 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011198 * Return a range (from start to end) of lines in rettv from the specified
11199 * buffer.
11200 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011201 */
11202 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011203get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011204 buf_T *buf;
11205 linenr_T start;
11206 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011207 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011208 typval_T *rettv;
11209{
11210 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011211
Bram Moolenaar959a1432013-12-14 12:17:38 +010011212 rettv->v_type = VAR_STRING;
11213 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011214 if (retlist && rettv_list_alloc(rettv) == FAIL)
11215 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011216
11217 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11218 return;
11219
11220 if (!retlist)
11221 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011222 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11223 p = ml_get_buf(buf, start, FALSE);
11224 else
11225 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011226 rettv->vval.v_string = vim_strsave(p);
11227 }
11228 else
11229 {
11230 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011231 return;
11232
11233 if (start < 1)
11234 start = 1;
11235 if (end > buf->b_ml.ml_line_count)
11236 end = buf->b_ml.ml_line_count;
11237 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011238 if (list_append_string(rettv->vval.v_list,
11239 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011240 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011241 }
11242}
11243
11244/*
11245 * "getbufline()" function
11246 */
11247 static void
11248f_getbufline(argvars, rettv)
11249 typval_T *argvars;
11250 typval_T *rettv;
11251{
11252 linenr_T lnum;
11253 linenr_T end;
11254 buf_T *buf;
11255
11256 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11257 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011258 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011259 --emsg_off;
11260
Bram Moolenaar661b1822005-07-28 22:36:45 +000011261 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011262 if (argvars[2].v_type == VAR_UNKNOWN)
11263 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011264 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011265 end = get_tv_lnum_buf(&argvars[2], buf);
11266
Bram Moolenaar342337a2005-07-21 21:11:17 +000011267 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011268}
11269
Bram Moolenaar0d660222005-01-07 21:51:51 +000011270/*
11271 * "getbufvar()" function
11272 */
11273 static void
11274f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 typval_T *argvars;
11276 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277{
11278 buf_T *buf;
11279 buf_T *save_curbuf;
11280 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011281 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011282 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011283
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11285 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011286 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011287 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011288
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011289 rettv->v_type = VAR_STRING;
11290 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291
11292 if (buf != NULL && varname != NULL)
11293 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011294 /* set curbuf to be our buf, temporarily */
11295 save_curbuf = curbuf;
11296 curbuf = buf;
11297
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011299 {
11300 if (get_option_tv(&varname, rettv, TRUE) == OK)
11301 done = TRUE;
11302 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011303 else if (STRCMP(varname, "changedtick") == 0)
11304 {
11305 rettv->v_type = VAR_NUMBER;
11306 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011307 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011308 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011309 else
11310 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011311 /* Look up the variable. */
11312 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11313 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11314 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011315 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011316 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011318 done = TRUE;
11319 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011320 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011321
11322 /* restore previous notion of curbuf */
11323 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011324 }
11325
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011326 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11327 /* use the default value */
11328 copy_tv(&argvars[2], rettv);
11329
Bram Moolenaar0d660222005-01-07 21:51:51 +000011330 --emsg_off;
11331}
11332
11333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 * "getchar()" function
11335 */
11336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011337f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011338 typval_T *argvars;
11339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
11341 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011344 /* Position the cursor. Needed after a message that ends in a space. */
11345 windgoto(msg_row, msg_col);
11346
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 ++no_mapping;
11348 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011349 for (;;)
11350 {
11351 if (argvars[0].v_type == VAR_UNKNOWN)
11352 /* getchar(): blocking wait. */
11353 n = safe_vgetc();
11354 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11355 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011356 n = vpeekc_any();
11357 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011358 /* illegal argument or getchar(0) and no char avail: return zero */
11359 n = 0;
11360 else
11361 /* getchar(0) and char avail: return char */
11362 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011363
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011364 if (n == K_IGNORE)
11365 continue;
11366 break;
11367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 --no_mapping;
11369 --allow_keys;
11370
Bram Moolenaar219b8702006-11-01 14:32:36 +000011371 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11372 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11373 vimvars[VV_MOUSE_COL].vv_nr = 0;
11374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 if (IS_SPECIAL(n) || mod_mask != 0)
11377 {
11378 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11379 int i = 0;
11380
11381 /* Turn a special key into three bytes, plus modifier. */
11382 if (mod_mask != 0)
11383 {
11384 temp[i++] = K_SPECIAL;
11385 temp[i++] = KS_MODIFIER;
11386 temp[i++] = mod_mask;
11387 }
11388 if (IS_SPECIAL(n))
11389 {
11390 temp[i++] = K_SPECIAL;
11391 temp[i++] = K_SECOND(n);
11392 temp[i++] = K_THIRD(n);
11393 }
11394#ifdef FEAT_MBYTE
11395 else if (has_mbyte)
11396 i += (*mb_char2bytes)(n, temp + i);
11397#endif
11398 else
11399 temp[i++] = n;
11400 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011403
11404#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011405 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011406 {
11407 int row = mouse_row;
11408 int col = mouse_col;
11409 win_T *win;
11410 linenr_T lnum;
11411# ifdef FEAT_WINDOWS
11412 win_T *wp;
11413# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011414 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011415
11416 if (row >= 0 && col >= 0)
11417 {
11418 /* Find the window at the mouse coordinates and compute the
11419 * text position. */
11420 win = mouse_find_win(&row, &col);
11421 (void)mouse_comp_pos(win, &row, &col, &lnum);
11422# ifdef FEAT_WINDOWS
11423 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011424 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011425# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011426 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011427 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11428 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11429 }
11430 }
11431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433}
11434
11435/*
11436 * "getcharmod()" function
11437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444}
11445
11446/*
11447 * "getcmdline()" function
11448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011450f_getcmdline(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 Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->v_type = VAR_STRING;
11455 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456}
11457
11458/*
11459 * "getcmdpos()" function
11460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011463 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467}
11468
11469/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011470 * "getcmdtype()" function
11471 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011472 static void
11473f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011474 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011475 typval_T *rettv;
11476{
11477 rettv->v_type = VAR_STRING;
11478 rettv->vval.v_string = alloc(2);
11479 if (rettv->vval.v_string != NULL)
11480 {
11481 rettv->vval.v_string[0] = get_cmdline_type();
11482 rettv->vval.v_string[1] = NUL;
11483 }
11484}
11485
11486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 * "getcwd()" function
11488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011491 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011494 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011497 rettv->vval.v_string = NULL;
11498 cwd = alloc(MAXPATHL);
11499 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011501 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11502 {
11503 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011505 if (rettv->vval.v_string != NULL)
11506 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011508 }
11509 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 }
11511}
11512
11513/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011514 * "getfontname()" function
11515 */
11516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011518 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011519 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011520{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->v_type = VAR_STRING;
11522 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011523#ifdef FEAT_GUI
11524 if (gui.in_use)
11525 {
11526 GuiFont font;
11527 char_u *name = NULL;
11528
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011529 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011530 {
11531 /* Get the "Normal" font. Either the name saved by
11532 * hl_set_font_name() or from the font ID. */
11533 font = gui.norm_font;
11534 name = hl_get_font_name();
11535 }
11536 else
11537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011538 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011539 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11540 return;
11541 font = gui_mch_get_font(name, FALSE);
11542 if (font == NOFONT)
11543 return; /* Invalid font name, return empty string. */
11544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011546 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011547 gui_mch_free_font(font);
11548 }
11549#endif
11550}
11551
11552/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011553 * "getfperm({fname})" function
11554 */
11555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011556f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011557 typval_T *argvars;
11558 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011559{
11560 char_u *fname;
11561 struct stat st;
11562 char_u *perm = NULL;
11563 char_u flags[] = "rwx";
11564 int i;
11565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011568 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011569 if (mch_stat((char *)fname, &st) >= 0)
11570 {
11571 perm = vim_strsave((char_u *)"---------");
11572 if (perm != NULL)
11573 {
11574 for (i = 0; i < 9; i++)
11575 {
11576 if (st.st_mode & (1 << (8 - i)))
11577 perm[i] = flags[i % 3];
11578 }
11579 }
11580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011582}
11583
11584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 * "getfsize({fname})" function
11586 */
11587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011589 typval_T *argvars;
11590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591{
11592 char_u *fname;
11593 struct stat st;
11594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598
11599 if (mch_stat((char *)fname, &st) >= 0)
11600 {
11601 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011605 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011606
11607 /* non-perfect check for overflow */
11608 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11609 rettv->vval.v_number = -2;
11610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 }
11612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011613 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614}
11615
11616/*
11617 * "getftime({fname})" function
11618 */
11619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011621 typval_T *argvars;
11622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623{
11624 char_u *fname;
11625 struct stat st;
11626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628
11629 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011633}
11634
11635/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011636 * "getftype({fname})" function
11637 */
11638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011640 typval_T *argvars;
11641 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011642{
11643 char_u *fname;
11644 struct stat st;
11645 char_u *type = NULL;
11646 char *t;
11647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011648 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011651 if (mch_lstat((char *)fname, &st) >= 0)
11652 {
11653#ifdef S_ISREG
11654 if (S_ISREG(st.st_mode))
11655 t = "file";
11656 else if (S_ISDIR(st.st_mode))
11657 t = "dir";
11658# ifdef S_ISLNK
11659 else if (S_ISLNK(st.st_mode))
11660 t = "link";
11661# endif
11662# ifdef S_ISBLK
11663 else if (S_ISBLK(st.st_mode))
11664 t = "bdev";
11665# endif
11666# ifdef S_ISCHR
11667 else if (S_ISCHR(st.st_mode))
11668 t = "cdev";
11669# endif
11670# ifdef S_ISFIFO
11671 else if (S_ISFIFO(st.st_mode))
11672 t = "fifo";
11673# endif
11674# ifdef S_ISSOCK
11675 else if (S_ISSOCK(st.st_mode))
11676 t = "fifo";
11677# endif
11678 else
11679 t = "other";
11680#else
11681# ifdef S_IFMT
11682 switch (st.st_mode & S_IFMT)
11683 {
11684 case S_IFREG: t = "file"; break;
11685 case S_IFDIR: t = "dir"; break;
11686# ifdef S_IFLNK
11687 case S_IFLNK: t = "link"; break;
11688# endif
11689# ifdef S_IFBLK
11690 case S_IFBLK: t = "bdev"; break;
11691# endif
11692# ifdef S_IFCHR
11693 case S_IFCHR: t = "cdev"; break;
11694# endif
11695# ifdef S_IFIFO
11696 case S_IFIFO: t = "fifo"; break;
11697# endif
11698# ifdef S_IFSOCK
11699 case S_IFSOCK: t = "socket"; break;
11700# endif
11701 default: t = "other";
11702 }
11703# else
11704 if (mch_isdir(fname))
11705 t = "dir";
11706 else
11707 t = "file";
11708# endif
11709#endif
11710 type = vim_strsave((char_u *)t);
11711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011713}
11714
11715/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011716 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717 */
11718 static void
11719f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011720 typval_T *argvars;
11721 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011722{
11723 linenr_T lnum;
11724 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011725 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011726
11727 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011728 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011729 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011730 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011731 retlist = FALSE;
11732 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011733 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011734 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011735 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011736 retlist = TRUE;
11737 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011738
Bram Moolenaar342337a2005-07-21 21:11:17 +000011739 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011740}
11741
11742/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011743 * "getmatches()" function
11744 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011745 static void
11746f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011747 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011748 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011749{
11750#ifdef FEAT_SEARCH_EXTRA
11751 dict_T *dict;
11752 matchitem_T *cur = curwin->w_match_head;
11753
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011754 if (rettv_list_alloc(rettv) == OK)
11755 {
11756 while (cur != NULL)
11757 {
11758 dict = dict_alloc();
11759 if (dict == NULL)
11760 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011761 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11762 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11763 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11764 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11765 list_append_dict(rettv->vval.v_list, dict);
11766 cur = cur->next;
11767 }
11768 }
11769#endif
11770}
11771
11772/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011773 * "getpid()" function
11774 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011775 static void
11776f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011777 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011778 typval_T *rettv;
11779{
11780 rettv->vval.v_number = mch_get_pid();
11781}
11782
11783/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011784 * "getpos(string)" function
11785 */
11786 static void
11787f_getpos(argvars, rettv)
11788 typval_T *argvars;
11789 typval_T *rettv;
11790{
11791 pos_T *fp;
11792 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011793 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011794
11795 if (rettv_list_alloc(rettv) == OK)
11796 {
11797 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011798 fp = var2fpos(&argvars[0], TRUE, &fnum);
11799 if (fnum != -1)
11800 list_append_number(l, (varnumber_T)fnum);
11801 else
11802 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011803 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11804 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011805 list_append_number(l, (fp != NULL)
11806 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011807 : (varnumber_T)0);
11808 list_append_number(l,
11809#ifdef FEAT_VIRTUALEDIT
11810 (fp != NULL) ? (varnumber_T)fp->coladd :
11811#endif
11812 (varnumber_T)0);
Bram Moolenaar493c1782014-05-28 14:34:46 +020011813 if (fp == &curwin->w_cursor)
11814 list_append_number(l, (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011815 }
11816 else
11817 rettv->vval.v_number = FALSE;
11818}
11819
11820/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011821 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011822 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011823 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011824f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011825 typval_T *argvars UNUSED;
11826 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011827{
11828#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011829 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011830#endif
11831
Bram Moolenaar2641f772005-03-25 21:58:17 +000011832#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011833 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011834 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011835 wp = NULL;
11836 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11837 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011838 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011839 if (wp == NULL)
11840 return;
11841 }
11842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011843 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011844 }
11845#endif
11846}
11847
11848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 * "getreg()" function
11850 */
11851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011853 typval_T *argvars;
11854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855{
11856 char_u *strregname;
11857 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011858 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011859 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011861
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011862 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011863 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 strregname = get_tv_string_chk(&argvars[0]);
11865 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011866 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011869 if (!error && argvars[2].v_type != VAR_UNKNOWN)
11870 return_list = get_tv_number_chk(&argvars[2], &error);
11871 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011875
11876 if (error)
11877 return;
11878
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 regname = (strregname == NULL ? '"' : *strregname);
11880 if (regname == 0)
11881 regname = '"';
11882
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011883 if (return_list)
11884 {
11885 rettv->v_type = VAR_LIST;
11886 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
11887 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
11888 }
11889 else
11890 {
11891 rettv->v_type = VAR_STRING;
11892 rettv->vval.v_string = get_reg_contents(regname,
11893 arg2 ? GREG_EXPR_SRC : 0);
11894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895}
11896
11897/*
11898 * "getregtype()" function
11899 */
11900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *argvars;
11903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904{
11905 char_u *strregname;
11906 int regname;
11907 char_u buf[NUMBUFLEN + 2];
11908 long reglen = 0;
11909
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011910 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011911 {
11912 strregname = get_tv_string_chk(&argvars[0]);
11913 if (strregname == NULL) /* type error; errmsg already given */
11914 {
11915 rettv->v_type = VAR_STRING;
11916 rettv->vval.v_string = NULL;
11917 return;
11918 }
11919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920 else
11921 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011922 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
11924 regname = (strregname == NULL ? '"' : *strregname);
11925 if (regname == 0)
11926 regname = '"';
11927
11928 buf[0] = NUL;
11929 buf[1] = NUL;
11930 switch (get_reg_type(regname, &reglen))
11931 {
11932 case MLINE: buf[0] = 'V'; break;
11933 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 case MBLOCK:
11935 buf[0] = Ctrl_V;
11936 sprintf((char *)buf + 1, "%ld", reglen + 1);
11937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 rettv->v_type = VAR_STRING;
11940 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941}
11942
11943/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011944 * "gettabvar()" function
11945 */
11946 static void
11947f_gettabvar(argvars, rettv)
11948 typval_T *argvars;
11949 typval_T *rettv;
11950{
11951 tabpage_T *tp;
11952 dictitem_T *v;
11953 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011954 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011955
11956 rettv->v_type = VAR_STRING;
11957 rettv->vval.v_string = NULL;
11958
11959 varname = get_tv_string_chk(&argvars[1]);
11960 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11961 if (tp != NULL && varname != NULL)
11962 {
11963 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011964 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011965 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011966 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011967 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011968 done = TRUE;
11969 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011970 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011971
11972 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011973 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011974}
11975
11976/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011977 * "gettabwinvar()" function
11978 */
11979 static void
11980f_gettabwinvar(argvars, rettv)
11981 typval_T *argvars;
11982 typval_T *rettv;
11983{
11984 getwinvar(argvars, rettv, 1);
11985}
11986
11987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 * "getwinposx()" function
11989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011992 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996#ifdef FEAT_GUI
11997 if (gui.in_use)
11998 {
11999 int x, y;
12000
12001 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 }
12004#endif
12005}
12006
12007/*
12008 * "getwinposy()" function
12009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012012 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016#ifdef FEAT_GUI
12017 if (gui.in_use)
12018 {
12019 int x, y;
12020
12021 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 }
12024#endif
12025}
12026
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012027/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012028 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012029 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012030 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012031find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012032 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012033 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012034{
12035#ifdef FEAT_WINDOWS
12036 win_T *wp;
12037#endif
12038 int nr;
12039
12040 nr = get_tv_number_chk(vp, NULL);
12041
12042#ifdef FEAT_WINDOWS
12043 if (nr < 0)
12044 return NULL;
12045 if (nr == 0)
12046 return curwin;
12047
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012048 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12049 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012050 if (--nr <= 0)
12051 break;
12052 return wp;
12053#else
12054 if (nr == 0 || nr == 1)
12055 return curwin;
12056 return NULL;
12057#endif
12058}
12059
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060/*
12061 * "getwinvar()" function
12062 */
12063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012065 typval_T *argvars;
12066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012068 getwinvar(argvars, rettv, 0);
12069}
12070
12071/*
12072 * getwinvar() and gettabwinvar()
12073 */
12074 static void
12075getwinvar(argvars, rettv, off)
12076 typval_T *argvars;
12077 typval_T *rettv;
12078 int off; /* 1 for gettabwinvar() */
12079{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080 win_T *win, *oldcurwin;
12081 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012082 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012083 tabpage_T *tp = NULL;
12084 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012085 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012087#ifdef FEAT_WINDOWS
12088 if (off == 1)
12089 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12090 else
12091 tp = curtab;
12092#endif
12093 win = find_win_by_nr(&argvars[off], tp);
12094 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012097 rettv->v_type = VAR_STRING;
12098 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099
12100 if (win != NULL && varname != NULL)
12101 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012102 /* Set curwin to be our win, temporarily. Also set the tabpage,
12103 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012104 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012105
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012107 {
12108 if (get_option_tv(&varname, rettv, 1) == OK)
12109 done = TRUE;
12110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 else
12112 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012113 /* Look up the variable. */
12114 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12115 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012117 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012118 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012119 done = TRUE;
12120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012122
12123 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012124 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 }
12126
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012127 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12128 /* use the default return value */
12129 copy_tv(&argvars[off + 2], rettv);
12130
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 --emsg_off;
12132}
12133
12134/*
12135 * "glob()" function
12136 */
12137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012139 typval_T *argvars;
12140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012142 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012144 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012146 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012147 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012149 if (argvars[1].v_type != VAR_UNKNOWN)
12150 {
12151 if (get_tv_number_chk(&argvars[1], &error))
12152 options |= WILD_KEEP_ALL;
12153 if (argvars[2].v_type != VAR_UNKNOWN
12154 && get_tv_number_chk(&argvars[2], &error))
12155 {
12156 rettv->v_type = VAR_LIST;
12157 rettv->vval.v_list = NULL;
12158 }
12159 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012160 if (!error)
12161 {
12162 ExpandInit(&xpc);
12163 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012164 if (p_wic)
12165 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012166 if (rettv->v_type == VAR_STRING)
12167 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012168 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012169 else if (rettv_list_alloc(rettv) != FAIL)
12170 {
12171 int i;
12172
12173 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12174 NULL, options, WILD_ALL_KEEP);
12175 for (i = 0; i < xpc.xp_numfiles; i++)
12176 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12177
12178 ExpandCleanup(&xpc);
12179 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012180 }
12181 else
12182 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183}
12184
12185/*
12186 * "globpath()" function
12187 */
12188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 typval_T *argvars;
12191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012193 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012195 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012196 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012197 garray_T ga;
12198 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012200 /* When the optional second argument is non-zero, don't remove matches
12201 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012203 if (argvars[2].v_type != VAR_UNKNOWN)
12204 {
12205 if (get_tv_number_chk(&argvars[2], &error))
12206 flags |= WILD_KEEP_ALL;
12207 if (argvars[3].v_type != VAR_UNKNOWN
12208 && get_tv_number_chk(&argvars[3], &error))
12209 {
12210 rettv->v_type = VAR_LIST;
12211 rettv->vval.v_list = NULL;
12212 }
12213 }
12214 if (file != NULL && !error)
12215 {
12216 ga_init2(&ga, (int)sizeof(char_u *), 10);
12217 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12218 if (rettv->v_type == VAR_STRING)
12219 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12220 else if (rettv_list_alloc(rettv) != FAIL)
12221 for (i = 0; i < ga.ga_len; ++i)
12222 list_append_string(rettv->vval.v_list,
12223 ((char_u **)(ga.ga_data))[i], -1);
12224 ga_clear_strings(&ga);
12225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012226 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012227 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228}
12229
12230/*
12231 * "has()" function
12232 */
12233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012235 typval_T *argvars;
12236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237{
12238 int i;
12239 char_u *name;
12240 int n = FALSE;
12241 static char *(has_list[]) =
12242 {
12243#ifdef AMIGA
12244 "amiga",
12245# ifdef FEAT_ARP
12246 "arp",
12247# endif
12248#endif
12249#ifdef __BEOS__
12250 "beos",
12251#endif
12252#ifdef MSDOS
12253# ifdef DJGPP
12254 "dos32",
12255# else
12256 "dos16",
12257# endif
12258#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012259#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 "mac",
12261#endif
12262#if defined(MACOS_X_UNIX)
12263 "macunix",
12264#endif
12265#ifdef OS2
12266 "os2",
12267#endif
12268#ifdef __QNX__
12269 "qnx",
12270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271#ifdef UNIX
12272 "unix",
12273#endif
12274#ifdef VMS
12275 "vms",
12276#endif
12277#ifdef WIN16
12278 "win16",
12279#endif
12280#ifdef WIN32
12281 "win32",
12282#endif
12283#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12284 "win32unix",
12285#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012286#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 "win64",
12288#endif
12289#ifdef EBCDIC
12290 "ebcdic",
12291#endif
12292#ifndef CASE_INSENSITIVE_FILENAME
12293 "fname_case",
12294#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012295#ifdef HAVE_ACL
12296 "acl",
12297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#ifdef FEAT_ARABIC
12299 "arabic",
12300#endif
12301#ifdef FEAT_AUTOCMD
12302 "autocmd",
12303#endif
12304#ifdef FEAT_BEVAL
12305 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012306# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12307 "balloon_multiline",
12308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309#endif
12310#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12311 "builtin_terms",
12312# ifdef ALL_BUILTIN_TCAPS
12313 "all_builtin_terms",
12314# endif
12315#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012316#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12317 || defined(FEAT_GUI_W32) \
12318 || defined(FEAT_GUI_MOTIF))
12319 "browsefilter",
12320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321#ifdef FEAT_BYTEOFF
12322 "byte_offset",
12323#endif
12324#ifdef FEAT_CINDENT
12325 "cindent",
12326#endif
12327#ifdef FEAT_CLIENTSERVER
12328 "clientserver",
12329#endif
12330#ifdef FEAT_CLIPBOARD
12331 "clipboard",
12332#endif
12333#ifdef FEAT_CMDL_COMPL
12334 "cmdline_compl",
12335#endif
12336#ifdef FEAT_CMDHIST
12337 "cmdline_hist",
12338#endif
12339#ifdef FEAT_COMMENTS
12340 "comments",
12341#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012342#ifdef FEAT_CONCEAL
12343 "conceal",
12344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345#ifdef FEAT_CRYPT
12346 "cryptv",
12347#endif
12348#ifdef FEAT_CSCOPE
12349 "cscope",
12350#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012351#ifdef FEAT_CURSORBIND
12352 "cursorbind",
12353#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012354#ifdef CURSOR_SHAPE
12355 "cursorshape",
12356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357#ifdef DEBUG
12358 "debug",
12359#endif
12360#ifdef FEAT_CON_DIALOG
12361 "dialog_con",
12362#endif
12363#ifdef FEAT_GUI_DIALOG
12364 "dialog_gui",
12365#endif
12366#ifdef FEAT_DIFF
12367 "diff",
12368#endif
12369#ifdef FEAT_DIGRAPHS
12370 "digraphs",
12371#endif
12372#ifdef FEAT_DND
12373 "dnd",
12374#endif
12375#ifdef FEAT_EMACS_TAGS
12376 "emacs_tags",
12377#endif
12378 "eval", /* always present, of course! */
12379#ifdef FEAT_EX_EXTRA
12380 "ex_extra",
12381#endif
12382#ifdef FEAT_SEARCH_EXTRA
12383 "extra_search",
12384#endif
12385#ifdef FEAT_FKMAP
12386 "farsi",
12387#endif
12388#ifdef FEAT_SEARCHPATH
12389 "file_in_path",
12390#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012391#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012392 "filterpipe",
12393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394#ifdef FEAT_FIND_ID
12395 "find_in_path",
12396#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012397#ifdef FEAT_FLOAT
12398 "float",
12399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#ifdef FEAT_FOLDING
12401 "folding",
12402#endif
12403#ifdef FEAT_FOOTER
12404 "footer",
12405#endif
12406#if !defined(USE_SYSTEM) && defined(UNIX)
12407 "fork",
12408#endif
12409#ifdef FEAT_GETTEXT
12410 "gettext",
12411#endif
12412#ifdef FEAT_GUI
12413 "gui",
12414#endif
12415#ifdef FEAT_GUI_ATHENA
12416# ifdef FEAT_GUI_NEXTAW
12417 "gui_neXtaw",
12418# else
12419 "gui_athena",
12420# endif
12421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422#ifdef FEAT_GUI_GTK
12423 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012426#ifdef FEAT_GUI_GNOME
12427 "gui_gnome",
12428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429#ifdef FEAT_GUI_MAC
12430 "gui_mac",
12431#endif
12432#ifdef FEAT_GUI_MOTIF
12433 "gui_motif",
12434#endif
12435#ifdef FEAT_GUI_PHOTON
12436 "gui_photon",
12437#endif
12438#ifdef FEAT_GUI_W16
12439 "gui_win16",
12440#endif
12441#ifdef FEAT_GUI_W32
12442 "gui_win32",
12443#endif
12444#ifdef FEAT_HANGULIN
12445 "hangul_input",
12446#endif
12447#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12448 "iconv",
12449#endif
12450#ifdef FEAT_INS_EXPAND
12451 "insert_expand",
12452#endif
12453#ifdef FEAT_JUMPLIST
12454 "jumplist",
12455#endif
12456#ifdef FEAT_KEYMAP
12457 "keymap",
12458#endif
12459#ifdef FEAT_LANGMAP
12460 "langmap",
12461#endif
12462#ifdef FEAT_LIBCALL
12463 "libcall",
12464#endif
12465#ifdef FEAT_LINEBREAK
12466 "linebreak",
12467#endif
12468#ifdef FEAT_LISP
12469 "lispindent",
12470#endif
12471#ifdef FEAT_LISTCMDS
12472 "listcmds",
12473#endif
12474#ifdef FEAT_LOCALMAP
12475 "localmap",
12476#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012477#ifdef FEAT_LUA
12478# ifndef DYNAMIC_LUA
12479 "lua",
12480# endif
12481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482#ifdef FEAT_MENU
12483 "menu",
12484#endif
12485#ifdef FEAT_SESSION
12486 "mksession",
12487#endif
12488#ifdef FEAT_MODIFY_FNAME
12489 "modify_fname",
12490#endif
12491#ifdef FEAT_MOUSE
12492 "mouse",
12493#endif
12494#ifdef FEAT_MOUSESHAPE
12495 "mouseshape",
12496#endif
12497#if defined(UNIX) || defined(VMS)
12498# ifdef FEAT_MOUSE_DEC
12499 "mouse_dec",
12500# endif
12501# ifdef FEAT_MOUSE_GPM
12502 "mouse_gpm",
12503# endif
12504# ifdef FEAT_MOUSE_JSB
12505 "mouse_jsbterm",
12506# endif
12507# ifdef FEAT_MOUSE_NET
12508 "mouse_netterm",
12509# endif
12510# ifdef FEAT_MOUSE_PTERM
12511 "mouse_pterm",
12512# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012513# ifdef FEAT_MOUSE_SGR
12514 "mouse_sgr",
12515# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012516# ifdef FEAT_SYSMOUSE
12517 "mouse_sysmouse",
12518# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012519# ifdef FEAT_MOUSE_URXVT
12520 "mouse_urxvt",
12521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522# ifdef FEAT_MOUSE_XTERM
12523 "mouse_xterm",
12524# endif
12525#endif
12526#ifdef FEAT_MBYTE
12527 "multi_byte",
12528#endif
12529#ifdef FEAT_MBYTE_IME
12530 "multi_byte_ime",
12531#endif
12532#ifdef FEAT_MULTI_LANG
12533 "multi_lang",
12534#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012535#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012536#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012537 "mzscheme",
12538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540#ifdef FEAT_OLE
12541 "ole",
12542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543#ifdef FEAT_PATH_EXTRA
12544 "path_extra",
12545#endif
12546#ifdef FEAT_PERL
12547#ifndef DYNAMIC_PERL
12548 "perl",
12549#endif
12550#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012551#ifdef FEAT_PERSISTENT_UNDO
12552 "persistent_undo",
12553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554#ifdef FEAT_PYTHON
12555#ifndef DYNAMIC_PYTHON
12556 "python",
12557#endif
12558#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012559#ifdef FEAT_PYTHON3
12560#ifndef DYNAMIC_PYTHON3
12561 "python3",
12562#endif
12563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564#ifdef FEAT_POSTSCRIPT
12565 "postscript",
12566#endif
12567#ifdef FEAT_PRINTER
12568 "printer",
12569#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012570#ifdef FEAT_PROFILE
12571 "profile",
12572#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012573#ifdef FEAT_RELTIME
12574 "reltime",
12575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576#ifdef FEAT_QUICKFIX
12577 "quickfix",
12578#endif
12579#ifdef FEAT_RIGHTLEFT
12580 "rightleft",
12581#endif
12582#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12583 "ruby",
12584#endif
12585#ifdef FEAT_SCROLLBIND
12586 "scrollbind",
12587#endif
12588#ifdef FEAT_CMDL_INFO
12589 "showcmd",
12590 "cmdline_info",
12591#endif
12592#ifdef FEAT_SIGNS
12593 "signs",
12594#endif
12595#ifdef FEAT_SMARTINDENT
12596 "smartindent",
12597#endif
12598#ifdef FEAT_SNIFF
12599 "sniff",
12600#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012601#ifdef STARTUPTIME
12602 "startuptime",
12603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604#ifdef FEAT_STL_OPT
12605 "statusline",
12606#endif
12607#ifdef FEAT_SUN_WORKSHOP
12608 "sun_workshop",
12609#endif
12610#ifdef FEAT_NETBEANS_INTG
12611 "netbeans_intg",
12612#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012613#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012614 "spell",
12615#endif
12616#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617 "syntax",
12618#endif
12619#if defined(USE_SYSTEM) || !defined(UNIX)
12620 "system",
12621#endif
12622#ifdef FEAT_TAG_BINS
12623 "tag_binary",
12624#endif
12625#ifdef FEAT_TAG_OLDSTATIC
12626 "tag_old_static",
12627#endif
12628#ifdef FEAT_TAG_ANYWHITE
12629 "tag_any_white",
12630#endif
12631#ifdef FEAT_TCL
12632# ifndef DYNAMIC_TCL
12633 "tcl",
12634# endif
12635#endif
12636#ifdef TERMINFO
12637 "terminfo",
12638#endif
12639#ifdef FEAT_TERMRESPONSE
12640 "termresponse",
12641#endif
12642#ifdef FEAT_TEXTOBJ
12643 "textobjects",
12644#endif
12645#ifdef HAVE_TGETENT
12646 "tgetent",
12647#endif
12648#ifdef FEAT_TITLE
12649 "title",
12650#endif
12651#ifdef FEAT_TOOLBAR
12652 "toolbar",
12653#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012654#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12655 "unnamedplus",
12656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657#ifdef FEAT_USR_CMDS
12658 "user-commands", /* was accidentally included in 5.4 */
12659 "user_commands",
12660#endif
12661#ifdef FEAT_VIMINFO
12662 "viminfo",
12663#endif
12664#ifdef FEAT_VERTSPLIT
12665 "vertsplit",
12666#endif
12667#ifdef FEAT_VIRTUALEDIT
12668 "virtualedit",
12669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671#ifdef FEAT_VISUALEXTRA
12672 "visualextra",
12673#endif
12674#ifdef FEAT_VREPLACE
12675 "vreplace",
12676#endif
12677#ifdef FEAT_WILDIGN
12678 "wildignore",
12679#endif
12680#ifdef FEAT_WILDMENU
12681 "wildmenu",
12682#endif
12683#ifdef FEAT_WINDOWS
12684 "windows",
12685#endif
12686#ifdef FEAT_WAK
12687 "winaltkeys",
12688#endif
12689#ifdef FEAT_WRITEBACKUP
12690 "writebackup",
12691#endif
12692#ifdef FEAT_XIM
12693 "xim",
12694#endif
12695#ifdef FEAT_XFONTSET
12696 "xfontset",
12697#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012698#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012699 "xpm",
12700 "xpm_w32", /* for backward compatibility */
12701#else
12702# if defined(HAVE_XPM)
12703 "xpm",
12704# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706#ifdef USE_XSMP
12707 "xsmp",
12708#endif
12709#ifdef USE_XSMP_INTERACT
12710 "xsmp_interact",
12711#endif
12712#ifdef FEAT_XCLIPBOARD
12713 "xterm_clipboard",
12714#endif
12715#ifdef FEAT_XTERM_SAVE
12716 "xterm_save",
12717#endif
12718#if defined(UNIX) && defined(FEAT_X11)
12719 "X11",
12720#endif
12721 NULL
12722 };
12723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 for (i = 0; has_list[i] != NULL; ++i)
12726 if (STRICMP(name, has_list[i]) == 0)
12727 {
12728 n = TRUE;
12729 break;
12730 }
12731
12732 if (n == FALSE)
12733 {
12734 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012735 {
12736 if (name[5] == '-'
12737 && STRLEN(name) > 11
12738 && vim_isdigit(name[6])
12739 && vim_isdigit(name[8])
12740 && vim_isdigit(name[10]))
12741 {
12742 int major = atoi((char *)name + 6);
12743 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012744
12745 /* Expect "patch-9.9.01234". */
12746 n = (major < VIM_VERSION_MAJOR
12747 || (major == VIM_VERSION_MAJOR
12748 && (minor < VIM_VERSION_MINOR
12749 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012750 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012751 }
12752 else
12753 n = has_patch(atoi((char *)name + 5));
12754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 else if (STRICMP(name, "vim_starting") == 0)
12756 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012757#ifdef FEAT_MBYTE
12758 else if (STRICMP(name, "multi_byte_encoding") == 0)
12759 n = has_mbyte;
12760#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012761#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12762 else if (STRICMP(name, "balloon_multiline") == 0)
12763 n = multiline_balloon_available();
12764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765#ifdef DYNAMIC_TCL
12766 else if (STRICMP(name, "tcl") == 0)
12767 n = tcl_enabled(FALSE);
12768#endif
12769#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12770 else if (STRICMP(name, "iconv") == 0)
12771 n = iconv_enabled(FALSE);
12772#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012773#ifdef DYNAMIC_LUA
12774 else if (STRICMP(name, "lua") == 0)
12775 n = lua_enabled(FALSE);
12776#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012777#ifdef DYNAMIC_MZSCHEME
12778 else if (STRICMP(name, "mzscheme") == 0)
12779 n = mzscheme_enabled(FALSE);
12780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781#ifdef DYNAMIC_RUBY
12782 else if (STRICMP(name, "ruby") == 0)
12783 n = ruby_enabled(FALSE);
12784#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012785#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786#ifdef DYNAMIC_PYTHON
12787 else if (STRICMP(name, "python") == 0)
12788 n = python_enabled(FALSE);
12789#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012790#endif
12791#ifdef FEAT_PYTHON3
12792#ifdef DYNAMIC_PYTHON3
12793 else if (STRICMP(name, "python3") == 0)
12794 n = python3_enabled(FALSE);
12795#endif
12796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797#ifdef DYNAMIC_PERL
12798 else if (STRICMP(name, "perl") == 0)
12799 n = perl_enabled(FALSE);
12800#endif
12801#ifdef FEAT_GUI
12802 else if (STRICMP(name, "gui_running") == 0)
12803 n = (gui.in_use || gui.starting);
12804# ifdef FEAT_GUI_W32
12805 else if (STRICMP(name, "gui_win32s") == 0)
12806 n = gui_is_win32s();
12807# endif
12808# ifdef FEAT_BROWSE
12809 else if (STRICMP(name, "browse") == 0)
12810 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12811# endif
12812#endif
12813#ifdef FEAT_SYN_HL
12814 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012815 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816#endif
12817#if defined(WIN3264)
12818 else if (STRICMP(name, "win95") == 0)
12819 n = mch_windows95();
12820#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012821#ifdef FEAT_NETBEANS_INTG
12822 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012823 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 }
12826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012831 * "has_key()" function
12832 */
12833 static void
12834f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 typval_T *argvars;
12836 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012837{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012838 if (argvars[0].v_type != VAR_DICT)
12839 {
12840 EMSG(_(e_dictreq));
12841 return;
12842 }
12843 if (argvars[0].vval.v_dict == NULL)
12844 return;
12845
12846 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012847 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012848}
12849
12850/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012851 * "haslocaldir()" function
12852 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012853 static void
12854f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012855 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012856 typval_T *rettv;
12857{
12858 rettv->vval.v_number = (curwin->w_localdir != NULL);
12859}
12860
12861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 * "hasmapto()" function
12863 */
12864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012866 typval_T *argvars;
12867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868{
12869 char_u *name;
12870 char_u *mode;
12871 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012872 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012875 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 mode = (char_u *)"nvo";
12877 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012880 if (argvars[2].v_type != VAR_UNKNOWN)
12881 abbr = get_tv_number(&argvars[2]);
12882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883
Bram Moolenaar2c932302006-03-18 21:42:09 +000012884 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888}
12889
12890/*
12891 * "histadd()" function
12892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897{
12898#ifdef FEAT_CMDHIST
12899 int histype;
12900 char_u *str;
12901 char_u buf[NUMBUFLEN];
12902#endif
12903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 if (check_restricted() || check_secure())
12906 return;
12907#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012908 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12909 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910 if (histype >= 0)
12911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 if (*str != NUL)
12914 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012915 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918 return;
12919 }
12920 }
12921#endif
12922}
12923
12924/*
12925 * "histdel()" function
12926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012929 typval_T *argvars UNUSED;
12930 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931{
12932#ifdef FEAT_CMDHIST
12933 int n;
12934 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012935 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012937 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12938 if (str == NULL)
12939 n = 0;
12940 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012943 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012945 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 else
12948 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012949 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950 get_tv_string_buf(&argvars[1], buf));
12951 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952#endif
12953}
12954
12955/*
12956 * "histget()" function
12957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012960 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962{
12963#ifdef FEAT_CMDHIST
12964 int type;
12965 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012966 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012968 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12969 if (str == NULL)
12970 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972 {
12973 type = get_histtype(str);
12974 if (argvars[1].v_type == VAR_UNKNOWN)
12975 idx = get_history_idx(type);
12976 else
12977 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12978 /* -1 on type error */
12979 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
12987/*
12988 * "histnr()" function
12989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012992 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994{
12995 int i;
12996
12997#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012998 char_u *history = get_tv_string_chk(&argvars[0]);
12999
13000 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001 if (i >= HIST_CMD && i < HIST_COUNT)
13002 i = get_history_idx(i);
13003 else
13004#endif
13005 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013006 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007}
13008
13009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 * "highlightID(name)" function
13011 */
13012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013014 typval_T *argvars;
13015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013017 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018}
13019
13020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013021 * "highlight_exists()" function
13022 */
13023 static void
13024f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013025 typval_T *argvars;
13026 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013027{
13028 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13029}
13030
13031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032 * "hostname()" function
13033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013036 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038{
13039 char_u hostname[256];
13040
13041 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 rettv->v_type = VAR_STRING;
13043 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044}
13045
13046/*
13047 * iconv() function
13048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013051 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
13054#ifdef FEAT_MBYTE
13055 char_u buf1[NUMBUFLEN];
13056 char_u buf2[NUMBUFLEN];
13057 char_u *from, *to, *str;
13058 vimconv_T vimconv;
13059#endif
13060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->v_type = VAR_STRING;
13062 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063
13064#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 str = get_tv_string(&argvars[0]);
13066 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13067 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 vimconv.vc_type = CONV_NONE;
13069 convert_setup(&vimconv, from, to);
13070
13071 /* If the encodings are equal, no conversion needed. */
13072 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076
13077 convert_setup(&vimconv, NULL, NULL);
13078 vim_free(from);
13079 vim_free(to);
13080#endif
13081}
13082
13083/*
13084 * "indent()" function
13085 */
13086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013087f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013088 typval_T *argvars;
13089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090{
13091 linenr_T lnum;
13092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013093 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098}
13099
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013100/*
13101 * "index()" function
13102 */
13103 static void
13104f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013105 typval_T *argvars;
13106 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013107{
Bram Moolenaar33570922005-01-25 22:26:29 +000013108 list_T *l;
13109 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013110 long idx = 0;
13111 int ic = FALSE;
13112
13113 rettv->vval.v_number = -1;
13114 if (argvars[0].v_type != VAR_LIST)
13115 {
13116 EMSG(_(e_listreq));
13117 return;
13118 }
13119 l = argvars[0].vval.v_list;
13120 if (l != NULL)
13121 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013122 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013123 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013125 int error = FALSE;
13126
Bram Moolenaar758711c2005-02-02 23:11:38 +000013127 /* Start at specified item. Use the cached index that list_find()
13128 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013129 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013130 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013131 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013132 ic = get_tv_number_chk(&argvars[3], &error);
13133 if (error)
13134 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013135 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013136
Bram Moolenaar758711c2005-02-02 23:11:38 +000013137 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013138 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013139 {
13140 rettv->vval.v_number = idx;
13141 break;
13142 }
13143 }
13144}
13145
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146static int inputsecret_flag = 0;
13147
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013148static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13149
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013151 * This function is used by f_input() and f_inputdialog() functions. The third
13152 * argument to f_input() specifies the type of completion to use at the
13153 * prompt. The third argument to f_inputdialog() specifies the value to return
13154 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 */
13156 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013157get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013158 typval_T *argvars;
13159 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013160 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013162 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 char_u *p = NULL;
13164 int c;
13165 char_u buf[NUMBUFLEN];
13166 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013167 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013168 int xp_type = EXPAND_NOTHING;
13169 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013172 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173
13174#ifdef NO_CONSOLE_INPUT
13175 /* While starting up, there is no place to enter text. */
13176 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178#endif
13179
13180 cmd_silent = FALSE; /* Want to see the prompt. */
13181 if (prompt != NULL)
13182 {
13183 /* Only the part of the message after the last NL is considered as
13184 * prompt for the command line */
13185 p = vim_strrchr(prompt, '\n');
13186 if (p == NULL)
13187 p = prompt;
13188 else
13189 {
13190 ++p;
13191 c = *p;
13192 *p = NUL;
13193 msg_start();
13194 msg_clr_eos();
13195 msg_puts_attr(prompt, echo_attr);
13196 msg_didout = FALSE;
13197 msg_starthere();
13198 *p = c;
13199 }
13200 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013202 if (argvars[1].v_type != VAR_UNKNOWN)
13203 {
13204 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13205 if (defstr != NULL)
13206 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013208 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013209 {
13210 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013211 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013212 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013213
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013214 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013215 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013216
Bram Moolenaar4463f292005-09-25 22:20:24 +000013217 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13218 if (xp_name == NULL)
13219 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013221 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013222
Bram Moolenaar4463f292005-09-25 22:20:24 +000013223 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13224 &xp_arg) == FAIL)
13225 return;
13226 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013227 }
13228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013229 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013230 {
13231# ifdef FEAT_EX_EXTRA
13232 int save_ex_normal_busy = ex_normal_busy;
13233 ex_normal_busy = 0;
13234# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013236 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13237 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013238# ifdef FEAT_EX_EXTRA
13239 ex_normal_busy = save_ex_normal_busy;
13240# endif
13241 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013242 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013243 && argvars[1].v_type != VAR_UNKNOWN
13244 && argvars[2].v_type != VAR_UNKNOWN)
13245 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13246 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013247
13248 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013250 /* since the user typed this, no need to wait for return */
13251 need_wait_return = FALSE;
13252 msg_didout = FALSE;
13253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 cmd_silent = cmd_silent_save;
13255}
13256
13257/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013258 * "input()" function
13259 * Also handles inputsecret() when inputsecret is set.
13260 */
13261 static void
13262f_input(argvars, rettv)
13263 typval_T *argvars;
13264 typval_T *rettv;
13265{
13266 get_user_input(argvars, rettv, FALSE);
13267}
13268
13269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 * "inputdialog()" function
13271 */
13272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 typval_T *argvars;
13275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276{
13277#if defined(FEAT_GUI_TEXTDIALOG)
13278 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13279 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13280 {
13281 char_u *message;
13282 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 message = get_tv_string_chk(&argvars[0]);
13286 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013287 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013288 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 else
13290 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 if (message != NULL && defstr != NULL
13292 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013293 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013294 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 else
13296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 if (message != NULL && defstr != NULL
13298 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013299 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 rettv->vval.v_string = vim_strsave(
13301 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013303 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 }
13307 else
13308#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013309 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310}
13311
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013312/*
13313 * "inputlist()" function
13314 */
13315 static void
13316f_inputlist(argvars, rettv)
13317 typval_T *argvars;
13318 typval_T *rettv;
13319{
13320 listitem_T *li;
13321 int selected;
13322 int mouse_used;
13323
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013324#ifdef NO_CONSOLE_INPUT
13325 /* While starting up, there is no place to enter text. */
13326 if (no_console_input())
13327 return;
13328#endif
13329 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13330 {
13331 EMSG2(_(e_listarg), "inputlist()");
13332 return;
13333 }
13334
13335 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013336 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013337 lines_left = Rows; /* avoid more prompt */
13338 msg_scroll = TRUE;
13339 msg_clr_eos();
13340
13341 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13342 {
13343 msg_puts(get_tv_string(&li->li_tv));
13344 msg_putchar('\n');
13345 }
13346
13347 /* Ask for choice. */
13348 selected = prompt_for_number(&mouse_used);
13349 if (mouse_used)
13350 selected -= lines_left;
13351
13352 rettv->vval.v_number = selected;
13353}
13354
13355
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13357
13358/*
13359 * "inputrestore()" function
13360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013363 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365{
13366 if (ga_userinput.ga_len > 0)
13367 {
13368 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13370 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013371 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 }
13373 else if (p_verbose > 1)
13374 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013375 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 }
13378}
13379
13380/*
13381 * "inputsave()" function
13382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013388 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 if (ga_grow(&ga_userinput, 1) == OK)
13390 {
13391 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13392 + ga_userinput.ga_len);
13393 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013394 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 }
13396 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398}
13399
13400/*
13401 * "inputsecret()" function
13402 */
13403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013405 typval_T *argvars;
13406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407{
13408 ++cmdline_star;
13409 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 --cmdline_star;
13412 --inputsecret_flag;
13413}
13414
13415/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013416 * "insert()" function
13417 */
13418 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013419f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013420 typval_T *argvars;
13421 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013422{
13423 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 listitem_T *item;
13425 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013426 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013427
13428 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013429 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013430 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013431 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013432 {
13433 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 before = get_tv_number_chk(&argvars[2], &error);
13435 if (error)
13436 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013437
Bram Moolenaar758711c2005-02-02 23:11:38 +000013438 if (before == l->lv_len)
13439 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013440 else
13441 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013442 item = list_find(l, before);
13443 if (item == NULL)
13444 {
13445 EMSGN(_(e_listidx), before);
13446 l = NULL;
13447 }
13448 }
13449 if (l != NULL)
13450 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013451 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013452 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013453 }
13454 }
13455}
13456
13457/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013458 * "invert(expr)" function
13459 */
13460 static void
13461f_invert(argvars, rettv)
13462 typval_T *argvars;
13463 typval_T *rettv;
13464{
13465 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13466}
13467
13468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469 * "isdirectory()" function
13470 */
13471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013473 typval_T *argvars;
13474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477}
13478
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013479/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013480 * "islocked()" function
13481 */
13482 static void
13483f_islocked(argvars, rettv)
13484 typval_T *argvars;
13485 typval_T *rettv;
13486{
13487 lval_T lv;
13488 char_u *end;
13489 dictitem_T *di;
13490
13491 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013492 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13493 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013494 if (end != NULL && lv.ll_name != NULL)
13495 {
13496 if (*end != NUL)
13497 EMSG(_(e_trailing));
13498 else
13499 {
13500 if (lv.ll_tv == NULL)
13501 {
13502 if (check_changedtick(lv.ll_name))
13503 rettv->vval.v_number = 1; /* always locked */
13504 else
13505 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013506 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013507 if (di != NULL)
13508 {
13509 /* Consider a variable locked when:
13510 * 1. the variable itself is locked
13511 * 2. the value of the variable is locked.
13512 * 3. the List or Dict value is locked.
13513 */
13514 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13515 || tv_islocked(&di->di_tv));
13516 }
13517 }
13518 }
13519 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013520 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013521 else if (lv.ll_newkey != NULL)
13522 EMSG2(_(e_dictkey), lv.ll_newkey);
13523 else if (lv.ll_list != NULL)
13524 /* List item. */
13525 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13526 else
13527 /* Dictionary item. */
13528 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13529 }
13530 }
13531
13532 clear_lval(&lv);
13533}
13534
Bram Moolenaar33570922005-01-25 22:26:29 +000013535static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013536
13537/*
13538 * Turn a dict into a list:
13539 * "what" == 0: list of keys
13540 * "what" == 1: list of values
13541 * "what" == 2: list of items
13542 */
13543 static void
13544dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013545 typval_T *argvars;
13546 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013547 int what;
13548{
Bram Moolenaar33570922005-01-25 22:26:29 +000013549 list_T *l2;
13550 dictitem_T *di;
13551 hashitem_T *hi;
13552 listitem_T *li;
13553 listitem_T *li2;
13554 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013555 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013556
Bram Moolenaar8c711452005-01-14 21:53:12 +000013557 if (argvars[0].v_type != VAR_DICT)
13558 {
13559 EMSG(_(e_dictreq));
13560 return;
13561 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013562 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013563 return;
13564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013566 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013568 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013570 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013571 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013573 --todo;
13574 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013576 li = listitem_alloc();
13577 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013578 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013579 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013580
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013581 if (what == 0)
13582 {
13583 /* keys() */
13584 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013585 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013586 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13587 }
13588 else if (what == 1)
13589 {
13590 /* values() */
13591 copy_tv(&di->di_tv, &li->li_tv);
13592 }
13593 else
13594 {
13595 /* items() */
13596 l2 = list_alloc();
13597 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013598 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013599 li->li_tv.vval.v_list = l2;
13600 if (l2 == NULL)
13601 break;
13602 ++l2->lv_refcount;
13603
13604 li2 = listitem_alloc();
13605 if (li2 == NULL)
13606 break;
13607 list_append(l2, li2);
13608 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013609 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013610 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13611
13612 li2 = listitem_alloc();
13613 if (li2 == NULL)
13614 break;
13615 list_append(l2, li2);
13616 copy_tv(&di->di_tv, &li2->li_tv);
13617 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013618 }
13619 }
13620}
13621
13622/*
13623 * "items(dict)" function
13624 */
13625 static void
13626f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013627 typval_T *argvars;
13628 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013629{
13630 dict_list(argvars, rettv, 2);
13631}
13632
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013634 * "join()" function
13635 */
13636 static void
13637f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *argvars;
13639 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013640{
13641 garray_T ga;
13642 char_u *sep;
13643
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013644 if (argvars[0].v_type != VAR_LIST)
13645 {
13646 EMSG(_(e_listreq));
13647 return;
13648 }
13649 if (argvars[0].vval.v_list == NULL)
13650 return;
13651 if (argvars[1].v_type == VAR_UNKNOWN)
13652 sep = (char_u *)" ";
13653 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013654 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013655
13656 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657
13658 if (sep != NULL)
13659 {
13660 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013661 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 ga_append(&ga, NUL);
13663 rettv->vval.v_string = (char_u *)ga.ga_data;
13664 }
13665 else
13666 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013667}
13668
13669/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013670 * "keys()" function
13671 */
13672 static void
13673f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013674 typval_T *argvars;
13675 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013676{
13677 dict_list(argvars, rettv, 0);
13678}
13679
13680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 * "last_buffer_nr()" function.
13682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013684f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013685 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687{
13688 int n = 0;
13689 buf_T *buf;
13690
13691 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13692 if (n < buf->b_fnum)
13693 n = buf->b_fnum;
13694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013695 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013696}
13697
13698/*
13699 * "len()" function
13700 */
13701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013703 typval_T *argvars;
13704 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013705{
13706 switch (argvars[0].v_type)
13707 {
13708 case VAR_STRING:
13709 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013710 rettv->vval.v_number = (varnumber_T)STRLEN(
13711 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013712 break;
13713 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013715 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013716 case VAR_DICT:
13717 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13718 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013719 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013720 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013721 break;
13722 }
13723}
13724
Bram Moolenaar33570922005-01-25 22:26:29 +000013725static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013726
13727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013729 typval_T *argvars;
13730 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013731 int type;
13732{
13733#ifdef FEAT_LIBCALL
13734 char_u *string_in;
13735 char_u **string_result;
13736 int nr_result;
13737#endif
13738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013740 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013742
13743 if (check_restricted() || check_secure())
13744 return;
13745
13746#ifdef FEAT_LIBCALL
13747 /* The first two args must be strings, otherwise its meaningless */
13748 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13749 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013750 string_in = NULL;
13751 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013752 string_in = argvars[2].vval.v_string;
13753 if (type == VAR_NUMBER)
13754 string_result = NULL;
13755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013757 if (mch_libcall(argvars[0].vval.v_string,
13758 argvars[1].vval.v_string,
13759 string_in,
13760 argvars[2].vval.v_number,
13761 string_result,
13762 &nr_result) == OK
13763 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013764 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013765 }
13766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767}
13768
13769/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013770 * "libcall()" function
13771 */
13772 static void
13773f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013774 typval_T *argvars;
13775 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013776{
13777 libcall_common(argvars, rettv, VAR_STRING);
13778}
13779
13780/*
13781 * "libcallnr()" function
13782 */
13783 static void
13784f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013785 typval_T *argvars;
13786 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787{
13788 libcall_common(argvars, rettv, VAR_NUMBER);
13789}
13790
13791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 * "line(string)" function
13793 */
13794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *argvars;
13797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798{
13799 linenr_T lnum = 0;
13800 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013801 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013803 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 if (fp != NULL)
13805 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013806 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807}
13808
13809/*
13810 * "line2byte(lnum)" function
13811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013813f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
13817#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#else
13820 linenr_T lnum;
13821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13827 if (rettv->vval.v_number >= 0)
13828 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829#endif
13830}
13831
13832/*
13833 * "lispindent(lnum)" function
13834 */
13835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839{
13840#ifdef FEAT_LISP
13841 pos_T pos;
13842 linenr_T lnum;
13843
13844 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13847 {
13848 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013849 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 curwin->w_cursor = pos;
13851 }
13852 else
13853#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855}
13856
13857/*
13858 * "localtime()" function
13859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013865 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866}
13867
Bram Moolenaar33570922005-01-25 22:26:29 +000013868static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869
13870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013872 typval_T *argvars;
13873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 int exact;
13875{
13876 char_u *keys;
13877 char_u *which;
13878 char_u buf[NUMBUFLEN];
13879 char_u *keys_buf = NULL;
13880 char_u *rhs;
13881 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013882 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013883 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013884 mapblock_T *mp;
13885 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886
13887 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 rettv->v_type = VAR_STRING;
13889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 if (*keys == NUL)
13893 return;
13894
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013895 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013898 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013899 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013900 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013901 if (argvars[3].v_type != VAR_UNKNOWN)
13902 get_dict = get_tv_number(&argvars[3]);
13903 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 else
13906 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013907 if (which == NULL)
13908 return;
13909
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 mode = get_map_mode(&which, 0);
13911
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013912 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013913 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013915
13916 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013918 /* Return a string. */
13919 if (rhs != NULL)
13920 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
Bram Moolenaarbd743252010-10-20 21:23:33 +020013922 }
13923 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13924 {
13925 /* Return a dictionary. */
13926 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13927 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13928 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929
Bram Moolenaarbd743252010-10-20 21:23:33 +020013930 dict_add_nr_str(dict, "lhs", 0L, lhs);
13931 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13932 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13933 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13934 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13935 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13936 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013937 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013938 dict_add_nr_str(dict, "mode", 0L, mapmode);
13939
13940 vim_free(lhs);
13941 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 }
13943}
13944
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013945#ifdef FEAT_FLOAT
13946/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013947 * "log()" function
13948 */
13949 static void
13950f_log(argvars, rettv)
13951 typval_T *argvars;
13952 typval_T *rettv;
13953{
13954 float_T f;
13955
13956 rettv->v_type = VAR_FLOAT;
13957 if (get_float_arg(argvars, &f) == OK)
13958 rettv->vval.v_float = log(f);
13959 else
13960 rettv->vval.v_float = 0.0;
13961}
13962
13963/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013964 * "log10()" function
13965 */
13966 static void
13967f_log10(argvars, rettv)
13968 typval_T *argvars;
13969 typval_T *rettv;
13970{
13971 float_T f;
13972
13973 rettv->v_type = VAR_FLOAT;
13974 if (get_float_arg(argvars, &f) == OK)
13975 rettv->vval.v_float = log10(f);
13976 else
13977 rettv->vval.v_float = 0.0;
13978}
13979#endif
13980
Bram Moolenaar1dced572012-04-05 16:54:08 +020013981#ifdef FEAT_LUA
13982/*
13983 * "luaeval()" function
13984 */
13985 static void
13986f_luaeval(argvars, rettv)
13987 typval_T *argvars;
13988 typval_T *rettv;
13989{
13990 char_u *str;
13991 char_u buf[NUMBUFLEN];
13992
13993 str = get_tv_string_buf(&argvars[0], buf);
13994 do_luaeval(str, argvars + 1, rettv);
13995}
13996#endif
13997
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013999 * "map()" function
14000 */
14001 static void
14002f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014003 typval_T *argvars;
14004 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014005{
14006 filter_map(argvars, rettv, TRUE);
14007}
14008
14009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014010 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 */
14012 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014013f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014014 typval_T *argvars;
14015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014017 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018}
14019
14020/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014021 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 */
14023 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014024f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014025 typval_T *argvars;
14026 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014028 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029}
14030
Bram Moolenaar33570922005-01-25 22:26:29 +000014031static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032
14033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 typval_T *argvars;
14036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 int type;
14038{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014039 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014040 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014041 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 char_u *pat;
14043 regmatch_T regmatch;
14044 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014045 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 char_u *save_cpo;
14047 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014048 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014049 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014050 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014051 list_T *l = NULL;
14052 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014053 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014054 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055
14056 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14057 save_cpo = p_cpo;
14058 p_cpo = (char_u *)"";
14059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014060 rettv->vval.v_number = -1;
14061 if (type == 3)
14062 {
14063 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014064 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014065 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014066 }
14067 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 rettv->v_type = VAR_STRING;
14070 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014073 if (argvars[0].v_type == VAR_LIST)
14074 {
14075 if ((l = argvars[0].vval.v_list) == NULL)
14076 goto theend;
14077 li = l->lv_first;
14078 }
14079 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014080 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014081 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014082 len = (long)STRLEN(str);
14083 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14086 if (pat == NULL)
14087 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014088
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014089 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 int error = FALSE;
14092
14093 start = get_tv_number_chk(&argvars[2], &error);
14094 if (error)
14095 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014096 if (l != NULL)
14097 {
14098 li = list_find(l, start);
14099 if (li == NULL)
14100 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014101 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014102 }
14103 else
14104 {
14105 if (start < 0)
14106 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014107 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014108 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014109 /* When "count" argument is there ignore matches before "start",
14110 * otherwise skip part of the string. Differs when pattern is "^"
14111 * or "\<". */
14112 if (argvars[3].v_type != VAR_UNKNOWN)
14113 startcol = start;
14114 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014115 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014116 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014117 len -= start;
14118 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014119 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014120
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014121 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014122 nth = get_tv_number_chk(&argvars[3], &error);
14123 if (error)
14124 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 }
14126
14127 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14128 if (regmatch.regprog != NULL)
14129 {
14130 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014131
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014132 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014133 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014134 if (l != NULL)
14135 {
14136 if (li == NULL)
14137 {
14138 match = FALSE;
14139 break;
14140 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014141 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014142 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014143 if (str == NULL)
14144 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014145 }
14146
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014147 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014148
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014149 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014150 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014151 if (l == NULL && !match)
14152 break;
14153
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014154 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014155 if (l != NULL)
14156 {
14157 li = li->li_next;
14158 ++idx;
14159 }
14160 else
14161 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014162#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014163 startcol = (colnr_T)(regmatch.startp[0]
14164 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014165#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014166 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014167#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014168 if (startcol > (colnr_T)len
14169 || str + startcol <= regmatch.startp[0])
14170 {
14171 match = FALSE;
14172 break;
14173 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014174 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014175 }
14176
14177 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014179 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014180 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014181 int i;
14182
14183 /* return list with matched string and submatches */
14184 for (i = 0; i < NSUBEXP; ++i)
14185 {
14186 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014187 {
14188 if (list_append_string(rettv->vval.v_list,
14189 (char_u *)"", 0) == FAIL)
14190 break;
14191 }
14192 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014193 regmatch.startp[i],
14194 (int)(regmatch.endp[i] - regmatch.startp[i]))
14195 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014196 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014197 }
14198 }
14199 else if (type == 2)
14200 {
14201 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014202 if (l != NULL)
14203 copy_tv(&li->li_tv, rettv);
14204 else
14205 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014207 }
14208 else if (l != NULL)
14209 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 else
14211 {
14212 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014213 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 (varnumber_T)(regmatch.startp[0] - str);
14215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014218 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219 }
14220 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014221 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 }
14223
14224theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014225 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 p_cpo = save_cpo;
14227}
14228
14229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230 * "match()" function
14231 */
14232 static void
14233f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014234 typval_T *argvars;
14235 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014236{
14237 find_some_match(argvars, rettv, 1);
14238}
14239
14240/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014241 * "matchadd()" function
14242 */
14243 static void
14244f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014245 typval_T *argvars UNUSED;
14246 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014247{
14248#ifdef FEAT_SEARCH_EXTRA
14249 char_u buf[NUMBUFLEN];
14250 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14251 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14252 int prio = 10; /* default priority */
14253 int id = -1;
14254 int error = FALSE;
14255
14256 rettv->vval.v_number = -1;
14257
14258 if (grp == NULL || pat == NULL)
14259 return;
14260 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014261 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014262 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014263 if (argvars[3].v_type != VAR_UNKNOWN)
14264 id = get_tv_number_chk(&argvars[3], &error);
14265 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014266 if (error == TRUE)
14267 return;
14268 if (id >= 1 && id <= 3)
14269 {
14270 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14271 return;
14272 }
14273
14274 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14275#endif
14276}
14277
14278/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014279 * "matcharg()" function
14280 */
14281 static void
14282f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014283 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014284 typval_T *rettv;
14285{
14286 if (rettv_list_alloc(rettv) == OK)
14287 {
14288#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014289 int id = get_tv_number(&argvars[0]);
14290 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014291
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014292 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014293 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014294 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14295 {
14296 list_append_string(rettv->vval.v_list,
14297 syn_id2name(m->hlg_id), -1);
14298 list_append_string(rettv->vval.v_list, m->pattern, -1);
14299 }
14300 else
14301 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014302 list_append_string(rettv->vval.v_list, NULL, -1);
14303 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014304 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014305 }
14306#endif
14307 }
14308}
14309
14310/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014311 * "matchdelete()" function
14312 */
14313 static void
14314f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014315 typval_T *argvars UNUSED;
14316 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014317{
14318#ifdef FEAT_SEARCH_EXTRA
14319 rettv->vval.v_number = match_delete(curwin,
14320 (int)get_tv_number(&argvars[0]), TRUE);
14321#endif
14322}
14323
14324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014325 * "matchend()" function
14326 */
14327 static void
14328f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014331{
14332 find_some_match(argvars, rettv, 0);
14333}
14334
14335/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014336 * "matchlist()" function
14337 */
14338 static void
14339f_matchlist(argvars, rettv)
14340 typval_T *argvars;
14341 typval_T *rettv;
14342{
14343 find_some_match(argvars, rettv, 3);
14344}
14345
14346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347 * "matchstr()" function
14348 */
14349 static void
14350f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014351 typval_T *argvars;
14352 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014353{
14354 find_some_match(argvars, rettv, 2);
14355}
14356
Bram Moolenaar33570922005-01-25 22:26:29 +000014357static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014358
14359 static void
14360max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014361 typval_T *argvars;
14362 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014363 int domax;
14364{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014365 long n = 0;
14366 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014368
14369 if (argvars[0].v_type == VAR_LIST)
14370 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014371 list_T *l;
14372 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014373
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014374 l = argvars[0].vval.v_list;
14375 if (l != NULL)
14376 {
14377 li = l->lv_first;
14378 if (li != NULL)
14379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014381 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014382 {
14383 li = li->li_next;
14384 if (li == NULL)
14385 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014387 if (domax ? i > n : i < n)
14388 n = i;
14389 }
14390 }
14391 }
14392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014393 else if (argvars[0].v_type == VAR_DICT)
14394 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014395 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014396 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014398 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014399
14400 d = argvars[0].vval.v_dict;
14401 if (d != NULL)
14402 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014403 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014404 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014405 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014406 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014408 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014410 if (first)
14411 {
14412 n = i;
14413 first = FALSE;
14414 }
14415 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014416 n = i;
14417 }
14418 }
14419 }
14420 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014421 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014422 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014423 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014424}
14425
14426/*
14427 * "max()" function
14428 */
14429 static void
14430f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014431 typval_T *argvars;
14432 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014433{
14434 max_min(argvars, rettv, TRUE);
14435}
14436
14437/*
14438 * "min()" function
14439 */
14440 static void
14441f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014442 typval_T *argvars;
14443 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014444{
14445 max_min(argvars, rettv, FALSE);
14446}
14447
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014448static int mkdir_recurse __ARGS((char_u *dir, int prot));
14449
14450/*
14451 * Create the directory in which "dir" is located, and higher levels when
14452 * needed.
14453 */
14454 static int
14455mkdir_recurse(dir, prot)
14456 char_u *dir;
14457 int prot;
14458{
14459 char_u *p;
14460 char_u *updir;
14461 int r = FAIL;
14462
14463 /* Get end of directory name in "dir".
14464 * We're done when it's "/" or "c:/". */
14465 p = gettail_sep(dir);
14466 if (p <= get_past_head(dir))
14467 return OK;
14468
14469 /* If the directory exists we're done. Otherwise: create it.*/
14470 updir = vim_strnsave(dir, (int)(p - dir));
14471 if (updir == NULL)
14472 return FAIL;
14473 if (mch_isdir(updir))
14474 r = OK;
14475 else if (mkdir_recurse(updir, prot) == OK)
14476 r = vim_mkdir_emsg(updir, prot);
14477 vim_free(updir);
14478 return r;
14479}
14480
14481#ifdef vim_mkdir
14482/*
14483 * "mkdir()" function
14484 */
14485 static void
14486f_mkdir(argvars, rettv)
14487 typval_T *argvars;
14488 typval_T *rettv;
14489{
14490 char_u *dir;
14491 char_u buf[NUMBUFLEN];
14492 int prot = 0755;
14493
14494 rettv->vval.v_number = FAIL;
14495 if (check_restricted() || check_secure())
14496 return;
14497
14498 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014499 if (*dir == NUL)
14500 rettv->vval.v_number = FAIL;
14501 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014502 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014503 if (*gettail(dir) == NUL)
14504 /* remove trailing slashes */
14505 *gettail_sep(dir) = NUL;
14506
14507 if (argvars[1].v_type != VAR_UNKNOWN)
14508 {
14509 if (argvars[2].v_type != VAR_UNKNOWN)
14510 prot = get_tv_number_chk(&argvars[2], NULL);
14511 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14512 mkdir_recurse(dir, prot);
14513 }
14514 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014515 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014516}
14517#endif
14518
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 * "mode()" function
14521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 typval_T *argvars;
14525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014527 char_u buf[3];
14528
14529 buf[1] = NUL;
14530 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 if (VIsual_active)
14533 {
14534 if (VIsual_select)
14535 buf[0] = VIsual_mode + 's' - 'v';
14536 else
14537 buf[0] = VIsual_mode;
14538 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014539 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014540 || State == CONFIRM)
14541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014543 if (State == ASKMORE)
14544 buf[1] = 'm';
14545 else if (State == CONFIRM)
14546 buf[1] = '?';
14547 }
14548 else if (State == EXTERNCMD)
14549 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 else if (State & INSERT)
14551 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014552#ifdef FEAT_VREPLACE
14553 if (State & VREPLACE_FLAG)
14554 {
14555 buf[0] = 'R';
14556 buf[1] = 'v';
14557 }
14558 else
14559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560 if (State & REPLACE_FLAG)
14561 buf[0] = 'R';
14562 else
14563 buf[0] = 'i';
14564 }
14565 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014568 if (exmode_active)
14569 buf[1] = 'v';
14570 }
14571 else if (exmode_active)
14572 {
14573 buf[0] = 'c';
14574 buf[1] = 'e';
14575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014579 if (finish_op)
14580 buf[1] = 'o';
14581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014583 /* Clear out the minor mode when the argument is not a non-zero number or
14584 * non-empty string. */
14585 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014586 buf[1] = NUL;
14587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_string = vim_strsave(buf);
14589 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590}
14591
Bram Moolenaar429fa852013-04-15 12:27:36 +020014592#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014593/*
14594 * "mzeval()" function
14595 */
14596 static void
14597f_mzeval(argvars, rettv)
14598 typval_T *argvars;
14599 typval_T *rettv;
14600{
14601 char_u *str;
14602 char_u buf[NUMBUFLEN];
14603
14604 str = get_tv_string_buf(&argvars[0], buf);
14605 do_mzeval(str, rettv);
14606}
Bram Moolenaar75676462013-01-30 14:55:42 +010014607
14608 void
14609mzscheme_call_vim(name, args, rettv)
14610 char_u *name;
14611 typval_T *args;
14612 typval_T *rettv;
14613{
14614 typval_T argvars[3];
14615
14616 argvars[0].v_type = VAR_STRING;
14617 argvars[0].vval.v_string = name;
14618 copy_tv(args, &argvars[1]);
14619 argvars[2].v_type = VAR_UNKNOWN;
14620 f_call(argvars, rettv);
14621 clear_tv(&argvars[1]);
14622}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014623#endif
14624
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626 * "nextnonblank()" function
14627 */
14628 static void
14629f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014630 typval_T *argvars;
14631 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632{
14633 linenr_T lnum;
14634
14635 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14636 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014637 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 {
14639 lnum = 0;
14640 break;
14641 }
14642 if (*skipwhite(ml_get(lnum)) != NUL)
14643 break;
14644 }
14645 rettv->vval.v_number = lnum;
14646}
14647
14648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649 * "nr2char()" function
14650 */
14651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014653 typval_T *argvars;
14654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655{
14656 char_u buf[NUMBUFLEN];
14657
14658#ifdef FEAT_MBYTE
14659 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014660 {
14661 int utf8 = 0;
14662
14663 if (argvars[1].v_type != VAR_UNKNOWN)
14664 utf8 = get_tv_number_chk(&argvars[1], NULL);
14665 if (utf8)
14666 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14667 else
14668 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670 else
14671#endif
14672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 buf[1] = NUL;
14675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->v_type = VAR_STRING;
14677 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014678}
14679
14680/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014681 * "or(expr, expr)" function
14682 */
14683 static void
14684f_or(argvars, rettv)
14685 typval_T *argvars;
14686 typval_T *rettv;
14687{
14688 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14689 | get_tv_number_chk(&argvars[1], NULL);
14690}
14691
14692/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014693 * "pathshorten()" function
14694 */
14695 static void
14696f_pathshorten(argvars, rettv)
14697 typval_T *argvars;
14698 typval_T *rettv;
14699{
14700 char_u *p;
14701
14702 rettv->v_type = VAR_STRING;
14703 p = get_tv_string_chk(&argvars[0]);
14704 if (p == NULL)
14705 rettv->vval.v_string = NULL;
14706 else
14707 {
14708 p = vim_strsave(p);
14709 rettv->vval.v_string = p;
14710 if (p != NULL)
14711 shorten_dir(p);
14712 }
14713}
14714
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014715#ifdef FEAT_FLOAT
14716/*
14717 * "pow()" function
14718 */
14719 static void
14720f_pow(argvars, rettv)
14721 typval_T *argvars;
14722 typval_T *rettv;
14723{
14724 float_T fx, fy;
14725
14726 rettv->v_type = VAR_FLOAT;
14727 if (get_float_arg(argvars, &fx) == OK
14728 && get_float_arg(&argvars[1], &fy) == OK)
14729 rettv->vval.v_float = pow(fx, fy);
14730 else
14731 rettv->vval.v_float = 0.0;
14732}
14733#endif
14734
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014735/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014736 * "prevnonblank()" function
14737 */
14738 static void
14739f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014740 typval_T *argvars;
14741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014742{
14743 linenr_T lnum;
14744
14745 lnum = get_tv_lnum(argvars);
14746 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14747 lnum = 0;
14748 else
14749 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14750 --lnum;
14751 rettv->vval.v_number = lnum;
14752}
14753
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014754#ifdef HAVE_STDARG_H
14755/* This dummy va_list is here because:
14756 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14757 * - locally in the function results in a "used before set" warning
14758 * - using va_start() to initialize it gives "function with fixed args" error */
14759static va_list ap;
14760#endif
14761
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014763 * "printf()" function
14764 */
14765 static void
14766f_printf(argvars, rettv)
14767 typval_T *argvars;
14768 typval_T *rettv;
14769{
14770 rettv->v_type = VAR_STRING;
14771 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014772#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014773 {
14774 char_u buf[NUMBUFLEN];
14775 int len;
14776 char_u *s;
14777 int saved_did_emsg = did_emsg;
14778 char *fmt;
14779
14780 /* Get the required length, allocate the buffer and do it for real. */
14781 did_emsg = FALSE;
14782 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014783 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014784 if (!did_emsg)
14785 {
14786 s = alloc(len + 1);
14787 if (s != NULL)
14788 {
14789 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014790 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014791 }
14792 }
14793 did_emsg |= saved_did_emsg;
14794 }
14795#endif
14796}
14797
14798/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014799 * "pumvisible()" function
14800 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014801 static void
14802f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014803 typval_T *argvars UNUSED;
14804 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014805{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014806#ifdef FEAT_INS_EXPAND
14807 if (pum_visible())
14808 rettv->vval.v_number = 1;
14809#endif
14810}
14811
Bram Moolenaardb913952012-06-29 12:54:53 +020014812#ifdef FEAT_PYTHON3
14813/*
14814 * "py3eval()" function
14815 */
14816 static void
14817f_py3eval(argvars, rettv)
14818 typval_T *argvars;
14819 typval_T *rettv;
14820{
14821 char_u *str;
14822 char_u buf[NUMBUFLEN];
14823
14824 str = get_tv_string_buf(&argvars[0], buf);
14825 do_py3eval(str, rettv);
14826}
14827#endif
14828
14829#ifdef FEAT_PYTHON
14830/*
14831 * "pyeval()" function
14832 */
14833 static void
14834f_pyeval(argvars, rettv)
14835 typval_T *argvars;
14836 typval_T *rettv;
14837{
14838 char_u *str;
14839 char_u buf[NUMBUFLEN];
14840
14841 str = get_tv_string_buf(&argvars[0], buf);
14842 do_pyeval(str, rettv);
14843}
14844#endif
14845
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014846/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014847 * "range()" function
14848 */
14849 static void
14850f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014851 typval_T *argvars;
14852 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014853{
14854 long start;
14855 long end;
14856 long stride = 1;
14857 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014860 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014861 if (argvars[1].v_type == VAR_UNKNOWN)
14862 {
14863 end = start - 1;
14864 start = 0;
14865 }
14866 else
14867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014869 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014870 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014871 }
14872
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014873 if (error)
14874 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014875 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014876 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014877 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014878 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014879 else
14880 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014881 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014882 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014883 if (list_append_number(rettv->vval.v_list,
14884 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014885 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014886 }
14887}
14888
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014889/*
14890 * "readfile()" function
14891 */
14892 static void
14893f_readfile(argvars, rettv)
14894 typval_T *argvars;
14895 typval_T *rettv;
14896{
14897 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014898 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899 char_u *fname;
14900 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014901 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14902 int io_size = sizeof(buf);
14903 int readlen; /* size of last fread() */
14904 char_u *prev = NULL; /* previously read bytes, if any */
14905 long prevlen = 0; /* length of data in prev */
14906 long prevsize = 0; /* size of prev buffer */
14907 long maxline = MAXLNUM;
14908 long cnt = 0;
14909 char_u *p; /* position in buf */
14910 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014911
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014912 if (argvars[1].v_type != VAR_UNKNOWN)
14913 {
14914 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14915 binary = TRUE;
14916 if (argvars[2].v_type != VAR_UNKNOWN)
14917 maxline = get_tv_number(&argvars[2]);
14918 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014919
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014920 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014921 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014922
14923 /* Always open the file in binary mode, library functions have a mind of
14924 * their own about CR-LF conversion. */
14925 fname = get_tv_string(&argvars[0]);
14926 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14927 {
14928 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14929 return;
14930 }
14931
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014932 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014933 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014935
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014936 /* This for loop processes what was read, but is also entered at end
14937 * of file so that either:
14938 * - an incomplete line gets written
14939 * - a "binary" file gets an empty line at the end if it ends in a
14940 * newline. */
14941 for (p = buf, start = buf;
14942 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14943 ++p)
14944 {
14945 if (*p == '\n' || readlen <= 0)
14946 {
14947 listitem_T *li;
14948 char_u *s = NULL;
14949 long_u len = p - start;
14950
14951 /* Finished a line. Remove CRs before NL. */
14952 if (readlen > 0 && !binary)
14953 {
14954 while (len > 0 && start[len - 1] == '\r')
14955 --len;
14956 /* removal may cross back to the "prev" string */
14957 if (len == 0)
14958 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14959 --prevlen;
14960 }
14961 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014962 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014963 else
14964 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014965 /* Change "prev" buffer to be the right size. This way
14966 * the bytes are only copied once, and very long lines are
14967 * allocated only once. */
14968 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014969 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014970 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014971 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014972 prev = NULL; /* the list will own the string */
14973 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014974 }
14975 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014976 if (s == NULL)
14977 {
14978 do_outofmem_msg((long_u) prevlen + len + 1);
14979 failed = TRUE;
14980 break;
14981 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014982
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014983 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014984 {
14985 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014986 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014987 break;
14988 }
14989 li->li_tv.v_type = VAR_STRING;
14990 li->li_tv.v_lock = 0;
14991 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014992 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014993
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014994 start = p + 1; /* step over newline */
14995 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014996 break;
14997 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014998 else if (*p == NUL)
14999 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015000#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015001 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15002 * when finding the BF and check the previous two bytes. */
15003 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015004 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015005 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15006 * + 1, these may be in the "prev" string. */
15007 char_u back1 = p >= buf + 1 ? p[-1]
15008 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15009 char_u back2 = p >= buf + 2 ? p[-2]
15010 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15011 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015012
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015013 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015014 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015015 char_u *dest = p - 2;
15016
15017 /* Usually a BOM is at the beginning of a file, and so at
15018 * the beginning of a line; then we can just step over it.
15019 */
15020 if (start == dest)
15021 start = p + 1;
15022 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015023 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015024 /* have to shuffle buf to close gap */
15025 int adjust_prevlen = 0;
15026
15027 if (dest < buf)
15028 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015029 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015030 dest = buf;
15031 }
15032 if (readlen > p - buf + 1)
15033 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15034 readlen -= 3 - adjust_prevlen;
15035 prevlen -= adjust_prevlen;
15036 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015037 }
15038 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015039 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015040#endif
15041 } /* for */
15042
15043 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15044 break;
15045 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015046 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015047 /* There's part of a line in buf, store it in "prev". */
15048 if (p - start + prevlen >= prevsize)
15049 {
15050 /* need bigger "prev" buffer */
15051 char_u *newprev;
15052
15053 /* A common use case is ordinary text files and "prev" gets a
15054 * fragment of a line, so the first allocation is made
15055 * small, to avoid repeatedly 'allocing' large and
15056 * 'reallocing' small. */
15057 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015058 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015059 else
15060 {
15061 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015062 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015063 prevsize = grow50pc > growmin ? grow50pc : growmin;
15064 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015065 newprev = prev == NULL ? alloc(prevsize)
15066 : vim_realloc(prev, prevsize);
15067 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015068 {
15069 do_outofmem_msg((long_u)prevsize);
15070 failed = TRUE;
15071 break;
15072 }
15073 prev = newprev;
15074 }
15075 /* Add the line part to end of "prev". */
15076 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015077 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015078 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015079 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015080
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015081 /*
15082 * For a negative line count use only the lines at the end of the file,
15083 * free the rest.
15084 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015085 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015086 while (cnt > -maxline)
15087 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015088 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015089 --cnt;
15090 }
15091
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015092 if (failed)
15093 {
15094 list_free(rettv->vval.v_list, TRUE);
15095 /* readfile doc says an empty list is returned on error */
15096 rettv->vval.v_list = list_alloc();
15097 }
15098
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015099 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015100 fclose(fd);
15101}
15102
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015103#if defined(FEAT_RELTIME)
15104static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15105
15106/*
15107 * Convert a List to proftime_T.
15108 * Return FAIL when there is something wrong.
15109 */
15110 static int
15111list2proftime(arg, tm)
15112 typval_T *arg;
15113 proftime_T *tm;
15114{
15115 long n1, n2;
15116 int error = FALSE;
15117
15118 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15119 || arg->vval.v_list->lv_len != 2)
15120 return FAIL;
15121 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15122 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15123# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015124 tm->HighPart = n1;
15125 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015126# else
15127 tm->tv_sec = n1;
15128 tm->tv_usec = n2;
15129# endif
15130 return error ? FAIL : OK;
15131}
15132#endif /* FEAT_RELTIME */
15133
15134/*
15135 * "reltime()" function
15136 */
15137 static void
15138f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015139 typval_T *argvars UNUSED;
15140 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015141{
15142#ifdef FEAT_RELTIME
15143 proftime_T res;
15144 proftime_T start;
15145
15146 if (argvars[0].v_type == VAR_UNKNOWN)
15147 {
15148 /* No arguments: get current time. */
15149 profile_start(&res);
15150 }
15151 else if (argvars[1].v_type == VAR_UNKNOWN)
15152 {
15153 if (list2proftime(&argvars[0], &res) == FAIL)
15154 return;
15155 profile_end(&res);
15156 }
15157 else
15158 {
15159 /* Two arguments: compute the difference. */
15160 if (list2proftime(&argvars[0], &start) == FAIL
15161 || list2proftime(&argvars[1], &res) == FAIL)
15162 return;
15163 profile_sub(&res, &start);
15164 }
15165
15166 if (rettv_list_alloc(rettv) == OK)
15167 {
15168 long n1, n2;
15169
15170# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015171 n1 = res.HighPart;
15172 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015173# else
15174 n1 = res.tv_sec;
15175 n2 = res.tv_usec;
15176# endif
15177 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15178 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15179 }
15180#endif
15181}
15182
15183/*
15184 * "reltimestr()" function
15185 */
15186 static void
15187f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015188 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015189 typval_T *rettv;
15190{
15191#ifdef FEAT_RELTIME
15192 proftime_T tm;
15193#endif
15194
15195 rettv->v_type = VAR_STRING;
15196 rettv->vval.v_string = NULL;
15197#ifdef FEAT_RELTIME
15198 if (list2proftime(&argvars[0], &tm) == OK)
15199 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15200#endif
15201}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015202
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15204static void make_connection __ARGS((void));
15205static int check_connection __ARGS((void));
15206
15207 static void
15208make_connection()
15209{
15210 if (X_DISPLAY == NULL
15211# ifdef FEAT_GUI
15212 && !gui.in_use
15213# endif
15214 )
15215 {
15216 x_force_connect = TRUE;
15217 setup_term_clip();
15218 x_force_connect = FALSE;
15219 }
15220}
15221
15222 static int
15223check_connection()
15224{
15225 make_connection();
15226 if (X_DISPLAY == NULL)
15227 {
15228 EMSG(_("E240: No connection to Vim server"));
15229 return FAIL;
15230 }
15231 return OK;
15232}
15233#endif
15234
15235#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015236static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015237
15238 static void
15239remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *argvars;
15241 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242 int expr;
15243{
15244 char_u *server_name;
15245 char_u *keys;
15246 char_u *r = NULL;
15247 char_u buf[NUMBUFLEN];
15248# ifdef WIN32
15249 HWND w;
15250# else
15251 Window w;
15252# endif
15253
15254 if (check_restricted() || check_secure())
15255 return;
15256
15257# ifdef FEAT_X11
15258 if (check_connection() == FAIL)
15259 return;
15260# endif
15261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015262 server_name = get_tv_string_chk(&argvars[0]);
15263 if (server_name == NULL)
15264 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015265 keys = get_tv_string_buf(&argvars[1], buf);
15266# ifdef WIN32
15267 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15268# else
15269 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15270 < 0)
15271# endif
15272 {
15273 if (r != NULL)
15274 EMSG(r); /* sending worked but evaluation failed */
15275 else
15276 EMSG2(_("E241: Unable to send to %s"), server_name);
15277 return;
15278 }
15279
15280 rettv->vval.v_string = r;
15281
15282 if (argvars[2].v_type != VAR_UNKNOWN)
15283 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015284 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015285 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015287
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015288 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 v.di_tv.v_type = VAR_STRING;
15290 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 idvar = get_tv_string_chk(&argvars[2]);
15292 if (idvar != NULL)
15293 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015294 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015295 }
15296}
15297#endif
15298
15299/*
15300 * "remote_expr()" function
15301 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015302 static void
15303f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015304 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015305 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015306{
15307 rettv->v_type = VAR_STRING;
15308 rettv->vval.v_string = NULL;
15309#ifdef FEAT_CLIENTSERVER
15310 remote_common(argvars, rettv, TRUE);
15311#endif
15312}
15313
15314/*
15315 * "remote_foreground()" function
15316 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317 static void
15318f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015319 typval_T *argvars UNUSED;
15320 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015321{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322#ifdef FEAT_CLIENTSERVER
15323# ifdef WIN32
15324 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 {
15326 char_u *server_name = get_tv_string_chk(&argvars[0]);
15327
15328 if (server_name != NULL)
15329 serverForeground(server_name);
15330 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015331# else
15332 /* Send a foreground() expression to the server. */
15333 argvars[1].v_type = VAR_STRING;
15334 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15335 argvars[2].v_type = VAR_UNKNOWN;
15336 remote_common(argvars, rettv, TRUE);
15337 vim_free(argvars[1].vval.v_string);
15338# endif
15339#endif
15340}
15341
Bram Moolenaar0d660222005-01-07 21:51:51 +000015342 static void
15343f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015345 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015346{
15347#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015349 char_u *s = NULL;
15350# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015351 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015352# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015353 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015354
15355 if (check_restricted() || check_secure())
15356 {
15357 rettv->vval.v_number = -1;
15358 return;
15359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 serverid = get_tv_string_chk(&argvars[0]);
15361 if (serverid == NULL)
15362 {
15363 rettv->vval.v_number = -1;
15364 return; /* type error; errmsg already given */
15365 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015366# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015367 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368 if (n == 0)
15369 rettv->vval.v_number = -1;
15370 else
15371 {
15372 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15373 rettv->vval.v_number = (s != NULL);
15374 }
15375# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015376 if (check_connection() == FAIL)
15377 return;
15378
15379 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015380 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015381# endif
15382
15383 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015385 char_u *retvar;
15386
Bram Moolenaar33570922005-01-25 22:26:29 +000015387 v.di_tv.v_type = VAR_STRING;
15388 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015389 retvar = get_tv_string_chk(&argvars[1]);
15390 if (retvar != NULL)
15391 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015392 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015393 }
15394#else
15395 rettv->vval.v_number = -1;
15396#endif
15397}
15398
Bram Moolenaar0d660222005-01-07 21:51:51 +000015399 static void
15400f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015401 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015402 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015403{
15404 char_u *r = NULL;
15405
15406#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015407 char_u *serverid = get_tv_string_chk(&argvars[0]);
15408
15409 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015410 {
15411# ifdef WIN32
15412 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015413 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015414
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015415 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015416 if (n != 0)
15417 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15418 if (r == NULL)
15419# else
15420 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015421 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422# endif
15423 EMSG(_("E277: Unable to read a server reply"));
15424 }
15425#endif
15426 rettv->v_type = VAR_STRING;
15427 rettv->vval.v_string = r;
15428}
15429
15430/*
15431 * "remote_send()" function
15432 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015433 static void
15434f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015435 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015436 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015437{
15438 rettv->v_type = VAR_STRING;
15439 rettv->vval.v_string = NULL;
15440#ifdef FEAT_CLIENTSERVER
15441 remote_common(argvars, rettv, FALSE);
15442#endif
15443}
15444
15445/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015446 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447 */
15448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015450 typval_T *argvars;
15451 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015452{
Bram Moolenaar33570922005-01-25 22:26:29 +000015453 list_T *l;
15454 listitem_T *item, *item2;
15455 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015456 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015457 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015458 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015459 dict_T *d;
15460 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015461 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462
Bram Moolenaar8c711452005-01-14 21:53:12 +000015463 if (argvars[0].v_type == VAR_DICT)
15464 {
15465 if (argvars[2].v_type != VAR_UNKNOWN)
15466 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015467 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015468 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 key = get_tv_string_chk(&argvars[1]);
15471 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015473 di = dict_find(d, key, -1);
15474 if (di == NULL)
15475 EMSG2(_(e_dictkey), key);
15476 else
15477 {
15478 *rettv = di->di_tv;
15479 init_tv(&di->di_tv);
15480 dictitem_remove(d, di);
15481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015482 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015483 }
15484 }
15485 else if (argvars[0].v_type != VAR_LIST)
15486 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015487 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015488 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015490 int error = FALSE;
15491
15492 idx = get_tv_number_chk(&argvars[1], &error);
15493 if (error)
15494 ; /* type error: do nothing, errmsg already given */
15495 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015496 EMSGN(_(e_listidx), idx);
15497 else
15498 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015499 if (argvars[2].v_type == VAR_UNKNOWN)
15500 {
15501 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015502 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015503 *rettv = item->li_tv;
15504 vim_free(item);
15505 }
15506 else
15507 {
15508 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015509 end = get_tv_number_chk(&argvars[2], &error);
15510 if (error)
15511 ; /* type error: do nothing */
15512 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015513 EMSGN(_(e_listidx), end);
15514 else
15515 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015516 int cnt = 0;
15517
15518 for (li = item; li != NULL; li = li->li_next)
15519 {
15520 ++cnt;
15521 if (li == item2)
15522 break;
15523 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015524 if (li == NULL) /* didn't find "item2" after "item" */
15525 EMSG(_(e_invrange));
15526 else
15527 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015528 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015529 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015530 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015531 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015532 l->lv_first = item;
15533 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015534 item->li_prev = NULL;
15535 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015536 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015537 }
15538 }
15539 }
15540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015541 }
15542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543}
15544
15545/*
15546 * "rename({from}, {to})" function
15547 */
15548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015550 typval_T *argvars;
15551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552{
15553 char_u buf[NUMBUFLEN];
15554
15555 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15559 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560}
15561
15562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015563 * "repeat()" function
15564 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015565 static void
15566f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 typval_T *argvars;
15568 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015569{
15570 char_u *p;
15571 int n;
15572 int slen;
15573 int len;
15574 char_u *r;
15575 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015576
15577 n = get_tv_number(&argvars[1]);
15578 if (argvars[0].v_type == VAR_LIST)
15579 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015580 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015581 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015582 if (list_extend(rettv->vval.v_list,
15583 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015584 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015585 }
15586 else
15587 {
15588 p = get_tv_string(&argvars[0]);
15589 rettv->v_type = VAR_STRING;
15590 rettv->vval.v_string = NULL;
15591
15592 slen = (int)STRLEN(p);
15593 len = slen * n;
15594 if (len <= 0)
15595 return;
15596
15597 r = alloc(len + 1);
15598 if (r != NULL)
15599 {
15600 for (i = 0; i < n; i++)
15601 mch_memmove(r + i * slen, p, (size_t)slen);
15602 r[len] = NUL;
15603 }
15604
15605 rettv->vval.v_string = r;
15606 }
15607}
15608
15609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 * "resolve()" function
15611 */
15612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015614 typval_T *argvars;
15615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616{
15617 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015618#ifdef HAVE_READLINK
15619 char_u *buf = NULL;
15620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015622 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623#ifdef FEAT_SHORTCUT
15624 {
15625 char_u *v = NULL;
15626
15627 v = mch_resolve_shortcut(p);
15628 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 }
15633#else
15634# ifdef HAVE_READLINK
15635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 char_u *cpy;
15637 int len;
15638 char_u *remain = NULL;
15639 char_u *q;
15640 int is_relative_to_current = FALSE;
15641 int has_trailing_pathsep = FALSE;
15642 int limit = 100;
15643
15644 p = vim_strsave(p);
15645
15646 if (p[0] == '.' && (vim_ispathsep(p[1])
15647 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15648 is_relative_to_current = TRUE;
15649
15650 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015651 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015654 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656
15657 q = getnextcomp(p);
15658 if (*q != NUL)
15659 {
15660 /* Separate the first path component in "p", and keep the
15661 * remainder (beginning with the path separator). */
15662 remain = vim_strsave(q - 1);
15663 q[-1] = NUL;
15664 }
15665
Bram Moolenaard9462e32011-04-11 21:35:11 +020015666 buf = alloc(MAXPATHL + 1);
15667 if (buf == NULL)
15668 goto fail;
15669
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 for (;;)
15671 {
15672 for (;;)
15673 {
15674 len = readlink((char *)p, (char *)buf, MAXPATHL);
15675 if (len <= 0)
15676 break;
15677 buf[len] = NUL;
15678
15679 if (limit-- == 0)
15680 {
15681 vim_free(p);
15682 vim_free(remain);
15683 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015684 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 goto fail;
15686 }
15687
15688 /* Ensure that the result will have a trailing path separator
15689 * if the argument has one. */
15690 if (remain == NULL && has_trailing_pathsep)
15691 add_pathsep(buf);
15692
15693 /* Separate the first path component in the link value and
15694 * concatenate the remainders. */
15695 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15696 if (*q != NUL)
15697 {
15698 if (remain == NULL)
15699 remain = vim_strsave(q - 1);
15700 else
15701 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015702 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 if (cpy != NULL)
15704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 vim_free(remain);
15706 remain = cpy;
15707 }
15708 }
15709 q[-1] = NUL;
15710 }
15711
15712 q = gettail(p);
15713 if (q > p && *q == NUL)
15714 {
15715 /* Ignore trailing path separator. */
15716 q[-1] = NUL;
15717 q = gettail(p);
15718 }
15719 if (q > p && !mch_isFullName(buf))
15720 {
15721 /* symlink is relative to directory of argument */
15722 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15723 if (cpy != NULL)
15724 {
15725 STRCPY(cpy, p);
15726 STRCPY(gettail(cpy), buf);
15727 vim_free(p);
15728 p = cpy;
15729 }
15730 }
15731 else
15732 {
15733 vim_free(p);
15734 p = vim_strsave(buf);
15735 }
15736 }
15737
15738 if (remain == NULL)
15739 break;
15740
15741 /* Append the first path component of "remain" to "p". */
15742 q = getnextcomp(remain + 1);
15743 len = q - remain - (*q != NUL);
15744 cpy = vim_strnsave(p, STRLEN(p) + len);
15745 if (cpy != NULL)
15746 {
15747 STRNCAT(cpy, remain, len);
15748 vim_free(p);
15749 p = cpy;
15750 }
15751 /* Shorten "remain". */
15752 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015753 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 else
15755 {
15756 vim_free(remain);
15757 remain = NULL;
15758 }
15759 }
15760
15761 /* If the result is a relative path name, make it explicitly relative to
15762 * the current directory if and only if the argument had this form. */
15763 if (!vim_ispathsep(*p))
15764 {
15765 if (is_relative_to_current
15766 && *p != NUL
15767 && !(p[0] == '.'
15768 && (p[1] == NUL
15769 || vim_ispathsep(p[1])
15770 || (p[1] == '.'
15771 && (p[2] == NUL
15772 || vim_ispathsep(p[2]))))))
15773 {
15774 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015775 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776 if (cpy != NULL)
15777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 vim_free(p);
15779 p = cpy;
15780 }
15781 }
15782 else if (!is_relative_to_current)
15783 {
15784 /* Strip leading "./". */
15785 q = p;
15786 while (q[0] == '.' && vim_ispathsep(q[1]))
15787 q += 2;
15788 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015789 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 }
15791 }
15792
15793 /* Ensure that the result will have no trailing path separator
15794 * if the argument had none. But keep "/" or "//". */
15795 if (!has_trailing_pathsep)
15796 {
15797 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015798 if (after_pathsep(p, q))
15799 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 }
15801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 }
15804# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015805 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806# endif
15807#endif
15808
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810
15811#ifdef HAVE_READLINK
15812fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015813 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015815 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816}
15817
15818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015819 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 */
15821 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015823 typval_T *argvars;
15824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825{
Bram Moolenaar33570922005-01-25 22:26:29 +000015826 list_T *l;
15827 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829 if (argvars[0].v_type != VAR_LIST)
15830 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015831 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015832 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015833 {
15834 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015835 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015836 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015837 while (li != NULL)
15838 {
15839 ni = li->li_prev;
15840 list_append(l, li);
15841 li = ni;
15842 }
15843 rettv->vval.v_list = l;
15844 rettv->v_type = VAR_LIST;
15845 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015846 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848}
15849
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015850#define SP_NOMOVE 0x01 /* don't move cursor */
15851#define SP_REPEAT 0x02 /* repeat to find outer pair */
15852#define SP_RETCOUNT 0x04 /* return matchcount */
15853#define SP_SETPCMARK 0x08 /* set previous context mark */
15854#define SP_START 0x10 /* accept match at start position */
15855#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15856#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015857
Bram Moolenaar33570922005-01-25 22:26:29 +000015858static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859
15860/*
15861 * Get flags for a search function.
15862 * Possibly sets "p_ws".
15863 * Returns BACKWARD, FORWARD or zero (for an error).
15864 */
15865 static int
15866get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015867 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015868 int *flagsp;
15869{
15870 int dir = FORWARD;
15871 char_u *flags;
15872 char_u nbuf[NUMBUFLEN];
15873 int mask;
15874
15875 if (varp->v_type != VAR_UNKNOWN)
15876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015877 flags = get_tv_string_buf_chk(varp, nbuf);
15878 if (flags == NULL)
15879 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015880 while (*flags != NUL)
15881 {
15882 switch (*flags)
15883 {
15884 case 'b': dir = BACKWARD; break;
15885 case 'w': p_ws = TRUE; break;
15886 case 'W': p_ws = FALSE; break;
15887 default: mask = 0;
15888 if (flagsp != NULL)
15889 switch (*flags)
15890 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015891 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015892 case 'e': mask = SP_END; break;
15893 case 'm': mask = SP_RETCOUNT; break;
15894 case 'n': mask = SP_NOMOVE; break;
15895 case 'p': mask = SP_SUBPAT; break;
15896 case 'r': mask = SP_REPEAT; break;
15897 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015898 }
15899 if (mask == 0)
15900 {
15901 EMSG2(_(e_invarg2), flags);
15902 dir = 0;
15903 }
15904 else
15905 *flagsp |= mask;
15906 }
15907 if (dir == 0)
15908 break;
15909 ++flags;
15910 }
15911 }
15912 return dir;
15913}
15914
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015916 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015918 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015919search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015920 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015921 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015922 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015924 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 char_u *pat;
15926 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015927 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 int save_p_ws = p_ws;
15929 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015930 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015931 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015932 proftime_T tm;
15933#ifdef FEAT_RELTIME
15934 long time_limit = 0;
15935#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015936 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015937 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015939 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015940 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015941 if (dir == 0)
15942 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015943 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015944 if (flags & SP_START)
15945 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015946 if (flags & SP_END)
15947 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015948
Bram Moolenaar76929292008-01-06 19:07:36 +000015949 /* Optional arguments: line number to stop searching and timeout. */
15950 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015951 {
15952 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15953 if (lnum_stop < 0)
15954 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015955#ifdef FEAT_RELTIME
15956 if (argvars[3].v_type != VAR_UNKNOWN)
15957 {
15958 time_limit = get_tv_number_chk(&argvars[3], NULL);
15959 if (time_limit < 0)
15960 goto theend;
15961 }
15962#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015963 }
15964
Bram Moolenaar76929292008-01-06 19:07:36 +000015965#ifdef FEAT_RELTIME
15966 /* Set the time limit, if there is one. */
15967 profile_setlimit(time_limit, &tm);
15968#endif
15969
Bram Moolenaar231334e2005-07-25 20:46:57 +000015970 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015971 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015972 * Check to make sure only those flags are set.
15973 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15974 * flags cannot be set. Check for that condition also.
15975 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015976 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015977 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015979 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015980 goto theend;
15981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015983 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015984 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015985 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015986 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015988 if (flags & SP_SUBPAT)
15989 retval = subpatnum;
15990 else
15991 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015992 if (flags & SP_SETPCMARK)
15993 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015995 if (match_pos != NULL)
15996 {
15997 /* Store the match cursor position */
15998 match_pos->lnum = pos.lnum;
15999 match_pos->col = pos.col + 1;
16000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016001 /* "/$" will put the cursor after the end of the line, may need to
16002 * correct that here */
16003 check_cursor();
16004 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016005
16006 /* If 'n' flag is used: restore cursor position. */
16007 if (flags & SP_NOMOVE)
16008 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016009 else
16010 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016011theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016013
16014 return retval;
16015}
16016
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016017#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016018
16019/*
16020 * round() is not in C90, use ceil() or floor() instead.
16021 */
16022 float_T
16023vim_round(f)
16024 float_T f;
16025{
16026 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16027}
16028
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016029/*
16030 * "round({float})" function
16031 */
16032 static void
16033f_round(argvars, rettv)
16034 typval_T *argvars;
16035 typval_T *rettv;
16036{
16037 float_T f;
16038
16039 rettv->v_type = VAR_FLOAT;
16040 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016041 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016042 else
16043 rettv->vval.v_float = 0.0;
16044}
16045#endif
16046
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016047/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016048 * "screenattr()" function
16049 */
16050 static void
16051f_screenattr(argvars, rettv)
16052 typval_T *argvars UNUSED;
16053 typval_T *rettv;
16054{
16055 int row;
16056 int col;
16057 int c;
16058
16059 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16060 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16061 if (row < 0 || row >= screen_Rows
16062 || col < 0 || col >= screen_Columns)
16063 c = -1;
16064 else
16065 c = ScreenAttrs[LineOffset[row] + col];
16066 rettv->vval.v_number = c;
16067}
16068
16069/*
16070 * "screenchar()" function
16071 */
16072 static void
16073f_screenchar(argvars, rettv)
16074 typval_T *argvars UNUSED;
16075 typval_T *rettv;
16076{
16077 int row;
16078 int col;
16079 int off;
16080 int c;
16081
16082 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16083 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16084 if (row < 0 || row >= screen_Rows
16085 || col < 0 || col >= screen_Columns)
16086 c = -1;
16087 else
16088 {
16089 off = LineOffset[row] + col;
16090#ifdef FEAT_MBYTE
16091 if (enc_utf8 && ScreenLinesUC[off] != 0)
16092 c = ScreenLinesUC[off];
16093 else
16094#endif
16095 c = ScreenLines[off];
16096 }
16097 rettv->vval.v_number = c;
16098}
16099
16100/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016101 * "screencol()" function
16102 *
16103 * First column is 1 to be consistent with virtcol().
16104 */
16105 static void
16106f_screencol(argvars, rettv)
16107 typval_T *argvars UNUSED;
16108 typval_T *rettv;
16109{
16110 rettv->vval.v_number = screen_screencol() + 1;
16111}
16112
16113/*
16114 * "screenrow()" function
16115 */
16116 static void
16117f_screenrow(argvars, rettv)
16118 typval_T *argvars UNUSED;
16119 typval_T *rettv;
16120{
16121 rettv->vval.v_number = screen_screenrow() + 1;
16122}
16123
16124/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016125 * "search()" function
16126 */
16127 static void
16128f_search(argvars, rettv)
16129 typval_T *argvars;
16130 typval_T *rettv;
16131{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016132 int flags = 0;
16133
16134 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135}
16136
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016138 * "searchdecl()" function
16139 */
16140 static void
16141f_searchdecl(argvars, rettv)
16142 typval_T *argvars;
16143 typval_T *rettv;
16144{
16145 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016146 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016147 int error = FALSE;
16148 char_u *name;
16149
16150 rettv->vval.v_number = 1; /* default: FAIL */
16151
16152 name = get_tv_string_chk(&argvars[0]);
16153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016154 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016155 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016156 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16157 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16158 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016159 if (!error && name != NULL)
16160 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016161 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016162}
16163
16164/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016165 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016167 static int
16168searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016170 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171{
16172 char_u *spat, *mpat, *epat;
16173 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 int dir;
16176 int flags = 0;
16177 char_u nbuf1[NUMBUFLEN];
16178 char_u nbuf2[NUMBUFLEN];
16179 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016180 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016181 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016182 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016185 spat = get_tv_string_chk(&argvars[0]);
16186 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16187 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16188 if (spat == NULL || mpat == NULL || epat == NULL)
16189 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 /* Handle the optional fourth argument: flags */
16192 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016193 if (dir == 0)
16194 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016195
16196 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016197 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16198 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016199 if ((flags & (SP_END | SP_SUBPAT)) != 0
16200 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016201 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016202 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016203 goto theend;
16204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016206 /* Using 'r' implies 'W', otherwise it doesn't work. */
16207 if (flags & SP_REPEAT)
16208 p_ws = FALSE;
16209
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016210 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016211 if (argvars[3].v_type == VAR_UNKNOWN
16212 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 skip = (char_u *)"";
16214 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016216 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016217 if (argvars[5].v_type != VAR_UNKNOWN)
16218 {
16219 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16220 if (lnum_stop < 0)
16221 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016222#ifdef FEAT_RELTIME
16223 if (argvars[6].v_type != VAR_UNKNOWN)
16224 {
16225 time_limit = get_tv_number_chk(&argvars[6], NULL);
16226 if (time_limit < 0)
16227 goto theend;
16228 }
16229#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016230 }
16231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016232 if (skip == NULL)
16233 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016235 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016236 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016237
16238theend:
16239 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016240
16241 return retval;
16242}
16243
16244/*
16245 * "searchpair()" function
16246 */
16247 static void
16248f_searchpair(argvars, rettv)
16249 typval_T *argvars;
16250 typval_T *rettv;
16251{
16252 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16253}
16254
16255/*
16256 * "searchpairpos()" function
16257 */
16258 static void
16259f_searchpairpos(argvars, rettv)
16260 typval_T *argvars;
16261 typval_T *rettv;
16262{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016263 pos_T match_pos;
16264 int lnum = 0;
16265 int col = 0;
16266
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016267 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016268 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016269
16270 if (searchpair_cmn(argvars, &match_pos) > 0)
16271 {
16272 lnum = match_pos.lnum;
16273 col = match_pos.col;
16274 }
16275
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016276 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16277 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016278}
16279
16280/*
16281 * Search for a start/middle/end thing.
16282 * Used by searchpair(), see its documentation for the details.
16283 * Returns 0 or -1 for no match,
16284 */
16285 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016286do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16287 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016288 char_u *spat; /* start pattern */
16289 char_u *mpat; /* middle pattern */
16290 char_u *epat; /* end pattern */
16291 int dir; /* BACKWARD or FORWARD */
16292 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016293 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016294 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016295 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016296 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016297{
16298 char_u *save_cpo;
16299 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16300 long retval = 0;
16301 pos_T pos;
16302 pos_T firstpos;
16303 pos_T foundpos;
16304 pos_T save_cursor;
16305 pos_T save_pos;
16306 int n;
16307 int r;
16308 int nest = 1;
16309 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016310 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016311 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016312
16313 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16314 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016315 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016316
Bram Moolenaar76929292008-01-06 19:07:36 +000016317#ifdef FEAT_RELTIME
16318 /* Set the time limit, if there is one. */
16319 profile_setlimit(time_limit, &tm);
16320#endif
16321
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016322 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16323 * start/middle/end (pat3, for the top pair). */
16324 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16325 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16326 if (pat2 == NULL || pat3 == NULL)
16327 goto theend;
16328 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16329 if (*mpat == NUL)
16330 STRCPY(pat3, pat2);
16331 else
16332 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16333 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016334 if (flags & SP_START)
16335 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016336
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 save_cursor = curwin->w_cursor;
16338 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016339 clearpos(&firstpos);
16340 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 pat = pat3;
16342 for (;;)
16343 {
16344 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016345 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16347 /* didn't find it or found the first match again: FAIL */
16348 break;
16349
16350 if (firstpos.lnum == 0)
16351 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016352 if (equalpos(pos, foundpos))
16353 {
16354 /* Found the same position again. Can happen with a pattern that
16355 * has "\zs" at the end and searching backwards. Advance one
16356 * character and try again. */
16357 if (dir == BACKWARD)
16358 decl(&pos);
16359 else
16360 incl(&pos);
16361 }
16362 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016364 /* clear the start flag to avoid getting stuck here */
16365 options &= ~SEARCH_START;
16366
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 /* If the skip pattern matches, ignore this match. */
16368 if (*skip != NUL)
16369 {
16370 save_pos = curwin->w_cursor;
16371 curwin->w_cursor = pos;
16372 r = eval_to_bool(skip, &err, NULL, FALSE);
16373 curwin->w_cursor = save_pos;
16374 if (err)
16375 {
16376 /* Evaluating {skip} caused an error, break here. */
16377 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016378 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 break;
16380 }
16381 if (r)
16382 continue;
16383 }
16384
16385 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16386 {
16387 /* Found end when searching backwards or start when searching
16388 * forward: nested pair. */
16389 ++nest;
16390 pat = pat2; /* nested, don't search for middle */
16391 }
16392 else
16393 {
16394 /* Found end when searching forward or start when searching
16395 * backward: end of (nested) pair; or found middle in outer pair. */
16396 if (--nest == 1)
16397 pat = pat3; /* outer level, search for middle */
16398 }
16399
16400 if (nest == 0)
16401 {
16402 /* Found the match: return matchcount or line number. */
16403 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016404 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016406 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016407 if (flags & SP_SETPCMARK)
16408 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 curwin->w_cursor = pos;
16410 if (!(flags & SP_REPEAT))
16411 break;
16412 nest = 1; /* search for next unmatched */
16413 }
16414 }
16415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016416 if (match_pos != NULL)
16417 {
16418 /* Store the match cursor position */
16419 match_pos->lnum = curwin->w_cursor.lnum;
16420 match_pos->col = curwin->w_cursor.col + 1;
16421 }
16422
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016424 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 curwin->w_cursor = save_cursor;
16426
16427theend:
16428 vim_free(pat2);
16429 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016430 if (p_cpo == empty_option)
16431 p_cpo = save_cpo;
16432 else
16433 /* Darn, evaluating the {skip} expression changed the value. */
16434 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016435
16436 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437}
16438
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016439/*
16440 * "searchpos()" function
16441 */
16442 static void
16443f_searchpos(argvars, rettv)
16444 typval_T *argvars;
16445 typval_T *rettv;
16446{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016447 pos_T match_pos;
16448 int lnum = 0;
16449 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016450 int n;
16451 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016452
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016453 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016454 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016455
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016456 n = search_cmn(argvars, &match_pos, &flags);
16457 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016458 {
16459 lnum = match_pos.lnum;
16460 col = match_pos.col;
16461 }
16462
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016463 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16464 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016465 if (flags & SP_SUBPAT)
16466 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016467}
16468
16469
Bram Moolenaar0d660222005-01-07 21:51:51 +000016470 static void
16471f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016472 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016474{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475#ifdef FEAT_CLIENTSERVER
16476 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 char_u *server = get_tv_string_chk(&argvars[0]);
16478 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016481 if (server == NULL || reply == NULL)
16482 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483 if (check_restricted() || check_secure())
16484 return;
16485# ifdef FEAT_X11
16486 if (check_connection() == FAIL)
16487 return;
16488# endif
16489
16490 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 EMSG(_("E258: Unable to send to client"));
16493 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495 rettv->vval.v_number = 0;
16496#else
16497 rettv->vval.v_number = -1;
16498#endif
16499}
16500
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501 static void
16502f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016503 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016505{
16506 char_u *r = NULL;
16507
16508#ifdef FEAT_CLIENTSERVER
16509# ifdef WIN32
16510 r = serverGetVimNames();
16511# else
16512 make_connection();
16513 if (X_DISPLAY != NULL)
16514 r = serverGetVimNames(X_DISPLAY);
16515# endif
16516#endif
16517 rettv->v_type = VAR_STRING;
16518 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519}
16520
16521/*
16522 * "setbufvar()" function
16523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016525f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016526 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016527 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528{
16529 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016531 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016532 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 char_u nbuf[NUMBUFLEN];
16534
16535 if (check_restricted() || check_secure())
16536 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016537 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16538 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016539 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 varp = &argvars[2];
16541
16542 if (buf != NULL && varname != NULL && varp != NULL)
16543 {
16544 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546
16547 if (*varname == '&')
16548 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016549 long numval;
16550 char_u *strval;
16551 int error = FALSE;
16552
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 numval = get_tv_number_chk(varp, &error);
16555 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 if (!error && strval != NULL)
16557 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 }
16559 else
16560 {
16561 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16562 if (bufvarname != NULL)
16563 {
16564 STRCPY(bufvarname, "b:");
16565 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016566 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 vim_free(bufvarname);
16568 }
16569 }
16570
16571 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574}
16575
16576/*
16577 * "setcmdpos()" function
16578 */
16579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016580f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016581 typval_T *argvars;
16582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016584 int pos = (int)get_tv_number(&argvars[0]) - 1;
16585
16586 if (pos >= 0)
16587 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588}
16589
16590/*
16591 * "setline()" function
16592 */
16593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016594f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 typval_T *argvars;
16596 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016597{
16598 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016599 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016600 list_T *l = NULL;
16601 listitem_T *li = NULL;
16602 long added = 0;
16603 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016605 lnum = get_tv_lnum(&argvars[0]);
16606 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016608 l = argvars[1].vval.v_list;
16609 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016611 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016612 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016613
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016614 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016615 for (;;)
16616 {
16617 if (l != NULL)
16618 {
16619 /* list argument, get next string */
16620 if (li == NULL)
16621 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016622 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016623 li = li->li_next;
16624 }
16625
16626 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016627 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016628 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016629
16630 /* When coming here from Insert mode, sync undo, so that this can be
16631 * undone separately from what was previously inserted. */
16632 if (u_sync_once == 2)
16633 {
16634 u_sync_once = 1; /* notify that u_sync() was called */
16635 u_sync(TRUE);
16636 }
16637
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016638 if (lnum <= curbuf->b_ml.ml_line_count)
16639 {
16640 /* existing line, replace it */
16641 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16642 {
16643 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016644 if (lnum == curwin->w_cursor.lnum)
16645 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016646 rettv->vval.v_number = 0; /* OK */
16647 }
16648 }
16649 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16650 {
16651 /* lnum is one past the last line, append the line */
16652 ++added;
16653 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16654 rettv->vval.v_number = 0; /* OK */
16655 }
16656
16657 if (l == NULL) /* only one string argument */
16658 break;
16659 ++lnum;
16660 }
16661
16662 if (added > 0)
16663 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664}
16665
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016666static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16667
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016669 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016670 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016671 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016672set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016673 win_T *wp UNUSED;
16674 typval_T *list_arg UNUSED;
16675 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016676 typval_T *rettv;
16677{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016678#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016679 char_u *act;
16680 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016681#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016682
Bram Moolenaar2641f772005-03-25 21:58:17 +000016683 rettv->vval.v_number = -1;
16684
16685#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016686 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016687 EMSG(_(e_listreq));
16688 else
16689 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016690 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016691
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016692 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016693 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016694 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016695 if (act == NULL)
16696 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016697 if (*act == 'a' || *act == 'r')
16698 action = *act;
16699 }
16700
Bram Moolenaar81484f42012-12-05 15:16:47 +010016701 if (l != NULL && set_errorlist(wp, l, action,
16702 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016703 rettv->vval.v_number = 0;
16704 }
16705#endif
16706}
16707
16708/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016709 * "setloclist()" function
16710 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016711 static void
16712f_setloclist(argvars, rettv)
16713 typval_T *argvars;
16714 typval_T *rettv;
16715{
16716 win_T *win;
16717
16718 rettv->vval.v_number = -1;
16719
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016720 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016721 if (win != NULL)
16722 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16723}
16724
16725/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016726 * "setmatches()" function
16727 */
16728 static void
16729f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016730 typval_T *argvars UNUSED;
16731 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016732{
16733#ifdef FEAT_SEARCH_EXTRA
16734 list_T *l;
16735 listitem_T *li;
16736 dict_T *d;
16737
16738 rettv->vval.v_number = -1;
16739 if (argvars[0].v_type != VAR_LIST)
16740 {
16741 EMSG(_(e_listreq));
16742 return;
16743 }
16744 if ((l = argvars[0].vval.v_list) != NULL)
16745 {
16746
16747 /* To some extent make sure that we are dealing with a list from
16748 * "getmatches()". */
16749 li = l->lv_first;
16750 while (li != NULL)
16751 {
16752 if (li->li_tv.v_type != VAR_DICT
16753 || (d = li->li_tv.vval.v_dict) == NULL)
16754 {
16755 EMSG(_(e_invarg));
16756 return;
16757 }
16758 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16759 && dict_find(d, (char_u *)"pattern", -1) != NULL
16760 && dict_find(d, (char_u *)"priority", -1) != NULL
16761 && dict_find(d, (char_u *)"id", -1) != NULL))
16762 {
16763 EMSG(_(e_invarg));
16764 return;
16765 }
16766 li = li->li_next;
16767 }
16768
16769 clear_matches(curwin);
16770 li = l->lv_first;
16771 while (li != NULL)
16772 {
16773 d = li->li_tv.vval.v_dict;
16774 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16775 get_dict_string(d, (char_u *)"pattern", FALSE),
16776 (int)get_dict_number(d, (char_u *)"priority"),
16777 (int)get_dict_number(d, (char_u *)"id"));
16778 li = li->li_next;
16779 }
16780 rettv->vval.v_number = 0;
16781 }
16782#endif
16783}
16784
16785/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016786 * "setpos()" function
16787 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016788 static void
16789f_setpos(argvars, rettv)
16790 typval_T *argvars;
16791 typval_T *rettv;
16792{
16793 pos_T pos;
16794 int fnum;
16795 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016796 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016797
Bram Moolenaar08250432008-02-13 11:42:46 +000016798 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016799 name = get_tv_string_chk(argvars);
16800 if (name != NULL)
16801 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020016802 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016803 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016804 if (--pos.col < 0)
16805 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016806 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016807 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016808 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016809 if (fnum == curbuf->b_fnum)
16810 {
16811 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016812 if (curswant >= 0)
16813 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016814 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016815 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016816 }
16817 else
16818 EMSG(_(e_invarg));
16819 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016820 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16821 {
16822 /* set mark */
16823 if (setmark_pos(name[1], &pos, fnum) == OK)
16824 rettv->vval.v_number = 0;
16825 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016826 else
16827 EMSG(_(e_invarg));
16828 }
16829 }
16830}
16831
16832/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016833 * "setqflist()" function
16834 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016835 static void
16836f_setqflist(argvars, rettv)
16837 typval_T *argvars;
16838 typval_T *rettv;
16839{
16840 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16841}
16842
16843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 * "setreg()" function
16845 */
16846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016847f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016848 typval_T *argvars;
16849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850{
16851 int regname;
16852 char_u *strregname;
16853 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855 int append;
16856 char_u yank_type;
16857 long block_len;
16858
16859 block_len = -1;
16860 yank_type = MAUTO;
16861 append = FALSE;
16862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016863 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016864 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 if (strregname == NULL)
16867 return; /* type error; errmsg already given */
16868 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 if (regname == 0 || regname == '@')
16870 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016872 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016874 stropt = get_tv_string_chk(&argvars[2]);
16875 if (stropt == NULL)
16876 return; /* type error */
16877 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 switch (*stropt)
16879 {
16880 case 'a': case 'A': /* append */
16881 append = TRUE;
16882 break;
16883 case 'v': case 'c': /* character-wise selection */
16884 yank_type = MCHAR;
16885 break;
16886 case 'V': case 'l': /* line-wise selection */
16887 yank_type = MLINE;
16888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 case 'b': case Ctrl_V: /* block-wise selection */
16890 yank_type = MBLOCK;
16891 if (VIM_ISDIGIT(stropt[1]))
16892 {
16893 ++stropt;
16894 block_len = getdigits(&stropt) - 1;
16895 --stropt;
16896 }
16897 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 }
16899 }
16900
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016901 if (argvars[1].v_type == VAR_LIST)
16902 {
16903 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016904 char_u **allocval;
16905 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016906 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016907 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016908 int len = argvars[1].vval.v_list->lv_len;
16909 listitem_T *li;
16910
Bram Moolenaar7d647822014-04-05 21:28:56 +020016911 /* First half: use for pointers to result lines; second half: use for
16912 * pointers to allocated copies. */
16913 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016914 if (lstval == NULL)
16915 return;
16916 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020016917 allocval = lstval + len + 2;
16918 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016919
16920 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
16921 li = li->li_next)
16922 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016923 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016924 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020016925 goto free_lstval;
16926 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016927 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020016928 /* Need to make a copy, next get_tv_string_buf_chk() will
16929 * overwrite the string. */
16930 strval = vim_strsave(buf);
16931 if (strval == NULL)
16932 goto free_lstval;
16933 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016934 }
16935 *curval++ = strval;
16936 }
16937 *curval++ = NULL;
16938
16939 write_reg_contents_lst(regname, lstval, -1,
16940 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020016941free_lstval:
16942 while (curallocval > allocval)
16943 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016944 vim_free(lstval);
16945 }
16946 else
16947 {
16948 strval = get_tv_string_chk(&argvars[1]);
16949 if (strval == NULL)
16950 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020016953 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016954 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955}
16956
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016957/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016958 * "settabvar()" function
16959 */
16960 static void
16961f_settabvar(argvars, rettv)
16962 typval_T *argvars;
16963 typval_T *rettv;
16964{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016965#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016966 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016967 tabpage_T *tp;
16968#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016969 char_u *varname, *tabvarname;
16970 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016971
16972 rettv->vval.v_number = 0;
16973
16974 if (check_restricted() || check_secure())
16975 return;
16976
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016977#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016978 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016979#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016980 varname = get_tv_string_chk(&argvars[1]);
16981 varp = &argvars[2];
16982
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016983 if (varname != NULL && varp != NULL
16984#ifdef FEAT_WINDOWS
16985 && tp != NULL
16986#endif
16987 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016988 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016989#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016990 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016991 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016992#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016993
16994 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16995 if (tabvarname != NULL)
16996 {
16997 STRCPY(tabvarname, "t:");
16998 STRCPY(tabvarname + 2, varname);
16999 set_var(tabvarname, varp, TRUE);
17000 vim_free(tabvarname);
17001 }
17002
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017003#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017004 /* Restore current tabpage */
17005 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017006 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017007#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017008 }
17009}
17010
17011/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017012 * "settabwinvar()" function
17013 */
17014 static void
17015f_settabwinvar(argvars, rettv)
17016 typval_T *argvars;
17017 typval_T *rettv;
17018{
17019 setwinvar(argvars, rettv, 1);
17020}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021
17022/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017023 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017027 typval_T *argvars;
17028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017029{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017030 setwinvar(argvars, rettv, 0);
17031}
17032
17033/*
17034 * "setwinvar()" and "settabwinvar()" functions
17035 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017036
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017037 static void
17038setwinvar(argvars, rettv, off)
17039 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017040 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017041 int off;
17042{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 win_T *win;
17044#ifdef FEAT_WINDOWS
17045 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017046 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047#endif
17048 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017051 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052
17053 if (check_restricted() || check_secure())
17054 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017055
17056#ifdef FEAT_WINDOWS
17057 if (off == 1)
17058 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17059 else
17060 tp = curtab;
17061#endif
17062 win = find_win_by_nr(&argvars[off], tp);
17063 varname = get_tv_string_chk(&argvars[off + 1]);
17064 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065
17066 if (win != NULL && varname != NULL && varp != NULL)
17067 {
17068#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017069 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017070 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071#endif
17072
17073 if (*varname == '&')
17074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 long numval;
17076 char_u *strval;
17077 int error = FALSE;
17078
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 numval = get_tv_number_chk(varp, &error);
17081 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 if (!error && strval != NULL)
17083 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 }
17085 else
17086 {
17087 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17088 if (winvarname != NULL)
17089 {
17090 STRCPY(winvarname, "w:");
17091 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017092 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093 vim_free(winvarname);
17094 }
17095 }
17096
17097#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017098 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099#endif
17100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101}
17102
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017103#ifdef FEAT_CRYPT
17104/*
17105 * "sha256({string})" function
17106 */
17107 static void
17108f_sha256(argvars, rettv)
17109 typval_T *argvars;
17110 typval_T *rettv;
17111{
17112 char_u *p;
17113
17114 p = get_tv_string(&argvars[0]);
17115 rettv->vval.v_string = vim_strsave(
17116 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17117 rettv->v_type = VAR_STRING;
17118}
17119#endif /* FEAT_CRYPT */
17120
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017122 * "shellescape({string})" function
17123 */
17124 static void
17125f_shellescape(argvars, rettv)
17126 typval_T *argvars;
17127 typval_T *rettv;
17128{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017129 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017130 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017131 rettv->v_type = VAR_STRING;
17132}
17133
17134/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017135 * shiftwidth() function
17136 */
17137 static void
17138f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017139 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017140 typval_T *rettv;
17141{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017142 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017143}
17144
17145/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 */
17148 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017150 typval_T *argvars;
17151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154
Bram Moolenaar0d660222005-01-07 21:51:51 +000017155 p = get_tv_string(&argvars[0]);
17156 rettv->vval.v_string = vim_strsave(p);
17157 simplify_filename(rettv->vval.v_string); /* simplify in place */
17158 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159}
17160
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017161#ifdef FEAT_FLOAT
17162/*
17163 * "sin()" function
17164 */
17165 static void
17166f_sin(argvars, rettv)
17167 typval_T *argvars;
17168 typval_T *rettv;
17169{
17170 float_T f;
17171
17172 rettv->v_type = VAR_FLOAT;
17173 if (get_float_arg(argvars, &f) == OK)
17174 rettv->vval.v_float = sin(f);
17175 else
17176 rettv->vval.v_float = 0.0;
17177}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017178
17179/*
17180 * "sinh()" function
17181 */
17182 static void
17183f_sinh(argvars, rettv)
17184 typval_T *argvars;
17185 typval_T *rettv;
17186{
17187 float_T f;
17188
17189 rettv->v_type = VAR_FLOAT;
17190 if (get_float_arg(argvars, &f) == OK)
17191 rettv->vval.v_float = sinh(f);
17192 else
17193 rettv->vval.v_float = 0.0;
17194}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017195#endif
17196
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197static int
17198#ifdef __BORLANDC__
17199 _RTLENTRYF
17200#endif
17201 item_compare __ARGS((const void *s1, const void *s2));
17202static int
17203#ifdef __BORLANDC__
17204 _RTLENTRYF
17205#endif
17206 item_compare2 __ARGS((const void *s1, const void *s2));
17207
17208static int item_compare_ic;
17209static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017210static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017211static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017212static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017213#define ITEM_COMPARE_FAIL 999
17214
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017216 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218 static int
17219#ifdef __BORLANDC__
17220_RTLENTRYF
17221#endif
17222item_compare(s1, s2)
17223 const void *s1;
17224 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226 char_u *p1, *p2;
17227 char_u *tofree1, *tofree2;
17228 int res;
17229 char_u numbuf1[NUMBUFLEN];
17230 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017232 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17233 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017234 if (p1 == NULL)
17235 p1 = (char_u *)"";
17236 if (p2 == NULL)
17237 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017238 if (item_compare_ic)
17239 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 res = STRCMP(p1, p2);
17242 vim_free(tofree1);
17243 vim_free(tofree2);
17244 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245}
17246
17247 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017248#ifdef __BORLANDC__
17249_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251item_compare2(s1, s2)
17252 const void *s1;
17253 const void *s2;
17254{
17255 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017256 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017257 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017260 /* shortcut after failure in previous call; compare all items equal */
17261 if (item_compare_func_err)
17262 return 0;
17263
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017264 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17265 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017266 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17267 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017268
17269 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017270 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017271 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17272 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017273 clear_tv(&argv[0]);
17274 clear_tv(&argv[1]);
17275
17276 if (res == FAIL)
17277 res = ITEM_COMPARE_FAIL;
17278 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017279 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17280 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017281 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017282 clear_tv(&rettv);
17283 return res;
17284}
17285
17286/*
17287 * "sort({list})" function
17288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017290do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017291 typval_T *argvars;
17292 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017293 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294{
Bram Moolenaar33570922005-01-25 22:26:29 +000017295 list_T *l;
17296 listitem_T *li;
17297 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298 long len;
17299 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017302 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 else
17304 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017305 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017306 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017307 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308 return;
17309 rettv->vval.v_list = l;
17310 rettv->v_type = VAR_LIST;
17311 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 len = list_len(l);
17314 if (len <= 1)
17315 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 item_compare_ic = FALSE;
17318 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017319 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 if (argvars[1].v_type != VAR_UNKNOWN)
17321 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017322 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017323 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017324 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017325 else
17326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017327 int error = FALSE;
17328
17329 i = get_tv_number_chk(&argvars[1], &error);
17330 if (error)
17331 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017332 if (i == 1)
17333 item_compare_ic = TRUE;
17334 else
17335 item_compare_func = get_tv_string(&argvars[1]);
17336 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017337
17338 if (argvars[2].v_type != VAR_UNKNOWN)
17339 {
17340 /* optional third argument: {dict} */
17341 if (argvars[2].v_type != VAR_DICT)
17342 {
17343 EMSG(_(e_dictreq));
17344 return;
17345 }
17346 item_compare_selfdict = argvars[2].vval.v_dict;
17347 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349
Bram Moolenaar0d660222005-01-07 21:51:51 +000017350 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017351 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017352 if (ptrs == NULL)
17353 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354
Bram Moolenaar327aa022014-03-25 18:24:23 +010017355 i = 0;
17356 if (sort)
17357 {
17358 /* sort(): ptrs will be the list to sort */
17359 for (li = l->lv_first; li != NULL; li = li->li_next)
17360 ptrs[i++] = li;
17361
17362 item_compare_func_err = FALSE;
17363 /* test the compare function */
17364 if (item_compare_func != NULL
17365 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017366 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017367 EMSG(_("E702: Sort compare function failed"));
17368 else
17369 {
17370 /* Sort the array with item pointers. */
17371 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17372 item_compare_func == NULL ? item_compare : item_compare2);
17373
17374 if (!item_compare_func_err)
17375 {
17376 /* Clear the List and append the items in sorted order. */
17377 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17378 l->lv_len = 0;
17379 for (i = 0; i < len; ++i)
17380 list_append(l, ptrs[i]);
17381 }
17382 }
17383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017385 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017386 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17387
17388 /* f_uniq(): ptrs will be a stack of items to remove */
17389 item_compare_func_err = FALSE;
17390 item_compare_func_ptr = item_compare_func
17391 ? item_compare2 : item_compare;
17392
17393 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17394 li = li->li_next)
17395 {
17396 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17397 == 0)
17398 ptrs[i++] = li;
17399 if (item_compare_func_err)
17400 {
17401 EMSG(_("E882: Uniq compare function failed"));
17402 break;
17403 }
17404 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017406 if (!item_compare_func_err)
17407 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017408 while (--i >= 0)
17409 {
17410 li = ptrs[i]->li_next;
17411 ptrs[i]->li_next = li->li_next;
17412 if (li->li_next != NULL)
17413 li->li_next->li_prev = ptrs[i];
17414 else
17415 l->lv_last = ptrs[i];
17416 list_fix_watch(l, li);
17417 listitem_free(li);
17418 l->lv_len--;
17419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421 }
17422
17423 vim_free(ptrs);
17424 }
17425}
17426
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017427/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017428 * "sort({list})" function
17429 */
17430 static void
17431f_sort(argvars, rettv)
17432 typval_T *argvars;
17433 typval_T *rettv;
17434{
17435 do_sort_uniq(argvars, rettv, TRUE);
17436}
17437
17438/*
17439 * "uniq({list})" function
17440 */
17441 static void
17442f_uniq(argvars, rettv)
17443 typval_T *argvars;
17444 typval_T *rettv;
17445{
17446 do_sort_uniq(argvars, rettv, FALSE);
17447}
17448
17449/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017450 * "soundfold({word})" function
17451 */
17452 static void
17453f_soundfold(argvars, rettv)
17454 typval_T *argvars;
17455 typval_T *rettv;
17456{
17457 char_u *s;
17458
17459 rettv->v_type = VAR_STRING;
17460 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017461#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017462 rettv->vval.v_string = eval_soundfold(s);
17463#else
17464 rettv->vval.v_string = vim_strsave(s);
17465#endif
17466}
17467
17468/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017469 * "spellbadword()" function
17470 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017471 static void
17472f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017473 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017474 typval_T *rettv;
17475{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017476 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017477 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017478 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017480 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017481 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017482
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017483#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017484 if (argvars[0].v_type == VAR_UNKNOWN)
17485 {
17486 /* Find the start and length of the badly spelled word. */
17487 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17488 if (len != 0)
17489 word = ml_get_cursor();
17490 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017491 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017492 {
17493 char_u *str = get_tv_string_chk(&argvars[0]);
17494 int capcol = -1;
17495
17496 if (str != NULL)
17497 {
17498 /* Check the argument for spelling. */
17499 while (*str != NUL)
17500 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017501 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017502 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017503 {
17504 word = str;
17505 break;
17506 }
17507 str += len;
17508 }
17509 }
17510 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017511#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017512
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017513 list_append_string(rettv->vval.v_list, word, len);
17514 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017515 attr == HLF_SPB ? "bad" :
17516 attr == HLF_SPR ? "rare" :
17517 attr == HLF_SPL ? "local" :
17518 attr == HLF_SPC ? "caps" :
17519 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017520}
17521
17522/*
17523 * "spellsuggest()" function
17524 */
17525 static void
17526f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017527 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017528 typval_T *rettv;
17529{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017530#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017531 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017532 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017533 int maxcount;
17534 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017535 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017536 listitem_T *li;
17537 int need_capital = FALSE;
17538#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017539
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017540 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017541 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017542
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017543#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017544 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017545 {
17546 str = get_tv_string(&argvars[0]);
17547 if (argvars[1].v_type != VAR_UNKNOWN)
17548 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017549 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017550 if (maxcount <= 0)
17551 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017552 if (argvars[2].v_type != VAR_UNKNOWN)
17553 {
17554 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17555 if (typeerr)
17556 return;
17557 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017558 }
17559 else
17560 maxcount = 25;
17561
Bram Moolenaar4770d092006-01-12 23:22:24 +000017562 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017563
17564 for (i = 0; i < ga.ga_len; ++i)
17565 {
17566 str = ((char_u **)ga.ga_data)[i];
17567
17568 li = listitem_alloc();
17569 if (li == NULL)
17570 vim_free(str);
17571 else
17572 {
17573 li->li_tv.v_type = VAR_STRING;
17574 li->li_tv.v_lock = 0;
17575 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017576 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017577 }
17578 }
17579 ga_clear(&ga);
17580 }
17581#endif
17582}
17583
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017585f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017586 typval_T *argvars;
17587 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588{
17589 char_u *str;
17590 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017591 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017592 regmatch_T regmatch;
17593 char_u patbuf[NUMBUFLEN];
17594 char_u *save_cpo;
17595 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017597 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017598 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017599
17600 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17601 save_cpo = p_cpo;
17602 p_cpo = (char_u *)"";
17603
17604 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017605 if (argvars[1].v_type != VAR_UNKNOWN)
17606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017607 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17608 if (pat == NULL)
17609 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017611 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017612 }
17613 if (pat == NULL || *pat == NUL)
17614 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017616 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017618 if (typeerr)
17619 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17622 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017624 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017625 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017627 if (*str == NUL)
17628 match = FALSE; /* empty item at the end */
17629 else
17630 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017631 if (match)
17632 end = regmatch.startp[0];
17633 else
17634 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017635 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17636 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017638 if (list_append_string(rettv->vval.v_list, str,
17639 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017640 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017641 }
17642 if (!match)
17643 break;
17644 /* Advance to just after the match. */
17645 if (regmatch.endp[0] > str)
17646 col = 0;
17647 else
17648 {
17649 /* Don't get stuck at the same match. */
17650#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017651 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017652#else
17653 col = 1;
17654#endif
17655 }
17656 str = regmatch.endp[0];
17657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658
Bram Moolenaar473de612013-06-08 18:19:48 +020017659 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661
Bram Moolenaar0d660222005-01-07 21:51:51 +000017662 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663}
17664
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017665#ifdef FEAT_FLOAT
17666/*
17667 * "sqrt()" function
17668 */
17669 static void
17670f_sqrt(argvars, rettv)
17671 typval_T *argvars;
17672 typval_T *rettv;
17673{
17674 float_T f;
17675
17676 rettv->v_type = VAR_FLOAT;
17677 if (get_float_arg(argvars, &f) == OK)
17678 rettv->vval.v_float = sqrt(f);
17679 else
17680 rettv->vval.v_float = 0.0;
17681}
17682
17683/*
17684 * "str2float()" function
17685 */
17686 static void
17687f_str2float(argvars, rettv)
17688 typval_T *argvars;
17689 typval_T *rettv;
17690{
17691 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17692
17693 if (*p == '+')
17694 p = skipwhite(p + 1);
17695 (void)string2float(p, &rettv->vval.v_float);
17696 rettv->v_type = VAR_FLOAT;
17697}
17698#endif
17699
Bram Moolenaar2c932302006-03-18 21:42:09 +000017700/*
17701 * "str2nr()" function
17702 */
17703 static void
17704f_str2nr(argvars, rettv)
17705 typval_T *argvars;
17706 typval_T *rettv;
17707{
17708 int base = 10;
17709 char_u *p;
17710 long n;
17711
17712 if (argvars[1].v_type != VAR_UNKNOWN)
17713 {
17714 base = get_tv_number(&argvars[1]);
17715 if (base != 8 && base != 10 && base != 16)
17716 {
17717 EMSG(_(e_invarg));
17718 return;
17719 }
17720 }
17721
17722 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017723 if (*p == '+')
17724 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017725 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17726 rettv->vval.v_number = n;
17727}
17728
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729#ifdef HAVE_STRFTIME
17730/*
17731 * "strftime({format}[, {time}])" function
17732 */
17733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017734f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017735 typval_T *argvars;
17736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737{
17738 char_u result_buf[256];
17739 struct tm *curtime;
17740 time_t seconds;
17741 char_u *p;
17742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017743 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017745 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017746 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 seconds = time(NULL);
17748 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 curtime = localtime(&seconds);
17751 /* MSVC returns NULL for an invalid value of seconds. */
17752 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754 else
17755 {
17756# ifdef FEAT_MBYTE
17757 vimconv_T conv;
17758 char_u *enc;
17759
17760 conv.vc_type = CONV_NONE;
17761 enc = enc_locale();
17762 convert_setup(&conv, p_enc, enc);
17763 if (conv.vc_type != CONV_NONE)
17764 p = string_convert(&conv, p, NULL);
17765# endif
17766 if (p != NULL)
17767 (void)strftime((char *)result_buf, sizeof(result_buf),
17768 (char *)p, curtime);
17769 else
17770 result_buf[0] = NUL;
17771
17772# ifdef FEAT_MBYTE
17773 if (conv.vc_type != CONV_NONE)
17774 vim_free(p);
17775 convert_setup(&conv, enc, p_enc);
17776 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 else
17779# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017780 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781
17782# ifdef FEAT_MBYTE
17783 /* Release conversion descriptors */
17784 convert_setup(&conv, NULL, NULL);
17785 vim_free(enc);
17786# endif
17787 }
17788}
17789#endif
17790
17791/*
17792 * "stridx()" function
17793 */
17794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017795f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017796 typval_T *argvars;
17797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798{
17799 char_u buf[NUMBUFLEN];
17800 char_u *needle;
17801 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017802 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017804 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017806 needle = get_tv_string_chk(&argvars[1]);
17807 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017808 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809 if (needle == NULL || haystack == NULL)
17810 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811
Bram Moolenaar33570922005-01-25 22:26:29 +000017812 if (argvars[2].v_type != VAR_UNKNOWN)
17813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 int error = FALSE;
17815
17816 start_idx = get_tv_number_chk(&argvars[2], &error);
17817 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017818 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017819 if (start_idx >= 0)
17820 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017821 }
17822
17823 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17824 if (pos != NULL)
17825 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826}
17827
17828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017829 * "string()" function
17830 */
17831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017832f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017833 typval_T *argvars;
17834 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017835{
17836 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017837 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017840 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017841 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017842 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844}
17845
17846/*
17847 * "strlen()" function
17848 */
17849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017850f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017851 typval_T *argvars;
17852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017854 rettv->vval.v_number = (varnumber_T)(STRLEN(
17855 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856}
17857
17858/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017859 * "strchars()" function
17860 */
17861 static void
17862f_strchars(argvars, rettv)
17863 typval_T *argvars;
17864 typval_T *rettv;
17865{
17866 char_u *s = get_tv_string(&argvars[0]);
17867#ifdef FEAT_MBYTE
17868 varnumber_T len = 0;
17869
17870 while (*s != NUL)
17871 {
17872 mb_cptr2char_adv(&s);
17873 ++len;
17874 }
17875 rettv->vval.v_number = len;
17876#else
17877 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17878#endif
17879}
17880
17881/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017882 * "strdisplaywidth()" function
17883 */
17884 static void
17885f_strdisplaywidth(argvars, rettv)
17886 typval_T *argvars;
17887 typval_T *rettv;
17888{
17889 char_u *s = get_tv_string(&argvars[0]);
17890 int col = 0;
17891
17892 if (argvars[1].v_type != VAR_UNKNOWN)
17893 col = get_tv_number(&argvars[1]);
17894
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017895 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017896}
17897
17898/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017899 * "strwidth()" function
17900 */
17901 static void
17902f_strwidth(argvars, rettv)
17903 typval_T *argvars;
17904 typval_T *rettv;
17905{
17906 char_u *s = get_tv_string(&argvars[0]);
17907
17908 rettv->vval.v_number = (varnumber_T)(
17909#ifdef FEAT_MBYTE
17910 mb_string2cells(s, -1)
17911#else
17912 STRLEN(s)
17913#endif
17914 );
17915}
17916
17917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 * "strpart()" function
17919 */
17920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017921f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017922 typval_T *argvars;
17923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924{
17925 char_u *p;
17926 int n;
17927 int len;
17928 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017929 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932 slen = (int)STRLEN(p);
17933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934 n = get_tv_number_chk(&argvars[1], &error);
17935 if (error)
17936 len = 0;
17937 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 else
17940 len = slen - n; /* default len: all bytes that are available. */
17941
17942 /*
17943 * Only return the overlap between the specified part and the actual
17944 * string.
17945 */
17946 if (n < 0)
17947 {
17948 len += n;
17949 n = 0;
17950 }
17951 else if (n > slen)
17952 n = slen;
17953 if (len < 0)
17954 len = 0;
17955 else if (n + len > slen)
17956 len = slen - n;
17957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958 rettv->v_type = VAR_STRING;
17959 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960}
17961
17962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017963 * "strridx()" function
17964 */
17965 static void
17966f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017967 typval_T *argvars;
17968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017969{
17970 char_u buf[NUMBUFLEN];
17971 char_u *needle;
17972 char_u *haystack;
17973 char_u *rest;
17974 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017975 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017976
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017977 needle = get_tv_string_chk(&argvars[1]);
17978 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017979
17980 rettv->vval.v_number = -1;
17981 if (needle == NULL || haystack == NULL)
17982 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017983
17984 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017985 if (argvars[2].v_type != VAR_UNKNOWN)
17986 {
17987 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017988 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017989 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017990 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017991 }
17992 else
17993 end_idx = haystack_len;
17994
Bram Moolenaar0d660222005-01-07 21:51:51 +000017995 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017996 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017998 lastmatch = haystack + end_idx;
17999 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018000 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018001 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018002 for (rest = haystack; *rest != '\0'; ++rest)
18003 {
18004 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018005 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 break;
18007 lastmatch = rest;
18008 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018009 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018010
18011 if (lastmatch == NULL)
18012 rettv->vval.v_number = -1;
18013 else
18014 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18015}
18016
18017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 * "strtrans()" function
18019 */
18020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018021f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018022 typval_T *argvars;
18023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025 rettv->v_type = VAR_STRING;
18026 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027}
18028
18029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018030 * "submatch()" function
18031 */
18032 static void
18033f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018034 typval_T *argvars;
18035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036{
Bram Moolenaar41571762014-04-02 19:00:58 +020018037 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018038 int no;
18039 int retList = 0;
18040
18041 no = (int)get_tv_number_chk(&argvars[0], &error);
18042 if (error)
18043 return;
18044 error = FALSE;
18045 if (argvars[1].v_type != VAR_UNKNOWN)
18046 retList = get_tv_number_chk(&argvars[1], &error);
18047 if (error)
18048 return;
18049
18050 if (retList == 0)
18051 {
18052 rettv->v_type = VAR_STRING;
18053 rettv->vval.v_string = reg_submatch(no);
18054 }
18055 else
18056 {
18057 rettv->v_type = VAR_LIST;
18058 rettv->vval.v_list = reg_submatch_list(no);
18059 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018060}
18061
18062/*
18063 * "substitute()" function
18064 */
18065 static void
18066f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018067 typval_T *argvars;
18068 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018069{
18070 char_u patbuf[NUMBUFLEN];
18071 char_u subbuf[NUMBUFLEN];
18072 char_u flagsbuf[NUMBUFLEN];
18073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018074 char_u *str = get_tv_string_chk(&argvars[0]);
18075 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18076 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18077 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18078
Bram Moolenaar0d660222005-01-07 21:51:51 +000018079 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018080 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18081 rettv->vval.v_string = NULL;
18082 else
18083 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018084}
18085
18086/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018087 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018090f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018091 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093{
18094 int id = 0;
18095#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018096 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 long col;
18098 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018099 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018101 lnum = get_tv_lnum(argvars); /* -1 on type error */
18102 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18103 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018105 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018106 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018107 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108#endif
18109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018110 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111}
18112
18113/*
18114 * "synIDattr(id, what [, mode])" function
18115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018117f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120{
18121 char_u *p = NULL;
18122#ifdef FEAT_SYN_HL
18123 int id;
18124 char_u *what;
18125 char_u *mode;
18126 char_u modebuf[NUMBUFLEN];
18127 int modec;
18128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129 id = get_tv_number(&argvars[0]);
18130 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018135 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 modec = 0; /* replace invalid with current */
18137 }
18138 else
18139 {
18140#ifdef FEAT_GUI
18141 if (gui.in_use)
18142 modec = 'g';
18143 else
18144#endif
18145 if (t_colors > 1)
18146 modec = 'c';
18147 else
18148 modec = 't';
18149 }
18150
18151
18152 switch (TOLOWER_ASC(what[0]))
18153 {
18154 case 'b':
18155 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18156 p = highlight_color(id, what, modec);
18157 else /* bold */
18158 p = highlight_has_attr(id, HL_BOLD, modec);
18159 break;
18160
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018161 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 p = highlight_color(id, what, modec);
18163 break;
18164
18165 case 'i':
18166 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18167 p = highlight_has_attr(id, HL_INVERSE, modec);
18168 else /* italic */
18169 p = highlight_has_attr(id, HL_ITALIC, modec);
18170 break;
18171
18172 case 'n': /* name */
18173 p = get_highlight_name(NULL, id - 1);
18174 break;
18175
18176 case 'r': /* reverse */
18177 p = highlight_has_attr(id, HL_INVERSE, modec);
18178 break;
18179
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018180 case 's':
18181 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18182 p = highlight_color(id, what, modec);
18183 else /* standout */
18184 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 break;
18186
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018187 case 'u':
18188 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18189 /* underline */
18190 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18191 else
18192 /* undercurl */
18193 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 break;
18195 }
18196
18197 if (p != NULL)
18198 p = vim_strsave(p);
18199#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 rettv->v_type = VAR_STRING;
18201 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202}
18203
18204/*
18205 * "synIDtrans(id)" function
18206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018208f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018209 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211{
18212 int id;
18213
18214#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018215 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
18217 if (id > 0)
18218 id = syn_get_final_id(id);
18219 else
18220#endif
18221 id = 0;
18222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018223 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224}
18225
18226/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018227 * "synconcealed(lnum, col)" function
18228 */
18229 static void
18230f_synconcealed(argvars, rettv)
18231 typval_T *argvars UNUSED;
18232 typval_T *rettv;
18233{
18234#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18235 long lnum;
18236 long col;
18237 int syntax_flags = 0;
18238 int cchar;
18239 int matchid = 0;
18240 char_u str[NUMBUFLEN];
18241#endif
18242
18243 rettv->v_type = VAR_LIST;
18244 rettv->vval.v_list = NULL;
18245
18246#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18247 lnum = get_tv_lnum(argvars); /* -1 on type error */
18248 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18249
18250 vim_memset(str, NUL, sizeof(str));
18251
18252 if (rettv_list_alloc(rettv) != FAIL)
18253 {
18254 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18255 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18256 && curwin->w_p_cole > 0)
18257 {
18258 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18259 syntax_flags = get_syntax_info(&matchid);
18260
18261 /* get the conceal character */
18262 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18263 {
18264 cchar = syn_get_sub_char();
18265 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18266 cchar = lcs_conceal;
18267 if (cchar != NUL)
18268 {
18269# ifdef FEAT_MBYTE
18270 if (has_mbyte)
18271 (*mb_char2bytes)(cchar, str);
18272 else
18273# endif
18274 str[0] = cchar;
18275 }
18276 }
18277 }
18278
18279 list_append_number(rettv->vval.v_list,
18280 (syntax_flags & HL_CONCEAL) != 0);
18281 /* -1 to auto-determine strlen */
18282 list_append_string(rettv->vval.v_list, str, -1);
18283 list_append_number(rettv->vval.v_list, matchid);
18284 }
18285#endif
18286}
18287
18288/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018289 * "synstack(lnum, col)" function
18290 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018291 static void
18292f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018293 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018294 typval_T *rettv;
18295{
18296#ifdef FEAT_SYN_HL
18297 long lnum;
18298 long col;
18299 int i;
18300 int id;
18301#endif
18302
18303 rettv->v_type = VAR_LIST;
18304 rettv->vval.v_list = NULL;
18305
18306#ifdef FEAT_SYN_HL
18307 lnum = get_tv_lnum(argvars); /* -1 on type error */
18308 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18309
18310 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018311 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018312 && rettv_list_alloc(rettv) != FAIL)
18313 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018314 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018315 for (i = 0; ; ++i)
18316 {
18317 id = syn_get_stack_item(i);
18318 if (id < 0)
18319 break;
18320 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18321 break;
18322 }
18323 }
18324#endif
18325}
18326
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018328get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018329 typval_T *argvars;
18330 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018331 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018333 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018335 char_u *infile = NULL;
18336 char_u buf[NUMBUFLEN];
18337 int err = FALSE;
18338 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018339 list_T *list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018341 rettv->v_type = VAR_STRING;
18342 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018343 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018344 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018345
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018346 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018347 {
18348 /*
18349 * Write the string to a temp file, to be used for input of the shell
18350 * command.
18351 */
18352 if ((infile = vim_tempname('i')) == NULL)
18353 {
18354 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018355 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018356 }
18357
18358 fd = mch_fopen((char *)infile, WRITEBIN);
18359 if (fd == NULL)
18360 {
18361 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018362 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018363 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018364 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018365 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018366 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18367 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018368 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018369 else
18370 {
18371 p = get_tv_string_buf_chk(&argvars[1], buf);
18372 if (p == NULL)
18373 {
18374 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018375 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018376 }
18377 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18378 err = TRUE;
18379 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018380 if (fclose(fd) != 0)
18381 err = TRUE;
18382 if (err)
18383 {
18384 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018385 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018386 }
18387 }
18388
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018389 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018391 int len;
18392 listitem_T *li;
18393 char_u *s = NULL;
18394 char_u *start;
18395 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018396 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018398 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18399 SHELL_SILENT | SHELL_COOKED, &len);
18400 if (res == NULL)
18401 goto errret;
18402
18403 list = list_alloc();
18404 if (list == NULL)
18405 goto errret;
18406
18407 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018409 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018410 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018411 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018412 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018413
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018414 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018415 if (s == NULL)
18416 goto errret;
18417
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018418 for (p = s; start < end; ++p, ++start)
18419 *p = *start == NUL ? NL : *start;
18420 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018421
18422 li = listitem_alloc();
18423 if (li == NULL)
18424 {
18425 vim_free(s);
18426 goto errret;
18427 }
18428 li->li_tv.v_type = VAR_STRING;
18429 li->li_tv.vval.v_string = s;
18430 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018432
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018433 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018434 rettv->v_type = VAR_LIST;
18435 rettv->vval.v_list = list;
18436 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018438 else
18439 {
18440 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18441 SHELL_SILENT | SHELL_COOKED, NULL);
18442#ifdef USE_CR
18443 /* translate <CR> into <NL> */
18444 if (res != NULL)
18445 {
18446 char_u *s;
18447
18448 for (s = res; *s; ++s)
18449 {
18450 if (*s == CAR)
18451 *s = NL;
18452 }
18453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018454#else
18455# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018456 /* translate <CR><NL> into <NL> */
18457 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018459 char_u *s, *d;
18460
18461 d = res;
18462 for (s = res; *s; ++s)
18463 {
18464 if (s[0] == CAR && s[1] == NL)
18465 ++s;
18466 *d++ = *s;
18467 }
18468 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470# endif
18471#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018472 rettv->vval.v_string = res;
18473 res = NULL;
18474 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018475
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018476errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018477 if (infile != NULL)
18478 {
18479 mch_remove(infile);
18480 vim_free(infile);
18481 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018482 if (res != NULL)
18483 vim_free(res);
18484 if (list != NULL)
18485 list_free(list, TRUE);
18486}
18487
18488/*
18489 * "system()" function
18490 */
18491 static void
18492f_system(argvars, rettv)
18493 typval_T *argvars;
18494 typval_T *rettv;
18495{
18496 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18497}
18498
18499/*
18500 * "systemlist()" function
18501 */
18502 static void
18503f_systemlist(argvars, rettv)
18504 typval_T *argvars;
18505 typval_T *rettv;
18506{
18507 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508}
18509
18510/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018511 * "tabpagebuflist()" function
18512 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018513 static void
18514f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018515 typval_T *argvars UNUSED;
18516 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018517{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018518#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018519 tabpage_T *tp;
18520 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018521
18522 if (argvars[0].v_type == VAR_UNKNOWN)
18523 wp = firstwin;
18524 else
18525 {
18526 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18527 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018528 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018529 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018530 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018531 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018532 for (; wp != NULL; wp = wp->w_next)
18533 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018534 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018535 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018536 }
18537#endif
18538}
18539
18540
18541/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018542 * "tabpagenr()" function
18543 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018544 static void
18545f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018546 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018547 typval_T *rettv;
18548{
18549 int nr = 1;
18550#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018551 char_u *arg;
18552
18553 if (argvars[0].v_type != VAR_UNKNOWN)
18554 {
18555 arg = get_tv_string_chk(&argvars[0]);
18556 nr = 0;
18557 if (arg != NULL)
18558 {
18559 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018560 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018561 else
18562 EMSG2(_(e_invexpr2), arg);
18563 }
18564 }
18565 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018566 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018567#endif
18568 rettv->vval.v_number = nr;
18569}
18570
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018571
18572#ifdef FEAT_WINDOWS
18573static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18574
18575/*
18576 * Common code for tabpagewinnr() and winnr().
18577 */
18578 static int
18579get_winnr(tp, argvar)
18580 tabpage_T *tp;
18581 typval_T *argvar;
18582{
18583 win_T *twin;
18584 int nr = 1;
18585 win_T *wp;
18586 char_u *arg;
18587
18588 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18589 if (argvar->v_type != VAR_UNKNOWN)
18590 {
18591 arg = get_tv_string_chk(argvar);
18592 if (arg == NULL)
18593 nr = 0; /* type error; errmsg already given */
18594 else if (STRCMP(arg, "$") == 0)
18595 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18596 else if (STRCMP(arg, "#") == 0)
18597 {
18598 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18599 if (twin == NULL)
18600 nr = 0;
18601 }
18602 else
18603 {
18604 EMSG2(_(e_invexpr2), arg);
18605 nr = 0;
18606 }
18607 }
18608
18609 if (nr > 0)
18610 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18611 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018612 {
18613 if (wp == NULL)
18614 {
18615 /* didn't find it in this tabpage */
18616 nr = 0;
18617 break;
18618 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018619 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018620 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018621 return nr;
18622}
18623#endif
18624
18625/*
18626 * "tabpagewinnr()" function
18627 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018628 static void
18629f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018630 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018631 typval_T *rettv;
18632{
18633 int nr = 1;
18634#ifdef FEAT_WINDOWS
18635 tabpage_T *tp;
18636
18637 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18638 if (tp == NULL)
18639 nr = 0;
18640 else
18641 nr = get_winnr(tp, &argvars[1]);
18642#endif
18643 rettv->vval.v_number = nr;
18644}
18645
18646
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018647/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018648 * "tagfiles()" function
18649 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018650 static void
18651f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018652 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018653 typval_T *rettv;
18654{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018655 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018656 tagname_T tn;
18657 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018658
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018659 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018660 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018661 fname = alloc(MAXPATHL);
18662 if (fname == NULL)
18663 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018664
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018665 for (first = TRUE; ; first = FALSE)
18666 if (get_tagfname(&tn, first, fname) == FAIL
18667 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018668 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018669 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018670 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018671}
18672
18673/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018674 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018675 */
18676 static void
18677f_taglist(argvars, rettv)
18678 typval_T *argvars;
18679 typval_T *rettv;
18680{
18681 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018682
18683 tag_pattern = get_tv_string(&argvars[0]);
18684
18685 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018686 if (*tag_pattern == NUL)
18687 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018688
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018689 if (rettv_list_alloc(rettv) == OK)
18690 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018691}
18692
18693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 * "tempname()" function
18695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018698 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700{
18701 static int x = 'A';
18702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 rettv->v_type = VAR_STRING;
18704 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705
18706 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18707 * names. Skip 'I' and 'O', they are used for shell redirection. */
18708 do
18709 {
18710 if (x == 'Z')
18711 x = '0';
18712 else if (x == '9')
18713 x = 'A';
18714 else
18715 {
18716#ifdef EBCDIC
18717 if (x == 'I')
18718 x = 'J';
18719 else if (x == 'R')
18720 x = 'S';
18721 else
18722#endif
18723 ++x;
18724 }
18725 } while (x == 'I' || x == 'O');
18726}
18727
18728/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018729 * "test(list)" function: Just checking the walls...
18730 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018731 static void
18732f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018733 typval_T *argvars UNUSED;
18734 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018735{
18736 /* Used for unit testing. Change the code below to your liking. */
18737#if 0
18738 listitem_T *li;
18739 list_T *l;
18740 char_u *bad, *good;
18741
18742 if (argvars[0].v_type != VAR_LIST)
18743 return;
18744 l = argvars[0].vval.v_list;
18745 if (l == NULL)
18746 return;
18747 li = l->lv_first;
18748 if (li == NULL)
18749 return;
18750 bad = get_tv_string(&li->li_tv);
18751 li = li->li_next;
18752 if (li == NULL)
18753 return;
18754 good = get_tv_string(&li->li_tv);
18755 rettv->vval.v_number = test_edit_score(bad, good);
18756#endif
18757}
18758
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018759#ifdef FEAT_FLOAT
18760/*
18761 * "tan()" function
18762 */
18763 static void
18764f_tan(argvars, rettv)
18765 typval_T *argvars;
18766 typval_T *rettv;
18767{
18768 float_T f;
18769
18770 rettv->v_type = VAR_FLOAT;
18771 if (get_float_arg(argvars, &f) == OK)
18772 rettv->vval.v_float = tan(f);
18773 else
18774 rettv->vval.v_float = 0.0;
18775}
18776
18777/*
18778 * "tanh()" function
18779 */
18780 static void
18781f_tanh(argvars, rettv)
18782 typval_T *argvars;
18783 typval_T *rettv;
18784{
18785 float_T f;
18786
18787 rettv->v_type = VAR_FLOAT;
18788 if (get_float_arg(argvars, &f) == OK)
18789 rettv->vval.v_float = tanh(f);
18790 else
18791 rettv->vval.v_float = 0.0;
18792}
18793#endif
18794
Bram Moolenaard52d9742005-08-21 22:20:28 +000018795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 * "tolower(string)" function
18797 */
18798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018799f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018800 typval_T *argvars;
18801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802{
18803 char_u *p;
18804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 p = vim_strsave(get_tv_string(&argvars[0]));
18806 rettv->v_type = VAR_STRING;
18807 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808
18809 if (p != NULL)
18810 while (*p != NUL)
18811 {
18812#ifdef FEAT_MBYTE
18813 int l;
18814
18815 if (enc_utf8)
18816 {
18817 int c, lc;
18818
18819 c = utf_ptr2char(p);
18820 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018821 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 /* TODO: reallocate string when byte count changes. */
18823 if (utf_char2len(lc) == l)
18824 utf_char2bytes(lc, p);
18825 p += l;
18826 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018827 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 p += l; /* skip multi-byte character */
18829 else
18830#endif
18831 {
18832 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18833 ++p;
18834 }
18835 }
18836}
18837
18838/*
18839 * "toupper(string)" function
18840 */
18841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018843 typval_T *argvars;
18844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018846 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018847 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848}
18849
18850/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018851 * "tr(string, fromstr, tostr)" function
18852 */
18853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018854f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018855 typval_T *argvars;
18856 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018857{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018858 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018859 char_u *fromstr;
18860 char_u *tostr;
18861 char_u *p;
18862#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018863 int inlen;
18864 int fromlen;
18865 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018866 int idx;
18867 char_u *cpstr;
18868 int cplen;
18869 int first = TRUE;
18870#endif
18871 char_u buf[NUMBUFLEN];
18872 char_u buf2[NUMBUFLEN];
18873 garray_T ga;
18874
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018875 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018876 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18877 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018878
18879 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018880 rettv->v_type = VAR_STRING;
18881 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018882 if (fromstr == NULL || tostr == NULL)
18883 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018884 ga_init2(&ga, (int)sizeof(char), 80);
18885
18886#ifdef FEAT_MBYTE
18887 if (!has_mbyte)
18888#endif
18889 /* not multi-byte: fromstr and tostr must be the same length */
18890 if (STRLEN(fromstr) != STRLEN(tostr))
18891 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018892#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018893error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018894#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018895 EMSG2(_(e_invarg2), fromstr);
18896 ga_clear(&ga);
18897 return;
18898 }
18899
18900 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018901 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018902 {
18903#ifdef FEAT_MBYTE
18904 if (has_mbyte)
18905 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018906 inlen = (*mb_ptr2len)(in_str);
18907 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018908 cplen = inlen;
18909 idx = 0;
18910 for (p = fromstr; *p != NUL; p += fromlen)
18911 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018912 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018913 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018914 {
18915 for (p = tostr; *p != NUL; p += tolen)
18916 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018917 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018918 if (idx-- == 0)
18919 {
18920 cplen = tolen;
18921 cpstr = p;
18922 break;
18923 }
18924 }
18925 if (*p == NUL) /* tostr is shorter than fromstr */
18926 goto error;
18927 break;
18928 }
18929 ++idx;
18930 }
18931
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018932 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018933 {
18934 /* Check that fromstr and tostr have the same number of
18935 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018936 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018937 first = FALSE;
18938 for (p = tostr; *p != NUL; p += tolen)
18939 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018940 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018941 --idx;
18942 }
18943 if (idx != 0)
18944 goto error;
18945 }
18946
18947 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018948 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018949 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018950
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018951 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018952 }
18953 else
18954#endif
18955 {
18956 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018957 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018958 if (p != NULL)
18959 ga_append(&ga, tostr[p - fromstr]);
18960 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018961 ga_append(&ga, *in_str);
18962 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018963 }
18964 }
18965
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018966 /* add a terminating NUL */
18967 ga_grow(&ga, 1);
18968 ga_append(&ga, NUL);
18969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018970 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018971}
18972
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018973#ifdef FEAT_FLOAT
18974/*
18975 * "trunc({float})" function
18976 */
18977 static void
18978f_trunc(argvars, rettv)
18979 typval_T *argvars;
18980 typval_T *rettv;
18981{
18982 float_T f;
18983
18984 rettv->v_type = VAR_FLOAT;
18985 if (get_float_arg(argvars, &f) == OK)
18986 /* trunc() is not in C90, use floor() or ceil() instead. */
18987 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18988 else
18989 rettv->vval.v_float = 0.0;
18990}
18991#endif
18992
Bram Moolenaar8299df92004-07-10 09:47:34 +000018993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 * "type(expr)" function
18995 */
18996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018997f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018998 typval_T *argvars;
18999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019001 int n;
19002
19003 switch (argvars[0].v_type)
19004 {
19005 case VAR_NUMBER: n = 0; break;
19006 case VAR_STRING: n = 1; break;
19007 case VAR_FUNC: n = 2; break;
19008 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019009 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019010#ifdef FEAT_FLOAT
19011 case VAR_FLOAT: n = 5; break;
19012#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019013 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19014 }
19015 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016}
19017
19018/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019019 * "undofile(name)" function
19020 */
19021 static void
19022f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019023 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019024 typval_T *rettv;
19025{
19026 rettv->v_type = VAR_STRING;
19027#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019028 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019029 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019030
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019031 if (*fname == NUL)
19032 {
19033 /* If there is no file name there will be no undo file. */
19034 rettv->vval.v_string = NULL;
19035 }
19036 else
19037 {
19038 char_u *ffname = FullName_save(fname, FALSE);
19039
19040 if (ffname != NULL)
19041 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19042 vim_free(ffname);
19043 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019044 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019045#else
19046 rettv->vval.v_string = NULL;
19047#endif
19048}
19049
19050/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019051 * "undotree()" function
19052 */
19053 static void
19054f_undotree(argvars, rettv)
19055 typval_T *argvars UNUSED;
19056 typval_T *rettv;
19057{
19058 if (rettv_dict_alloc(rettv) == OK)
19059 {
19060 dict_T *dict = rettv->vval.v_dict;
19061 list_T *list;
19062
Bram Moolenaar730cde92010-06-27 05:18:54 +020019063 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019064 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019065 dict_add_nr_str(dict, "save_last",
19066 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019067 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19068 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019069 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019070
19071 list = list_alloc();
19072 if (list != NULL)
19073 {
19074 u_eval_tree(curbuf->b_u_oldhead, list);
19075 dict_add_list(dict, "entries", list);
19076 }
19077 }
19078}
19079
19080/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019081 * "values(dict)" function
19082 */
19083 static void
19084f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 typval_T *argvars;
19086 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019087{
19088 dict_list(argvars, rettv, 1);
19089}
19090
19091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 * "virtcol(string)" function
19093 */
19094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 typval_T *argvars;
19097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098{
19099 colnr_T vcol = 0;
19100 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019101 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019103 fp = var2fpos(&argvars[0], FALSE, &fnum);
19104 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19105 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106 {
19107 getvvcol(curwin, fp, NULL, NULL, &vcol);
19108 ++vcol;
19109 }
19110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019111 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112}
19113
19114/*
19115 * "visualmode()" function
19116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019118f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019119 typval_T *argvars UNUSED;
19120 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 char_u str[2];
19123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 str[0] = curbuf->b_visual_mode_eval;
19126 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128
19129 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019130 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132}
19133
19134/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019135 * "wildmenumode()" function
19136 */
19137 static void
19138f_wildmenumode(argvars, rettv)
19139 typval_T *argvars UNUSED;
19140 typval_T *rettv UNUSED;
19141{
19142#ifdef FEAT_WILDMENU
19143 if (wild_menu_showing)
19144 rettv->vval.v_number = 1;
19145#endif
19146}
19147
19148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149 * "winbufnr(nr)" function
19150 */
19151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019152f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 typval_T *argvars;
19154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155{
19156 win_T *wp;
19157
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019158 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163}
19164
19165/*
19166 * "wincol()" function
19167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019169f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172{
19173 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175}
19176
19177/*
19178 * "winheight(nr)" function
19179 */
19180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 typval_T *argvars;
19183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184{
19185 win_T *wp;
19186
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019187 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019191 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192}
19193
19194/*
19195 * "winline()" function
19196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019198f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019199 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201{
19202 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204}
19205
19206/*
19207 * "winnr()" function
19208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019210f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019211 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213{
19214 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019215
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019217 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220}
19221
19222/*
19223 * "winrestcmd()" function
19224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019227 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229{
19230#ifdef FEAT_WINDOWS
19231 win_T *wp;
19232 int winnr = 1;
19233 garray_T ga;
19234 char_u buf[50];
19235
19236 ga_init2(&ga, (int)sizeof(char), 70);
19237 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19238 {
19239 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19240 ga_concat(&ga, buf);
19241# ifdef FEAT_VERTSPLIT
19242 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19243 ga_concat(&ga, buf);
19244# endif
19245 ++winnr;
19246 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019247 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019249 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019253 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254}
19255
19256/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019257 * "winrestview()" function
19258 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019259 static void
19260f_winrestview(argvars, rettv)
19261 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019262 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019263{
19264 dict_T *dict;
19265
19266 if (argvars[0].v_type != VAR_DICT
19267 || (dict = argvars[0].vval.v_dict) == NULL)
19268 EMSG(_(e_invarg));
19269 else
19270 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019271 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19272 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19273 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19274 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019275#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019276 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19277 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019278#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019279 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19280 {
19281 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19282 curwin->w_set_curswant = FALSE;
19283 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019284
Bram Moolenaar82c25852014-05-28 16:47:16 +020019285 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19286 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019287#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019288 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19289 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019290#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019291 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19292 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19293 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19294 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019295
19296 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019297 win_new_height(curwin, curwin->w_height);
19298# ifdef FEAT_VERTSPLIT
19299 win_new_width(curwin, W_WIDTH(curwin));
19300# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019301 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019302
19303 if (curwin->w_topline == 0)
19304 curwin->w_topline = 1;
19305 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19306 curwin->w_topline = curbuf->b_ml.ml_line_count;
19307#ifdef FEAT_DIFF
19308 check_topfill(curwin, TRUE);
19309#endif
19310 }
19311}
19312
19313/*
19314 * "winsaveview()" function
19315 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019316 static void
19317f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019318 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019319 typval_T *rettv;
19320{
19321 dict_T *dict;
19322
Bram Moolenaara800b422010-06-27 01:15:55 +020019323 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019324 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019325 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019326
19327 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19328 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19329#ifdef FEAT_VIRTUALEDIT
19330 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19331#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019332 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019333 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19334
19335 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19336#ifdef FEAT_DIFF
19337 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19338#endif
19339 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19340 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19341}
19342
19343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 * "winwidth(nr)" function
19345 */
19346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019347f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019348 typval_T *argvars;
19349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019350{
19351 win_T *wp;
19352
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019353 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356 else
19357#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019358 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019360 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361#endif
19362}
19363
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019365 * Write list of strings to file
19366 */
19367 static int
19368write_list(fd, list, binary)
19369 FILE *fd;
19370 list_T *list;
19371 int binary;
19372{
19373 listitem_T *li;
19374 int c;
19375 int ret = OK;
19376 char_u *s;
19377
19378 for (li = list->lv_first; li != NULL; li = li->li_next)
19379 {
19380 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19381 {
19382 if (*s == '\n')
19383 c = putc(NUL, fd);
19384 else
19385 c = putc(*s, fd);
19386 if (c == EOF)
19387 {
19388 ret = FAIL;
19389 break;
19390 }
19391 }
19392 if (!binary || li->li_next != NULL)
19393 if (putc('\n', fd) == EOF)
19394 {
19395 ret = FAIL;
19396 break;
19397 }
19398 if (ret == FAIL)
19399 {
19400 EMSG(_(e_write));
19401 break;
19402 }
19403 }
19404 return ret;
19405}
19406
19407/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019408 * "writefile()" function
19409 */
19410 static void
19411f_writefile(argvars, rettv)
19412 typval_T *argvars;
19413 typval_T *rettv;
19414{
19415 int binary = FALSE;
19416 char_u *fname;
19417 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019418 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019419
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019420 if (check_restricted() || check_secure())
19421 return;
19422
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019423 if (argvars[0].v_type != VAR_LIST)
19424 {
19425 EMSG2(_(e_listarg), "writefile()");
19426 return;
19427 }
19428 if (argvars[0].vval.v_list == NULL)
19429 return;
19430
19431 if (argvars[2].v_type != VAR_UNKNOWN
19432 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19433 binary = TRUE;
19434
19435 /* Always open the file in binary mode, library functions have a mind of
19436 * their own about CR-LF conversion. */
19437 fname = get_tv_string(&argvars[1]);
19438 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19439 {
19440 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19441 ret = -1;
19442 }
19443 else
19444 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019445 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19446 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019447 fclose(fd);
19448 }
19449
19450 rettv->vval.v_number = ret;
19451}
19452
19453/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019454 * "xor(expr, expr)" function
19455 */
19456 static void
19457f_xor(argvars, rettv)
19458 typval_T *argvars;
19459 typval_T *rettv;
19460{
19461 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19462 ^ get_tv_number_chk(&argvars[1], NULL);
19463}
19464
19465
19466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019468 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 */
19470 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019471var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019472 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019473 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019474 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019476 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019478 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479
Bram Moolenaara5525202006-03-02 22:52:09 +000019480 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019481 if (varp->v_type == VAR_LIST)
19482 {
19483 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019484 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019485 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019486 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019487
19488 l = varp->vval.v_list;
19489 if (l == NULL)
19490 return NULL;
19491
19492 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019493 pos.lnum = list_find_nr(l, 0L, &error);
19494 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019495 return NULL; /* invalid line number */
19496
19497 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019498 pos.col = list_find_nr(l, 1L, &error);
19499 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019500 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019501 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019502
19503 /* We accept "$" for the column number: last column. */
19504 li = list_find(l, 1L);
19505 if (li != NULL && li->li_tv.v_type == VAR_STRING
19506 && li->li_tv.vval.v_string != NULL
19507 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19508 pos.col = len + 1;
19509
Bram Moolenaara5525202006-03-02 22:52:09 +000019510 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019511 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019512 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019513 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019514
Bram Moolenaara5525202006-03-02 22:52:09 +000019515#ifdef FEAT_VIRTUALEDIT
19516 /* Get the virtual offset. Defaults to zero. */
19517 pos.coladd = list_find_nr(l, 2L, &error);
19518 if (error)
19519 pos.coladd = 0;
19520#endif
19521
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019522 return &pos;
19523 }
19524
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019525 name = get_tv_string_chk(varp);
19526 if (name == NULL)
19527 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019528 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019530 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19531 {
19532 if (VIsual_active)
19533 return &VIsual;
19534 return &curwin->w_cursor;
19535 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019536 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019538 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19540 return NULL;
19541 return pp;
19542 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019543
19544#ifdef FEAT_VIRTUALEDIT
19545 pos.coladd = 0;
19546#endif
19547
Bram Moolenaar477933c2007-07-17 14:32:23 +000019548 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019549 {
19550 pos.col = 0;
19551 if (name[1] == '0') /* "w0": first visible line */
19552 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019553 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019554 pos.lnum = curwin->w_topline;
19555 return &pos;
19556 }
19557 else if (name[1] == '$') /* "w$": last visible line */
19558 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019559 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019560 pos.lnum = curwin->w_botline - 1;
19561 return &pos;
19562 }
19563 }
19564 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019566 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 {
19568 pos.lnum = curbuf->b_ml.ml_line_count;
19569 pos.col = 0;
19570 }
19571 else
19572 {
19573 pos.lnum = curwin->w_cursor.lnum;
19574 pos.col = (colnr_T)STRLEN(ml_get_curline());
19575 }
19576 return &pos;
19577 }
19578 return NULL;
19579}
19580
19581/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019582 * Convert list in "arg" into a position and optional file number.
19583 * When "fnump" is NULL there is no file number, only 3 items.
19584 * Note that the column is passed on as-is, the caller may want to decrement
19585 * it to use 1 for the first column.
19586 * Return FAIL when conversion is not possible, doesn't check the position for
19587 * validity.
19588 */
19589 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019590list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019591 typval_T *arg;
19592 pos_T *posp;
19593 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019594 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019595{
19596 list_T *l = arg->vval.v_list;
19597 long i = 0;
19598 long n;
19599
Bram Moolenaar493c1782014-05-28 14:34:46 +020019600 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19601 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019602 if (arg->v_type != VAR_LIST
19603 || l == NULL
19604 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019605 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019606 return FAIL;
19607
19608 if (fnump != NULL)
19609 {
19610 n = list_find_nr(l, i++, NULL); /* fnum */
19611 if (n < 0)
19612 return FAIL;
19613 if (n == 0)
19614 n = curbuf->b_fnum; /* current buffer */
19615 *fnump = n;
19616 }
19617
19618 n = list_find_nr(l, i++, NULL); /* lnum */
19619 if (n < 0)
19620 return FAIL;
19621 posp->lnum = n;
19622
19623 n = list_find_nr(l, i++, NULL); /* col */
19624 if (n < 0)
19625 return FAIL;
19626 posp->col = n;
19627
19628#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019629 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019630 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019631 posp->coladd = 0;
19632 else
19633 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019634#endif
19635
Bram Moolenaar493c1782014-05-28 14:34:46 +020019636 if (curswantp != NULL)
19637 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19638
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019639 return OK;
19640}
19641
19642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 * Get the length of an environment variable name.
19644 * Advance "arg" to the first character after the name.
19645 * Return 0 for error.
19646 */
19647 static int
19648get_env_len(arg)
19649 char_u **arg;
19650{
19651 char_u *p;
19652 int len;
19653
19654 for (p = *arg; vim_isIDc(*p); ++p)
19655 ;
19656 if (p == *arg) /* no name found */
19657 return 0;
19658
19659 len = (int)(p - *arg);
19660 *arg = p;
19661 return len;
19662}
19663
19664/*
19665 * Get the length of the name of a function or internal variable.
19666 * "arg" is advanced to the first non-white character after the name.
19667 * Return 0 if something is wrong.
19668 */
19669 static int
19670get_id_len(arg)
19671 char_u **arg;
19672{
19673 char_u *p;
19674 int len;
19675
19676 /* Find the end of the name. */
19677 for (p = *arg; eval_isnamec(*p); ++p)
19678 ;
19679 if (p == *arg) /* no name found */
19680 return 0;
19681
19682 len = (int)(p - *arg);
19683 *arg = skipwhite(p);
19684
19685 return len;
19686}
19687
19688/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019689 * Get the length of the name of a variable or function.
19690 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019692 * Return -1 if curly braces expansion failed.
19693 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 * If the name contains 'magic' {}'s, expand them and return the
19695 * expanded name in an allocated string via 'alias' - caller must free.
19696 */
19697 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019698get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019699 char_u **arg;
19700 char_u **alias;
19701 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019702 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
19704 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 char_u *p;
19706 char_u *expr_start;
19707 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708
19709 *alias = NULL; /* default to no alias */
19710
19711 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19712 && (*arg)[2] == (int)KE_SNR)
19713 {
19714 /* hard coded <SNR>, already translated */
19715 *arg += 3;
19716 return get_id_len(arg) + 3;
19717 }
19718 len = eval_fname_script(*arg);
19719 if (len > 0)
19720 {
19721 /* literal "<SID>", "s:" or "<SNR>" */
19722 *arg += len;
19723 }
19724
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019728 p = find_name_end(*arg, &expr_start, &expr_end,
19729 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 if (expr_start != NULL)
19731 {
19732 char_u *temp_string;
19733
19734 if (!evaluate)
19735 {
19736 len += (int)(p - *arg);
19737 *arg = skipwhite(p);
19738 return len;
19739 }
19740
19741 /*
19742 * Include any <SID> etc in the expanded string:
19743 * Thus the -len here.
19744 */
19745 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19746 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019747 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 *alias = temp_string;
19749 *arg = skipwhite(p);
19750 return (int)STRLEN(temp_string);
19751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752
19753 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019754 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 EMSG2(_(e_invexpr2), *arg);
19756
19757 return len;
19758}
19759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760/*
19761 * Find the end of a variable or function name, taking care of magic braces.
19762 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19763 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019764 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 * Return a pointer to just after the name. Equal to "arg" if there is no
19766 * valid name.
19767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019769find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 char_u *arg;
19771 char_u **expr_start;
19772 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019773 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019775 int mb_nest = 0;
19776 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 char_u *p;
19778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781 *expr_start = NULL;
19782 *expr_end = NULL;
19783 }
19784
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019785 /* Quick check for valid starting character. */
19786 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19787 return arg;
19788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019789 for (p = arg; *p != NUL
19790 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019791 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019792 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019794 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019796 if (*p == '\'')
19797 {
19798 /* skip over 'string' to avoid counting [ and ] inside it. */
19799 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19800 ;
19801 if (*p == NUL)
19802 break;
19803 }
19804 else if (*p == '"')
19805 {
19806 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19807 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19808 if (*p == '\\' && p[1] != NUL)
19809 ++p;
19810 if (*p == NUL)
19811 break;
19812 }
19813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019816 if (*p == '[')
19817 ++br_nest;
19818 else if (*p == ']')
19819 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 if (*p == '{')
19825 {
19826 mb_nest++;
19827 if (expr_start != NULL && *expr_start == NULL)
19828 *expr_start = p;
19829 }
19830 else if (*p == '}')
19831 {
19832 mb_nest--;
19833 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19834 *expr_end = p;
19835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 }
19838
19839 return p;
19840}
19841
19842/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019843 * Expands out the 'magic' {}'s in a variable/function name.
19844 * Note that this can call itself recursively, to deal with
19845 * constructs like foo{bar}{baz}{bam}
19846 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19847 * "in_start" ^
19848 * "expr_start" ^
19849 * "expr_end" ^
19850 * "in_end" ^
19851 *
19852 * Returns a new allocated string, which the caller must free.
19853 * Returns NULL for failure.
19854 */
19855 static char_u *
19856make_expanded_name(in_start, expr_start, expr_end, in_end)
19857 char_u *in_start;
19858 char_u *expr_start;
19859 char_u *expr_end;
19860 char_u *in_end;
19861{
19862 char_u c1;
19863 char_u *retval = NULL;
19864 char_u *temp_result;
19865 char_u *nextcmd = NULL;
19866
19867 if (expr_end == NULL || in_end == NULL)
19868 return NULL;
19869 *expr_start = NUL;
19870 *expr_end = NUL;
19871 c1 = *in_end;
19872 *in_end = NUL;
19873
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019874 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019875 if (temp_result != NULL && nextcmd == NULL)
19876 {
19877 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19878 + (in_end - expr_end) + 1));
19879 if (retval != NULL)
19880 {
19881 STRCPY(retval, in_start);
19882 STRCAT(retval, temp_result);
19883 STRCAT(retval, expr_end + 1);
19884 }
19885 }
19886 vim_free(temp_result);
19887
19888 *in_end = c1; /* put char back for error messages */
19889 *expr_start = '{';
19890 *expr_end = '}';
19891
19892 if (retval != NULL)
19893 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019894 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019895 if (expr_start != NULL)
19896 {
19897 /* Further expansion! */
19898 temp_result = make_expanded_name(retval, expr_start,
19899 expr_end, temp_result);
19900 vim_free(retval);
19901 retval = temp_result;
19902 }
19903 }
19904
19905 return retval;
19906}
19907
19908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019910 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 */
19912 static int
19913eval_isnamec(c)
19914 int c;
19915{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019916 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19917}
19918
19919/*
19920 * Return TRUE if character "c" can be used as the first character in a
19921 * variable or function name (excluding '{' and '}').
19922 */
19923 static int
19924eval_isnamec1(c)
19925 int c;
19926{
19927 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928}
19929
19930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931 * Set number v: variable to "val".
19932 */
19933 void
19934set_vim_var_nr(idx, val)
19935 int idx;
19936 long val;
19937{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019938 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939}
19940
19941/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019942 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 */
19944 long
19945get_vim_var_nr(idx)
19946 int idx;
19947{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019948 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949}
19950
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019951/*
19952 * Get string v: variable value. Uses a static buffer, can only be used once.
19953 */
19954 char_u *
19955get_vim_var_str(idx)
19956 int idx;
19957{
19958 return get_tv_string(&vimvars[idx].vv_tv);
19959}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019960
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019962 * Get List v: variable value. Caller must take care of reference count when
19963 * needed.
19964 */
19965 list_T *
19966get_vim_var_list(idx)
19967 int idx;
19968{
19969 return vimvars[idx].vv_list;
19970}
19971
19972/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019973 * Set v:char to character "c".
19974 */
19975 void
19976set_vim_var_char(c)
19977 int c;
19978{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019979 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019980
19981#ifdef FEAT_MBYTE
19982 if (has_mbyte)
19983 buf[(*mb_char2bytes)(c, buf)] = NUL;
19984 else
19985#endif
19986 {
19987 buf[0] = c;
19988 buf[1] = NUL;
19989 }
19990 set_vim_var_string(VV_CHAR, buf, -1);
19991}
19992
19993/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019994 * Set v:count to "count" and v:count1 to "count1".
19995 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 */
19997 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019998set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 long count;
20000 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020001 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020003 if (set_prevcount)
20004 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020005 vimvars[VV_COUNT].vv_nr = count;
20006 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007}
20008
20009/*
20010 * Set string v: variable to a copy of "val".
20011 */
20012 void
20013set_vim_var_string(idx, val, len)
20014 int idx;
20015 char_u *val;
20016 int len; /* length of "val" to use or -1 (whole string) */
20017{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020018 /* Need to do this (at least) once, since we can't initialize a union.
20019 * Will always be invoked when "v:progname" is set. */
20020 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20021
Bram Moolenaare9a41262005-01-15 22:18:47 +000020022 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020024 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020026 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020028 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029}
20030
20031/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020032 * Set List v: variable to "val".
20033 */
20034 void
20035set_vim_var_list(idx, val)
20036 int idx;
20037 list_T *val;
20038{
20039 list_unref(vimvars[idx].vv_list);
20040 vimvars[idx].vv_list = val;
20041 if (val != NULL)
20042 ++val->lv_refcount;
20043}
20044
20045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 * Set v:register if needed.
20047 */
20048 void
20049set_reg_var(c)
20050 int c;
20051{
20052 char_u regname;
20053
20054 if (c == 0 || c == ' ')
20055 regname = '"';
20056 else
20057 regname = c;
20058 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020059 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 set_vim_var_string(VV_REG, &regname, 1);
20061}
20062
20063/*
20064 * Get or set v:exception. If "oldval" == NULL, return the current value.
20065 * Otherwise, restore the value to "oldval" and return NULL.
20066 * Must always be called in pairs to save and restore v:exception! Does not
20067 * take care of memory allocations.
20068 */
20069 char_u *
20070v_exception(oldval)
20071 char_u *oldval;
20072{
20073 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020074 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075
Bram Moolenaare9a41262005-01-15 22:18:47 +000020076 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 return NULL;
20078}
20079
20080/*
20081 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20082 * Otherwise, restore the value to "oldval" and return NULL.
20083 * Must always be called in pairs to save and restore v:throwpoint! Does not
20084 * take care of memory allocations.
20085 */
20086 char_u *
20087v_throwpoint(oldval)
20088 char_u *oldval;
20089{
20090 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020091 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092
Bram Moolenaare9a41262005-01-15 22:18:47 +000020093 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 return NULL;
20095}
20096
20097#if defined(FEAT_AUTOCMD) || defined(PROTO)
20098/*
20099 * Set v:cmdarg.
20100 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20101 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20102 * Must always be called in pairs!
20103 */
20104 char_u *
20105set_cmdarg(eap, oldarg)
20106 exarg_T *eap;
20107 char_u *oldarg;
20108{
20109 char_u *oldval;
20110 char_u *newval;
20111 unsigned len;
20112
Bram Moolenaare9a41262005-01-15 22:18:47 +000020113 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020114 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020116 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020117 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020118 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 }
20120
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020121 if (eap->force_bin == FORCE_BIN)
20122 len = 6;
20123 else if (eap->force_bin == FORCE_NOBIN)
20124 len = 8;
20125 else
20126 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020127
20128 if (eap->read_edit)
20129 len += 7;
20130
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020131 if (eap->force_ff != 0)
20132 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20133# ifdef FEAT_MBYTE
20134 if (eap->force_enc != 0)
20135 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020136 if (eap->bad_char != 0)
20137 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020138# endif
20139
20140 newval = alloc(len + 1);
20141 if (newval == NULL)
20142 return NULL;
20143
20144 if (eap->force_bin == FORCE_BIN)
20145 sprintf((char *)newval, " ++bin");
20146 else if (eap->force_bin == FORCE_NOBIN)
20147 sprintf((char *)newval, " ++nobin");
20148 else
20149 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020150
20151 if (eap->read_edit)
20152 STRCAT(newval, " ++edit");
20153
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020154 if (eap->force_ff != 0)
20155 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20156 eap->cmd + eap->force_ff);
20157# ifdef FEAT_MBYTE
20158 if (eap->force_enc != 0)
20159 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20160 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020161 if (eap->bad_char == BAD_KEEP)
20162 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20163 else if (eap->bad_char == BAD_DROP)
20164 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20165 else if (eap->bad_char != 0)
20166 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020167# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020168 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020169 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170}
20171#endif
20172
20173/*
20174 * Get the value of internal variable "name".
20175 * Return OK or FAIL.
20176 */
20177 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020178get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 char_u *name;
20180 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020182 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020183 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184{
20185 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 typval_T *tv = NULL;
20187 typval_T atv;
20188 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190
20191 /* truncate the name, so that we can use strcmp() */
20192 cc = name[len];
20193 name[len] = NUL;
20194
20195 /*
20196 * Check for "b:changedtick".
20197 */
20198 if (STRCMP(name, "b:changedtick") == 0)
20199 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020200 atv.v_type = VAR_NUMBER;
20201 atv.vval.v_number = curbuf->b_changedtick;
20202 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 }
20204
20205 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 * Check for user-defined variables.
20207 */
20208 else
20209 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020210 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 }
20214
Bram Moolenaare9a41262005-01-15 22:18:47 +000020215 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020217 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 ret = FAIL;
20220 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020221 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020222 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223
20224 name[len] = cc;
20225
20226 return ret;
20227}
20228
20229/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020230 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20231 * Also handle function call with Funcref variable: func(expr)
20232 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20233 */
20234 static int
20235handle_subscript(arg, rettv, evaluate, verbose)
20236 char_u **arg;
20237 typval_T *rettv;
20238 int evaluate; /* do more than finding the end */
20239 int verbose; /* give error messages */
20240{
20241 int ret = OK;
20242 dict_T *selfdict = NULL;
20243 char_u *s;
20244 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020245 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020246
20247 while (ret == OK
20248 && (**arg == '['
20249 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020250 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020251 && !vim_iswhite(*(*arg - 1)))
20252 {
20253 if (**arg == '(')
20254 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020255 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020256 if (evaluate)
20257 {
20258 functv = *rettv;
20259 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020260
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020261 /* Invoke the function. Recursive! */
20262 s = functv.vval.v_string;
20263 }
20264 else
20265 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020266 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020267 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20268 &len, evaluate, selfdict);
20269
20270 /* Clear the funcref afterwards, so that deleting it while
20271 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020272 if (evaluate)
20273 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020274
20275 /* Stop the expression evaluation when immediately aborting on
20276 * error, or when an interrupt occurred or an exception was thrown
20277 * but not caught. */
20278 if (aborting())
20279 {
20280 if (ret == OK)
20281 clear_tv(rettv);
20282 ret = FAIL;
20283 }
20284 dict_unref(selfdict);
20285 selfdict = NULL;
20286 }
20287 else /* **arg == '[' || **arg == '.' */
20288 {
20289 dict_unref(selfdict);
20290 if (rettv->v_type == VAR_DICT)
20291 {
20292 selfdict = rettv->vval.v_dict;
20293 if (selfdict != NULL)
20294 ++selfdict->dv_refcount;
20295 }
20296 else
20297 selfdict = NULL;
20298 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20299 {
20300 clear_tv(rettv);
20301 ret = FAIL;
20302 }
20303 }
20304 }
20305 dict_unref(selfdict);
20306 return ret;
20307}
20308
20309/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020310 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020311 * value).
20312 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020313 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020314alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020315{
Bram Moolenaar33570922005-01-25 22:26:29 +000020316 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020317}
20318
20319/*
20320 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 * The string "s" must have been allocated, it is consumed.
20322 * Return NULL for out of memory, the variable otherwise.
20323 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020325alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 char_u *s;
20327{
Bram Moolenaar33570922005-01-25 22:26:29 +000020328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020330 rettv = alloc_tv();
20331 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020333 rettv->v_type = VAR_STRING;
20334 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 }
20336 else
20337 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020338 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339}
20340
20341/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020342 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020344 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020345free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347{
20348 if (varp != NULL)
20349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020350 switch (varp->v_type)
20351 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020352 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020353 func_unref(varp->vval.v_string);
20354 /*FALLTHROUGH*/
20355 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020356 vim_free(varp->vval.v_string);
20357 break;
20358 case VAR_LIST:
20359 list_unref(varp->vval.v_list);
20360 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020361 case VAR_DICT:
20362 dict_unref(varp->vval.v_dict);
20363 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020364 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020365#ifdef FEAT_FLOAT
20366 case VAR_FLOAT:
20367#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020368 case VAR_UNKNOWN:
20369 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020370 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020371 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020372 break;
20373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 vim_free(varp);
20375 }
20376}
20377
20378/*
20379 * Free the memory for a variable value and set the value to NULL or 0.
20380 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020381 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384{
20385 if (varp != NULL)
20386 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020387 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020389 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020390 func_unref(varp->vval.v_string);
20391 /*FALLTHROUGH*/
20392 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020393 vim_free(varp->vval.v_string);
20394 varp->vval.v_string = NULL;
20395 break;
20396 case VAR_LIST:
20397 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020398 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020400 case VAR_DICT:
20401 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020402 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020403 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020404 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020405 varp->vval.v_number = 0;
20406 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020407#ifdef FEAT_FLOAT
20408 case VAR_FLOAT:
20409 varp->vval.v_float = 0.0;
20410 break;
20411#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 case VAR_UNKNOWN:
20413 break;
20414 default:
20415 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020417 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 }
20419}
20420
20421/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 * Set the value of a variable to NULL without freeing items.
20423 */
20424 static void
20425init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020427{
20428 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020429 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020430}
20431
20432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433 * Get the number value of a variable.
20434 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020435 * For incompatible types, return 0.
20436 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20437 * caller of incompatible types: it sets *denote to TRUE if "denote"
20438 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 */
20440 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020441get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020442 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020444 int error = FALSE;
20445
20446 return get_tv_number_chk(varp, &error); /* return 0L on error */
20447}
20448
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020449 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020450get_tv_number_chk(varp, denote)
20451 typval_T *varp;
20452 int *denote;
20453{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020454 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020456 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020458 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020459 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020460#ifdef FEAT_FLOAT
20461 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020462 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020463 break;
20464#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020465 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020466 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020467 break;
20468 case VAR_STRING:
20469 if (varp->vval.v_string != NULL)
20470 vim_str2nr(varp->vval.v_string, NULL, NULL,
20471 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020472 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020473 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020474 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020475 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020476 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020477 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020479 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020480 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020483 if (denote == NULL) /* useful for values that must be unsigned */
20484 n = -1;
20485 else
20486 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020487 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488}
20489
20490/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020491 * Get the lnum from the first argument.
20492 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020493 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 */
20495 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020496get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020497 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498{
Bram Moolenaar33570922005-01-25 22:26:29 +000020499 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 linenr_T lnum;
20501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020502 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 if (lnum == 0) /* no valid number, try using line() */
20504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020505 rettv.v_type = VAR_NUMBER;
20506 f_line(argvars, &rettv);
20507 lnum = rettv.vval.v_number;
20508 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 }
20510 return lnum;
20511}
20512
20513/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020514 * Get the lnum from the first argument.
20515 * Also accepts "$", then "buf" is used.
20516 * Returns 0 on error.
20517 */
20518 static linenr_T
20519get_tv_lnum_buf(argvars, buf)
20520 typval_T *argvars;
20521 buf_T *buf;
20522{
20523 if (argvars[0].v_type == VAR_STRING
20524 && argvars[0].vval.v_string != NULL
20525 && argvars[0].vval.v_string[0] == '$'
20526 && buf != NULL)
20527 return buf->b_ml.ml_line_count;
20528 return get_tv_number_chk(&argvars[0], NULL);
20529}
20530
20531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 * Get the string value of a variable.
20533 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020534 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20535 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536 * If the String variable has never been set, return an empty string.
20537 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020538 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20539 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 */
20541 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020543 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544{
20545 static char_u mybuf[NUMBUFLEN];
20546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020547 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548}
20549
20550 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020552 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020555 char_u *res = get_tv_string_buf_chk(varp, buf);
20556
20557 return res != NULL ? res : (char_u *)"";
20558}
20559
Bram Moolenaar7d647822014-04-05 21:28:56 +020020560/*
20561 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20562 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020563 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020564get_tv_string_chk(varp)
20565 typval_T *varp;
20566{
20567 static char_u mybuf[NUMBUFLEN];
20568
20569 return get_tv_string_buf_chk(varp, mybuf);
20570}
20571
20572 static char_u *
20573get_tv_string_buf_chk(varp, buf)
20574 typval_T *varp;
20575 char_u *buf;
20576{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020577 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020579 case VAR_NUMBER:
20580 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20581 return buf;
20582 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020583 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020584 break;
20585 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020586 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020587 break;
20588 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020589 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020591#ifdef FEAT_FLOAT
20592 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020593 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020594 break;
20595#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020596 case VAR_STRING:
20597 if (varp->vval.v_string != NULL)
20598 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020599 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020600 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020601 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020602 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020604 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605}
20606
20607/*
20608 * Find variable "name" in the list of variables.
20609 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020610 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020611 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020612 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020615find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020618 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020621 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622
Bram Moolenaara7043832005-01-21 11:56:39 +000020623 ht = find_var_ht(name, &varname);
20624 if (htp != NULL)
20625 *htp = ht;
20626 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020628 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629}
20630
20631/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020632 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020633 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020634 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020635 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020636find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020637 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020638 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020639 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020640 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020641{
Bram Moolenaar33570922005-01-25 22:26:29 +000020642 hashitem_T *hi;
20643
20644 if (*varname == NUL)
20645 {
20646 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020647 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020648 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020649 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020650 case 'g': return &globvars_var;
20651 case 'v': return &vimvars_var;
20652 case 'b': return &curbuf->b_bufvar;
20653 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020654#ifdef FEAT_WINDOWS
20655 case 't': return &curtab->tp_winvar;
20656#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020657 case 'l': return current_funccal == NULL
20658 ? NULL : &current_funccal->l_vars_var;
20659 case 'a': return current_funccal == NULL
20660 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020661 }
20662 return NULL;
20663 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020664
20665 hi = hash_find(ht, varname);
20666 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020667 {
20668 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020669 * worked find the variable again. Don't auto-load a script if it was
20670 * loaded already, otherwise it would be loaded every time when
20671 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020672 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020673 {
20674 /* Note: script_autoload() may make "hi" invalid. It must either
20675 * be obtained again or not used. */
20676 if (!script_autoload(varname, FALSE) || aborting())
20677 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020678 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020679 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020680 if (HASHITEM_EMPTY(hi))
20681 return NULL;
20682 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020683 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020684}
20685
20686/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020687 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020688 * Set "varname" to the start of name without ':'.
20689 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020691find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 char_u *name;
20693 char_u **varname;
20694{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020695 hashitem_T *hi;
20696
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 if (name[1] != ':')
20698 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020699 /* The name must not start with a colon or #. */
20700 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 return NULL;
20702 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020703
20704 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020705 hi = hash_find(&compat_hashtab, name);
20706 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020707 return &compat_hashtab;
20708
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020710 return &globvarht; /* global variable */
20711 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 }
20713 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020714 if (*name == 'g') /* global variable */
20715 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020716 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20717 */
20718 if (vim_strchr(name + 2, ':') != NULL
20719 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020720 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020722 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020724 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020725#ifdef FEAT_WINDOWS
20726 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020727 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020728#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020729 if (*name == 'v') /* v: variable */
20730 return &vimvarht;
20731 if (*name == 'a' && current_funccal != NULL) /* function argument */
20732 return &current_funccal->l_avars.dv_hashtab;
20733 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20734 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 if (*name == 's' /* script variable */
20736 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20737 return &SCRIPT_VARS(current_SID);
20738 return NULL;
20739}
20740
20741/*
20742 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020743 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 * Returns NULL when it doesn't exist.
20745 */
20746 char_u *
20747get_var_value(name)
20748 char_u *name;
20749{
Bram Moolenaar33570922005-01-25 22:26:29 +000020750 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020752 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 if (v == NULL)
20754 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020755 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756}
20757
20758/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020759 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 * sourcing this script and when executing functions defined in the script.
20761 */
20762 void
20763new_script_vars(id)
20764 scid_T id;
20765{
Bram Moolenaara7043832005-01-21 11:56:39 +000020766 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020767 hashtab_T *ht;
20768 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020769
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20771 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020772 /* Re-allocating ga_data means that an ht_array pointing to
20773 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020774 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020775 for (i = 1; i <= ga_scripts.ga_len; ++i)
20776 {
20777 ht = &SCRIPT_VARS(i);
20778 if (ht->ht_mask == HT_INIT_SIZE - 1)
20779 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020780 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020781 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020782 }
20783
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 while (ga_scripts.ga_len < id)
20785 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020786 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020787 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020788 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 }
20791 }
20792}
20793
20794/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20796 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 */
20798 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020799init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020800 dict_T *dict;
20801 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020802 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803{
Bram Moolenaar33570922005-01-25 22:26:29 +000020804 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020805 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020806 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020807 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020808 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 dict_var->di_tv.vval.v_dict = dict;
20810 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020811 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020812 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20813 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814}
20815
20816/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020817 * Unreference a dictionary initialized by init_var_dict().
20818 */
20819 void
20820unref_var_dict(dict)
20821 dict_T *dict;
20822{
20823 /* Now the dict needs to be freed if no one else is using it, go back to
20824 * normal reference counting. */
20825 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20826 dict_unref(dict);
20827}
20828
20829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020831 * Frees all allocated variables and the value they contain.
20832 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 */
20834 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020835vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020836 hashtab_T *ht;
20837{
20838 vars_clear_ext(ht, TRUE);
20839}
20840
20841/*
20842 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20843 */
20844 static void
20845vars_clear_ext(ht, free_val)
20846 hashtab_T *ht;
20847 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848{
Bram Moolenaara7043832005-01-21 11:56:39 +000020849 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020850 hashitem_T *hi;
20851 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852
Bram Moolenaar33570922005-01-25 22:26:29 +000020853 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020854 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020855 for (hi = ht->ht_array; todo > 0; ++hi)
20856 {
20857 if (!HASHITEM_EMPTY(hi))
20858 {
20859 --todo;
20860
Bram Moolenaar33570922005-01-25 22:26:29 +000020861 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020862 * ht_array might change then. hash_clear() takes care of it
20863 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020864 v = HI2DI(hi);
20865 if (free_val)
20866 clear_tv(&v->di_tv);
20867 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20868 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020869 }
20870 }
20871 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020872 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873}
20874
Bram Moolenaara7043832005-01-21 11:56:39 +000020875/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020876 * Delete a variable from hashtab "ht" at item "hi".
20877 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020880delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020881 hashtab_T *ht;
20882 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883{
Bram Moolenaar33570922005-01-25 22:26:29 +000020884 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020885
20886 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020887 clear_tv(&di->di_tv);
20888 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889}
20890
20891/*
20892 * List the value of one internal variable.
20893 */
20894 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020895list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020896 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020898 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020900 char_u *tofree;
20901 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020902 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020903
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020904 current_copyID += COPYID_INC;
20905 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020906 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020907 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020908 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909}
20910
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020912list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 char_u *prefix;
20914 char_u *name;
20915 int type;
20916 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020917 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918{
Bram Moolenaar31859182007-08-14 20:41:13 +000020919 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20920 msg_start();
20921 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 if (name != NULL) /* "a:" vars don't have a name stored */
20923 msg_puts(name);
20924 msg_putchar(' ');
20925 msg_advance(22);
20926 if (type == VAR_NUMBER)
20927 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020928 else if (type == VAR_FUNC)
20929 msg_putchar('*');
20930 else if (type == VAR_LIST)
20931 {
20932 msg_putchar('[');
20933 if (*string == '[')
20934 ++string;
20935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020936 else if (type == VAR_DICT)
20937 {
20938 msg_putchar('{');
20939 if (*string == '{')
20940 ++string;
20941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 else
20943 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020946
20947 if (type == VAR_FUNC)
20948 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020949 if (*first)
20950 {
20951 msg_clr_eos();
20952 *first = FALSE;
20953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954}
20955
20956/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020957 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 * If the variable already exists, the value is updated.
20959 * Otherwise the variable is created.
20960 */
20961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020962set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020964 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020965 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966{
Bram Moolenaar33570922005-01-25 22:26:29 +000020967 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020969 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020971 ht = find_var_ht(name, &varname);
20972 if (ht == NULL || *varname == NUL)
20973 {
20974 EMSG2(_(e_illvar), name);
20975 return;
20976 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020977 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020978
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020979 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20980 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020981
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020984 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020985 if (var_check_ro(v->di_flags, name)
20986 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020987 return;
20988 if (v->di_tv.v_type != tv->v_type
20989 && !((v->di_tv.v_type == VAR_STRING
20990 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020991 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020992 || tv->v_type == VAR_NUMBER))
20993#ifdef FEAT_FLOAT
20994 && !((v->di_tv.v_type == VAR_NUMBER
20995 || v->di_tv.v_type == VAR_FLOAT)
20996 && (tv->v_type == VAR_NUMBER
20997 || tv->v_type == VAR_FLOAT))
20998#endif
20999 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021000 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021001 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021002 return;
21003 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021004
21005 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021006 * Handle setting internal v: variables separately: we don't change
21007 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021008 */
21009 if (ht == &vimvarht)
21010 {
21011 if (v->di_tv.v_type == VAR_STRING)
21012 {
21013 vim_free(v->di_tv.vval.v_string);
21014 if (copy || tv->v_type != VAR_STRING)
21015 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21016 else
21017 {
21018 /* Take over the string to avoid an extra alloc/free. */
21019 v->di_tv.vval.v_string = tv->vval.v_string;
21020 tv->vval.v_string = NULL;
21021 }
21022 }
21023 else if (v->di_tv.v_type != VAR_NUMBER)
21024 EMSG2(_(e_intern2), "set_var()");
21025 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021026 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021027 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021028 if (STRCMP(varname, "searchforward") == 0)
21029 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021030#ifdef FEAT_SEARCH_EXTRA
21031 else if (STRCMP(varname, "hlsearch") == 0)
21032 {
21033 no_hlsearch = !v->di_tv.vval.v_number;
21034 redraw_all_later(SOME_VALID);
21035 }
21036#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021037 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021038 return;
21039 }
21040
21041 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 }
21043 else /* add a new variable */
21044 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021045 /* Can't add "v:" variable. */
21046 if (ht == &vimvarht)
21047 {
21048 EMSG2(_(e_illvar), name);
21049 return;
21050 }
21051
Bram Moolenaar92124a32005-06-17 22:03:40 +000021052 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021053 if (!valid_varname(varname))
21054 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021055
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021056 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21057 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021058 if (v == NULL)
21059 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021060 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021061 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021063 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 return;
21065 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021066 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021068
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021069 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021070 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021071 else
21072 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021074 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021075 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077}
21078
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021079/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021080 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021081 * Also give an error message.
21082 */
21083 static int
21084var_check_ro(flags, name)
21085 int flags;
21086 char_u *name;
21087{
21088 if (flags & DI_FLAGS_RO)
21089 {
21090 EMSG2(_(e_readonlyvar), name);
21091 return TRUE;
21092 }
21093 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21094 {
21095 EMSG2(_(e_readonlysbx), name);
21096 return TRUE;
21097 }
21098 return FALSE;
21099}
21100
21101/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021102 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21103 * Also give an error message.
21104 */
21105 static int
21106var_check_fixed(flags, name)
21107 int flags;
21108 char_u *name;
21109{
21110 if (flags & DI_FLAGS_FIX)
21111 {
21112 EMSG2(_("E795: Cannot delete variable %s"), name);
21113 return TRUE;
21114 }
21115 return FALSE;
21116}
21117
21118/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021119 * Check if a funcref is assigned to a valid variable name.
21120 * Return TRUE and give an error if not.
21121 */
21122 static int
21123var_check_func_name(name, new_var)
21124 char_u *name; /* points to start of variable name */
21125 int new_var; /* TRUE when creating the variable */
21126{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021127 /* Allow for w: b: s: and t:. */
21128 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021129 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21130 ? name[2] : name[0]))
21131 {
21132 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21133 name);
21134 return TRUE;
21135 }
21136 /* Don't allow hiding a function. When "v" is not NULL we might be
21137 * assigning another function to the same var, the type is checked
21138 * below. */
21139 if (new_var && function_exists(name))
21140 {
21141 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21142 name);
21143 return TRUE;
21144 }
21145 return FALSE;
21146}
21147
21148/*
21149 * Check if a variable name is valid.
21150 * Return FALSE and give an error if not.
21151 */
21152 static int
21153valid_varname(varname)
21154 char_u *varname;
21155{
21156 char_u *p;
21157
21158 for (p = varname; *p != NUL; ++p)
21159 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21160 && *p != AUTOLOAD_CHAR)
21161 {
21162 EMSG2(_(e_illvar), varname);
21163 return FALSE;
21164 }
21165 return TRUE;
21166}
21167
21168/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021169 * Return TRUE if typeval "tv" is set to be locked (immutable).
21170 * Also give an error message, using "name".
21171 */
21172 static int
21173tv_check_lock(lock, name)
21174 int lock;
21175 char_u *name;
21176{
21177 if (lock & VAR_LOCKED)
21178 {
21179 EMSG2(_("E741: Value is locked: %s"),
21180 name == NULL ? (char_u *)_("Unknown") : name);
21181 return TRUE;
21182 }
21183 if (lock & VAR_FIXED)
21184 {
21185 EMSG2(_("E742: Cannot change value of %s"),
21186 name == NULL ? (char_u *)_("Unknown") : name);
21187 return TRUE;
21188 }
21189 return FALSE;
21190}
21191
21192/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021193 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021194 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021195 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021196 * It is OK for "from" and "to" to point to the same item. This is used to
21197 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021198 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021199 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021200copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021201 typval_T *from;
21202 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021204 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021205 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021206 switch (from->v_type)
21207 {
21208 case VAR_NUMBER:
21209 to->vval.v_number = from->vval.v_number;
21210 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021211#ifdef FEAT_FLOAT
21212 case VAR_FLOAT:
21213 to->vval.v_float = from->vval.v_float;
21214 break;
21215#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021216 case VAR_STRING:
21217 case VAR_FUNC:
21218 if (from->vval.v_string == NULL)
21219 to->vval.v_string = NULL;
21220 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021221 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021222 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021223 if (from->v_type == VAR_FUNC)
21224 func_ref(to->vval.v_string);
21225 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021226 break;
21227 case VAR_LIST:
21228 if (from->vval.v_list == NULL)
21229 to->vval.v_list = NULL;
21230 else
21231 {
21232 to->vval.v_list = from->vval.v_list;
21233 ++to->vval.v_list->lv_refcount;
21234 }
21235 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021236 case VAR_DICT:
21237 if (from->vval.v_dict == NULL)
21238 to->vval.v_dict = NULL;
21239 else
21240 {
21241 to->vval.v_dict = from->vval.v_dict;
21242 ++to->vval.v_dict->dv_refcount;
21243 }
21244 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021245 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021247 break;
21248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249}
21250
21251/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021252 * Make a copy of an item.
21253 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021254 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21255 * reference to an already copied list/dict can be used.
21256 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021257 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021258 static int
21259item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021260 typval_T *from;
21261 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021262 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021263 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021264{
21265 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021266 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021267
Bram Moolenaar33570922005-01-25 22:26:29 +000021268 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021269 {
21270 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021271 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021272 }
21273 ++recurse;
21274
21275 switch (from->v_type)
21276 {
21277 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021278#ifdef FEAT_FLOAT
21279 case VAR_FLOAT:
21280#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021281 case VAR_STRING:
21282 case VAR_FUNC:
21283 copy_tv(from, to);
21284 break;
21285 case VAR_LIST:
21286 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021287 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021288 if (from->vval.v_list == NULL)
21289 to->vval.v_list = NULL;
21290 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21291 {
21292 /* use the copy made earlier */
21293 to->vval.v_list = from->vval.v_list->lv_copylist;
21294 ++to->vval.v_list->lv_refcount;
21295 }
21296 else
21297 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21298 if (to->vval.v_list == NULL)
21299 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021300 break;
21301 case VAR_DICT:
21302 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021303 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021304 if (from->vval.v_dict == NULL)
21305 to->vval.v_dict = NULL;
21306 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21307 {
21308 /* use the copy made earlier */
21309 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21310 ++to->vval.v_dict->dv_refcount;
21311 }
21312 else
21313 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21314 if (to->vval.v_dict == NULL)
21315 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021316 break;
21317 default:
21318 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021319 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021320 }
21321 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021322 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021323}
21324
21325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 * ":echo expr1 ..." print each argument separated with a space, add a
21327 * newline at the end.
21328 * ":echon expr1 ..." print each argument plain.
21329 */
21330 void
21331ex_echo(eap)
21332 exarg_T *eap;
21333{
21334 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021335 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021336 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 char_u *p;
21338 int needclr = TRUE;
21339 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341
21342 if (eap->skip)
21343 ++emsg_skip;
21344 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21345 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021346 /* If eval1() causes an error message the text from the command may
21347 * still need to be cleared. E.g., "echo 22,44". */
21348 need_clr_eos = needclr;
21349
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 {
21353 /*
21354 * Report the invalid expression unless the expression evaluation
21355 * has been cancelled due to an aborting error, an interrupt, or an
21356 * exception.
21357 */
21358 if (!aborting())
21359 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021360 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 break;
21362 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021363 need_clr_eos = FALSE;
21364
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 if (!eap->skip)
21366 {
21367 if (atstart)
21368 {
21369 atstart = FALSE;
21370 /* Call msg_start() after eval1(), evaluating the expression
21371 * may cause a message to appear. */
21372 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021373 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021374 /* Mark the saved text as finishing the line, so that what
21375 * follows is displayed on a new line when scrolling back
21376 * at the more prompt. */
21377 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 }
21381 else if (eap->cmdidx == CMD_echo)
21382 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021383 current_copyID += COPYID_INC;
21384 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021385 if (p != NULL)
21386 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021388 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021390 if (*p != TAB && needclr)
21391 {
21392 /* remove any text still there from the command */
21393 msg_clr_eos();
21394 needclr = FALSE;
21395 }
21396 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 }
21398 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021399 {
21400#ifdef FEAT_MBYTE
21401 if (has_mbyte)
21402 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021403 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021404
21405 (void)msg_outtrans_len_attr(p, i, echo_attr);
21406 p += i - 1;
21407 }
21408 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021410 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021413 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021415 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 arg = skipwhite(arg);
21417 }
21418 eap->nextcmd = check_nextcmd(arg);
21419
21420 if (eap->skip)
21421 --emsg_skip;
21422 else
21423 {
21424 /* remove text that may still be there from the command */
21425 if (needclr)
21426 msg_clr_eos();
21427 if (eap->cmdidx == CMD_echo)
21428 msg_end();
21429 }
21430}
21431
21432/*
21433 * ":echohl {name}".
21434 */
21435 void
21436ex_echohl(eap)
21437 exarg_T *eap;
21438{
21439 int id;
21440
21441 id = syn_name2id(eap->arg);
21442 if (id == 0)
21443 echo_attr = 0;
21444 else
21445 echo_attr = syn_id2attr(id);
21446}
21447
21448/*
21449 * ":execute expr1 ..." execute the result of an expression.
21450 * ":echomsg expr1 ..." Print a message
21451 * ":echoerr expr1 ..." Print an error
21452 * Each gets spaces around each argument and a newline at the end for
21453 * echo commands
21454 */
21455 void
21456ex_execute(eap)
21457 exarg_T *eap;
21458{
21459 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021460 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 int ret = OK;
21462 char_u *p;
21463 garray_T ga;
21464 int len;
21465 int save_did_emsg;
21466
21467 ga_init2(&ga, 1, 80);
21468
21469 if (eap->skip)
21470 ++emsg_skip;
21471 while (*arg != NUL && *arg != '|' && *arg != '\n')
21472 {
21473 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021474 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 {
21476 /*
21477 * Report the invalid expression unless the expression evaluation
21478 * has been cancelled due to an aborting error, an interrupt, or an
21479 * exception.
21480 */
21481 if (!aborting())
21482 EMSG2(_(e_invexpr2), p);
21483 ret = FAIL;
21484 break;
21485 }
21486
21487 if (!eap->skip)
21488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021489 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 len = (int)STRLEN(p);
21491 if (ga_grow(&ga, len + 2) == FAIL)
21492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 ret = FAIL;
21495 break;
21496 }
21497 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 ga.ga_len += len;
21501 }
21502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 arg = skipwhite(arg);
21505 }
21506
21507 if (ret != FAIL && ga.ga_data != NULL)
21508 {
21509 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021512 out_flush();
21513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 else if (eap->cmdidx == CMD_echoerr)
21515 {
21516 /* We don't want to abort following commands, restore did_emsg. */
21517 save_did_emsg = did_emsg;
21518 EMSG((char_u *)ga.ga_data);
21519 if (!force_abort)
21520 did_emsg = save_did_emsg;
21521 }
21522 else if (eap->cmdidx == CMD_execute)
21523 do_cmdline((char_u *)ga.ga_data,
21524 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21525 }
21526
21527 ga_clear(&ga);
21528
21529 if (eap->skip)
21530 --emsg_skip;
21531
21532 eap->nextcmd = check_nextcmd(arg);
21533}
21534
21535/*
21536 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21537 * "arg" points to the "&" or '+' when called, to "option" when returning.
21538 * Returns NULL when no option name found. Otherwise pointer to the char
21539 * after the option name.
21540 */
21541 static char_u *
21542find_option_end(arg, opt_flags)
21543 char_u **arg;
21544 int *opt_flags;
21545{
21546 char_u *p = *arg;
21547
21548 ++p;
21549 if (*p == 'g' && p[1] == ':')
21550 {
21551 *opt_flags = OPT_GLOBAL;
21552 p += 2;
21553 }
21554 else if (*p == 'l' && p[1] == ':')
21555 {
21556 *opt_flags = OPT_LOCAL;
21557 p += 2;
21558 }
21559 else
21560 *opt_flags = 0;
21561
21562 if (!ASCII_ISALPHA(*p))
21563 return NULL;
21564 *arg = p;
21565
21566 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21567 p += 4; /* termcap option */
21568 else
21569 while (ASCII_ISALPHA(*p))
21570 ++p;
21571 return p;
21572}
21573
21574/*
21575 * ":function"
21576 */
21577 void
21578ex_function(eap)
21579 exarg_T *eap;
21580{
21581 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021582 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 int j;
21584 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021586 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 char_u *name = NULL;
21588 char_u *p;
21589 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021590 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 garray_T newargs;
21592 garray_T newlines;
21593 int varargs = FALSE;
21594 int mustend = FALSE;
21595 int flags = 0;
21596 ufunc_T *fp;
21597 int indent;
21598 int nesting;
21599 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021600 dictitem_T *v;
21601 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021602 static int func_nr = 0; /* number for nameless function */
21603 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021604 hashtab_T *ht;
21605 int todo;
21606 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021607 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608
21609 /*
21610 * ":function" without argument: list functions.
21611 */
21612 if (ends_excmd(*eap->arg))
21613 {
21614 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021615 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021616 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021617 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021618 {
21619 if (!HASHITEM_EMPTY(hi))
21620 {
21621 --todo;
21622 fp = HI2UF(hi);
21623 if (!isdigit(*fp->uf_name))
21624 list_func_head(fp, FALSE);
21625 }
21626 }
21627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 eap->nextcmd = check_nextcmd(eap->arg);
21629 return;
21630 }
21631
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021633 * ":function /pat": list functions matching pattern.
21634 */
21635 if (*eap->arg == '/')
21636 {
21637 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21638 if (!eap->skip)
21639 {
21640 regmatch_T regmatch;
21641
21642 c = *p;
21643 *p = NUL;
21644 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21645 *p = c;
21646 if (regmatch.regprog != NULL)
21647 {
21648 regmatch.rm_ic = p_ic;
21649
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021650 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021651 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21652 {
21653 if (!HASHITEM_EMPTY(hi))
21654 {
21655 --todo;
21656 fp = HI2UF(hi);
21657 if (!isdigit(*fp->uf_name)
21658 && vim_regexec(&regmatch, fp->uf_name, 0))
21659 list_func_head(fp, FALSE);
21660 }
21661 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021662 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021663 }
21664 }
21665 if (*p == '/')
21666 ++p;
21667 eap->nextcmd = check_nextcmd(p);
21668 return;
21669 }
21670
21671 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672 * Get the function name. There are these situations:
21673 * func normal function name
21674 * "name" == func, "fudi.fd_dict" == NULL
21675 * dict.func new dictionary entry
21676 * "name" == NULL, "fudi.fd_dict" set,
21677 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21678 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021679 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021680 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21681 * dict.func existing dict entry that's not a Funcref
21682 * "name" == NULL, "fudi.fd_dict" set,
21683 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021684 * s:func script-local function name
21685 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021688 name = trans_function_name(&p, eap->skip, 0, &fudi);
21689 paren = (vim_strchr(p, '(') != NULL);
21690 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 {
21692 /*
21693 * Return on an invalid expression in braces, unless the expression
21694 * evaluation has been cancelled due to an aborting error, an
21695 * interrupt, or an exception.
21696 */
21697 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021699 if (!eap->skip && fudi.fd_newkey != NULL)
21700 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021701 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 else
21705 eap->skip = TRUE;
21706 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021707
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 /* An error in a function call during evaluation of an expression in magic
21709 * braces should not cause the function not to be defined. */
21710 saved_did_emsg = did_emsg;
21711 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712
21713 /*
21714 * ":function func" with only function name: list function.
21715 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021716 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 {
21718 if (!ends_excmd(*skipwhite(p)))
21719 {
21720 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021721 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 }
21723 eap->nextcmd = check_nextcmd(p);
21724 if (eap->nextcmd != NULL)
21725 *p = NUL;
21726 if (!eap->skip && !got_int)
21727 {
21728 fp = find_func(name);
21729 if (fp != NULL)
21730 {
21731 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021732 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021734 if (FUNCLINE(fp, j) == NULL)
21735 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 msg_putchar('\n');
21737 msg_outnum((long)(j + 1));
21738 if (j < 9)
21739 msg_putchar(' ');
21740 if (j < 99)
21741 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021742 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 out_flush(); /* show a line at a time */
21744 ui_breakcheck();
21745 }
21746 if (!got_int)
21747 {
21748 msg_putchar('\n');
21749 msg_puts((char_u *)" endfunction");
21750 }
21751 }
21752 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021753 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021755 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 }
21757
21758 /*
21759 * ":function name(arg1, arg2)" Define function.
21760 */
21761 p = skipwhite(p);
21762 if (*p != '(')
21763 {
21764 if (!eap->skip)
21765 {
21766 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021767 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 }
21769 /* attempt to continue by skipping some text */
21770 if (vim_strchr(p, '(') != NULL)
21771 p = vim_strchr(p, '(');
21772 }
21773 p = skipwhite(p + 1);
21774
21775 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21776 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21777
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021778 if (!eap->skip)
21779 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021780 /* Check the name of the function. Unless it's a dictionary function
21781 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021782 if (name != NULL)
21783 arg = name;
21784 else
21785 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021786 if (arg != NULL && (fudi.fd_di == NULL
21787 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021788 {
21789 if (*arg == K_SPECIAL)
21790 j = 3;
21791 else
21792 j = 0;
21793 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21794 : eval_isnamec(arg[j])))
21795 ++j;
21796 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021797 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021798 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021799 /* Disallow using the g: dict. */
21800 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21801 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021802 }
21803
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 /*
21805 * Isolate the arguments: "arg1, arg2, ...)"
21806 */
21807 while (*p != ')')
21808 {
21809 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21810 {
21811 varargs = TRUE;
21812 p += 3;
21813 mustend = TRUE;
21814 }
21815 else
21816 {
21817 arg = p;
21818 while (ASCII_ISALNUM(*p) || *p == '_')
21819 ++p;
21820 if (arg == p || isdigit(*arg)
21821 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21822 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21823 {
21824 if (!eap->skip)
21825 EMSG2(_("E125: Illegal argument: %s"), arg);
21826 break;
21827 }
21828 if (ga_grow(&newargs, 1) == FAIL)
21829 goto erret;
21830 c = *p;
21831 *p = NUL;
21832 arg = vim_strsave(arg);
21833 if (arg == NULL)
21834 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021835
21836 /* Check for duplicate argument name. */
21837 for (i = 0; i < newargs.ga_len; ++i)
21838 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21839 {
21840 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021841 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021842 goto erret;
21843 }
21844
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21846 *p = c;
21847 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 if (*p == ',')
21849 ++p;
21850 else
21851 mustend = TRUE;
21852 }
21853 p = skipwhite(p);
21854 if (mustend && *p != ')')
21855 {
21856 if (!eap->skip)
21857 EMSG2(_(e_invarg2), eap->arg);
21858 break;
21859 }
21860 }
21861 ++p; /* skip the ')' */
21862
Bram Moolenaare9a41262005-01-15 22:18:47 +000021863 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 for (;;)
21865 {
21866 p = skipwhite(p);
21867 if (STRNCMP(p, "range", 5) == 0)
21868 {
21869 flags |= FC_RANGE;
21870 p += 5;
21871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021872 else if (STRNCMP(p, "dict", 4) == 0)
21873 {
21874 flags |= FC_DICT;
21875 p += 4;
21876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 else if (STRNCMP(p, "abort", 5) == 0)
21878 {
21879 flags |= FC_ABORT;
21880 p += 5;
21881 }
21882 else
21883 break;
21884 }
21885
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021886 /* When there is a line break use what follows for the function body.
21887 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21888 if (*p == '\n')
21889 line_arg = p + 1;
21890 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 EMSG(_(e_trailing));
21892
21893 /*
21894 * Read the body of the function, until ":endfunction" is found.
21895 */
21896 if (KeyTyped)
21897 {
21898 /* Check if the function already exists, don't let the user type the
21899 * whole function before telling him it doesn't work! For a script we
21900 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021901 if (!eap->skip && !eap->forceit)
21902 {
21903 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21904 EMSG(_(e_funcdict));
21905 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021906 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021909 if (!eap->skip && did_emsg)
21910 goto erret;
21911
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 msg_putchar('\n'); /* don't overwrite the function name */
21913 cmdline_row = msg_row;
21914 }
21915
21916 indent = 2;
21917 nesting = 0;
21918 for (;;)
21919 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021920 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021921 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021922 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021923 saved_wait_return = FALSE;
21924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021926 sourcing_lnum_off = sourcing_lnum;
21927
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021928 if (line_arg != NULL)
21929 {
21930 /* Use eap->arg, split up in parts by line breaks. */
21931 theline = line_arg;
21932 p = vim_strchr(theline, '\n');
21933 if (p == NULL)
21934 line_arg += STRLEN(line_arg);
21935 else
21936 {
21937 *p = NUL;
21938 line_arg = p + 1;
21939 }
21940 }
21941 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 theline = getcmdline(':', 0L, indent);
21943 else
21944 theline = eap->getline(':', eap->cookie, indent);
21945 if (KeyTyped)
21946 lines_left = Rows - 1;
21947 if (theline == NULL)
21948 {
21949 EMSG(_("E126: Missing :endfunction"));
21950 goto erret;
21951 }
21952
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021953 /* Detect line continuation: sourcing_lnum increased more than one. */
21954 if (sourcing_lnum > sourcing_lnum_off + 1)
21955 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21956 else
21957 sourcing_lnum_off = 0;
21958
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 if (skip_until != NULL)
21960 {
21961 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21962 * don't check for ":endfunc". */
21963 if (STRCMP(theline, skip_until) == 0)
21964 {
21965 vim_free(skip_until);
21966 skip_until = NULL;
21967 }
21968 }
21969 else
21970 {
21971 /* skip ':' and blanks*/
21972 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21973 ;
21974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021975 /* Check for "endfunction". */
21976 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021978 if (line_arg == NULL)
21979 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 break;
21981 }
21982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021983 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 * at "end". */
21985 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21986 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021987 else if (STRNCMP(p, "if", 2) == 0
21988 || STRNCMP(p, "wh", 2) == 0
21989 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 || STRNCMP(p, "try", 3) == 0)
21991 indent += 2;
21992
21993 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021994 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021996 if (*p == '!')
21997 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 p += eval_fname_script(p);
21999 if (ASCII_ISALPHA(*p))
22000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022001 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 if (*skipwhite(p) == '(')
22003 {
22004 ++nesting;
22005 indent += 2;
22006 }
22007 }
22008 }
22009
22010 /* Check for ":append" or ":insert". */
22011 p = skip_range(p, NULL);
22012 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22013 || (p[0] == 'i'
22014 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22015 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22016 skip_until = vim_strsave((char_u *)".");
22017
22018 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22019 arg = skipwhite(skiptowhite(p));
22020 if (arg[0] == '<' && arg[1] =='<'
22021 && ((p[0] == 'p' && p[1] == 'y'
22022 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22023 || (p[0] == 'p' && p[1] == 'e'
22024 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22025 || (p[0] == 't' && p[1] == 'c'
22026 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022027 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22028 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22030 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022031 || (p[0] == 'm' && p[1] == 'z'
22032 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 ))
22034 {
22035 /* ":python <<" continues until a dot, like ":append" */
22036 p = skipwhite(arg + 2);
22037 if (*p == NUL)
22038 skip_until = vim_strsave((char_u *)".");
22039 else
22040 skip_until = vim_strsave(p);
22041 }
22042 }
22043
22044 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022045 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022046 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022047 if (line_arg == NULL)
22048 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022050 }
22051
22052 /* Copy the line to newly allocated memory. get_one_sourceline()
22053 * allocates 250 bytes per line, this saves 80% on average. The cost
22054 * is an extra alloc/free. */
22055 p = vim_strsave(theline);
22056 if (p != NULL)
22057 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022058 if (line_arg == NULL)
22059 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022060 theline = p;
22061 }
22062
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022063 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22064
22065 /* Add NULL lines for continuation lines, so that the line count is
22066 * equal to the index in the growarray. */
22067 while (sourcing_lnum_off-- > 0)
22068 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022069
22070 /* Check for end of eap->arg. */
22071 if (line_arg != NULL && *line_arg == NUL)
22072 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 }
22074
22075 /* Don't define the function when skipping commands or when an error was
22076 * detected. */
22077 if (eap->skip || did_emsg)
22078 goto erret;
22079
22080 /*
22081 * If there are no errors, add the function
22082 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022083 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022085 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022086 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022087 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022088 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022089 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022090 goto erret;
22091 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022092
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 fp = find_func(name);
22094 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022096 if (!eap->forceit)
22097 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022098 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022099 goto erret;
22100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022101 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022102 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022103 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022104 name);
22105 goto erret;
22106 }
22107 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022108 ga_clear_strings(&(fp->uf_args));
22109 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022110 vim_free(name);
22111 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113 }
22114 else
22115 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022116 char numbuf[20];
22117
22118 fp = NULL;
22119 if (fudi.fd_newkey == NULL && !eap->forceit)
22120 {
22121 EMSG(_(e_funcdict));
22122 goto erret;
22123 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022124 if (fudi.fd_di == NULL)
22125 {
22126 /* Can't add a function to a locked dictionary */
22127 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22128 goto erret;
22129 }
22130 /* Can't change an existing function if it is locked */
22131 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22132 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022133
22134 /* Give the function a sequential number. Can only be used with a
22135 * Funcref! */
22136 vim_free(name);
22137 sprintf(numbuf, "%d", ++func_nr);
22138 name = vim_strsave((char_u *)numbuf);
22139 if (name == NULL)
22140 goto erret;
22141 }
22142
22143 if (fp == NULL)
22144 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022145 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022146 {
22147 int slen, plen;
22148 char_u *scriptname;
22149
22150 /* Check that the autoload name matches the script name. */
22151 j = FAIL;
22152 if (sourcing_name != NULL)
22153 {
22154 scriptname = autoload_name(name);
22155 if (scriptname != NULL)
22156 {
22157 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022158 plen = (int)STRLEN(p);
22159 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022160 if (slen > plen && fnamecmp(p,
22161 sourcing_name + slen - plen) == 0)
22162 j = OK;
22163 vim_free(scriptname);
22164 }
22165 }
22166 if (j == FAIL)
22167 {
22168 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22169 goto erret;
22170 }
22171 }
22172
22173 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 if (fp == NULL)
22175 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022176
22177 if (fudi.fd_dict != NULL)
22178 {
22179 if (fudi.fd_di == NULL)
22180 {
22181 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022182 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022183 if (fudi.fd_di == NULL)
22184 {
22185 vim_free(fp);
22186 goto erret;
22187 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022188 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22189 {
22190 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022191 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022192 goto erret;
22193 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022194 }
22195 else
22196 /* overwrite existing dict entry */
22197 clear_tv(&fudi.fd_di->di_tv);
22198 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022199 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022200 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022201 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022202
22203 /* behave like "dict" was used */
22204 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022205 }
22206
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022208 STRCPY(fp->uf_name, name);
22209 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022211 fp->uf_args = newargs;
22212 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022213#ifdef FEAT_PROFILE
22214 fp->uf_tml_count = NULL;
22215 fp->uf_tml_total = NULL;
22216 fp->uf_tml_self = NULL;
22217 fp->uf_profiling = FALSE;
22218 if (prof_def_func())
22219 func_do_profile(fp);
22220#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022221 fp->uf_varargs = varargs;
22222 fp->uf_flags = flags;
22223 fp->uf_calls = 0;
22224 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022225 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226
22227erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 ga_clear_strings(&newargs);
22229 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022230ret_free:
22231 vim_free(skip_until);
22232 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022235 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236}
22237
22238/*
22239 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022240 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022242 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022243 * TFN_INT: internal function name OK
22244 * TFN_QUIET: be quiet
22245 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 * Advances "pp" to just after the function name (if no error).
22247 */
22248 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022249trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 char_u **pp;
22251 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022252 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022253 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022255 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 char_u *start;
22257 char_u *end;
22258 int lead;
22259 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022261 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022262
22263 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022264 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022266
22267 /* Check for hard coded <SNR>: already translated function ID (from a user
22268 * command). */
22269 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22270 && (*pp)[2] == (int)KE_SNR)
22271 {
22272 *pp += 3;
22273 len = get_id_len(pp) + 3;
22274 return vim_strnsave(start, len);
22275 }
22276
22277 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22278 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022280 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022282
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022283 /* Note that TFN_ flags use the same values as GLV_ flags. */
22284 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022285 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022286 if (end == start)
22287 {
22288 if (!skip)
22289 EMSG(_("E129: Function name required"));
22290 goto theend;
22291 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022292 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022293 {
22294 /*
22295 * Report an invalid expression in braces, unless the expression
22296 * evaluation has been cancelled due to an aborting error, an
22297 * interrupt, or an exception.
22298 */
22299 if (!aborting())
22300 {
22301 if (end != NULL)
22302 EMSG2(_(e_invarg2), start);
22303 }
22304 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022305 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022306 goto theend;
22307 }
22308
22309 if (lv.ll_tv != NULL)
22310 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022311 if (fdp != NULL)
22312 {
22313 fdp->fd_dict = lv.ll_dict;
22314 fdp->fd_newkey = lv.ll_newkey;
22315 lv.ll_newkey = NULL;
22316 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022317 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022318 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22319 {
22320 name = vim_strsave(lv.ll_tv->vval.v_string);
22321 *pp = end;
22322 }
22323 else
22324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022325 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22326 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022327 EMSG(_(e_funcref));
22328 else
22329 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022330 name = NULL;
22331 }
22332 goto theend;
22333 }
22334
22335 if (lv.ll_name == NULL)
22336 {
22337 /* Error found, but continue after the function name. */
22338 *pp = end;
22339 goto theend;
22340 }
22341
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022342 /* Check if the name is a Funcref. If so, use the value. */
22343 if (lv.ll_exp_name != NULL)
22344 {
22345 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022346 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022347 if (name == lv.ll_exp_name)
22348 name = NULL;
22349 }
22350 else
22351 {
22352 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022353 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022354 if (name == *pp)
22355 name = NULL;
22356 }
22357 if (name != NULL)
22358 {
22359 name = vim_strsave(name);
22360 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022361 if (STRNCMP(name, "<SNR>", 5) == 0)
22362 {
22363 /* Change "<SNR>" to the byte sequence. */
22364 name[0] = K_SPECIAL;
22365 name[1] = KS_EXTRA;
22366 name[2] = (int)KE_SNR;
22367 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22368 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022369 goto theend;
22370 }
22371
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022372 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022373 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022374 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022375 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22376 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22377 {
22378 /* When there was "s:" already or the name expanded to get a
22379 * leading "s:" then remove it. */
22380 lv.ll_name += 2;
22381 len -= 2;
22382 lead = 2;
22383 }
22384 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022385 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022386 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022387 /* skip over "s:" and "g:" */
22388 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022389 lv.ll_name += 2;
22390 len = (int)(end - lv.ll_name);
22391 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022392
22393 /*
22394 * Copy the function name to allocated memory.
22395 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22396 * Accept <SNR>123_name() outside a script.
22397 */
22398 if (skip)
22399 lead = 0; /* do nothing */
22400 else if (lead > 0)
22401 {
22402 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022403 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22404 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022405 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022406 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022407 if (current_SID <= 0)
22408 {
22409 EMSG(_(e_usingsid));
22410 goto theend;
22411 }
22412 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22413 lead += (int)STRLEN(sid_buf);
22414 }
22415 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022416 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022417 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022418 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022419 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022420 goto theend;
22421 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022422 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022423 {
22424 char_u *cp = vim_strchr(lv.ll_name, ':');
22425
22426 if (cp != NULL && cp < end)
22427 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022428 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022429 goto theend;
22430 }
22431 }
22432
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022433 name = alloc((unsigned)(len + lead + 1));
22434 if (name != NULL)
22435 {
22436 if (lead > 0)
22437 {
22438 name[0] = K_SPECIAL;
22439 name[1] = KS_EXTRA;
22440 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022441 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022442 STRCPY(name + 3, sid_buf);
22443 }
22444 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022445 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022446 }
22447 *pp = end;
22448
22449theend:
22450 clear_lval(&lv);
22451 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452}
22453
22454/*
22455 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22456 * Return 2 if "p" starts with "s:".
22457 * Return 0 otherwise.
22458 */
22459 static int
22460eval_fname_script(p)
22461 char_u *p;
22462{
22463 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22464 || STRNICMP(p + 1, "SNR>", 4) == 0))
22465 return 5;
22466 if (p[0] == 's' && p[1] == ':')
22467 return 2;
22468 return 0;
22469}
22470
22471/*
22472 * Return TRUE if "p" starts with "<SID>" or "s:".
22473 * Only works if eval_fname_script() returned non-zero for "p"!
22474 */
22475 static int
22476eval_fname_sid(p)
22477 char_u *p;
22478{
22479 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22480}
22481
22482/*
22483 * List the head of the function: "name(arg1, arg2)".
22484 */
22485 static void
22486list_func_head(fp, indent)
22487 ufunc_T *fp;
22488 int indent;
22489{
22490 int j;
22491
22492 msg_start();
22493 if (indent)
22494 MSG_PUTS(" ");
22495 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022496 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 {
22498 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022499 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 }
22501 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022502 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022504 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505 {
22506 if (j)
22507 MSG_PUTS(", ");
22508 msg_puts(FUNCARG(fp, j));
22509 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022510 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 {
22512 if (j)
22513 MSG_PUTS(", ");
22514 MSG_PUTS("...");
22515 }
22516 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022517 if (fp->uf_flags & FC_ABORT)
22518 MSG_PUTS(" abort");
22519 if (fp->uf_flags & FC_RANGE)
22520 MSG_PUTS(" range");
22521 if (fp->uf_flags & FC_DICT)
22522 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022523 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022524 if (p_verbose > 0)
22525 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526}
22527
22528/*
22529 * Find a function by name, return pointer to it in ufuncs.
22530 * Return NULL for unknown function.
22531 */
22532 static ufunc_T *
22533find_func(name)
22534 char_u *name;
22535{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022536 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022538 hi = hash_find(&func_hashtab, name);
22539 if (!HASHITEM_EMPTY(hi))
22540 return HI2UF(hi);
22541 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542}
22543
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022544#if defined(EXITFREE) || defined(PROTO)
22545 void
22546free_all_functions()
22547{
22548 hashitem_T *hi;
22549
22550 /* Need to start all over every time, because func_free() may change the
22551 * hash table. */
22552 while (func_hashtab.ht_used > 0)
22553 for (hi = func_hashtab.ht_array; ; ++hi)
22554 if (!HASHITEM_EMPTY(hi))
22555 {
22556 func_free(HI2UF(hi));
22557 break;
22558 }
22559}
22560#endif
22561
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022562 int
22563translated_function_exists(name)
22564 char_u *name;
22565{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022566 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022567 return find_internal_func(name) >= 0;
22568 return find_func(name) != NULL;
22569}
22570
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022571/*
22572 * Return TRUE if a function "name" exists.
22573 */
22574 static int
22575function_exists(name)
22576 char_u *name;
22577{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022578 char_u *nm = name;
22579 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022580 int n = FALSE;
22581
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022582 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22583 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022584 nm = skipwhite(nm);
22585
22586 /* Only accept "funcname", "funcname ", "funcname (..." and
22587 * "funcname(...", not "funcname!...". */
22588 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022589 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022590 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022591 return n;
22592}
22593
Bram Moolenaara1544c02013-05-30 12:35:52 +020022594 char_u *
22595get_expanded_name(name, check)
22596 char_u *name;
22597 int check;
22598{
22599 char_u *nm = name;
22600 char_u *p;
22601
22602 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22603
22604 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022605 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022606 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022607
Bram Moolenaara1544c02013-05-30 12:35:52 +020022608 vim_free(p);
22609 return NULL;
22610}
22611
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022612/*
22613 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022614 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22615 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022616 */
22617 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022618builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022619 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022620 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022621{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022622 char_u *p;
22623
22624 if (!ASCII_ISLOWER(name[0]))
22625 return FALSE;
22626 p = vim_strchr(name, AUTOLOAD_CHAR);
22627 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022628}
22629
Bram Moolenaar05159a02005-02-26 23:04:13 +000022630#if defined(FEAT_PROFILE) || defined(PROTO)
22631/*
22632 * Start profiling function "fp".
22633 */
22634 static void
22635func_do_profile(fp)
22636 ufunc_T *fp;
22637{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022638 int len = fp->uf_lines.ga_len;
22639
22640 if (len == 0)
22641 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022642 fp->uf_tm_count = 0;
22643 profile_zero(&fp->uf_tm_self);
22644 profile_zero(&fp->uf_tm_total);
22645 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022646 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022647 if (fp->uf_tml_total == NULL)
22648 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022649 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022650 if (fp->uf_tml_self == NULL)
22651 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022652 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022653 fp->uf_tml_idx = -1;
22654 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22655 || fp->uf_tml_self == NULL)
22656 return; /* out of memory */
22657
22658 fp->uf_profiling = TRUE;
22659}
22660
22661/*
22662 * Dump the profiling results for all functions in file "fd".
22663 */
22664 void
22665func_dump_profile(fd)
22666 FILE *fd;
22667{
22668 hashitem_T *hi;
22669 int todo;
22670 ufunc_T *fp;
22671 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022672 ufunc_T **sorttab;
22673 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022675 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022676 if (todo == 0)
22677 return; /* nothing to dump */
22678
Bram Moolenaar73830342005-02-28 22:48:19 +000022679 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22680
Bram Moolenaar05159a02005-02-26 23:04:13 +000022681 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22682 {
22683 if (!HASHITEM_EMPTY(hi))
22684 {
22685 --todo;
22686 fp = HI2UF(hi);
22687 if (fp->uf_profiling)
22688 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022689 if (sorttab != NULL)
22690 sorttab[st_len++] = fp;
22691
Bram Moolenaar05159a02005-02-26 23:04:13 +000022692 if (fp->uf_name[0] == K_SPECIAL)
22693 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22694 else
22695 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22696 if (fp->uf_tm_count == 1)
22697 fprintf(fd, "Called 1 time\n");
22698 else
22699 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22700 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22701 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22702 fprintf(fd, "\n");
22703 fprintf(fd, "count total (s) self (s)\n");
22704
22705 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22706 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022707 if (FUNCLINE(fp, i) == NULL)
22708 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022709 prof_func_line(fd, fp->uf_tml_count[i],
22710 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022711 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22712 }
22713 fprintf(fd, "\n");
22714 }
22715 }
22716 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022717
22718 if (sorttab != NULL && st_len > 0)
22719 {
22720 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22721 prof_total_cmp);
22722 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22723 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22724 prof_self_cmp);
22725 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22726 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022727
22728 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022729}
Bram Moolenaar73830342005-02-28 22:48:19 +000022730
22731 static void
22732prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22733 FILE *fd;
22734 ufunc_T **sorttab;
22735 int st_len;
22736 char *title;
22737 int prefer_self; /* when equal print only self time */
22738{
22739 int i;
22740 ufunc_T *fp;
22741
22742 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22743 fprintf(fd, "count total (s) self (s) function\n");
22744 for (i = 0; i < 20 && i < st_len; ++i)
22745 {
22746 fp = sorttab[i];
22747 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22748 prefer_self);
22749 if (fp->uf_name[0] == K_SPECIAL)
22750 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22751 else
22752 fprintf(fd, " %s()\n", fp->uf_name);
22753 }
22754 fprintf(fd, "\n");
22755}
22756
22757/*
22758 * Print the count and times for one function or function line.
22759 */
22760 static void
22761prof_func_line(fd, count, total, self, prefer_self)
22762 FILE *fd;
22763 int count;
22764 proftime_T *total;
22765 proftime_T *self;
22766 int prefer_self; /* when equal print only self time */
22767{
22768 if (count > 0)
22769 {
22770 fprintf(fd, "%5d ", count);
22771 if (prefer_self && profile_equal(total, self))
22772 fprintf(fd, " ");
22773 else
22774 fprintf(fd, "%s ", profile_msg(total));
22775 if (!prefer_self && profile_equal(total, self))
22776 fprintf(fd, " ");
22777 else
22778 fprintf(fd, "%s ", profile_msg(self));
22779 }
22780 else
22781 fprintf(fd, " ");
22782}
22783
22784/*
22785 * Compare function for total time sorting.
22786 */
22787 static int
22788#ifdef __BORLANDC__
22789_RTLENTRYF
22790#endif
22791prof_total_cmp(s1, s2)
22792 const void *s1;
22793 const void *s2;
22794{
22795 ufunc_T *p1, *p2;
22796
22797 p1 = *(ufunc_T **)s1;
22798 p2 = *(ufunc_T **)s2;
22799 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22800}
22801
22802/*
22803 * Compare function for self time sorting.
22804 */
22805 static int
22806#ifdef __BORLANDC__
22807_RTLENTRYF
22808#endif
22809prof_self_cmp(s1, s2)
22810 const void *s1;
22811 const void *s2;
22812{
22813 ufunc_T *p1, *p2;
22814
22815 p1 = *(ufunc_T **)s1;
22816 p2 = *(ufunc_T **)s2;
22817 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22818}
22819
Bram Moolenaar05159a02005-02-26 23:04:13 +000022820#endif
22821
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022822/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022823 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022824 * Return TRUE if a package was loaded.
22825 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022826 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022827script_autoload(name, reload)
22828 char_u *name;
22829 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022830{
22831 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022832 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022833 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022834 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022835
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022836 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022837 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022838 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022839 return FALSE;
22840
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022841 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022842
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022843 /* Find the name in the list of previously loaded package names. Skip
22844 * "autoload/", it's always the same. */
22845 for (i = 0; i < ga_loaded.ga_len; ++i)
22846 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22847 break;
22848 if (!reload && i < ga_loaded.ga_len)
22849 ret = FALSE; /* was loaded already */
22850 else
22851 {
22852 /* Remember the name if it wasn't loaded already. */
22853 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22854 {
22855 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22856 tofree = NULL;
22857 }
22858
22859 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022860 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022861 ret = TRUE;
22862 }
22863
22864 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022865 return ret;
22866}
22867
22868/*
22869 * Return the autoload script name for a function or variable name.
22870 * Returns NULL when out of memory.
22871 */
22872 static char_u *
22873autoload_name(name)
22874 char_u *name;
22875{
22876 char_u *p;
22877 char_u *scriptname;
22878
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022879 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022880 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22881 if (scriptname == NULL)
22882 return FALSE;
22883 STRCPY(scriptname, "autoload/");
22884 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022885 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022886 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022887 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022888 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022889 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022890}
22891
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22893
22894/*
22895 * Function given to ExpandGeneric() to obtain the list of user defined
22896 * function names.
22897 */
22898 char_u *
22899get_user_func_name(xp, idx)
22900 expand_T *xp;
22901 int idx;
22902{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022903 static long_u done;
22904 static hashitem_T *hi;
22905 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906
22907 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022909 done = 0;
22910 hi = func_hashtab.ht_array;
22911 }
22912 if (done < func_hashtab.ht_used)
22913 {
22914 if (done++ > 0)
22915 ++hi;
22916 while (HASHITEM_EMPTY(hi))
22917 ++hi;
22918 fp = HI2UF(hi);
22919
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022920 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022921 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022922
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022923 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22924 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925
22926 cat_func_name(IObuff, fp);
22927 if (xp->xp_context != EXPAND_USER_FUNC)
22928 {
22929 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022930 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 STRCAT(IObuff, ")");
22932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 return IObuff;
22934 }
22935 return NULL;
22936}
22937
22938#endif /* FEAT_CMDL_COMPL */
22939
22940/*
22941 * Copy the function name of "fp" to buffer "buf".
22942 * "buf" must be able to hold the function name plus three bytes.
22943 * Takes care of script-local function names.
22944 */
22945 static void
22946cat_func_name(buf, fp)
22947 char_u *buf;
22948 ufunc_T *fp;
22949{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022950 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 {
22952 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022953 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 }
22955 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022956 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957}
22958
22959/*
22960 * ":delfunction {name}"
22961 */
22962 void
22963ex_delfunction(eap)
22964 exarg_T *eap;
22965{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022966 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 char_u *p;
22968 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022969 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970
22971 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022972 name = trans_function_name(&p, eap->skip, 0, &fudi);
22973 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022975 {
22976 if (fudi.fd_dict != NULL && !eap->skip)
22977 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 if (!ends_excmd(*skipwhite(p)))
22981 {
22982 vim_free(name);
22983 EMSG(_(e_trailing));
22984 return;
22985 }
22986 eap->nextcmd = check_nextcmd(p);
22987 if (eap->nextcmd != NULL)
22988 *p = NUL;
22989
22990 if (!eap->skip)
22991 fp = find_func(name);
22992 vim_free(name);
22993
22994 if (!eap->skip)
22995 {
22996 if (fp == NULL)
22997 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022998 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 return;
23000 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023001 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 {
23003 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23004 return;
23005 }
23006
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023007 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023009 /* Delete the dict item that refers to the function, it will
23010 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023011 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023013 else
23014 func_free(fp);
23015 }
23016}
23017
23018/*
23019 * Free a function and remove it from the list of functions.
23020 */
23021 static void
23022func_free(fp)
23023 ufunc_T *fp;
23024{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023025 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023026
23027 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023028 ga_clear_strings(&(fp->uf_args));
23029 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023030#ifdef FEAT_PROFILE
23031 vim_free(fp->uf_tml_count);
23032 vim_free(fp->uf_tml_total);
23033 vim_free(fp->uf_tml_self);
23034#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023036 /* remove the function from the function hashtable */
23037 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23038 if (HASHITEM_EMPTY(hi))
23039 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023040 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023041 hash_remove(&func_hashtab, hi);
23042
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023043 vim_free(fp);
23044}
23045
23046/*
23047 * Unreference a Function: decrement the reference count and free it when it
23048 * becomes zero. Only for numbered functions.
23049 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023050 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023051func_unref(name)
23052 char_u *name;
23053{
23054 ufunc_T *fp;
23055
23056 if (name != NULL && isdigit(*name))
23057 {
23058 fp = find_func(name);
23059 if (fp == NULL)
23060 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023061 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023062 {
23063 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023064 * when "uf_calls" becomes zero. */
23065 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023066 func_free(fp);
23067 }
23068 }
23069}
23070
23071/*
23072 * Count a reference to a Function.
23073 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023074 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023075func_ref(name)
23076 char_u *name;
23077{
23078 ufunc_T *fp;
23079
23080 if (name != NULL && isdigit(*name))
23081 {
23082 fp = find_func(name);
23083 if (fp == NULL)
23084 EMSG2(_(e_intern2), "func_ref()");
23085 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023086 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 }
23088}
23089
23090/*
23091 * Call a user function.
23092 */
23093 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023094call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 ufunc_T *fp; /* pointer to function */
23096 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023097 typval_T *argvars; /* arguments */
23098 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 linenr_T firstline; /* first line of range */
23100 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102{
Bram Moolenaar33570922005-01-25 22:26:29 +000023103 char_u *save_sourcing_name;
23104 linenr_T save_sourcing_lnum;
23105 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023106 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023107 int save_did_emsg;
23108 static int depth = 0;
23109 dictitem_T *v;
23110 int fixvar_idx = 0; /* index in fixvar[] */
23111 int i;
23112 int ai;
23113 char_u numbuf[NUMBUFLEN];
23114 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023115#ifdef FEAT_PROFILE
23116 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023117 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119
23120 /* If depth of calling is getting too high, don't execute the function */
23121 if (depth >= p_mfd)
23122 {
23123 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023124 rettv->v_type = VAR_NUMBER;
23125 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 return;
23127 }
23128 ++depth;
23129
23130 line_breakcheck(); /* check for CTRL-C hit */
23131
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023132 fc = (funccall_T *)alloc(sizeof(funccall_T));
23133 fc->caller = current_funccal;
23134 current_funccal = fc;
23135 fc->func = fp;
23136 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023137 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023138 fc->linenr = 0;
23139 fc->returned = FALSE;
23140 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023142 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23143 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144
Bram Moolenaar33570922005-01-25 22:26:29 +000023145 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023146 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23148 * each argument variable and saves a lot of time.
23149 */
23150 /*
23151 * Init l: variables.
23152 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023153 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023154 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023155 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023156 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23157 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023158 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023159 name = v->di_key;
23160 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023161 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023162 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023163 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023164 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023165 v->di_tv.vval.v_dict = selfdict;
23166 ++selfdict->dv_refcount;
23167 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023168
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 /*
23170 * Init a: variables.
23171 * Set a:0 to "argcount".
23172 * Set a:000 to a list with room for the "..." arguments.
23173 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023174 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023175 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023176 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023177 /* Use "name" to avoid a warning from some compiler that checks the
23178 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023179 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023180 name = v->di_key;
23181 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023183 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023184 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023185 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023186 v->di_tv.vval.v_list = &fc->l_varlist;
23187 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23188 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23189 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023190
23191 /*
23192 * Set a:firstline to "firstline" and a:lastline to "lastline".
23193 * Set a:name to named arguments.
23194 * Set a:N to the "..." arguments.
23195 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023196 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023197 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023198 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023199 (varnumber_T)lastline);
23200 for (i = 0; i < argcount; ++i)
23201 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 if (ai < 0)
23204 /* named argument a:name */
23205 name = FUNCARG(fp, i);
23206 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023207 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023208 /* "..." argument a:1, a:2, etc. */
23209 sprintf((char *)numbuf, "%d", ai + 1);
23210 name = numbuf;
23211 }
23212 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23213 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023214 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023215 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23216 }
23217 else
23218 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023219 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23220 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 if (v == NULL)
23222 break;
23223 v->di_flags = DI_FLAGS_RO;
23224 }
23225 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023226 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023227
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023228 /* Note: the values are copied directly to avoid alloc/free.
23229 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023230 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023231 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023232
23233 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23234 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023235 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23236 fc->l_listitems[ai].li_tv = argvars[i];
23237 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023238 }
23239 }
23240
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 /* Don't redraw while executing the function. */
23242 ++RedrawingDisabled;
23243 save_sourcing_name = sourcing_name;
23244 save_sourcing_lnum = sourcing_lnum;
23245 sourcing_lnum = 1;
23246 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023247 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 if (sourcing_name != NULL)
23249 {
23250 if (save_sourcing_name != NULL
23251 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23252 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23253 else
23254 STRCPY(sourcing_name, "function ");
23255 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23256
23257 if (p_verbose >= 12)
23258 {
23259 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023260 verbose_enter_scroll();
23261
Bram Moolenaar555b2802005-05-19 21:08:39 +000023262 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 if (p_verbose >= 14)
23264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023266 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023267 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023268 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269
23270 msg_puts((char_u *)"(");
23271 for (i = 0; i < argcount; ++i)
23272 {
23273 if (i > 0)
23274 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023275 if (argvars[i].v_type == VAR_NUMBER)
23276 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 else
23278 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023279 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
23280 if (s != NULL)
23281 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023282 if (vim_strsize(s) > MSG_BUF_CLEN)
23283 {
23284 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23285 s = buf;
23286 }
23287 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023288 vim_free(tofree);
23289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 }
23291 }
23292 msg_puts((char_u *)")");
23293 }
23294 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023295
23296 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 --no_wait_return;
23298 }
23299 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023300#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023301 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023302 {
23303 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23304 func_do_profile(fp);
23305 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023306 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023307 {
23308 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023309 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023310 profile_zero(&fp->uf_tm_children);
23311 }
23312 script_prof_save(&wait_start);
23313 }
23314#endif
23315
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023317 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 save_did_emsg = did_emsg;
23319 did_emsg = FALSE;
23320
23321 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023322 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23324
23325 --RedrawingDisabled;
23326
23327 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023328 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023330 clear_tv(rettv);
23331 rettv->v_type = VAR_NUMBER;
23332 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 }
23334
Bram Moolenaar05159a02005-02-26 23:04:13 +000023335#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023336 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023337 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023338 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023339 profile_end(&call_start);
23340 profile_sub_wait(&wait_start, &call_start);
23341 profile_add(&fp->uf_tm_total, &call_start);
23342 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023343 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023344 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023345 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23346 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023347 }
23348 }
23349#endif
23350
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 /* when being verbose, mention the return value */
23352 if (p_verbose >= 12)
23353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023355 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023358 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023359 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023360 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023361 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023364 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023365 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023366 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023367 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023368
Bram Moolenaar555b2802005-05-19 21:08:39 +000023369 /* The value may be very long. Skip the middle part, so that we
23370 * have some idea how it starts and ends. smsg() would always
23371 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023372 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023373 if (s != NULL)
23374 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023375 if (vim_strsize(s) > MSG_BUF_CLEN)
23376 {
23377 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23378 s = buf;
23379 }
23380 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023381 vim_free(tofree);
23382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 }
23384 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023385
23386 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 --no_wait_return;
23388 }
23389
23390 vim_free(sourcing_name);
23391 sourcing_name = save_sourcing_name;
23392 sourcing_lnum = save_sourcing_lnum;
23393 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023394#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023395 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023396 script_prof_restore(&wait_start);
23397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398
23399 if (p_verbose >= 12 && sourcing_name != NULL)
23400 {
23401 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023402 verbose_enter_scroll();
23403
Bram Moolenaar555b2802005-05-19 21:08:39 +000023404 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023406
23407 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 --no_wait_return;
23409 }
23410
23411 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023412 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023414
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023415 /* If the a:000 list and the l: and a: dicts are not referenced we can
23416 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023417 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23418 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23419 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23420 {
23421 free_funccal(fc, FALSE);
23422 }
23423 else
23424 {
23425 hashitem_T *hi;
23426 listitem_T *li;
23427 int todo;
23428
23429 /* "fc" is still in use. This can happen when returning "a:000" or
23430 * assigning "l:" to a global variable.
23431 * Link "fc" in the list for garbage collection later. */
23432 fc->caller = previous_funccal;
23433 previous_funccal = fc;
23434
23435 /* Make a copy of the a: variables, since we didn't do that above. */
23436 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23437 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23438 {
23439 if (!HASHITEM_EMPTY(hi))
23440 {
23441 --todo;
23442 v = HI2DI(hi);
23443 copy_tv(&v->di_tv, &v->di_tv);
23444 }
23445 }
23446
23447 /* Make a copy of the a:000 items, since we didn't do that above. */
23448 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23449 copy_tv(&li->li_tv, &li->li_tv);
23450 }
23451}
23452
23453/*
23454 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023455 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023456 */
23457 static int
23458can_free_funccal(fc, copyID)
23459 funccall_T *fc;
23460 int copyID;
23461{
23462 return (fc->l_varlist.lv_copyID != copyID
23463 && fc->l_vars.dv_copyID != copyID
23464 && fc->l_avars.dv_copyID != copyID);
23465}
23466
23467/*
23468 * Free "fc" and what it contains.
23469 */
23470 static void
23471free_funccal(fc, free_val)
23472 funccall_T *fc;
23473 int free_val; /* a: vars were allocated */
23474{
23475 listitem_T *li;
23476
23477 /* The a: variables typevals may not have been allocated, only free the
23478 * allocated variables. */
23479 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23480
23481 /* free all l: variables */
23482 vars_clear(&fc->l_vars.dv_hashtab);
23483
23484 /* Free the a:000 variables if they were allocated. */
23485 if (free_val)
23486 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23487 clear_tv(&li->li_tv);
23488
23489 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490}
23491
23492/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023493 * Add a number variable "name" to dict "dp" with value "nr".
23494 */
23495 static void
23496add_nr_var(dp, v, name, nr)
23497 dict_T *dp;
23498 dictitem_T *v;
23499 char *name;
23500 varnumber_T nr;
23501{
23502 STRCPY(v->di_key, name);
23503 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23504 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23505 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023506 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 v->di_tv.vval.v_number = nr;
23508}
23509
23510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 * ":return [expr]"
23512 */
23513 void
23514ex_return(eap)
23515 exarg_T *eap;
23516{
23517 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023518 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 int returning = FALSE;
23520
23521 if (current_funccal == NULL)
23522 {
23523 EMSG(_("E133: :return not inside a function"));
23524 return;
23525 }
23526
23527 if (eap->skip)
23528 ++emsg_skip;
23529
23530 eap->nextcmd = NULL;
23531 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023532 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 {
23534 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023535 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 }
23539 /* It's safer to return also on error. */
23540 else if (!eap->skip)
23541 {
23542 /*
23543 * Return unless the expression evaluation has been cancelled due to an
23544 * aborting error, an interrupt, or an exception.
23545 */
23546 if (!aborting())
23547 returning = do_return(eap, FALSE, TRUE, NULL);
23548 }
23549
23550 /* When skipping or the return gets pending, advance to the next command
23551 * in this line (!returning). Otherwise, ignore the rest of the line.
23552 * Following lines will be ignored by get_func_line(). */
23553 if (returning)
23554 eap->nextcmd = NULL;
23555 else if (eap->nextcmd == NULL) /* no argument */
23556 eap->nextcmd = check_nextcmd(arg);
23557
23558 if (eap->skip)
23559 --emsg_skip;
23560}
23561
23562/*
23563 * Return from a function. Possibly makes the return pending. Also called
23564 * for a pending return at the ":endtry" or after returning from an extra
23565 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023566 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023567 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 * FALSE when the return gets pending.
23569 */
23570 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023571do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 exarg_T *eap;
23573 int reanimate;
23574 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023575 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576{
23577 int idx;
23578 struct condstack *cstack = eap->cstack;
23579
23580 if (reanimate)
23581 /* Undo the return. */
23582 current_funccal->returned = FALSE;
23583
23584 /*
23585 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23586 * not in its finally clause (which then is to be executed next) is found.
23587 * In this case, make the ":return" pending for execution at the ":endtry".
23588 * Otherwise, return normally.
23589 */
23590 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23591 if (idx >= 0)
23592 {
23593 cstack->cs_pending[idx] = CSTP_RETURN;
23594
23595 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023596 /* A pending return again gets pending. "rettv" points to an
23597 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023599 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 else
23601 {
23602 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023603 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023605 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023607 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 {
23609 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023610 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023611 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 else
23613 EMSG(_(e_outofmem));
23614 }
23615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023616 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617
23618 if (reanimate)
23619 {
23620 /* The pending return value could be overwritten by a ":return"
23621 * without argument in a finally clause; reset the default
23622 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023623 current_funccal->rettv->v_type = VAR_NUMBER;
23624 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 }
23626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023627 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 }
23629 else
23630 {
23631 current_funccal->returned = TRUE;
23632
23633 /* If the return is carried out now, store the return value. For
23634 * a return immediately after reanimation, the value is already
23635 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023636 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023638 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023639 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023641 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 }
23643 }
23644
23645 return idx < 0;
23646}
23647
23648/*
23649 * Free the variable with a pending return value.
23650 */
23651 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023652discard_pending_return(rettv)
23653 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654{
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656}
23657
23658/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023659 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 * is an allocated string. Used by report_pending() for verbose messages.
23661 */
23662 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023663get_return_cmd(rettv)
23664 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023666 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023667 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023668 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023670 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023671 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023672 if (s == NULL)
23673 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023674
23675 STRCPY(IObuff, ":return ");
23676 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23677 if (STRLEN(s) + 8 >= IOSIZE)
23678 STRCPY(IObuff + IOSIZE - 4, "...");
23679 vim_free(tofree);
23680 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681}
23682
23683/*
23684 * Get next function line.
23685 * Called by do_cmdline() to get the next line.
23686 * Returns allocated string, or NULL for end of function.
23687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688 char_u *
23689get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023690 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023692 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693{
Bram Moolenaar33570922005-01-25 22:26:29 +000023694 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023695 ufunc_T *fp = fcp->func;
23696 char_u *retval;
23697 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698
23699 /* If breakpoints have been added/deleted need to check for it. */
23700 if (fcp->dbg_tick != debug_tick)
23701 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023702 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 sourcing_lnum);
23704 fcp->dbg_tick = debug_tick;
23705 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023706#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023707 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023708 func_line_end(cookie);
23709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710
Bram Moolenaar05159a02005-02-26 23:04:13 +000023711 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023712 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23713 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 retval = NULL;
23715 else
23716 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023717 /* Skip NULL lines (continuation lines). */
23718 while (fcp->linenr < gap->ga_len
23719 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23720 ++fcp->linenr;
23721 if (fcp->linenr >= gap->ga_len)
23722 retval = NULL;
23723 else
23724 {
23725 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23726 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023727#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023728 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023729 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023730#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 }
23733
23734 /* Did we encounter a breakpoint? */
23735 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23736 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023737 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023739 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 sourcing_lnum);
23741 fcp->dbg_tick = debug_tick;
23742 }
23743
23744 return retval;
23745}
23746
Bram Moolenaar05159a02005-02-26 23:04:13 +000023747#if defined(FEAT_PROFILE) || defined(PROTO)
23748/*
23749 * Called when starting to read a function line.
23750 * "sourcing_lnum" must be correct!
23751 * When skipping lines it may not actually be executed, but we won't find out
23752 * until later and we need to store the time now.
23753 */
23754 void
23755func_line_start(cookie)
23756 void *cookie;
23757{
23758 funccall_T *fcp = (funccall_T *)cookie;
23759 ufunc_T *fp = fcp->func;
23760
23761 if (fp->uf_profiling && sourcing_lnum >= 1
23762 && sourcing_lnum <= fp->uf_lines.ga_len)
23763 {
23764 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023765 /* Skip continuation lines. */
23766 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23767 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023768 fp->uf_tml_execed = FALSE;
23769 profile_start(&fp->uf_tml_start);
23770 profile_zero(&fp->uf_tml_children);
23771 profile_get_wait(&fp->uf_tml_wait);
23772 }
23773}
23774
23775/*
23776 * Called when actually executing a function line.
23777 */
23778 void
23779func_line_exec(cookie)
23780 void *cookie;
23781{
23782 funccall_T *fcp = (funccall_T *)cookie;
23783 ufunc_T *fp = fcp->func;
23784
23785 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23786 fp->uf_tml_execed = TRUE;
23787}
23788
23789/*
23790 * Called when done with a function line.
23791 */
23792 void
23793func_line_end(cookie)
23794 void *cookie;
23795{
23796 funccall_T *fcp = (funccall_T *)cookie;
23797 ufunc_T *fp = fcp->func;
23798
23799 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23800 {
23801 if (fp->uf_tml_execed)
23802 {
23803 ++fp->uf_tml_count[fp->uf_tml_idx];
23804 profile_end(&fp->uf_tml_start);
23805 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023806 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023807 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23808 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023809 }
23810 fp->uf_tml_idx = -1;
23811 }
23812}
23813#endif
23814
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815/*
23816 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023817 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 */
23819 int
23820func_has_ended(cookie)
23821 void *cookie;
23822{
Bram Moolenaar33570922005-01-25 22:26:29 +000023823 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824
23825 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23826 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023827 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 || fcp->returned);
23829}
23830
23831/*
23832 * return TRUE if cookie indicates a function which "abort"s on errors.
23833 */
23834 int
23835func_has_abort(cookie)
23836 void *cookie;
23837{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023838 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839}
23840
23841#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23842typedef enum
23843{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023844 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23845 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23846 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847} var_flavour_T;
23848
23849static var_flavour_T var_flavour __ARGS((char_u *varname));
23850
23851 static var_flavour_T
23852var_flavour(varname)
23853 char_u *varname;
23854{
23855 char_u *p = varname;
23856
23857 if (ASCII_ISUPPER(*p))
23858 {
23859 while (*(++p))
23860 if (ASCII_ISLOWER(*p))
23861 return VAR_FLAVOUR_SESSION;
23862 return VAR_FLAVOUR_VIMINFO;
23863 }
23864 else
23865 return VAR_FLAVOUR_DEFAULT;
23866}
23867#endif
23868
23869#if defined(FEAT_VIMINFO) || defined(PROTO)
23870/*
23871 * Restore global vars that start with a capital from the viminfo file
23872 */
23873 int
23874read_viminfo_varlist(virp, writing)
23875 vir_T *virp;
23876 int writing;
23877{
23878 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023879 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023880 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881
23882 if (!writing && (find_viminfo_parameter('!') != NULL))
23883 {
23884 tab = vim_strchr(virp->vir_line + 1, '\t');
23885 if (tab != NULL)
23886 {
23887 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023888 switch (*tab)
23889 {
23890 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023891#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023892 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023893#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023894 case 'D': type = VAR_DICT; break;
23895 case 'L': type = VAR_LIST; break;
23896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897
23898 tab = vim_strchr(tab, '\t');
23899 if (tab != NULL)
23900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023901 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023902 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023903 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023905#ifdef FEAT_FLOAT
23906 else if (type == VAR_FLOAT)
23907 (void)string2float(tab + 1, &tv.vval.v_float);
23908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023910 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023911 if (type == VAR_DICT || type == VAR_LIST)
23912 {
23913 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23914
23915 if (etv == NULL)
23916 /* Failed to parse back the dict or list, use it as a
23917 * string. */
23918 tv.v_type = VAR_STRING;
23919 else
23920 {
23921 vim_free(tv.vval.v_string);
23922 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023923 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023924 }
23925 }
23926
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023927 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023928
23929 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023930 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023931 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23932 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 }
23934 }
23935 }
23936
23937 return viminfo_readline(virp);
23938}
23939
23940/*
23941 * Write global vars that start with a capital to the viminfo file
23942 */
23943 void
23944write_viminfo_varlist(fp)
23945 FILE *fp;
23946{
Bram Moolenaar33570922005-01-25 22:26:29 +000023947 hashitem_T *hi;
23948 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023949 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023950 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023951 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023952 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023953 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954
23955 if (find_viminfo_parameter('!') == NULL)
23956 return;
23957
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023958 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023959
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023960 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023961 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023963 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023965 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023966 this_var = HI2DI(hi);
23967 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023968 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023969 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023970 {
23971 case VAR_STRING: s = "STR"; break;
23972 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023973#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023974 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023975#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023976 case VAR_DICT: s = "DIC"; break;
23977 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023978 default: continue;
23979 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023980 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023981 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023982 if (p != NULL)
23983 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023984 vim_free(tofree);
23985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 }
23987 }
23988}
23989#endif
23990
23991#if defined(FEAT_SESSION) || defined(PROTO)
23992 int
23993store_session_globals(fd)
23994 FILE *fd;
23995{
Bram Moolenaar33570922005-01-25 22:26:29 +000023996 hashitem_T *hi;
23997 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023998 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 char_u *p, *t;
24000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024001 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024002 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024006 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024007 this_var = HI2DI(hi);
24008 if ((this_var->di_tv.v_type == VAR_NUMBER
24009 || this_var->di_tv.v_type == VAR_STRING)
24010 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024011 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024012 /* Escape special characters with a backslash. Turn a LF and
24013 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024014 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024015 (char_u *)"\\\"\n\r");
24016 if (p == NULL) /* out of memory */
24017 break;
24018 for (t = p; *t != NUL; ++t)
24019 if (*t == '\n')
24020 *t = 'n';
24021 else if (*t == '\r')
24022 *t = 'r';
24023 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024024 this_var->di_key,
24025 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24026 : ' ',
24027 p,
24028 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24029 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024030 || put_eol(fd) == FAIL)
24031 {
24032 vim_free(p);
24033 return FAIL;
24034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 vim_free(p);
24036 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024037#ifdef FEAT_FLOAT
24038 else if (this_var->di_tv.v_type == VAR_FLOAT
24039 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24040 {
24041 float_T f = this_var->di_tv.vval.v_float;
24042 int sign = ' ';
24043
24044 if (f < 0)
24045 {
24046 f = -f;
24047 sign = '-';
24048 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024049 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024050 this_var->di_key, sign, f) < 0)
24051 || put_eol(fd) == FAIL)
24052 return FAIL;
24053 }
24054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 }
24056 }
24057 return OK;
24058}
24059#endif
24060
Bram Moolenaar661b1822005-07-28 22:36:45 +000024061/*
24062 * Display script name where an item was last set.
24063 * Should only be invoked when 'verbose' is non-zero.
24064 */
24065 void
24066last_set_msg(scriptID)
24067 scid_T scriptID;
24068{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024069 char_u *p;
24070
Bram Moolenaar661b1822005-07-28 22:36:45 +000024071 if (scriptID != 0)
24072 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024073 p = home_replace_save(NULL, get_scriptname(scriptID));
24074 if (p != NULL)
24075 {
24076 verbose_enter();
24077 MSG_PUTS(_("\n\tLast set from "));
24078 MSG_PUTS(p);
24079 vim_free(p);
24080 verbose_leave();
24081 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024082 }
24083}
24084
Bram Moolenaard812df62008-11-09 12:46:09 +000024085/*
24086 * List v:oldfiles in a nice way.
24087 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024088 void
24089ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024090 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024091{
24092 list_T *l = vimvars[VV_OLDFILES].vv_list;
24093 listitem_T *li;
24094 int nr = 0;
24095
24096 if (l == NULL)
24097 msg((char_u *)_("No old files"));
24098 else
24099 {
24100 msg_start();
24101 msg_scroll = TRUE;
24102 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24103 {
24104 msg_outnum((long)++nr);
24105 MSG_PUTS(": ");
24106 msg_outtrans(get_tv_string(&li->li_tv));
24107 msg_putchar('\n');
24108 out_flush(); /* output one line at a time */
24109 ui_breakcheck();
24110 }
24111 /* Assume "got_int" was set to truncate the listing. */
24112 got_int = FALSE;
24113
24114#ifdef FEAT_BROWSE_CMD
24115 if (cmdmod.browse)
24116 {
24117 quit_more = FALSE;
24118 nr = prompt_for_number(FALSE);
24119 msg_starthere();
24120 if (nr > 0)
24121 {
24122 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24123 (long)nr);
24124
24125 if (p != NULL)
24126 {
24127 p = expand_env_save(p);
24128 eap->arg = p;
24129 eap->cmdidx = CMD_edit;
24130 cmdmod.browse = FALSE;
24131 do_exedit(eap, NULL);
24132 vim_free(p);
24133 }
24134 }
24135 }
24136#endif
24137 }
24138}
24139
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140#endif /* FEAT_EVAL */
24141
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024143#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144
24145#ifdef WIN3264
24146/*
24147 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24148 */
24149static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24150static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24151static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24152
24153/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024154 * Get the short path (8.3) for the filename in "fnamep".
24155 * Only works for a valid file name.
24156 * When the path gets longer "fnamep" is changed and the allocated buffer
24157 * is put in "bufp".
24158 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24159 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 */
24161 static int
24162get_short_pathname(fnamep, bufp, fnamelen)
24163 char_u **fnamep;
24164 char_u **bufp;
24165 int *fnamelen;
24166{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024167 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 char_u *newbuf;
24169
24170 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 l = GetShortPathName(*fnamep, *fnamep, len);
24172 if (l > len - 1)
24173 {
24174 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024175 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 newbuf = vim_strnsave(*fnamep, l);
24177 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024178 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179
24180 vim_free(*bufp);
24181 *fnamep = *bufp = newbuf;
24182
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024183 /* Really should always succeed, as the buffer is big enough. */
24184 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 }
24186
24187 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024188 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189}
24190
24191/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024192 * Get the short path (8.3) for the filename in "fname". The converted
24193 * path is returned in "bufp".
24194 *
24195 * Some of the directories specified in "fname" may not exist. This function
24196 * will shorten the existing directories at the beginning of the path and then
24197 * append the remaining non-existing path.
24198 *
24199 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024200 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024201 * bufp - Pointer to an allocated buffer for the filename.
24202 * fnamelen - Length of the filename pointed to by fname
24203 *
24204 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 */
24206 static int
24207shortpath_for_invalid_fname(fname, bufp, fnamelen)
24208 char_u **fname;
24209 char_u **bufp;
24210 int *fnamelen;
24211{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024212 char_u *short_fname, *save_fname, *pbuf_unused;
24213 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024215 int old_len, len;
24216 int new_len, sfx_len;
24217 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218
24219 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024220 old_len = *fnamelen;
24221 save_fname = vim_strnsave(*fname, old_len);
24222 pbuf_unused = NULL;
24223 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024225 endp = save_fname + old_len - 1; /* Find the end of the copy */
24226 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024228 /*
24229 * Try shortening the supplied path till it succeeds by removing one
24230 * directory at a time from the tail of the path.
24231 */
24232 len = 0;
24233 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024235 /* go back one path-separator */
24236 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24237 --endp;
24238 if (endp <= save_fname)
24239 break; /* processed the complete path */
24240
24241 /*
24242 * Replace the path separator with a NUL and try to shorten the
24243 * resulting path.
24244 */
24245 ch = *endp;
24246 *endp = 0;
24247 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024248 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024249 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24250 {
24251 retval = FAIL;
24252 goto theend;
24253 }
24254 *endp = ch; /* preserve the string */
24255
24256 if (len > 0)
24257 break; /* successfully shortened the path */
24258
24259 /* failed to shorten the path. Skip the path separator */
24260 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 }
24262
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024263 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024265 /*
24266 * Succeeded in shortening the path. Now concatenate the shortened
24267 * path with the remaining path at the tail.
24268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024270 /* Compute the length of the new path. */
24271 sfx_len = (int)(save_endp - endp) + 1;
24272 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024274 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024276 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024278 /* There is not enough space in the currently allocated string,
24279 * copy it to a buffer big enough. */
24280 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024282 {
24283 retval = FAIL;
24284 goto theend;
24285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 }
24287 else
24288 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024289 /* Transfer short_fname to the main buffer (it's big enough),
24290 * unless get_short_pathname() did its work in-place. */
24291 *fname = *bufp = save_fname;
24292 if (short_fname != save_fname)
24293 vim_strncpy(save_fname, short_fname, len);
24294 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024296
24297 /* concat the not-shortened part of the path */
24298 vim_strncpy(*fname + len, endp, sfx_len);
24299 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024301
24302theend:
24303 vim_free(pbuf_unused);
24304 vim_free(save_fname);
24305
24306 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307}
24308
24309/*
24310 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024311 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312 */
24313 static int
24314shortpath_for_partial(fnamep, bufp, fnamelen)
24315 char_u **fnamep;
24316 char_u **bufp;
24317 int *fnamelen;
24318{
24319 int sepcount, len, tflen;
24320 char_u *p;
24321 char_u *pbuf, *tfname;
24322 int hasTilde;
24323
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024324 /* Count up the path separators from the RHS.. so we know which part
24325 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024327 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 if (vim_ispathsep(*p))
24329 ++sepcount;
24330
24331 /* Need full path first (use expand_env() to remove a "~/") */
24332 hasTilde = (**fnamep == '~');
24333 if (hasTilde)
24334 pbuf = tfname = expand_env_save(*fnamep);
24335 else
24336 pbuf = tfname = FullName_save(*fnamep, FALSE);
24337
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024338 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024340 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342
24343 if (len == 0)
24344 {
24345 /* Don't have a valid filename, so shorten the rest of the
24346 * path if we can. This CAN give us invalid 8.3 filenames, but
24347 * there's not a lot of point in guessing what it might be.
24348 */
24349 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024350 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 }
24353
24354 /* Count the paths backward to find the beginning of the desired string. */
24355 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024356 {
24357#ifdef FEAT_MBYTE
24358 if (has_mbyte)
24359 p -= mb_head_off(tfname, p);
24360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361 if (vim_ispathsep(*p))
24362 {
24363 if (sepcount == 0 || (hasTilde && sepcount == 1))
24364 break;
24365 else
24366 sepcount --;
24367 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 if (hasTilde)
24370 {
24371 --p;
24372 if (p >= tfname)
24373 *p = '~';
24374 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024375 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 }
24377 else
24378 ++p;
24379
24380 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24381 vim_free(*bufp);
24382 *fnamelen = (int)STRLEN(p);
24383 *bufp = pbuf;
24384 *fnamep = p;
24385
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024386 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387}
24388#endif /* WIN3264 */
24389
24390/*
24391 * Adjust a filename, according to a string of modifiers.
24392 * *fnamep must be NUL terminated when called. When returning, the length is
24393 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024394 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 * When there is an error, *fnamep is set to NULL.
24396 */
24397 int
24398modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24399 char_u *src; /* string with modifiers */
24400 int *usedlen; /* characters after src that are used */
24401 char_u **fnamep; /* file name so far */
24402 char_u **bufp; /* buffer for allocated file name or NULL */
24403 int *fnamelen; /* length of fnamep */
24404{
24405 int valid = 0;
24406 char_u *tail;
24407 char_u *s, *p, *pbuf;
24408 char_u dirname[MAXPATHL];
24409 int c;
24410 int has_fullname = 0;
24411#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024412 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 int has_shortname = 0;
24414#endif
24415
24416repeat:
24417 /* ":p" - full path/file_name */
24418 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24419 {
24420 has_fullname = 1;
24421
24422 valid |= VALID_PATH;
24423 *usedlen += 2;
24424
24425 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24426 if ((*fnamep)[0] == '~'
24427#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24428 && ((*fnamep)[1] == '/'
24429# ifdef BACKSLASH_IN_FILENAME
24430 || (*fnamep)[1] == '\\'
24431# endif
24432 || (*fnamep)[1] == NUL)
24433
24434#endif
24435 )
24436 {
24437 *fnamep = expand_env_save(*fnamep);
24438 vim_free(*bufp); /* free any allocated file name */
24439 *bufp = *fnamep;
24440 if (*fnamep == NULL)
24441 return -1;
24442 }
24443
24444 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024445 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 {
24447 if (vim_ispathsep(*p)
24448 && p[1] == '.'
24449 && (p[2] == NUL
24450 || vim_ispathsep(p[2])
24451 || (p[2] == '.'
24452 && (p[3] == NUL || vim_ispathsep(p[3])))))
24453 break;
24454 }
24455
24456 /* FullName_save() is slow, don't use it when not needed. */
24457 if (*p != NUL || !vim_isAbsName(*fnamep))
24458 {
24459 *fnamep = FullName_save(*fnamep, *p != NUL);
24460 vim_free(*bufp); /* free any allocated file name */
24461 *bufp = *fnamep;
24462 if (*fnamep == NULL)
24463 return -1;
24464 }
24465
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024466#ifdef WIN3264
24467# if _WIN32_WINNT >= 0x0500
24468 if (vim_strchr(*fnamep, '~') != NULL)
24469 {
24470 /* Expand 8.3 filename to full path. Needed to make sure the same
24471 * file does not have two different names.
24472 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24473 p = alloc(_MAX_PATH + 1);
24474 if (p != NULL)
24475 {
24476 if (GetLongPathName(*fnamep, p, MAXPATHL))
24477 {
24478 vim_free(*bufp);
24479 *bufp = *fnamep = p;
24480 }
24481 else
24482 vim_free(p);
24483 }
24484 }
24485# endif
24486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 /* Append a path separator to a directory. */
24488 if (mch_isdir(*fnamep))
24489 {
24490 /* Make room for one or two extra characters. */
24491 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24492 vim_free(*bufp); /* free any allocated file name */
24493 *bufp = *fnamep;
24494 if (*fnamep == NULL)
24495 return -1;
24496 add_pathsep(*fnamep);
24497 }
24498 }
24499
24500 /* ":." - path relative to the current directory */
24501 /* ":~" - path relative to the home directory */
24502 /* ":8" - shortname path - postponed till after */
24503 while (src[*usedlen] == ':'
24504 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24505 {
24506 *usedlen += 2;
24507 if (c == '8')
24508 {
24509#ifdef WIN3264
24510 has_shortname = 1; /* Postpone this. */
24511#endif
24512 continue;
24513 }
24514 pbuf = NULL;
24515 /* Need full path first (use expand_env() to remove a "~/") */
24516 if (!has_fullname)
24517 {
24518 if (c == '.' && **fnamep == '~')
24519 p = pbuf = expand_env_save(*fnamep);
24520 else
24521 p = pbuf = FullName_save(*fnamep, FALSE);
24522 }
24523 else
24524 p = *fnamep;
24525
24526 has_fullname = 0;
24527
24528 if (p != NULL)
24529 {
24530 if (c == '.')
24531 {
24532 mch_dirname(dirname, MAXPATHL);
24533 s = shorten_fname(p, dirname);
24534 if (s != NULL)
24535 {
24536 *fnamep = s;
24537 if (pbuf != NULL)
24538 {
24539 vim_free(*bufp); /* free any allocated file name */
24540 *bufp = pbuf;
24541 pbuf = NULL;
24542 }
24543 }
24544 }
24545 else
24546 {
24547 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24548 /* Only replace it when it starts with '~' */
24549 if (*dirname == '~')
24550 {
24551 s = vim_strsave(dirname);
24552 if (s != NULL)
24553 {
24554 *fnamep = s;
24555 vim_free(*bufp);
24556 *bufp = s;
24557 }
24558 }
24559 }
24560 vim_free(pbuf);
24561 }
24562 }
24563
24564 tail = gettail(*fnamep);
24565 *fnamelen = (int)STRLEN(*fnamep);
24566
24567 /* ":h" - head, remove "/file_name", can be repeated */
24568 /* Don't remove the first "/" or "c:\" */
24569 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24570 {
24571 valid |= VALID_HEAD;
24572 *usedlen += 2;
24573 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024574 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024575 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 *fnamelen = (int)(tail - *fnamep);
24577#ifdef VMS
24578 if (*fnamelen > 0)
24579 *fnamelen += 1; /* the path separator is part of the path */
24580#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024581 if (*fnamelen == 0)
24582 {
24583 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24584 p = vim_strsave((char_u *)".");
24585 if (p == NULL)
24586 return -1;
24587 vim_free(*bufp);
24588 *bufp = *fnamep = tail = p;
24589 *fnamelen = 1;
24590 }
24591 else
24592 {
24593 while (tail > s && !after_pathsep(s, tail))
24594 mb_ptr_back(*fnamep, tail);
24595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 }
24597
24598 /* ":8" - shortname */
24599 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24600 {
24601 *usedlen += 2;
24602#ifdef WIN3264
24603 has_shortname = 1;
24604#endif
24605 }
24606
24607#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024608 /*
24609 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 */
24611 if (has_shortname)
24612 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024613 /* Copy the string if it is shortened by :h and when it wasn't copied
24614 * yet, because we are going to change it in place. Avoids changing
24615 * the buffer name for "%:8". */
24616 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 {
24618 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024619 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 return -1;
24621 vim_free(*bufp);
24622 *bufp = *fnamep = p;
24623 }
24624
24625 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024626 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 if (!has_fullname && !vim_isAbsName(*fnamep))
24628 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024629 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 return -1;
24631 }
24632 else
24633 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024634 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635
Bram Moolenaardc935552011-08-17 15:23:23 +020024636 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024638 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 return -1;
24640
24641 if (l == 0)
24642 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024643 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024645 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 return -1;
24647 }
24648 *fnamelen = l;
24649 }
24650 }
24651#endif /* WIN3264 */
24652
24653 /* ":t" - tail, just the basename */
24654 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24655 {
24656 *usedlen += 2;
24657 *fnamelen -= (int)(tail - *fnamep);
24658 *fnamep = tail;
24659 }
24660
24661 /* ":e" - extension, can be repeated */
24662 /* ":r" - root, without extension, can be repeated */
24663 while (src[*usedlen] == ':'
24664 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24665 {
24666 /* find a '.' in the tail:
24667 * - for second :e: before the current fname
24668 * - otherwise: The last '.'
24669 */
24670 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24671 s = *fnamep - 2;
24672 else
24673 s = *fnamep + *fnamelen - 1;
24674 for ( ; s > tail; --s)
24675 if (s[0] == '.')
24676 break;
24677 if (src[*usedlen + 1] == 'e') /* :e */
24678 {
24679 if (s > tail)
24680 {
24681 *fnamelen += (int)(*fnamep - (s + 1));
24682 *fnamep = s + 1;
24683#ifdef VMS
24684 /* cut version from the extension */
24685 s = *fnamep + *fnamelen - 1;
24686 for ( ; s > *fnamep; --s)
24687 if (s[0] == ';')
24688 break;
24689 if (s > *fnamep)
24690 *fnamelen = s - *fnamep;
24691#endif
24692 }
24693 else if (*fnamep <= tail)
24694 *fnamelen = 0;
24695 }
24696 else /* :r */
24697 {
24698 if (s > tail) /* remove one extension */
24699 *fnamelen = (int)(s - *fnamep);
24700 }
24701 *usedlen += 2;
24702 }
24703
24704 /* ":s?pat?foo?" - substitute */
24705 /* ":gs?pat?foo?" - global substitute */
24706 if (src[*usedlen] == ':'
24707 && (src[*usedlen + 1] == 's'
24708 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24709 {
24710 char_u *str;
24711 char_u *pat;
24712 char_u *sub;
24713 int sep;
24714 char_u *flags;
24715 int didit = FALSE;
24716
24717 flags = (char_u *)"";
24718 s = src + *usedlen + 2;
24719 if (src[*usedlen + 1] == 'g')
24720 {
24721 flags = (char_u *)"g";
24722 ++s;
24723 }
24724
24725 sep = *s++;
24726 if (sep)
24727 {
24728 /* find end of pattern */
24729 p = vim_strchr(s, sep);
24730 if (p != NULL)
24731 {
24732 pat = vim_strnsave(s, (int)(p - s));
24733 if (pat != NULL)
24734 {
24735 s = p + 1;
24736 /* find end of substitution */
24737 p = vim_strchr(s, sep);
24738 if (p != NULL)
24739 {
24740 sub = vim_strnsave(s, (int)(p - s));
24741 str = vim_strnsave(*fnamep, *fnamelen);
24742 if (sub != NULL && str != NULL)
24743 {
24744 *usedlen = (int)(p + 1 - src);
24745 s = do_string_sub(str, pat, sub, flags);
24746 if (s != NULL)
24747 {
24748 *fnamep = s;
24749 *fnamelen = (int)STRLEN(s);
24750 vim_free(*bufp);
24751 *bufp = s;
24752 didit = TRUE;
24753 }
24754 }
24755 vim_free(sub);
24756 vim_free(str);
24757 }
24758 vim_free(pat);
24759 }
24760 }
24761 /* after using ":s", repeat all the modifiers */
24762 if (didit)
24763 goto repeat;
24764 }
24765 }
24766
Bram Moolenaar26df0922014-02-23 23:39:13 +010024767 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24768 {
24769 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24770 if (p == NULL)
24771 return -1;
24772 vim_free(*bufp);
24773 *bufp = *fnamep = p;
24774 *fnamelen = (int)STRLEN(p);
24775 *usedlen += 2;
24776 }
24777
Bram Moolenaar071d4272004-06-13 20:20:40 +000024778 return valid;
24779}
24780
24781/*
24782 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24783 * "flags" can be "g" to do a global substitute.
24784 * Returns an allocated string, NULL for error.
24785 */
24786 char_u *
24787do_string_sub(str, pat, sub, flags)
24788 char_u *str;
24789 char_u *pat;
24790 char_u *sub;
24791 char_u *flags;
24792{
24793 int sublen;
24794 regmatch_T regmatch;
24795 int i;
24796 int do_all;
24797 char_u *tail;
24798 garray_T ga;
24799 char_u *ret;
24800 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024801 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802
24803 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24804 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024805 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024806
24807 ga_init2(&ga, 1, 200);
24808
24809 do_all = (flags[0] == 'g');
24810
24811 regmatch.rm_ic = p_ic;
24812 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24813 if (regmatch.regprog != NULL)
24814 {
24815 tail = str;
24816 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24817 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024818 /* Skip empty match except for first match. */
24819 if (regmatch.startp[0] == regmatch.endp[0])
24820 {
24821 if (zero_width == regmatch.startp[0])
24822 {
24823 /* avoid getting stuck on a match with an empty string */
24824 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24825 ++ga.ga_len;
24826 continue;
24827 }
24828 zero_width = regmatch.startp[0];
24829 }
24830
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 /*
24832 * Get some space for a temporary buffer to do the substitution
24833 * into. It will contain:
24834 * - The text up to where the match is.
24835 * - The substituted text.
24836 * - The text after the match.
24837 */
24838 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24839 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24840 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24841 {
24842 ga_clear(&ga);
24843 break;
24844 }
24845
24846 /* copy the text up to where the match is */
24847 i = (int)(regmatch.startp[0] - tail);
24848 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24849 /* add the substituted text */
24850 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24851 + ga.ga_len + i, TRUE, TRUE, FALSE);
24852 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024853 tail = regmatch.endp[0];
24854 if (*tail == NUL)
24855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 if (!do_all)
24857 break;
24858 }
24859
24860 if (ga.ga_data != NULL)
24861 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24862
Bram Moolenaar473de612013-06-08 18:19:48 +020024863 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 }
24865
24866 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24867 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024868 if (p_cpo == empty_option)
24869 p_cpo = save_cpo;
24870 else
24871 /* Darn, evaluating {sub} expression changed the value. */
24872 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873
24874 return ret;
24875}
24876
24877#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */