blob: 65419cb367b5e732dcb67d620254cf1fabf0ae71 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200375static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000376#define vimvarht vimvardict.dv_hashtab
377
Bram Moolenaara40058a2005-07-11 22:42:07 +0000378static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
379static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000380static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
381static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
382static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
384static void list_glob_vars __ARGS((int *first));
385static void list_buf_vars __ARGS((int *first));
386static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000387#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000388static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000389#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000390static void list_vim_vars __ARGS((int *first));
391static void list_script_vars __ARGS((int *first));
392static void list_func_vars __ARGS((int *first));
393static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000394static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
395static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100396static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static void clear_lval __ARGS((lval_T *lp));
398static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
399static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
401static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
402static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
403static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
404static void item_lock __ARGS((typval_T *tv, int deep, int lock));
405static int tv_islocked __ARGS((typval_T *tv));
406
Bram Moolenaar33570922005-01-25 22:26:29 +0000407static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
408static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000413static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
414static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000416static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000421static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100423static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
424static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
425static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000426static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000428static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
430static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000431static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000432static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100433static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000435static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200436static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
438static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000439static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000441static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000443static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
444static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000446#ifdef FEAT_FLOAT
447static int string2float __ARGS((char_u *text, float_T *value));
448#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
450static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100451static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200453static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000454static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000455static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#ifdef FEAT_FLOAT
458static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200459static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100462static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200468static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000469static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200470static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000472static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100481static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100483static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#ifdef FEAT_FLOAT
486static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
487#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000488static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000491static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000493#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000494static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000495static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
501static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200502static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000504static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
507static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200517static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200519#ifdef FEAT_FLOAT
520static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000524static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000525static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
531static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200533static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000535static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000536static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000544static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000546static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000552static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000560static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000561static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000562static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000563static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200566static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000567static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000575static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000576static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000589static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100594static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000596static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000597static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200609static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000610static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
611#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200612#ifdef FEAT_LUA
613static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
614#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000620static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000621static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627#ifdef vim_mkdir
628static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000630static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100631#ifdef FEAT_MZSCHEME
632static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
635static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100636static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000637static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000638#ifdef FEAT_FLOAT
639static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000642static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000643static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200644#ifdef FEAT_PYTHON3
645static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
647#ifdef FEAT_PYTHON
648static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
649#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000651static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000652static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
665static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
666#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200667static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100669static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000672static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000674static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000681static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000682static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000683static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000684static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000685static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200686static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000687static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100689#ifdef FEAT_CRYPT
690static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
691#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200693static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
696static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200697static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000699static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000700static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000701static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
705static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000708static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200709static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710#ifdef HAVE_STRFTIME
711static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
712#endif
713static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200719static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200720static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000726static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200727static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000730static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000731static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000732static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000733static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000735static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200736#ifdef FEAT_FLOAT
737static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
739#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000740static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000743#ifdef FEAT_FLOAT
744static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
745#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200747static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200748static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100749static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000750static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100753static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000760static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
761static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000763static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100764static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000766static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000767static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int get_env_len __ARGS((char_u **arg));
769static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
772#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
773#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
774 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000775static 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 +0000776static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000777static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100778static 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 +0000779static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static typval_T *alloc_tv __ARGS((void));
781static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static void init_tv __ARGS((typval_T *varp));
783static long get_tv_number __ARGS((typval_T *varp));
784static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000785static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static char_u *get_tv_string __ARGS((typval_T *varp));
787static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000788static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100789static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
790static 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 +0000791static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
792static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
793static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000794static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
795static 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 +0000796static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
797static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000798static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200799static int var_check_func_name __ARGS((char_u *name, int new_var));
800static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000801static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000802static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000803static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
804static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
805static int eval_fname_script __ARGS((char_u *p));
806static int eval_fname_sid __ARGS((char_u *p));
807static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000808static ufunc_T *find_func __ARGS((char_u *name));
809static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000810static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
812static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000813static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
814static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
815static int
816# ifdef __BORLANDC__
817 _RTLENTRYF
818# endif
819 prof_total_cmp __ARGS((const void *s1, const void *s2));
820static int
821# ifdef __BORLANDC__
822 _RTLENTRYF
823# endif
824 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200826static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000827static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000828static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static 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 +0000831static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
832static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
835static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000836static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000837static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000838static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000839
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200840
841#ifdef EBCDIC
842static int compare_func_name __ARGS((const void *s1, const void *s2));
843static void sortFunctions __ARGS(());
844#endif
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200855 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
856 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200857 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000858 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000859 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
861 for (i = 0; i < VV_LEN; ++i)
862 {
863 p = &vimvars[i];
864 STRCPY(p->vv_di.di_key, p->vv_name);
865 if (p->vv_flags & VV_RO)
866 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
867 else if (p->vv_flags & VV_RO_SBX)
868 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
869 else
870 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000871
872 /* add to v: scope dict, unless the value is not always available */
873 if (p->vv_type != VAR_UNKNOWN)
874 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000876 /* add to compat scope dict */
877 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000878 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000879 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100880 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200881 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200882
883#ifdef EBCDIC
884 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100885 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200886 */
887 sortFunctions();
888#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000889}
890
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000891#if defined(EXITFREE) || defined(PROTO)
892 void
893eval_clear()
894{
895 int i;
896 struct vimvar *p;
897
898 for (i = 0; i < VV_LEN; ++i)
899 {
900 p = &vimvars[i];
901 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000903 vim_free(p->vv_str);
904 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000905 }
906 else if (p->vv_di.di_tv.v_type == VAR_LIST)
907 {
908 list_unref(p->vv_list);
909 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 }
912 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000913 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914 hash_clear(&compat_hashtab);
915
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100917# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200918 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100919# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000920
921 /* global variables */
922 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000923
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000924 /* autoloaded script names */
925 ga_clear_strings(&ga_loaded);
926
Bram Moolenaarcca74132013-09-25 21:00:28 +0200927 /* Script-local variables. First clear all the variables and in a second
928 * loop free the scriptvar_T, because a variable in one script might hold
929 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200932 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200933 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200934 ga_clear(&ga_scripts);
935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000936 /* unreferenced lists and dicts */
937 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000938
939 /* functions */
940 free_all_functions();
941 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942}
943#endif
944
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 * Return the name of the executed function.
947 */
948 char_u *
949func_name(cookie)
950 void *cookie;
951{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000952 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the next breakpoint line for a funccall cookie.
957 */
958 linenr_T *
959func_breakpoint(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the debug tick for a funccall cookie.
967 */
968 int *
969func_dbg_tick(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the nesting level for a funccall cookie.
977 */
978 int
979func_level(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000986funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000988/* pointer to list of previously used funccal, still around because some
989 * item in it is still being used. */
990funccall_T *previous_funccal = NULL;
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/*
993 * Return TRUE when a function was ended by a ":return" command.
994 */
995 int
996current_func_returned()
997{
998 return current_funccal->returned;
999}
1000
1001
1002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * Set an internal variable to a string value. Creates the variable if it does
1004 * not already exist.
1005 */
1006 void
1007set_internal_string_var(name, value)
1008 char_u *name;
1009 char_u *value;
1010{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 val = vim_strsave(value);
1015 if (val != NULL)
1016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001018 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001020 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001021 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023 }
1024}
1025
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001027static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028static char_u *redir_endp = NULL;
1029static char_u *redir_varname = NULL;
1030
1031/*
1032 * Start recording command output to a variable
1033 * Returns OK if successfully completed the setup. FAIL otherwise.
1034 */
1035 int
1036var_redir_start(name, append)
1037 char_u *name;
1038 int append; /* append to an existing variable */
1039{
1040 int save_emsg;
1041 int err;
1042 typval_T tv;
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001045 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 {
1047 EMSG(_(e_invarg));
1048 return FAIL;
1049 }
1050
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001051 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 redir_varname = vim_strsave(name);
1053 if (redir_varname == NULL)
1054 return FAIL;
1055
1056 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1057 if (redir_lval == NULL)
1058 {
1059 var_redir_stop();
1060 return FAIL;
1061 }
1062
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001063 /* The output is stored in growarray "redir_ga" until redirection ends. */
1064 ga_init2(&redir_ga, (int)sizeof(char), 500);
1065
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001067 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001068 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1070 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001071 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072 if (redir_endp != NULL && *redir_endp != NUL)
1073 /* Trailing characters are present after the variable name */
1074 EMSG(_(e_trailing));
1075 else
1076 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001077 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 var_redir_stop();
1079 return FAIL;
1080 }
1081
1082 /* check if we can write to the variable: set it to or append an empty
1083 * string */
1084 save_emsg = did_emsg;
1085 did_emsg = FALSE;
1086 tv.v_type = VAR_STRING;
1087 tv.vval.v_string = (char_u *)"";
1088 if (append)
1089 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1090 else
1091 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001092 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001094 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 if (err)
1096 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001097 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 var_redir_stop();
1099 return FAIL;
1100 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106 * Append "value[value_len]" to the variable set by var_redir_start().
1107 * The actual appending is postponed until redirection ends, because the value
1108 * appended may in fact be the string we write to, changing it may cause freed
1109 * memory to be used:
1110 * :redir => foo
1111 * :let foo
1112 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 */
1114 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
1121 if (redir_lval == NULL)
1122 return;
1123
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001127 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 {
1131 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001132 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133 }
1134 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136}
1137
1138/*
1139 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 */
1142 void
1143var_redir_stop()
1144{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001145 typval_T tv;
1146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 if (redir_lval != NULL)
1148 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* If there was no error: assign the text to the variable. */
1150 if (redir_endp != NULL)
1151 {
1152 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1153 tv.v_type = VAR_STRING;
1154 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 /* Call get_lval() again, if it's inside a Dict or List it may
1156 * have changed. */
1157 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001158 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1160 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1161 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001163
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001164 /* free the collected output */
1165 vim_free(redir_ga.ga_data);
1166 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 vim_free(redir_lval);
1169 redir_lval = NULL;
1170 }
1171 vim_free(redir_varname);
1172 redir_varname = NULL;
1173}
1174
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175# if defined(FEAT_MBYTE) || defined(PROTO)
1176 int
1177eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1178 char_u *enc_from;
1179 char_u *enc_to;
1180 char_u *fname_from;
1181 char_u *fname_to;
1182{
1183 int err = FALSE;
1184
1185 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1186 set_vim_var_string(VV_CC_TO, enc_to, -1);
1187 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1188 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1189 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1190 err = TRUE;
1191 set_vim_var_string(VV_CC_FROM, NULL, -1);
1192 set_vim_var_string(VV_CC_TO, NULL, -1);
1193 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1194 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1195
1196 if (err)
1197 return FAIL;
1198 return OK;
1199}
1200# endif
1201
1202# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1203 int
1204eval_printexpr(fname, args)
1205 char_u *fname;
1206 char_u *args;
1207{
1208 int err = FALSE;
1209
1210 set_vim_var_string(VV_FNAME_IN, fname, -1);
1211 set_vim_var_string(VV_CMDARG, args, -1);
1212 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1213 err = TRUE;
1214 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1215 set_vim_var_string(VV_CMDARG, NULL, -1);
1216
1217 if (err)
1218 {
1219 mch_remove(fname);
1220 return FAIL;
1221 }
1222 return OK;
1223}
1224# endif
1225
1226# if defined(FEAT_DIFF) || defined(PROTO)
1227 void
1228eval_diff(origfile, newfile, outfile)
1229 char_u *origfile;
1230 char_u *newfile;
1231 char_u *outfile;
1232{
1233 int err = FALSE;
1234
1235 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1236 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1237 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1238 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1239 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1240 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1241 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1242}
1243
1244 void
1245eval_patch(origfile, difffile, outfile)
1246 char_u *origfile;
1247 char_u *difffile;
1248 char_u *outfile;
1249{
1250 int err;
1251
1252 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1253 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1254 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1255 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1258 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1259}
1260# endif
1261
1262/*
1263 * Top level evaluation function, returning a boolean.
1264 * Sets "error" to TRUE if there was an error.
1265 * Return TRUE or FALSE.
1266 */
1267 int
1268eval_to_bool(arg, error, nextcmd, skip)
1269 char_u *arg;
1270 int *error;
1271 char_u **nextcmd;
1272 int skip; /* only parse, don't execute */
1273{
Bram Moolenaar33570922005-01-25 22:26:29 +00001274 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 int retval = FALSE;
1276
1277 if (skip)
1278 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else
1282 {
1283 *error = FALSE;
1284 if (!skip)
1285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001286 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001287 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289 }
1290 if (skip)
1291 --emsg_skip;
1292
1293 return retval;
1294}
1295
1296/*
1297 * Top level evaluation function, returning a string. If "skip" is TRUE,
1298 * only parsing to "nextcmd" is done, without reporting errors. Return
1299 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1300 */
1301 char_u *
1302eval_to_string_skip(arg, nextcmd, skip)
1303 char_u *arg;
1304 char_u **nextcmd;
1305 int skip; /* only parse, don't execute */
1306{
Bram Moolenaar33570922005-01-25 22:26:29 +00001307 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 char_u *retval;
1309
1310 if (skip)
1311 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001312 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 retval = NULL;
1314 else
1315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001316 retval = vim_strsave(get_tv_string(&tv));
1317 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319 if (skip)
1320 --emsg_skip;
1321
1322 return retval;
1323}
1324
1325/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326 * Skip over an expression at "*pp".
1327 * Return FAIL for an error, OK otherwise.
1328 */
1329 int
1330skip_expr(pp)
1331 char_u **pp;
1332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334
1335 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001336 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001337}
1338
1339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 * When "convert" is TRUE convert a List into a sequence of lines and convert
1342 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 * Return pointer to allocated memory, or NULL for failure.
1344 */
1345 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 char_u *arg;
1348 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001349 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001354#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 retval = NULL;
1360 else
1361 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 {
1364 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001365 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001366 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001367 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001368 if (tv.vval.v_list->lv_len > 0)
1369 ga_append(&ga, NL);
1370 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 ga_append(&ga, NUL);
1372 retval = (char_u *)ga.ga_data;
1373 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001374#ifdef FEAT_FLOAT
1375 else if (convert && tv.v_type == VAR_FLOAT)
1376 {
1377 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1378 retval = vim_strsave(numbuf);
1379 }
1380#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 else
1382 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 * Call eval_to_string() without using current local variables and using
1391 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 */
1393 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *arg;
1396 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
1399 char_u *retval;
1400 void *save_funccalp;
1401
1402 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001403 if (use_sandbox)
1404 ++sandbox;
1405 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 if (use_sandbox)
1408 --sandbox;
1409 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 restore_funccal(save_funccalp);
1411 return retval;
1412}
1413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414/*
1415 * Top level evaluation function, returning a number.
1416 * Evaluates "expr" silently.
1417 * Returns -1 for an error.
1418 */
1419 int
1420eval_to_number(expr)
1421 char_u *expr;
1422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001425 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427 ++emsg_off;
1428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 retval = -1;
1431 else
1432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001433 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 }
1436 --emsg_off;
1437
1438 return retval;
1439}
1440
Bram Moolenaara40058a2005-07-11 22:42:07 +00001441/*
1442 * Prepare v: variable "idx" to be used.
1443 * Save the current typeval in "save_tv".
1444 * When not used yet add the variable to the v: hashtable.
1445 */
1446 static void
1447prepare_vimvar(idx, save_tv)
1448 int idx;
1449 typval_T *save_tv;
1450{
1451 *save_tv = vimvars[idx].vv_tv;
1452 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1453 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1454}
1455
1456/*
1457 * Restore v: variable "idx" to typeval "save_tv".
1458 * When no longer defined, remove the variable from the v: hashtable.
1459 */
1460 static void
1461restore_vimvar(idx, save_tv)
1462 int idx;
1463 typval_T *save_tv;
1464{
1465 hashitem_T *hi;
1466
Bram Moolenaara40058a2005-07-11 22:42:07 +00001467 vimvars[idx].vv_tv = *save_tv;
1468 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1469 {
1470 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1471 if (HASHITEM_EMPTY(hi))
1472 EMSG2(_(e_intern2), "restore_vimvar()");
1473 else
1474 hash_remove(&vimvarht, hi);
1475 }
1476}
1477
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001478#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001479/*
1480 * Evaluate an expression to a list with suggestions.
1481 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001482 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001483 */
1484 list_T *
1485eval_spell_expr(badword, expr)
1486 char_u *badword;
1487 char_u *expr;
1488{
1489 typval_T save_val;
1490 typval_T rettv;
1491 list_T *list = NULL;
1492 char_u *p = skipwhite(expr);
1493
1494 /* Set "v:val" to the bad word. */
1495 prepare_vimvar(VV_VAL, &save_val);
1496 vimvars[VV_VAL].vv_type = VAR_STRING;
1497 vimvars[VV_VAL].vv_str = badword;
1498 if (p_verbose == 0)
1499 ++emsg_off;
1500
1501 if (eval1(&p, &rettv, TRUE) == OK)
1502 {
1503 if (rettv.v_type != VAR_LIST)
1504 clear_tv(&rettv);
1505 else
1506 list = rettv.vval.v_list;
1507 }
1508
1509 if (p_verbose == 0)
1510 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001511 restore_vimvar(VV_VAL, &save_val);
1512
1513 return list;
1514}
1515
1516/*
1517 * "list" is supposed to contain two items: a word and a number. Return the
1518 * word in "pp" and the number as the return value.
1519 * Return -1 if anything isn't right.
1520 * Used to get the good word and score from the eval_spell_expr() result.
1521 */
1522 int
1523get_spellword(list, pp)
1524 list_T *list;
1525 char_u **pp;
1526{
1527 listitem_T *li;
1528
1529 li = list->lv_first;
1530 if (li == NULL)
1531 return -1;
1532 *pp = get_tv_string(&li->li_tv);
1533
1534 li = li->li_next;
1535 if (li == NULL)
1536 return -1;
1537 return get_tv_number(&li->li_tv);
1538}
1539#endif
1540
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001541/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 * Top level evaluation function.
1543 * Returns an allocated typval_T with the result.
1544 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 */
1546 typval_T *
1547eval_expr(arg, nextcmd)
1548 char_u *arg;
1549 char_u **nextcmd;
1550{
1551 typval_T *tv;
1552
1553 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 {
1556 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001557 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001558 }
1559
1560 return tv;
1561}
1562
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001566 * Uses argv[argc] for the function arguments. Only Number and String
1567 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001570 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001571call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 char_u *func;
1573 int argc;
1574 char_u **argv;
1575 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001576 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578{
Bram Moolenaar33570922005-01-25 22:26:29 +00001579 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 long n;
1581 int len;
1582 int i;
1583 int doesrange;
1584 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001585 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001587 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001589 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 for (i = 0; i < argc; i++)
1592 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001593 /* Pass a NULL or empty argument as an empty string */
1594 if (argv[i] == NULL || *argv[i] == NUL)
1595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001596 argvars[i].v_type = VAR_STRING;
1597 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001598 continue;
1599 }
1600
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001601 if (str_arg_only)
1602 len = 0;
1603 else
1604 /* Recognize a number argument, the others must be strings. */
1605 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (len != 0 && len == (int)STRLEN(argv[i]))
1607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001608 argvars[i].v_type = VAR_NUMBER;
1609 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
1612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001613 argvars[i].v_type = VAR_STRING;
1614 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 }
1617
1618 if (safe)
1619 {
1620 save_funccalp = save_funccal();
1621 ++sandbox;
1622 }
1623
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1625 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (safe)
1629 {
1630 --sandbox;
1631 restore_funccal(save_funccalp);
1632 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 vim_free(argvars);
1634
1635 if (ret == FAIL)
1636 clear_tv(rettv);
1637
1638 return ret;
1639}
1640
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001641/*
1642 * Call vimL function "func" and return the result as a number.
1643 * Returns -1 when calling the function fails.
1644 * Uses argv[argc] for the function arguments.
1645 */
1646 long
1647call_func_retnr(func, argc, argv, safe)
1648 char_u *func;
1649 int argc;
1650 char_u **argv;
1651 int safe; /* use the sandbox */
1652{
1653 typval_T rettv;
1654 long retval;
1655
1656 /* All arguments are passed as strings, no conversion to number. */
1657 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1658 return -1;
1659
1660 retval = get_tv_number_chk(&rettv, NULL);
1661 clear_tv(&rettv);
1662 return retval;
1663}
1664
1665#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1666 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1667
Bram Moolenaar4f688582007-07-24 12:34:30 +00001668# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001670 * Call vimL function "func" and return the result as a string.
1671 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 * Uses argv[argc] for the function arguments.
1673 */
1674 void *
1675call_func_retstr(func, argc, argv, safe)
1676 char_u *func;
1677 int argc;
1678 char_u **argv;
1679 int safe; /* use the sandbox */
1680{
1681 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001682 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001684 /* All arguments are passed as strings, no conversion to number. */
1685 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 return NULL;
1687
1688 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 return retval;
1691}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001692# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001694/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001695 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001697 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 */
1699 void *
1700call_func_retlist(func, argc, argv, safe)
1701 char_u *func;
1702 int argc;
1703 char_u **argv;
1704 int safe; /* use the sandbox */
1705{
1706 typval_T rettv;
1707
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001710 return NULL;
1711
1712 if (rettv.v_type != VAR_LIST)
1713 {
1714 clear_tv(&rettv);
1715 return NULL;
1716 }
1717
1718 return rettv.vval.v_list;
1719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721
1722/*
1723 * Save the current function call pointer, and set it to NULL.
1724 * Used when executing autocommands and for ":source".
1725 */
1726 void *
1727save_funccal()
1728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 current_funccal = NULL;
1732 return (void *)fc;
1733}
1734
1735 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736restore_funccal(vfc)
1737 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = (funccall_T *)vfc;
1740
1741 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742}
1743
Bram Moolenaar05159a02005-02-26 23:04:13 +00001744#if defined(FEAT_PROFILE) || defined(PROTO)
1745/*
1746 * Prepare profiling for entering a child or something else that is not
1747 * counted for the script/function itself.
1748 * Should always be called in pair with prof_child_exit().
1749 */
1750 void
1751prof_child_enter(tm)
1752 proftime_T *tm; /* place to store waittime */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 profile_start(&fc->prof_child);
1758 script_prof_save(tm);
1759}
1760
1761/*
1762 * Take care of time spent in a child.
1763 * Should always be called after prof_child_enter().
1764 */
1765 void
1766prof_child_exit(tm)
1767 proftime_T *tm; /* where waittime was stored */
1768{
1769 funccall_T *fc = current_funccal;
1770
1771 if (fc != NULL && fc->func->uf_profiling)
1772 {
1773 profile_end(&fc->prof_child);
1774 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1775 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1776 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1777 }
1778 script_prof_restore(tm);
1779}
1780#endif
1781
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783#ifdef FEAT_FOLDING
1784/*
1785 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1786 * it in "*cp". Doesn't give error messages.
1787 */
1788 int
1789eval_foldexpr(arg, cp)
1790 char_u *arg;
1791 int *cp;
1792{
Bram Moolenaar33570922005-01-25 22:26:29 +00001793 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 int retval;
1795 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001796 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1797 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
1799 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001800 if (use_sandbox)
1801 ++sandbox;
1802 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001804 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 retval = 0;
1806 else
1807 {
1808 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 if (tv.v_type == VAR_NUMBER)
1810 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001811 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 retval = 0;
1813 else
1814 {
1815 /* If the result is a string, check if there is a non-digit before
1816 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (!VIM_ISDIGIT(*s) && *s != '-')
1819 *cp = *s++;
1820 retval = atol((char *)s);
1821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001825 if (use_sandbox)
1826 --sandbox;
1827 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 return retval;
1830}
1831#endif
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 * ":let" list all variable values
1835 * ":let var1 var2" list variable values
1836 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 * ":let var += expr" assignment command.
1838 * ":let var -= expr" assignment command.
1839 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
1842 void
1843ex_let(eap)
1844 exarg_T *eap;
1845{
1846 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 int var_count = 0;
1851 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001852 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
Bram Moolenaardb552d602006-03-23 22:59:57 +00001856 argend = skip_var_list(arg, &var_count, &semicolon);
1857 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001859 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1860 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001861 expr = skipwhite(argend);
1862 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1863 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001865 /*
1866 * ":let" without "=": list variables
1867 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 if (*arg == '[')
1869 EMSG(_(e_invarg));
1870 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_glob_vars(&first);
1877 list_buf_vars(&first);
1878 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001879#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001880 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001881#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 list_script_vars(&first);
1883 list_func_vars(&first);
1884 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 eap->nextcmd = check_nextcmd(arg);
1887 }
1888 else
1889 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 op[0] = '=';
1891 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001892 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001894 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1895 op[0] = *expr; /* +=, -= or .= */
1896 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001898 else
1899 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (eap->skip)
1902 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (eap->skip)
1905 {
1906 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_skip;
1909 }
1910 else if (i != FAIL)
1911 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917}
1918
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919/*
1920 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1921 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * When "nextchars" is not NULL it points to a string with characters that
1923 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1924 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 * Returns OK or FAIL;
1926 */
1927 static int
1928ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1929 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001930 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 int copy; /* copy values from "tv", don't move */
1932 int semicolon; /* from skip_var_list() */
1933 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935{
1936 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001939 listitem_T *item;
1940 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941
1942 if (*arg != '[')
1943 {
1944 /*
1945 * ":let var = expr" or ":for var in list"
1946 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 return FAIL;
1949 return OK;
1950 }
1951
1952 /*
1953 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1954 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001955 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 EMSG(_(e_listreq));
1958 return FAIL;
1959 }
1960
1961 i = list_len(l);
1962 if (semicolon == 0 && var_count < i)
1963 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001964 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 return FAIL;
1966 }
1967 if (var_count - semicolon > i)
1968 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001969 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 return FAIL;
1971 }
1972
1973 item = l->lv_first;
1974 while (*arg != ']')
1975 {
1976 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 item = item->li_next;
1979 if (arg == NULL)
1980 return FAIL;
1981
1982 arg = skipwhite(arg);
1983 if (*arg == ';')
1984 {
1985 /* Put the rest of the list (may be empty) in the var after ';'.
1986 * Create a new list for this. */
1987 l = list_alloc();
1988 if (l == NULL)
1989 return FAIL;
1990 while (item != NULL)
1991 {
1992 list_append_tv(l, &item->li_tv);
1993 item = item->li_next;
1994 }
1995
1996 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001997 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 ltv.vval.v_list = l;
1999 l->lv_refcount = 1;
2000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2002 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 clear_tv(&ltv);
2004 if (arg == NULL)
2005 return FAIL;
2006 break;
2007 }
2008 else if (*arg != ',' && *arg != ']')
2009 {
2010 EMSG2(_(e_intern2), "ex_let_vars()");
2011 return FAIL;
2012 }
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * Skip over assignable variable "var" or list of variables "[var, var]".
2020 * Used for ":let varvar = expr" and ":for varvar in expr".
2021 * For "[var, var]" increment "*var_count" for each variable.
2022 * for "[var, var; var]" set "semicolon".
2023 * Return NULL for an error.
2024 */
2025 static char_u *
2026skip_var_list(arg, var_count, semicolon)
2027 char_u *arg;
2028 int *var_count;
2029 int *semicolon;
2030{
2031 char_u *p, *s;
2032
2033 if (*arg == '[')
2034 {
2035 /* "[var, var]": find the matching ']'. */
2036 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002037 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 {
2039 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2040 s = skip_var_one(p);
2041 if (s == p)
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 ++*var_count;
2047
2048 p = skipwhite(s);
2049 if (*p == ']')
2050 break;
2051 else if (*p == ';')
2052 {
2053 if (*semicolon == 1)
2054 {
2055 EMSG(_("Double ; in list of variables"));
2056 return NULL;
2057 }
2058 *semicolon = 1;
2059 }
2060 else if (*p != ',')
2061 {
2062 EMSG2(_(e_invarg2), p);
2063 return NULL;
2064 }
2065 }
2066 return p + 1;
2067 }
2068 else
2069 return skip_var_one(arg);
2070}
2071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002072/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002073 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002075 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076 static char_u *
2077skip_var_one(arg)
2078 char_u *arg;
2079{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002080 if (*arg == '@' && arg[1] != NUL)
2081 return arg + 2;
2082 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2083 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084}
2085
Bram Moolenaara7043832005-01-21 11:56:39 +00002086/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 * List variables for hashtab "ht" with prefix "prefix".
2088 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002092 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002095 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096{
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 hashitem_T *hi;
2098 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 int todo;
2100
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002101 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2103 {
2104 if (!HASHITEM_EMPTY(hi))
2105 {
2106 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 di = HI2DI(hi);
2108 if (empty || di->di_tv.v_type != VAR_STRING
2109 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111 }
2112 }
2113}
2114
2115/*
2116 * List global variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_glob_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002123}
2124
2125/*
2126 * List buffer variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_buf_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002132 char_u numbuf[NUMBUFLEN];
2133
Bram Moolenaar429fa852013-04-15 12:27:36 +02002134 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002135 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136
2137 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2139 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002140}
2141
2142/*
2143 * List window variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_win_vars(first)
2147 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002151}
2152
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153#ifdef FEAT_WINDOWS
2154/*
2155 * List tab page variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_tab_vars(first)
2159 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002160{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002161 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163}
2164#endif
2165
Bram Moolenaara7043832005-01-21 11:56:39 +00002166/*
2167 * List Vim variables.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_vim_vars(first)
2171 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174}
2175
2176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002177 * List script-local variables, if there is a script.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_script_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2185 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
2189 * List function variables, if there is a function.
2190 */
2191 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_func_vars(first)
2193 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002194{
2195 if (current_funccal != NULL)
2196 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002198}
2199
2200/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 * List variables in "arg".
2202 */
2203 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 exarg_T *eap;
2206 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208{
2209 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u *name_start;
2213 char_u *arg_subsc;
2214 char_u *tofree;
2215 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216
2217 while (!ends_excmd(*arg) && !got_int)
2218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002221 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2223 {
2224 emsg_severe = TRUE;
2225 EMSG(_(e_trailing));
2226 break;
2227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002228 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002231 /* get_name_len() takes care of expanding curly braces */
2232 name_start = name = arg;
2233 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2234 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* This is mainly to keep test 49 working: when expanding
2237 * curly braces fails overrule the exception error message. */
2238 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 emsg_severe = TRUE;
2241 EMSG2(_(e_invarg2), arg);
2242 break;
2243 }
2244 error = TRUE;
2245 }
2246 else
2247 {
2248 if (tofree != NULL)
2249 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002250 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 /* handle d.key, l[idx], f(expr) */
2255 arg_subsc = arg;
2256 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'g': list_glob_vars(first); break;
2265 case 'b': list_buf_vars(first); break;
2266 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002268 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 case 'v': list_vim_vars(first); break;
2271 case 's': list_script_vars(first); break;
2272 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 default:
2274 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002275 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
2277 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 {
2279 char_u numbuf[NUMBUFLEN];
2280 char_u *tf;
2281 int c;
2282 char_u *s;
2283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002284 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 c = *arg;
2286 *arg = NUL;
2287 list_one_var_a((char_u *)"",
2288 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 tv.v_type,
2290 s == NULL ? (char_u *)"" : s,
2291 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 *arg = c;
2293 vim_free(tf);
2294 }
2295 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 }
2298 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299
2300 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302
2303 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
2305
2306 return arg;
2307}
2308
2309/*
2310 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2311 * Returns a pointer to the char just after the var name.
2312 * Returns NULL if there is an error.
2313 */
2314 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002317 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002318 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002319 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002320 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321{
2322 int c1;
2323 char_u *name;
2324 char_u *p;
2325 char_u *arg_end = NULL;
2326 int len;
2327 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002328 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329
2330 /*
2331 * ":let $VAR = expr": Set environment variable.
2332 */
2333 if (*arg == '$')
2334 {
2335 /* Find the end of the name. */
2336 ++arg;
2337 name = arg;
2338 len = get_env_len(&arg);
2339 if (len == 0)
2340 EMSG2(_(e_invarg2), name - 1);
2341 else
2342 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002343 if (op != NULL && (*op == '+' || *op == '-'))
2344 EMSG2(_(e_letwrong), op);
2345 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002346 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002348 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 {
2350 c1 = name[len];
2351 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 p = get_tv_string_chk(tv);
2353 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 {
2355 int mustfree = FALSE;
2356 char_u *s = vim_getenv(name, &mustfree);
2357
2358 if (s != NULL)
2359 {
2360 p = tofree = concat_str(s, p);
2361 if (mustfree)
2362 vim_free(s);
2363 }
2364 }
2365 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002366 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 if (STRICMP(name, "HOME") == 0)
2369 init_homedir();
2370 else if (didset_vim && STRICMP(name, "VIM") == 0)
2371 didset_vim = FALSE;
2372 else if (didset_vimruntime
2373 && STRICMP(name, "VIMRUNTIME") == 0)
2374 didset_vimruntime = FALSE;
2375 arg_end = arg;
2376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380 }
2381 }
2382
2383 /*
2384 * ":let &option = expr": Set option value.
2385 * ":let &l:option = expr": Set local option value.
2386 * ":let &g:option = expr": Set global option value.
2387 */
2388 else if (*arg == '&')
2389 {
2390 /* Find the end of the name. */
2391 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002392 if (p == NULL || (endchars != NULL
2393 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 EMSG(_(e_letunexp));
2395 else
2396 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 long n;
2398 int opt_type;
2399 long numval;
2400 char_u *stringval = NULL;
2401 char_u *s;
2402
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403 c1 = *p;
2404 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405
2406 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 s = get_tv_string_chk(tv); /* != NULL if number or string */
2408 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002409 {
2410 opt_type = get_option_value(arg, &numval,
2411 &stringval, opt_flags);
2412 if ((opt_type == 1 && *op == '.')
2413 || (opt_type == 0 && *op != '.'))
2414 EMSG2(_(e_letwrong), op);
2415 else
2416 {
2417 if (opt_type == 1) /* number */
2418 {
2419 if (*op == '+')
2420 n = numval + n;
2421 else
2422 n = numval - n;
2423 }
2424 else if (opt_type == 0 && stringval != NULL) /* string */
2425 {
2426 s = concat_str(stringval, s);
2427 vim_free(stringval);
2428 stringval = s;
2429 }
2430 }
2431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002432 if (s != NULL)
2433 {
2434 set_option_value(arg, n, s, opt_flags);
2435 arg_end = p;
2436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 }
2440 }
2441
2442 /*
2443 * ":let @r = expr": Set register contents.
2444 */
2445 else if (*arg == '@')
2446 {
2447 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && (*op == '+' || *op == '-'))
2449 EMSG2(_(e_letwrong), op);
2450 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002451 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 EMSG(_(e_letunexp));
2453 else
2454 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 char_u *s;
2457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 p = get_tv_string_chk(tv);
2459 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002461 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462 if (s != NULL)
2463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002465 vim_free(s);
2466 }
2467 }
2468 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 arg_end = arg + 1;
2472 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002473 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002481 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002483 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002485 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 arg_end = p;
2494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 }
2498
2499 else
2500 EMSG2(_(e_invarg2), arg);
2501
2502 return arg_end;
2503}
2504
2505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2507 */
2508 static int
2509check_changedtick(arg)
2510 char_u *arg;
2511{
2512 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2513 {
2514 EMSG2(_(e_readonlyvar), arg);
2515 return TRUE;
2516 }
2517 return FALSE;
2518}
2519
2520/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 * Get an lval: variable, Dict item or List item that can be assigned a value
2522 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2523 * "name.key", "name.key[expr]" etc.
2524 * Indexing only works if "name" is an existing List or Dictionary.
2525 * "name" points to the start of the name.
2526 * If "rettv" is not NULL it points to the value to be assigned.
2527 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2528 * wrong; must end in space or cmd separator.
2529 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002530 * flags:
2531 * GLV_QUIET: do not give error messages
2532 * GLV_NO_AUTOLOAD: do not use script autoloading
2533 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002535 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 */
2538 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 typval_T *rettv;
2542 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 int unlet;
2544 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002545 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002546 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 char_u *expr_start, *expr_end;
2550 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 dictitem_T *v;
2552 typval_T var1;
2553 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002557 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002562 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563
2564 if (skip)
2565 {
2566 /* When skipping just find the end of the name. */
2567 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002568 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 }
2570
2571 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002572 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (expr_start != NULL)
2574 {
2575 /* Don't expand the name when we already know there is an error. */
2576 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2577 && *p != '[' && *p != '.')
2578 {
2579 EMSG(_(e_trailing));
2580 return NULL;
2581 }
2582
2583 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2584 if (lp->ll_exp_name == NULL)
2585 {
2586 /* Report an invalid expression in braces, unless the
2587 * expression evaluation has been cancelled due to an
2588 * aborting error, an interrupt, or an exception. */
2589 if (!aborting() && !quiet)
2590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002591 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 EMSG2(_(e_invarg2), name);
2593 return NULL;
2594 }
2595 }
2596 lp->ll_name = lp->ll_exp_name;
2597 }
2598 else
2599 lp->ll_name = name;
2600
2601 /* Without [idx] or .key we are done. */
2602 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2603 return p;
2604
2605 cc = *p;
2606 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002607 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 if (v == NULL && !quiet)
2609 EMSG2(_(e_undefvar), lp->ll_name);
2610 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 if (v == NULL)
2612 return NULL;
2613
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 /*
2615 * Loop until no more [idx] or .key is following.
2616 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2621 && !(lp->ll_tv->v_type == VAR_DICT
2622 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E689: Can only index a List or Dictionary"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!quiet)
2631 EMSG(_("E708: [:] must come last"));
2632 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 len = -1;
2636 if (*p == '.')
2637 {
2638 key = p + 1;
2639 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2640 ;
2641 if (len == 0)
2642 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!quiet)
2644 EMSG(_(e_emptykey));
2645 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002646 }
2647 p = key + len;
2648 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002653 if (*p == ':')
2654 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 else
2656 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 empty1 = FALSE;
2658 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002660 if (get_tv_string_chk(&var1) == NULL)
2661 {
2662 /* not a number or string */
2663 clear_tv(&var1);
2664 return NULL;
2665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 }
2667
2668 /* Optionally get the second index [ :expr]. */
2669 if (*p == ':')
2670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002674 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002675 if (!empty1)
2676 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (rettv != NULL && (rettv->v_type != VAR_LIST
2680 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (!empty1)
2685 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 }
2688 p = skipwhite(p + 1);
2689 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 else
2692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (!empty1)
2697 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 if (get_tv_string_chk(&var2) == NULL)
2701 {
2702 /* not a number or string */
2703 if (!empty1)
2704 clear_tv(&var1);
2705 clear_tv(&var2);
2706 return NULL;
2707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002713
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724
2725 /* Skip to past ']'. */
2726 ++p;
2727 }
2728
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 {
2731 if (len == -1)
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (*key == NUL)
2736 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 if (!quiet)
2738 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002743 lp->ll_list = NULL;
2744 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002745 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 /* When assigning to a scope dictionary check that a function and
2748 * variable name is valid (only variable name unless it is l: or
2749 * g: dictionary). Disallow overwriting a builtin function. */
2750 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002752 int prevval;
2753 int wrong;
2754
2755 if (len != -1)
2756 {
2757 prevval = key[len];
2758 key[len] = NUL;
2759 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002760 else
2761 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2763 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002764 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002765 || !valid_varname(key);
2766 if (len != -1)
2767 key[len] = prevval;
2768 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002769 return NULL;
2770 }
2771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 /* Can't add "v:" variable. */
2775 if (lp->ll_dict == &vimvardict)
2776 {
2777 EMSG2(_(e_illvar), name);
2778 return NULL;
2779 }
2780
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 if (len == -1)
2795 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 p = NULL;
2798 break;
2799 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 /* existing variable, need to check if it can be changed */
2801 else if (var_check_ro(lp->ll_di->di_flags, name))
2802 return NULL;
2803
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808 else
2809 {
2810 /*
2811 * Get the number and item for the only or first index of the List.
2812 */
2813 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 else
2816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002817 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 clear_tv(&var1);
2819 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002820 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 lp->ll_list = lp->ll_tv->vval.v_list;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002825 if (lp->ll_n1 < 0)
2826 {
2827 lp->ll_n1 = 0;
2828 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2829 }
2830 }
2831 if (lp->ll_li == NULL)
2832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002835 if (!quiet)
2836 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839
2840 /*
2841 * May need to find the item or absolute index for the second
2842 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 * When no index given: "lp->ll_empty2" is TRUE.
2844 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002854 {
2855 if (!quiet)
2856 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002858 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2863 if (lp->ll_n1 < 0)
2864 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2865 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 {
2867 if (!quiet)
2868 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002870 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 }
2872
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002875 }
2876
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 return p;
2878}
2879
2880/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 */
2883 static void
2884clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886{
2887 vim_free(lp->ll_exp_name);
2888 vim_free(lp->ll_newkey);
2889}
2890
2891/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002892 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002894 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 */
2896 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002900 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903{
2904 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002905 listitem_T *ri;
2906 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907
2908 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 cc = *endp;
2913 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002914 if (op != NULL && *op != '=')
2915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002917
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002918 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002919 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002920 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002921 {
2922 if (tv_op(&tv, rettv, op) == OK)
2923 set_var(lp->ll_name, &tv, FALSE);
2924 clear_tv(&tv);
2925 }
2926 }
2927 else
2928 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002932 else if (tv_check_lock(lp->ll_newkey == NULL
2933 ? lp->ll_tv->v_lock
2934 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2935 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 else if (lp->ll_range)
2937 {
2938 /*
2939 * Assign the List values to the list items.
2940 */
2941 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002943 if (op != NULL && *op != '=')
2944 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2945 else
2946 {
2947 clear_tv(&lp->ll_li->li_tv);
2948 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 ri = ri->li_next;
2951 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2952 break;
2953 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002956 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 ri = NULL;
2959 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002960 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002961 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 lp->ll_li = lp->ll_li->li_next;
2963 ++lp->ll_n1;
2964 }
2965 if (ri != NULL)
2966 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 else if (lp->ll_empty2
2968 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 : lp->ll_n1 != lp->ll_n2)
2970 EMSG(_("E711: List value has not enough items"));
2971 }
2972 else
2973 {
2974 /*
2975 * Assign to a List or Dictionary item.
2976 */
2977 if (lp->ll_newkey != NULL)
2978 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 if (op != NULL && *op != '=')
2980 {
2981 EMSG2(_(e_letwrong), op);
2982 return;
2983 }
2984
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002986 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 if (di == NULL)
2988 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002989 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2990 {
2991 vim_free(di);
2992 return;
2993 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002995 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 else if (op != NULL && *op != '=')
2997 {
2998 tv_op(lp->ll_tv, rettv, op);
2999 return;
3000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003003
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 /*
3005 * Assign the value to the variable or list item.
3006 */
3007 if (copy)
3008 copy_tv(rettv, lp->ll_tv);
3009 else
3010 {
3011 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003012 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003014 }
3015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003016}
3017
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003018/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3020 * Returns OK or FAIL.
3021 */
3022 static int
3023tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003024 typval_T *tv1;
3025 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 char_u *op;
3027{
3028 long n;
3029 char_u numbuf[NUMBUFLEN];
3030 char_u *s;
3031
3032 /* Can't do anything with a Funcref or a Dict on the right. */
3033 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3034 {
3035 switch (tv1->v_type)
3036 {
3037 case VAR_DICT:
3038 case VAR_FUNC:
3039 break;
3040
3041 case VAR_LIST:
3042 if (*op != '+' || tv2->v_type != VAR_LIST)
3043 break;
3044 /* List += List */
3045 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3046 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3047 return OK;
3048
3049 case VAR_NUMBER:
3050 case VAR_STRING:
3051 if (tv2->v_type == VAR_LIST)
3052 break;
3053 if (*op == '+' || *op == '-')
3054 {
3055 /* nr += nr or nr -= nr*/
3056 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003057#ifdef FEAT_FLOAT
3058 if (tv2->v_type == VAR_FLOAT)
3059 {
3060 float_T f = n;
3061
3062 if (*op == '+')
3063 f += tv2->vval.v_float;
3064 else
3065 f -= tv2->vval.v_float;
3066 clear_tv(tv1);
3067 tv1->v_type = VAR_FLOAT;
3068 tv1->vval.v_float = f;
3069 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003071#endif
3072 {
3073 if (*op == '+')
3074 n += get_tv_number(tv2);
3075 else
3076 n -= get_tv_number(tv2);
3077 clear_tv(tv1);
3078 tv1->v_type = VAR_NUMBER;
3079 tv1->vval.v_number = n;
3080 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 }
3082 else
3083 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084 if (tv2->v_type == VAR_FLOAT)
3085 break;
3086
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 /* str .= str */
3088 s = get_tv_string(tv1);
3089 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3090 clear_tv(tv1);
3091 tv1->v_type = VAR_STRING;
3092 tv1->vval.v_string = s;
3093 }
3094 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003095
3096#ifdef FEAT_FLOAT
3097 case VAR_FLOAT:
3098 {
3099 float_T f;
3100
3101 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3102 && tv2->v_type != VAR_NUMBER
3103 && tv2->v_type != VAR_STRING))
3104 break;
3105 if (tv2->v_type == VAR_FLOAT)
3106 f = tv2->vval.v_float;
3107 else
3108 f = get_tv_number(tv2);
3109 if (*op == '+')
3110 tv1->vval.v_float += f;
3111 else
3112 tv1->vval.v_float -= f;
3113 }
3114 return OK;
3115#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116 }
3117 }
3118
3119 EMSG2(_(e_letwrong), op);
3120 return FAIL;
3121}
3122
3123/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 * Add a watcher to a list.
3125 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003126 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003128 list_T *l;
3129 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130{
3131 lw->lw_next = l->lv_watch;
3132 l->lv_watch = lw;
3133}
3134
3135/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003136 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137 * No warning when it isn't found...
3138 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003139 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003140list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003141 list_T *l;
3142 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143{
Bram Moolenaar33570922005-01-25 22:26:29 +00003144 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003145
3146 lwp = &l->lv_watch;
3147 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3148 {
3149 if (lw == lwrem)
3150 {
3151 *lwp = lw->lw_next;
3152 break;
3153 }
3154 lwp = &lw->lw_next;
3155 }
3156}
3157
3158/*
3159 * Just before removing an item from a list: advance watchers to the next
3160 * item.
3161 */
3162 static void
3163list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 list_T *l;
3165 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166{
Bram Moolenaar33570922005-01-25 22:26:29 +00003167 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003168
3169 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3170 if (lw->lw_item == item)
3171 lw->lw_item = item->li_next;
3172}
3173
3174/*
3175 * Evaluate the expression used in a ":for var in expr" command.
3176 * "arg" points to "var".
3177 * Set "*errp" to TRUE for an error, FALSE otherwise;
3178 * Return a pointer that holds the info. Null when there is an error.
3179 */
3180 void *
3181eval_for_line(arg, errp, nextcmdp, skip)
3182 char_u *arg;
3183 int *errp;
3184 char_u **nextcmdp;
3185 int skip;
3186{
Bram Moolenaar33570922005-01-25 22:26:29 +00003187 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003189 typval_T tv;
3190 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191
3192 *errp = TRUE; /* default: there is an error */
3193
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 if (fi == NULL)
3196 return NULL;
3197
3198 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3199 if (expr == NULL)
3200 return fi;
3201
3202 expr = skipwhite(expr);
3203 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3204 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003205 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206 return fi;
3207 }
3208
3209 if (skip)
3210 ++emsg_skip;
3211 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3212 {
3213 *errp = FALSE;
3214 if (!skip)
3215 {
3216 l = tv.vval.v_list;
3217 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003218 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003220 clear_tv(&tv);
3221 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 else
3223 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003224 /* No need to increment the refcount, it's already set for the
3225 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 fi->fi_list = l;
3227 list_add_watch(l, &fi->fi_lw);
3228 fi->fi_lw.lw_item = l->lv_first;
3229 }
3230 }
3231 }
3232 if (skip)
3233 --emsg_skip;
3234
3235 return fi;
3236}
3237
3238/*
3239 * Use the first item in a ":for" list. Advance to the next.
3240 * Assign the values to the variable (list). "arg" points to the first one.
3241 * Return TRUE when a valid item was found, FALSE when at end of list or
3242 * something wrong.
3243 */
3244 int
3245next_for_item(fi_void, arg)
3246 void *fi_void;
3247 char_u *arg;
3248{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003249 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
3253 item = fi->fi_lw.lw_item;
3254 if (item == NULL)
3255 result = FALSE;
3256 else
3257 {
3258 fi->fi_lw.lw_item = item->li_next;
3259 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3260 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3261 }
3262 return result;
3263}
3264
3265/*
3266 * Free the structure used to store info used by ":for".
3267 */
3268 void
3269free_for_info(fi_void)
3270 void *fi_void;
3271{
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003274 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 list_unref(fi->fi_list);
3278 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 vim_free(fi);
3280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3283
3284 void
3285set_context_for_expression(xp, arg, cmdidx)
3286 expand_T *xp;
3287 char_u *arg;
3288 cmdidx_T cmdidx;
3289{
3290 int got_eq = FALSE;
3291 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 if (cmdidx == CMD_let)
3295 {
3296 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003297 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 {
3299 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003300 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 {
3302 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 if (vim_iswhite(*p))
3305 break;
3306 }
3307 return;
3308 }
3309 }
3310 else
3311 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3312 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 while ((xp->xp_pattern = vim_strpbrk(arg,
3314 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3315 {
3316 c = *xp->xp_pattern;
3317 if (c == '&')
3318 {
3319 c = xp->xp_pattern[1];
3320 if (c == '&')
3321 {
3322 ++xp->xp_pattern;
3323 xp->xp_context = cmdidx != CMD_let || got_eq
3324 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3325 }
3326 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003329 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3330 xp->xp_pattern += 2;
3331
3332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 else if (c == '$')
3335 {
3336 /* environment variable */
3337 xp->xp_context = EXPAND_ENV_VARS;
3338 }
3339 else if (c == '=')
3340 {
3341 got_eq = TRUE;
3342 xp->xp_context = EXPAND_EXPRESSION;
3343 }
3344 else if (c == '<'
3345 && xp->xp_context == EXPAND_FUNCTIONS
3346 && vim_strchr(xp->xp_pattern, '(') == NULL)
3347 {
3348 /* Function name can start with "<SNR>" */
3349 break;
3350 }
3351 else if (cmdidx != CMD_let || got_eq)
3352 {
3353 if (c == '"') /* string */
3354 {
3355 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3356 if (c == '\\' && xp->xp_pattern[1] != NUL)
3357 ++xp->xp_pattern;
3358 xp->xp_context = EXPAND_NOTHING;
3359 }
3360 else if (c == '\'') /* literal string */
3361 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003362 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3364 /* skip */ ;
3365 xp->xp_context = EXPAND_NOTHING;
3366 }
3367 else if (c == '|')
3368 {
3369 if (xp->xp_pattern[1] == '|')
3370 {
3371 ++xp->xp_pattern;
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
3375 xp->xp_context = EXPAND_COMMANDS;
3376 }
3377 else
3378 xp->xp_context = EXPAND_EXPRESSION;
3379 }
3380 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 /* Doesn't look like something valid, expand as an expression
3382 * anyway. */
3383 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 arg = xp->xp_pattern;
3385 if (*arg != NUL)
3386 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3387 /* skip */ ;
3388 }
3389 xp->xp_pattern = arg;
3390}
3391
3392#endif /* FEAT_CMDL_COMPL */
3393
3394/*
3395 * ":1,25call func(arg1, arg2)" function call.
3396 */
3397 void
3398ex_call(eap)
3399 exarg_T *eap;
3400{
3401 char_u *arg = eap->arg;
3402 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003404 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003406 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 linenr_T lnum;
3408 int doesrange;
3409 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003410 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003412 if (eap->skip)
3413 {
3414 /* trans_function_name() doesn't work well when skipping, use eval0()
3415 * instead to skip to any following command, e.g. for:
3416 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003417 ++emsg_skip;
3418 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3419 clear_tv(&rettv);
3420 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003421 return;
3422 }
3423
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003424 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003425 if (fudi.fd_newkey != NULL)
3426 {
3427 /* Still need to give an error message for missing key. */
3428 EMSG2(_(e_dictkey), fudi.fd_newkey);
3429 vim_free(fudi.fd_newkey);
3430 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 if (tofree == NULL)
3432 return;
3433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 /* Increase refcount on dictionary, it could get deleted when evaluating
3435 * the arguments. */
3436 if (fudi.fd_dict != NULL)
3437 ++fudi.fd_dict->dv_refcount;
3438
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003439 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003440 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003441 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaar532c7802005-01-27 14:44:31 +00003443 /* Skip white space to allow ":call func ()". Not good, but required for
3444 * backward compatibility. */
3445 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003446 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 if (*startarg != '(')
3449 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003450 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 goto end;
3452 }
3453
3454 /*
3455 * When skipping, evaluate the function once, to find the end of the
3456 * arguments.
3457 * When the function takes a range, this is discovered after the first
3458 * call, and the loop is broken.
3459 */
3460 if (eap->skip)
3461 {
3462 ++emsg_skip;
3463 lnum = eap->line2; /* do it once, also with an invalid range */
3464 }
3465 else
3466 lnum = eap->line1;
3467 for ( ; lnum <= eap->line2; ++lnum)
3468 {
3469 if (!eap->skip && eap->addr_count > 0)
3470 {
3471 curwin->w_cursor.lnum = lnum;
3472 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003473#ifdef FEAT_VIRTUALEDIT
3474 curwin->w_cursor.coladd = 0;
3475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 eap->line1, eap->line2, &doesrange,
3480 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 failed = TRUE;
3483 break;
3484 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003485
3486 /* Handle a function returning a Funcref, Dictionary or List. */
3487 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3488 {
3489 failed = TRUE;
3490 break;
3491 }
3492
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (doesrange || eap->skip)
3495 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003498 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003500 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (aborting())
3502 break;
3503 }
3504 if (eap->skip)
3505 --emsg_skip;
3506
3507 if (!failed)
3508 {
3509 /* Check for trailing illegal characters and a following command. */
3510 if (!ends_excmd(*arg))
3511 {
3512 emsg_severe = TRUE;
3513 EMSG(_(e_trailing));
3514 }
3515 else
3516 eap->nextcmd = check_nextcmd(arg);
3517 }
3518
3519end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003520 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003521 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522}
3523
3524/*
3525 * ":unlet[!] var1 ... " command.
3526 */
3527 void
3528ex_unlet(eap)
3529 exarg_T *eap;
3530{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003531 ex_unletlock(eap, eap->arg, 0);
3532}
3533
3534/*
3535 * ":lockvar" and ":unlockvar" commands
3536 */
3537 void
3538ex_lockvar(eap)
3539 exarg_T *eap;
3540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003542 int deep = 2;
3543
3544 if (eap->forceit)
3545 deep = -1;
3546 else if (vim_isdigit(*arg))
3547 {
3548 deep = getdigits(&arg);
3549 arg = skipwhite(arg);
3550 }
3551
3552 ex_unletlock(eap, arg, deep);
3553}
3554
3555/*
3556 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3557 */
3558 static void
3559ex_unletlock(eap, argstart, deep)
3560 exarg_T *eap;
3561 char_u *argstart;
3562 int deep;
3563{
3564 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003567 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 do
3570 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003572 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003573 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003574 if (lv.ll_name == NULL)
3575 error = TRUE; /* error but continue parsing */
3576 if (name_end == NULL || (!vim_iswhite(*name_end)
3577 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 if (name_end != NULL)
3580 {
3581 emsg_severe = TRUE;
3582 EMSG(_(e_trailing));
3583 }
3584 if (!(eap->skip || error))
3585 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587 }
3588
3589 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 {
3591 if (eap->cmdidx == CMD_unlet)
3592 {
3593 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3594 error = TRUE;
3595 }
3596 else
3597 {
3598 if (do_lock_var(&lv, name_end, deep,
3599 eap->cmdidx == CMD_lockvar) == FAIL)
3600 error = TRUE;
3601 }
3602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 if (!eap->skip)
3605 clear_lval(&lv);
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 arg = skipwhite(name_end);
3608 } while (!ends_excmd(*arg));
3609
3610 eap->nextcmd = check_nextcmd(arg);
3611}
3612
Bram Moolenaar8c711452005-01-14 21:53:12 +00003613 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 int forceit;
3618{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 int ret = OK;
3620 int cc;
3621
3622 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 cc = *name_end;
3625 *name_end = NUL;
3626
3627 /* Normal name or expanded name. */
3628 if (check_changedtick(lp->ll_name))
3629 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003633 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3635 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else if (lp->ll_range)
3637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003638 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639
3640 /* Delete a range of List items. */
3641 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3642 {
3643 li = lp->ll_li->li_next;
3644 listitem_remove(lp->ll_list, lp->ll_li);
3645 lp->ll_li = li;
3646 ++lp->ll_n1;
3647 }
3648 }
3649 else
3650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 /* unlet a List item. */
3653 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003654 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003656 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 }
3658
3659 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660}
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662/*
3663 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
3666 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003669 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
Bram Moolenaar33570922005-01-25 22:26:29 +00003671 hashtab_T *ht;
3672 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003674 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar33570922005-01-25 22:26:29 +00003676 ht = find_var_ht(name, &varname);
3677 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003679 hi = hash_find(ht, varname);
3680 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003681 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003682 di = HI2DI(hi);
3683 if (var_check_fixed(di->di_flags, name)
3684 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
3686 delete_var(ht, hi);
3687 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690 if (forceit)
3691 return OK;
3692 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 return FAIL;
3694}
3695
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696/*
3697 * Lock or unlock variable indicated by "lp".
3698 * "deep" is the levels to go (-1 for unlimited);
3699 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3700 */
3701 static int
3702do_lock_var(lp, name_end, deep, lock)
3703 lval_T *lp;
3704 char_u *name_end;
3705 int deep;
3706 int lock;
3707{
3708 int ret = OK;
3709 int cc;
3710 dictitem_T *di;
3711
3712 if (deep == 0) /* nothing to do */
3713 return OK;
3714
3715 if (lp->ll_tv == NULL)
3716 {
3717 cc = *name_end;
3718 *name_end = NUL;
3719
3720 /* Normal name or expanded name. */
3721 if (check_changedtick(lp->ll_name))
3722 ret = FAIL;
3723 else
3724 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003725 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 if (di == NULL)
3727 ret = FAIL;
3728 else
3729 {
3730 if (lock)
3731 di->di_flags |= DI_FLAGS_LOCK;
3732 else
3733 di->di_flags &= ~DI_FLAGS_LOCK;
3734 item_lock(&di->di_tv, deep, lock);
3735 }
3736 }
3737 *name_end = cc;
3738 }
3739 else if (lp->ll_range)
3740 {
3741 listitem_T *li = lp->ll_li;
3742
3743 /* (un)lock a range of List items. */
3744 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3745 {
3746 item_lock(&li->li_tv, deep, lock);
3747 li = li->li_next;
3748 ++lp->ll_n1;
3749 }
3750 }
3751 else if (lp->ll_list != NULL)
3752 /* (un)lock a List item. */
3753 item_lock(&lp->ll_li->li_tv, deep, lock);
3754 else
3755 /* un(lock) a Dictionary item. */
3756 item_lock(&lp->ll_di->di_tv, deep, lock);
3757
3758 return ret;
3759}
3760
3761/*
3762 * Lock or unlock an item. "deep" is nr of levels to go.
3763 */
3764 static void
3765item_lock(tv, deep, lock)
3766 typval_T *tv;
3767 int deep;
3768 int lock;
3769{
3770 static int recurse = 0;
3771 list_T *l;
3772 listitem_T *li;
3773 dict_T *d;
3774 hashitem_T *hi;
3775 int todo;
3776
3777 if (recurse >= DICT_MAXNEST)
3778 {
3779 EMSG(_("E743: variable nested too deep for (un)lock"));
3780 return;
3781 }
3782 if (deep == 0)
3783 return;
3784 ++recurse;
3785
3786 /* lock/unlock the item itself */
3787 if (lock)
3788 tv->v_lock |= VAR_LOCKED;
3789 else
3790 tv->v_lock &= ~VAR_LOCKED;
3791
3792 switch (tv->v_type)
3793 {
3794 case VAR_LIST:
3795 if ((l = tv->vval.v_list) != NULL)
3796 {
3797 if (lock)
3798 l->lv_lock |= VAR_LOCKED;
3799 else
3800 l->lv_lock &= ~VAR_LOCKED;
3801 if (deep < 0 || deep > 1)
3802 /* recursive: lock/unlock the items the List contains */
3803 for (li = l->lv_first; li != NULL; li = li->li_next)
3804 item_lock(&li->li_tv, deep - 1, lock);
3805 }
3806 break;
3807 case VAR_DICT:
3808 if ((d = tv->vval.v_dict) != NULL)
3809 {
3810 if (lock)
3811 d->dv_lock |= VAR_LOCKED;
3812 else
3813 d->dv_lock &= ~VAR_LOCKED;
3814 if (deep < 0 || deep > 1)
3815 {
3816 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003817 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3819 {
3820 if (!HASHITEM_EMPTY(hi))
3821 {
3822 --todo;
3823 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3824 }
3825 }
3826 }
3827 }
3828 }
3829 --recurse;
3830}
3831
Bram Moolenaara40058a2005-07-11 22:42:07 +00003832/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003833 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3834 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003835 */
3836 static int
3837tv_islocked(tv)
3838 typval_T *tv;
3839{
3840 return (tv->v_lock & VAR_LOCKED)
3841 || (tv->v_type == VAR_LIST
3842 && tv->vval.v_list != NULL
3843 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3844 || (tv->v_type == VAR_DICT
3845 && tv->vval.v_dict != NULL
3846 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3847}
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3850/*
3851 * Delete all "menutrans_" variables.
3852 */
3853 void
3854del_menutrans_vars()
3855{
Bram Moolenaar33570922005-01-25 22:26:29 +00003856 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003857 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
Bram Moolenaar33570922005-01-25 22:26:29 +00003859 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003860 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 {
3863 if (!HASHITEM_EMPTY(hi))
3864 {
3865 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003866 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3867 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003868 }
3869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871}
3872#endif
3873
3874#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3875
3876/*
3877 * Local string buffer for the next two functions to store a variable name
3878 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3879 * get_user_var_name().
3880 */
3881
3882static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3883
3884static char_u *varnamebuf = NULL;
3885static int varnamebuflen = 0;
3886
3887/*
3888 * Function to concatenate a prefix and a variable name.
3889 */
3890 static char_u *
3891cat_prefix_varname(prefix, name)
3892 int prefix;
3893 char_u *name;
3894{
3895 int len;
3896
3897 len = (int)STRLEN(name) + 3;
3898 if (len > varnamebuflen)
3899 {
3900 vim_free(varnamebuf);
3901 len += 10; /* some additional space */
3902 varnamebuf = alloc(len);
3903 if (varnamebuf == NULL)
3904 {
3905 varnamebuflen = 0;
3906 return NULL;
3907 }
3908 varnamebuflen = len;
3909 }
3910 *varnamebuf = prefix;
3911 varnamebuf[1] = ':';
3912 STRCPY(varnamebuf + 2, name);
3913 return varnamebuf;
3914}
3915
3916/*
3917 * Function given to ExpandGeneric() to obtain the list of user defined
3918 * (global/buffer/window/built-in) variable names.
3919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 char_u *
3921get_user_var_name(xp, idx)
3922 expand_T *xp;
3923 int idx;
3924{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static long_u gdone;
3926 static long_u bdone;
3927 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928#ifdef FEAT_WINDOWS
3929 static long_u tdone;
3930#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 static int vidx;
3932 static hashitem_T *hi;
3933 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003936 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003938#ifdef FEAT_WINDOWS
3939 tdone = 0;
3940#endif
3941 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003942
3943 /* Global variables */
3944 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003948 else
3949 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 while (HASHITEM_EMPTY(hi))
3951 ++hi;
3952 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3953 return cat_prefix_varname('g', hi->hi_key);
3954 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003961 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003971 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 return (char_u *)"b:changedtick";
3973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003974
3975 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003976 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003979 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003981 else
3982 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 while (HASHITEM_EMPTY(hi))
3984 ++hi;
3985 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003987
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003988#ifdef FEAT_WINDOWS
3989 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003990 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003991 if (tdone < ht->ht_used)
3992 {
3993 if (tdone++ == 0)
3994 hi = ht->ht_array;
3995 else
3996 ++hi;
3997 while (HASHITEM_EMPTY(hi))
3998 ++hi;
3999 return cat_prefix_varname('t', hi->hi_key);
4000 }
4001#endif
4002
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 /* v: variables */
4004 if (vidx < VV_LEN)
4005 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
4007 vim_free(varnamebuf);
4008 varnamebuf = NULL;
4009 varnamebuflen = 0;
4010 return NULL;
4011}
4012
4013#endif /* FEAT_CMDL_COMPL */
4014
4015/*
4016 * types for expressions.
4017 */
4018typedef enum
4019{
4020 TYPE_UNKNOWN = 0
4021 , TYPE_EQUAL /* == */
4022 , TYPE_NEQUAL /* != */
4023 , TYPE_GREATER /* > */
4024 , TYPE_GEQUAL /* >= */
4025 , TYPE_SMALLER /* < */
4026 , TYPE_SEQUAL /* <= */
4027 , TYPE_MATCH /* =~ */
4028 , TYPE_NOMATCH /* !~ */
4029} exptype_T;
4030
4031/*
4032 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4035 */
4036
4037/*
4038 * Handle zero level expression.
4039 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004041 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 * Return OK or FAIL.
4043 */
4044 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004045eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 char_u **nextcmd;
4049 int evaluate;
4050{
4051 int ret;
4052 char_u *p;
4053
4054 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (ret == FAIL || !ends_excmd(*p))
4057 {
4058 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /*
4061 * Report the invalid expression unless the expression evaluation has
4062 * been cancelled due to an aborting error, an interrupt, or an
4063 * exception.
4064 */
4065 if (!aborting())
4066 EMSG2(_(e_invexpr2), arg);
4067 ret = FAIL;
4068 }
4069 if (nextcmd != NULL)
4070 *nextcmd = check_nextcmd(p);
4071
4072 return ret;
4073}
4074
4075/*
4076 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004077 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 *
4079 * "arg" must point to the first non-white of the expression.
4080 * "arg" is advanced to the next non-white after the recognized expression.
4081 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004082 * Note: "rettv.v_lock" is not set.
4083 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * Return OK or FAIL.
4085 */
4086 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 int evaluate;
4091{
4092 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 /*
4096 * Get the first variable.
4097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004098 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 return FAIL;
4100
4101 if ((*arg)[0] == '?')
4102 {
4103 result = FALSE;
4104 if (evaluate)
4105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 int error = FALSE;
4107
4108 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004110 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004111 if (error)
4112 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
4114
4115 /*
4116 * Get the second variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 return FAIL;
4121
4122 /*
4123 * Check for the ":".
4124 */
4125 if ((*arg)[0] != ':')
4126 {
4127 EMSG(_("E109: Missing ':' after '?'"));
4128 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return FAIL;
4131 }
4132
4133 /*
4134 * Get the third variable.
4135 */
4136 *arg = skipwhite(*arg + 1);
4137 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4138 {
4139 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 return FAIL;
4142 }
4143 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146
4147 return OK;
4148}
4149
4150/*
4151 * Handle first level expression:
4152 * expr2 || expr2 || expr2 logical OR
4153 *
4154 * "arg" must point to the first non-white of the expression.
4155 * "arg" is advanced to the next non-white after the recognized expression.
4156 *
4157 * Return OK or FAIL.
4158 */
4159 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 int evaluate;
4164{
Bram Moolenaar33570922005-01-25 22:26:29 +00004165 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 long result;
4167 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 /*
4177 * Repeat until there is no following "||".
4178 */
4179 first = TRUE;
4180 result = FALSE;
4181 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4182 {
4183 if (evaluate && first)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (error)
4189 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 first = FALSE;
4191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 2);
4197 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4198 return FAIL;
4199
4200 /*
4201 * Compute the result.
4202 */
4203 if (evaluate && !result)
4204 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004205 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004208 if (error)
4209 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211 if (evaluate)
4212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 rettv->v_type = VAR_NUMBER;
4214 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 }
4216 }
4217
4218 return OK;
4219}
4220
4221/*
4222 * Handle second level expression:
4223 * expr3 && expr3 && expr3 logical AND
4224 *
4225 * "arg" must point to the first non-white of the expression.
4226 * "arg" is advanced to the next non-white after the recognized expression.
4227 *
4228 * Return OK or FAIL.
4229 */
4230 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int evaluate;
4235{
Bram Moolenaar33570922005-01-25 22:26:29 +00004236 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 long result;
4238 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 /*
4242 * Get the first variable.
4243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004244 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 return FAIL;
4246
4247 /*
4248 * Repeat until there is no following "&&".
4249 */
4250 first = TRUE;
4251 result = TRUE;
4252 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4253 {
4254 if (evaluate && first)
4255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004256 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (error)
4260 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 first = FALSE;
4262 }
4263
4264 /*
4265 * Get the second variable.
4266 */
4267 *arg = skipwhite(*arg + 2);
4268 if (eval4(arg, &var2, evaluate && result) == FAIL)
4269 return FAIL;
4270
4271 /*
4272 * Compute the result.
4273 */
4274 if (evaluate && result)
4275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004276 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282 if (evaluate)
4283 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 rettv->v_type = VAR_NUMBER;
4285 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 }
4288
4289 return OK;
4290}
4291
4292/*
4293 * Handle third level expression:
4294 * var1 == var2
4295 * var1 =~ var2
4296 * var1 != var2
4297 * var1 !~ var2
4298 * var1 > var2
4299 * var1 >= var2
4300 * var1 < var2
4301 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 * var1 is var2
4303 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 *
4305 * "arg" must point to the first non-white of the expression.
4306 * "arg" is advanced to the next non-white after the recognized expression.
4307 *
4308 * Return OK or FAIL.
4309 */
4310 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 int evaluate;
4315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 char_u *p;
4318 int i;
4319 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004320 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int len = 2;
4322 long n1, n2;
4323 char_u *s1, *s2;
4324 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4325 regmatch_T regmatch;
4326 int ic;
4327 char_u *save_cpo;
4328
4329 /*
4330 * Get the first variable.
4331 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 return FAIL;
4334
4335 p = *arg;
4336 switch (p[0])
4337 {
4338 case '=': if (p[1] == '=')
4339 type = TYPE_EQUAL;
4340 else if (p[1] == '~')
4341 type = TYPE_MATCH;
4342 break;
4343 case '!': if (p[1] == '=')
4344 type = TYPE_NEQUAL;
4345 else if (p[1] == '~')
4346 type = TYPE_NOMATCH;
4347 break;
4348 case '>': if (p[1] != '=')
4349 {
4350 type = TYPE_GREATER;
4351 len = 1;
4352 }
4353 else
4354 type = TYPE_GEQUAL;
4355 break;
4356 case '<': if (p[1] != '=')
4357 {
4358 type = TYPE_SMALLER;
4359 len = 1;
4360 }
4361 else
4362 type = TYPE_SEQUAL;
4363 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004364 case 'i': if (p[1] == 's')
4365 {
4366 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4367 len = 5;
4368 if (!vim_isIDc(p[len]))
4369 {
4370 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4371 type_is = TRUE;
4372 }
4373 }
4374 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376
4377 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004378 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 */
4380 if (type != TYPE_UNKNOWN)
4381 {
4382 /* extra question mark appended: ignore case */
4383 if (p[len] == '?')
4384 {
4385 ic = TRUE;
4386 ++len;
4387 }
4388 /* extra '#' appended: match case */
4389 else if (p[len] == '#')
4390 {
4391 ic = FALSE;
4392 ++len;
4393 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004394 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 else
4396 ic = p_ic;
4397
4398 /*
4399 * Get the second variable.
4400 */
4401 *arg = skipwhite(p + len);
4402 if (eval5(arg, &var2, evaluate) == FAIL)
4403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004404 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 return FAIL;
4406 }
4407
4408 if (evaluate)
4409 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004410 if (type_is && rettv->v_type != var2.v_type)
4411 {
4412 /* For "is" a different type always means FALSE, for "notis"
4413 * it means TRUE. */
4414 n1 = (type == TYPE_NEQUAL);
4415 }
4416 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4417 {
4418 if (type_is)
4419 {
4420 n1 = (rettv->v_type == var2.v_type
4421 && rettv->vval.v_list == var2.vval.v_list);
4422 if (type == TYPE_NEQUAL)
4423 n1 = !n1;
4424 }
4425 else if (rettv->v_type != var2.v_type
4426 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4427 {
4428 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004429 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004431 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 clear_tv(rettv);
4433 clear_tv(&var2);
4434 return FAIL;
4435 }
4436 else
4437 {
4438 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004439 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4440 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 if (type == TYPE_NEQUAL)
4442 n1 = !n1;
4443 }
4444 }
4445
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004446 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4447 {
4448 if (type_is)
4449 {
4450 n1 = (rettv->v_type == var2.v_type
4451 && rettv->vval.v_dict == var2.vval.v_dict);
4452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 else if (rettv->v_type != var2.v_type
4456 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4457 {
4458 if (rettv->v_type != var2.v_type)
4459 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4460 else
4461 EMSG(_("E736: Invalid operation for Dictionary"));
4462 clear_tv(rettv);
4463 clear_tv(&var2);
4464 return FAIL;
4465 }
4466 else
4467 {
4468 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004469 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4470 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004471 if (type == TYPE_NEQUAL)
4472 n1 = !n1;
4473 }
4474 }
4475
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004476 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4477 {
4478 if (rettv->v_type != var2.v_type
4479 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4480 {
4481 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004482 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004484 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004485 clear_tv(rettv);
4486 clear_tv(&var2);
4487 return FAIL;
4488 }
4489 else
4490 {
4491 /* Compare two Funcrefs for being equal or unequal. */
4492 if (rettv->vval.v_string == NULL
4493 || var2.vval.v_string == NULL)
4494 n1 = FALSE;
4495 else
4496 n1 = STRCMP(rettv->vval.v_string,
4497 var2.vval.v_string) == 0;
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 }
4502
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004503#ifdef FEAT_FLOAT
4504 /*
4505 * If one of the two variables is a float, compare as a float.
4506 * When using "=~" or "!~", always compare as string.
4507 */
4508 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4509 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4510 {
4511 float_T f1, f2;
4512
4513 if (rettv->v_type == VAR_FLOAT)
4514 f1 = rettv->vval.v_float;
4515 else
4516 f1 = get_tv_number(rettv);
4517 if (var2.v_type == VAR_FLOAT)
4518 f2 = var2.vval.v_float;
4519 else
4520 f2 = get_tv_number(&var2);
4521 n1 = FALSE;
4522 switch (type)
4523 {
4524 case TYPE_EQUAL: n1 = (f1 == f2); break;
4525 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4526 case TYPE_GREATER: n1 = (f1 > f2); break;
4527 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4528 case TYPE_SMALLER: n1 = (f1 < f2); break;
4529 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4530 case TYPE_UNKNOWN:
4531 case TYPE_MATCH:
4532 case TYPE_NOMATCH: break; /* avoid gcc warning */
4533 }
4534 }
4535#endif
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 /*
4538 * If one of the two variables is a number, compare as a number.
4539 * When using "=~" or "!~", always compare as string.
4540 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 n1 = get_tv_number(rettv);
4545 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 switch (type)
4547 {
4548 case TYPE_EQUAL: n1 = (n1 == n2); break;
4549 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4550 case TYPE_GREATER: n1 = (n1 > n2); break;
4551 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4552 case TYPE_SMALLER: n1 = (n1 < n2); break;
4553 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4554 case TYPE_UNKNOWN:
4555 case TYPE_MATCH:
4556 case TYPE_NOMATCH: break; /* avoid gcc warning */
4557 }
4558 }
4559 else
4560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004561 s1 = get_tv_string_buf(rettv, buf1);
4562 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4564 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4565 else
4566 i = 0;
4567 n1 = FALSE;
4568 switch (type)
4569 {
4570 case TYPE_EQUAL: n1 = (i == 0); break;
4571 case TYPE_NEQUAL: n1 = (i != 0); break;
4572 case TYPE_GREATER: n1 = (i > 0); break;
4573 case TYPE_GEQUAL: n1 = (i >= 0); break;
4574 case TYPE_SMALLER: n1 = (i < 0); break;
4575 case TYPE_SEQUAL: n1 = (i <= 0); break;
4576
4577 case TYPE_MATCH:
4578 case TYPE_NOMATCH:
4579 /* avoid 'l' flag in 'cpoptions' */
4580 save_cpo = p_cpo;
4581 p_cpo = (char_u *)"";
4582 regmatch.regprog = vim_regcomp(s2,
4583 RE_MAGIC + RE_STRING);
4584 regmatch.rm_ic = ic;
4585 if (regmatch.regprog != NULL)
4586 {
4587 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004588 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (type == TYPE_NOMATCH)
4590 n1 = !n1;
4591 }
4592 p_cpo = save_cpo;
4593 break;
4594
4595 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4596 }
4597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 clear_tv(rettv);
4599 clear_tv(&var2);
4600 rettv->v_type = VAR_NUMBER;
4601 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 }
4604
4605 return OK;
4606}
4607
4608/*
4609 * Handle fourth level expression:
4610 * + number addition
4611 * - number subtraction
4612 * . string concatenation
4613 *
4614 * "arg" must point to the first non-white of the expression.
4615 * "arg" is advanced to the next non-white after the recognized expression.
4616 *
4617 * Return OK or FAIL.
4618 */
4619 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004622 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 int evaluate;
4624{
Bram Moolenaar33570922005-01-25 22:26:29 +00004625 typval_T var2;
4626 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 int op;
4628 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004629#ifdef FEAT_FLOAT
4630 float_T f1 = 0, f2 = 0;
4631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 char_u *s1, *s2;
4633 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4634 char_u *p;
4635
4636 /*
4637 * Get the first variable.
4638 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004639 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 return FAIL;
4641
4642 /*
4643 * Repeat computing, until no '+', '-' or '.' is following.
4644 */
4645 for (;;)
4646 {
4647 op = **arg;
4648 if (op != '+' && op != '-' && op != '.')
4649 break;
4650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004651 if ((op != '+' || rettv->v_type != VAR_LIST)
4652#ifdef FEAT_FLOAT
4653 && (op == '.' || rettv->v_type != VAR_FLOAT)
4654#endif
4655 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004656 {
4657 /* For "list + ...", an illegal use of the first operand as
4658 * a number cannot be determined before evaluating the 2nd
4659 * operand: if this is also a list, all is ok.
4660 * For "something . ...", "something - ..." or "non-list + ...",
4661 * we know that the first operand needs to be a string or number
4662 * without evaluating the 2nd operand. So check before to avoid
4663 * side effects after an error. */
4664 if (evaluate && get_tv_string_chk(rettv) == NULL)
4665 {
4666 clear_tv(rettv);
4667 return FAIL;
4668 }
4669 }
4670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 /*
4672 * Get the second variable.
4673 */
4674 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004675 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679 }
4680
4681 if (evaluate)
4682 {
4683 /*
4684 * Compute the result.
4685 */
4686 if (op == '.')
4687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004688 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4689 s2 = get_tv_string_buf_chk(&var2, buf2);
4690 if (s2 == NULL) /* type error ? */
4691 {
4692 clear_tv(rettv);
4693 clear_tv(&var2);
4694 return FAIL;
4695 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004696 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697 clear_tv(rettv);
4698 rettv->v_type = VAR_STRING;
4699 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004701 else if (op == '+' && rettv->v_type == VAR_LIST
4702 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004703 {
4704 /* concatenate Lists */
4705 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4706 &var3) == FAIL)
4707 {
4708 clear_tv(rettv);
4709 clear_tv(&var2);
4710 return FAIL;
4711 }
4712 clear_tv(rettv);
4713 *rettv = var3;
4714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 else
4716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004717 int error = FALSE;
4718
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719#ifdef FEAT_FLOAT
4720 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 f1 = rettv->vval.v_float;
4723 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725 else
4726#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004727 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 n1 = get_tv_number_chk(rettv, &error);
4729 if (error)
4730 {
4731 /* This can only happen for "list + non-list". For
4732 * "non-list + ..." or "something - ...", we returned
4733 * before evaluating the 2nd operand. */
4734 clear_tv(rettv);
4735 return FAIL;
4736 }
4737#ifdef FEAT_FLOAT
4738 if (var2.v_type == VAR_FLOAT)
4739 f1 = n1;
4740#endif
4741 }
4742#ifdef FEAT_FLOAT
4743 if (var2.v_type == VAR_FLOAT)
4744 {
4745 f2 = var2.vval.v_float;
4746 n2 = 0;
4747 }
4748 else
4749#endif
4750 {
4751 n2 = get_tv_number_chk(&var2, &error);
4752 if (error)
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
4758#ifdef FEAT_FLOAT
4759 if (rettv->v_type == VAR_FLOAT)
4760 f2 = n2;
4761#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004764
4765#ifdef FEAT_FLOAT
4766 /* If there is a float on either side the result is a float. */
4767 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4768 {
4769 if (op == '+')
4770 f1 = f1 + f2;
4771 else
4772 f1 = f1 - f2;
4773 rettv->v_type = VAR_FLOAT;
4774 rettv->vval.v_float = f1;
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#endif
4778 {
4779 if (op == '+')
4780 n1 = n1 + n2;
4781 else
4782 n1 = n1 - n2;
4783 rettv->v_type = VAR_NUMBER;
4784 rettv->vval.v_number = n1;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 }
4790 return OK;
4791}
4792
4793/*
4794 * Handle fifth level expression:
4795 * * number multiplication
4796 * / number division
4797 * % number modulo
4798 *
4799 * "arg" must point to the first non-white of the expression.
4800 * "arg" is advanced to the next non-white after the recognized expression.
4801 *
4802 * Return OK or FAIL.
4803 */
4804 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004809 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
Bram Moolenaar33570922005-01-25 22:26:29 +00004811 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 int op;
4813 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814#ifdef FEAT_FLOAT
4815 int use_float = FALSE;
4816 float_T f1 = 0, f2;
4817#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 /*
4821 * Get the first variable.
4822 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004823 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 return FAIL;
4825
4826 /*
4827 * Repeat computing, until no '*', '/' or '%' is following.
4828 */
4829 for (;;)
4830 {
4831 op = **arg;
4832 if (op != '*' && op != '/' && op != '%')
4833 break;
4834
4835 if (evaluate)
4836 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#ifdef FEAT_FLOAT
4838 if (rettv->v_type == VAR_FLOAT)
4839 {
4840 f1 = rettv->vval.v_float;
4841 use_float = TRUE;
4842 n1 = 0;
4843 }
4844 else
4845#endif
4846 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004847 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 if (error)
4849 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 else
4852 n1 = 0;
4853
4854 /*
4855 * Get the second variable.
4856 */
4857 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004858 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 return FAIL;
4860
4861 if (evaluate)
4862 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863#ifdef FEAT_FLOAT
4864 if (var2.v_type == VAR_FLOAT)
4865 {
4866 if (!use_float)
4867 {
4868 f1 = n1;
4869 use_float = TRUE;
4870 }
4871 f2 = var2.vval.v_float;
4872 n2 = 0;
4873 }
4874 else
4875#endif
4876 {
4877 n2 = get_tv_number_chk(&var2, &error);
4878 clear_tv(&var2);
4879 if (error)
4880 return FAIL;
4881#ifdef FEAT_FLOAT
4882 if (use_float)
4883 f2 = n2;
4884#endif
4885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891#ifdef FEAT_FLOAT
4892 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 if (op == '*')
4895 f1 = f1 * f2;
4896 else if (op == '/')
4897 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898# ifdef VMS
4899 /* VMS crashes on divide by zero, work around it */
4900 if (f2 == 0.0)
4901 {
4902 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004903 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004904 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004905 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004906 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004907 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004908 }
4909 else
4910 f1 = f1 / f2;
4911# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 /* We rely on the floating point library to handle divide
4913 * by zero to result in "inf" and not a crash. */
4914 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004915# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004918 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004919 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 return FAIL;
4921 }
4922 rettv->v_type = VAR_FLOAT;
4923 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 if (op == '*')
4929 n1 = n1 * n2;
4930 else if (op == '/')
4931 {
4932 if (n2 == 0) /* give an error message? */
4933 {
4934 if (n1 == 0)
4935 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4936 else if (n1 < 0)
4937 n1 = -0x7fffffffL;
4938 else
4939 n1 = 0x7fffffffL;
4940 }
4941 else
4942 n1 = n1 / n2;
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945 {
4946 if (n2 == 0) /* give an error message? */
4947 n1 = 0;
4948 else
4949 n1 = n1 % n2;
4950 }
4951 rettv->v_type = VAR_NUMBER;
4952 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 }
4956
4957 return OK;
4958}
4959
4960/*
4961 * Handle sixth level expression:
4962 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004963 * "string" string constant
4964 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 * &option-name option value
4966 * @r register contents
4967 * identifier variable value
4968 * function() function call
4969 * $VAR environment variable
4970 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004971 * [expr, expr] List
4972 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 *
4974 * Also handle:
4975 * ! in front logical NOT
4976 * - in front unary minus
4977 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004978 * trailing [] subscript in String or List
4979 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 *
4981 * "arg" must point to the first non-white of the expression.
4982 * "arg" is advanced to the next non-white after the recognized expression.
4983 *
4984 * Return OK or FAIL.
4985 */
4986 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004987eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004991 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 long n;
4994 int len;
4995 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 char_u *start_leader, *end_leader;
4997 int ret = OK;
4998 char_u *alias;
4999
5000 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
5006 /*
5007 * Skip '!' and '-' characters. They are handled later.
5008 */
5009 start_leader = *arg;
5010 while (**arg == '!' || **arg == '-' || **arg == '+')
5011 *arg = skipwhite(*arg + 1);
5012 end_leader = *arg;
5013
5014 switch (**arg)
5015 {
5016 /*
5017 * Number constant.
5018 */
5019 case '0':
5020 case '1':
5021 case '2':
5022 case '3':
5023 case '4':
5024 case '5':
5025 case '6':
5026 case '7':
5027 case '8':
5028 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 {
5030#ifdef FEAT_FLOAT
5031 char_u *p = skipdigits(*arg + 1);
5032 int get_float = FALSE;
5033
5034 /* We accept a float when the format matches
5035 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005036 * strict to avoid backwards compatibility problems.
5037 * Don't look for a float after the "." operator, so that
5038 * ":let vers = 1.2.3" doesn't fail. */
5039 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005041 get_float = TRUE;
5042 p = skipdigits(p + 2);
5043 if (*p == 'e' || *p == 'E')
5044 {
5045 ++p;
5046 if (*p == '-' || *p == '+')
5047 ++p;
5048 if (!vim_isdigit(*p))
5049 get_float = FALSE;
5050 else
5051 p = skipdigits(p + 1);
5052 }
5053 if (ASCII_ISALPHA(*p) || *p == '.')
5054 get_float = FALSE;
5055 }
5056 if (get_float)
5057 {
5058 float_T f;
5059
5060 *arg += string2float(*arg, &f);
5061 if (evaluate)
5062 {
5063 rettv->v_type = VAR_FLOAT;
5064 rettv->vval.v_float = f;
5065 }
5066 }
5067 else
5068#endif
5069 {
5070 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5071 *arg += len;
5072 if (evaluate)
5073 {
5074 rettv->v_type = VAR_NUMBER;
5075 rettv->vval.v_number = n;
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 /*
5082 * String constant: "string".
5083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 break;
5086
5087 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 break;
5092
5093 /*
5094 * List: [expr, expr]
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 * Dictionary: {key: val, key: val}
5101 */
5102 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5103 break;
5104
5105 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005106 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005108 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Environment variable: $VAR.
5113 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005114 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
5116
5117 /*
5118 * Register contents: @r.
5119 */
5120 case '@': ++*arg;
5121 if (evaluate)
5122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005124 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 if (**arg != NUL)
5127 ++*arg;
5128 break;
5129
5130 /*
5131 * nested expression: (expression).
5132 */
5133 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (**arg == ')')
5136 ++*arg;
5137 else if (ret == OK)
5138 {
5139 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ret = FAIL;
5142 }
5143 break;
5144
Bram Moolenaar8c711452005-01-14 21:53:12 +00005145 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148
5149 if (ret == NOTDONE)
5150 {
5151 /*
5152 * Must be a variable or function name.
5153 * Can also be a curly-braces kind of name: {expr}.
5154 */
5155 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005156 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005157 if (alias != NULL)
5158 s = alias;
5159
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005160 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 ret = FAIL;
5162 else
5163 {
5164 if (**arg == '(') /* recursive! */
5165 {
5166 /* If "s" is the name of a variable of type VAR_FUNC
5167 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005168 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169
5170 /* Invoke the function. */
5171 ret = get_func_tv(s, len, rettv, arg,
5172 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005174
5175 /* If evaluate is FALSE rettv->v_type was not set in
5176 * get_func_tv, but it's needed in handle_subscript() to parse
5177 * what follows. So set it here. */
5178 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5179 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005180 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005181 rettv->v_type = VAR_FUNC;
5182 }
5183
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 /* Stop the expression evaluation when immediately
5185 * aborting on error, or when an interrupt occurred or
5186 * an exception was thrown but not caught. */
5187 if (aborting())
5188 {
5189 if (ret == OK)
5190 clear_tv(rettv);
5191 ret = FAIL;
5192 }
5193 }
5194 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005195 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005196 else
5197 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005198 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005199 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 }
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 *arg = skipwhite(*arg);
5203
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005204 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5205 * expr(expr). */
5206 if (ret == OK)
5207 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 /*
5210 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5211 */
5212 if (ret == OK && evaluate && end_leader > start_leader)
5213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005215 int val = 0;
5216#ifdef FEAT_FLOAT
5217 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 if (rettv->v_type == VAR_FLOAT)
5220 f = rettv->vval.v_float;
5221 else
5222#endif
5223 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 clear_tv(rettv);
5227 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229 else
5230 {
5231 while (end_leader > start_leader)
5232 {
5233 --end_leader;
5234 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235 {
5236#ifdef FEAT_FLOAT
5237 if (rettv->v_type == VAR_FLOAT)
5238 f = !f;
5239 else
5240#endif
5241 val = !val;
5242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005244 {
5245#ifdef FEAT_FLOAT
5246 if (rettv->v_type == VAR_FLOAT)
5247 f = -f;
5248 else
5249#endif
5250 val = -val;
5251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005252 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005253#ifdef FEAT_FLOAT
5254 if (rettv->v_type == VAR_FLOAT)
5255 {
5256 clear_tv(rettv);
5257 rettv->vval.v_float = f;
5258 }
5259 else
5260#endif
5261 {
5262 clear_tv(rettv);
5263 rettv->v_type = VAR_NUMBER;
5264 rettv->vval.v_number = val;
5265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268
5269 return ret;
5270}
5271
5272/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005273 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5274 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5276 */
5277 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005280 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005282 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283{
5284 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005285 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005286 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 long len = -1;
5288 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005294 if (verbose)
5295 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 return FAIL;
5297 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005298#ifdef FEAT_FLOAT
5299 else if (rettv->v_type == VAR_FLOAT)
5300 {
5301 if (verbose)
5302 EMSG(_(e_float_as_string));
5303 return FAIL;
5304 }
5305#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005306
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005308 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 /*
5310 * dict.name
5311 */
5312 key = *arg + 1;
5313 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5314 ;
5315 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005316 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005317 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 }
5319 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 /*
5322 * something[idx]
5323 *
5324 * Get the (first) variable from inside the [].
5325 */
5326 *arg = skipwhite(*arg + 1);
5327 if (**arg == ':')
5328 empty1 = TRUE;
5329 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5330 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5332 {
5333 /* not a number or string */
5334 clear_tv(&var1);
5335 return FAIL;
5336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005337
5338 /*
5339 * Get the second variable from inside the [:].
5340 */
5341 if (**arg == ':')
5342 {
5343 range = TRUE;
5344 *arg = skipwhite(*arg + 1);
5345 if (**arg == ']')
5346 empty2 = TRUE;
5347 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 if (!empty1)
5350 clear_tv(&var1);
5351 return FAIL;
5352 }
5353 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5354 {
5355 /* not a number or string */
5356 if (!empty1)
5357 clear_tv(&var1);
5358 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 return FAIL;
5360 }
5361 }
5362
5363 /* Check for the ']'. */
5364 if (**arg != ']')
5365 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005366 if (verbose)
5367 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005368 clear_tv(&var1);
5369 if (range)
5370 clear_tv(&var2);
5371 return FAIL;
5372 }
5373 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375
5376 if (evaluate)
5377 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005378 n1 = 0;
5379 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 n1 = get_tv_number(&var1);
5382 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 }
5384 if (range)
5385 {
5386 if (empty2)
5387 n2 = -1;
5388 else
5389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 n2 = get_tv_number(&var2);
5391 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 }
5393 }
5394
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005395 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
5397 case VAR_NUMBER:
5398 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005399 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 len = (long)STRLEN(s);
5401 if (range)
5402 {
5403 /* The resulting variable is a substring. If the indexes
5404 * are out of range the result is empty. */
5405 if (n1 < 0)
5406 {
5407 n1 = len + n1;
5408 if (n1 < 0)
5409 n1 = 0;
5410 }
5411 if (n2 < 0)
5412 n2 = len + n2;
5413 else if (n2 >= len)
5414 n2 = len;
5415 if (n1 >= len || n2 < 0 || n1 > n2)
5416 s = NULL;
5417 else
5418 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5419 }
5420 else
5421 {
5422 /* The resulting variable is a string of a single
5423 * character. If the index is too big or negative the
5424 * result is empty. */
5425 if (n1 >= len || n1 < 0)
5426 s = NULL;
5427 else
5428 s = vim_strnsave(s + n1, 1);
5429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 clear_tv(rettv);
5431 rettv->v_type = VAR_STRING;
5432 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 break;
5434
5435 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 if (n1 < 0)
5438 n1 = len + n1;
5439 if (!empty1 && (n1 < 0 || n1 >= len))
5440 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005441 /* For a range we allow invalid values and return an empty
5442 * list. A list index out of range is an error. */
5443 if (!range)
5444 {
5445 if (verbose)
5446 EMSGN(_(e_listidx), n1);
5447 return FAIL;
5448 }
5449 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451 if (range)
5452 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 list_T *l;
5454 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455
5456 if (n2 < 0)
5457 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005458 else if (n2 >= len)
5459 n2 = len - 1;
5460 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005461 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 l = list_alloc();
5463 if (l == NULL)
5464 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 n1 <= n2; ++n1)
5467 {
5468 if (list_append_tv(l, &item->li_tv) == FAIL)
5469 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005470 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 return FAIL;
5472 }
5473 item = item->li_next;
5474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 clear_tv(rettv);
5476 rettv->v_type = VAR_LIST;
5477 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005478 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 else
5481 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005482 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 clear_tv(rettv);
5484 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 }
5486 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487
5488 case VAR_DICT:
5489 if (range)
5490 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005491 if (verbose)
5492 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 if (len == -1)
5494 clear_tv(&var1);
5495 return FAIL;
5496 }
5497 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499
5500 if (len == -1)
5501 {
5502 key = get_tv_string(&var1);
5503 if (*key == NUL)
5504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005505 if (verbose)
5506 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 clear_tv(&var1);
5508 return FAIL;
5509 }
5510 }
5511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005512 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005513
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005514 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005515 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 if (len == -1)
5517 clear_tv(&var1);
5518 if (item == NULL)
5519 return FAIL;
5520
5521 copy_tv(&item->di_tv, &var1);
5522 clear_tv(rettv);
5523 *rettv = var1;
5524 }
5525 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 }
5527 }
5528
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 return OK;
5530}
5531
5532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 * Get an option value.
5534 * "arg" points to the '&' or '+' before the option name.
5535 * "arg" is advanced to character after the option name.
5536 * Return OK or FAIL.
5537 */
5538 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 int evaluate;
5543{
5544 char_u *option_end;
5545 long numval;
5546 char_u *stringval;
5547 int opt_type;
5548 int c;
5549 int working = (**arg == '+'); /* has("+option") */
5550 int ret = OK;
5551 int opt_flags;
5552
5553 /*
5554 * Isolate the option name and find its value.
5555 */
5556 option_end = find_option_end(arg, &opt_flags);
5557 if (option_end == NULL)
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 EMSG2(_("E112: Option name missing: %s"), *arg);
5561 return FAIL;
5562 }
5563
5564 if (!evaluate)
5565 {
5566 *arg = option_end;
5567 return OK;
5568 }
5569
5570 c = *option_end;
5571 *option_end = NUL;
5572 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
5575 if (opt_type == -3) /* invalid name */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 EMSG2(_("E113: Unknown option: %s"), *arg);
5579 ret = FAIL;
5580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 {
5583 if (opt_type == -2) /* hidden string option */
5584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 rettv->v_type = VAR_STRING;
5586 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588 else if (opt_type == -1) /* hidden number option */
5589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005590 rettv->v_type = VAR_NUMBER;
5591 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593 else if (opt_type == 1) /* number option */
5594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595 rettv->v_type = VAR_NUMBER;
5596 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598 else /* string option */
5599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 rettv->v_type = VAR_STRING;
5601 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 }
5604 else if (working && (opt_type == -2 || opt_type == -1))
5605 ret = FAIL;
5606
5607 *option_end = c; /* put back for error messages */
5608 *arg = option_end;
5609
5610 return ret;
5611}
5612
5613/*
5614 * Allocate a variable for a string constant.
5615 * Return OK or FAIL.
5616 */
5617 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 int evaluate;
5622{
5623 char_u *p;
5624 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 int extra = 0;
5626
5627 /*
5628 * Find the end of the string, skipping backslashed characters.
5629 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005630 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 if (*p == '\\' && p[1] != NUL)
5633 {
5634 ++p;
5635 /* A "\<x>" form occupies at least 4 characters, and produces up
5636 * to 6 characters: reserve space for 2 extra */
5637 if (*p == '<')
5638 extra += 2;
5639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641
5642 if (*p != '"')
5643 {
5644 EMSG2(_("E114: Missing quote: %s"), *arg);
5645 return FAIL;
5646 }
5647
5648 /* If only parsing, set *arg and return here */
5649 if (!evaluate)
5650 {
5651 *arg = p + 1;
5652 return OK;
5653 }
5654
5655 /*
5656 * Copy the string into allocated memory, handling backslashed
5657 * characters.
5658 */
5659 name = alloc((unsigned)(p - *arg + extra));
5660 if (name == NULL)
5661 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 rettv->v_type = VAR_STRING;
5663 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
5667 if (*p == '\\')
5668 {
5669 switch (*++p)
5670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 case 'b': *name++ = BS; ++p; break;
5672 case 'e': *name++ = ESC; ++p; break;
5673 case 'f': *name++ = FF; ++p; break;
5674 case 'n': *name++ = NL; ++p; break;
5675 case 'r': *name++ = CAR; ++p; break;
5676 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 case 'X': /* hex: "\x1", "\x12" */
5679 case 'x':
5680 case 'u': /* Unicode: "\u0023" */
5681 case 'U':
5682 if (vim_isxdigit(p[1]))
5683 {
5684 int n, nr;
5685 int c = toupper(*p);
5686
5687 if (c == 'X')
5688 n = 2;
5689 else
5690 n = 4;
5691 nr = 0;
5692 while (--n >= 0 && vim_isxdigit(p[1]))
5693 {
5694 ++p;
5695 nr = (nr << 4) + hex2nr(*p);
5696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698#ifdef FEAT_MBYTE
5699 /* For "\u" store the number according to
5700 * 'encoding'. */
5701 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 else
5704#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708
5709 /* octal: "\1", "\12", "\123" */
5710 case '0':
5711 case '1':
5712 case '2':
5713 case '3':
5714 case '4':
5715 case '5':
5716 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005717 case '7': *name = *p++ - '0';
5718 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 *name = (*name << 3) + *p++ - '0';
5721 if (*p >= '0' && *p <= '7')
5722 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 break;
5726
5727 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (extra != 0)
5730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005731 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 break;
5733 }
5734 /* FALLTHROUGH */
5735
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 break;
5738 }
5739 }
5740 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 *arg = p + 1;
5746
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 return OK;
5748}
5749
5750/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 * Return OK or FAIL.
5753 */
5754 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005755get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 int evaluate;
5759{
5760 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 int reduce = 0;
5763
5764 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 */
5767 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 break;
5773 ++reduce;
5774 ++p;
5775 }
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781 return FAIL;
5782 }
5783
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 if (!evaluate)
5786 {
5787 *arg = p + 1;
5788 return OK;
5789 }
5790
5791 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 */
5794 str = alloc((unsigned)((p - *arg) - reduce));
5795 if (str == NULL)
5796 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 rettv->v_type = VAR_STRING;
5798 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 break;
5806 ++p;
5807 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 *arg = p + 1;
5812
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005813 return OK;
5814}
5815
5816/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005817 * Allocate a variable for a List and fill it from "*arg".
5818 * Return OK or FAIL.
5819 */
5820 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005821get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005823 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 int evaluate;
5825{
Bram Moolenaar33570922005-01-25 22:26:29 +00005826 list_T *l = NULL;
5827 typval_T tv;
5828 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829
5830 if (evaluate)
5831 {
5832 l = list_alloc();
5833 if (l == NULL)
5834 return FAIL;
5835 }
5836
5837 *arg = skipwhite(*arg + 1);
5838 while (**arg != ']' && **arg != NUL)
5839 {
5840 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5841 goto failret;
5842 if (evaluate)
5843 {
5844 item = listitem_alloc();
5845 if (item != NULL)
5846 {
5847 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005848 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005849 list_append(l, item);
5850 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005851 else
5852 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 }
5854
5855 if (**arg == ']')
5856 break;
5857 if (**arg != ',')
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 goto failret;
5861 }
5862 *arg = skipwhite(*arg + 1);
5863 }
5864
5865 if (**arg != ']')
5866 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868failret:
5869 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005870 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 return FAIL;
5872 }
5873
5874 *arg = skipwhite(*arg + 1);
5875 if (evaluate)
5876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005877 rettv->v_type = VAR_LIST;
5878 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 ++l->lv_refcount;
5880 }
5881
5882 return OK;
5883}
5884
5885/*
5886 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005887 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005889 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890list_alloc()
5891{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 list_T *l;
5893
5894 l = (list_T *)alloc_clear(sizeof(list_T));
5895 if (l != NULL)
5896 {
5897 /* Prepend the list to the list of lists for garbage collection. */
5898 if (first_list != NULL)
5899 first_list->lv_used_prev = l;
5900 l->lv_used_prev = NULL;
5901 l->lv_used_next = first_list;
5902 first_list = l;
5903 }
5904 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005905}
5906
5907/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005908 * Allocate an empty list for a return value.
5909 * Returns OK or FAIL.
5910 */
5911 static int
5912rettv_list_alloc(rettv)
5913 typval_T *rettv;
5914{
5915 list_T *l = list_alloc();
5916
5917 if (l == NULL)
5918 return FAIL;
5919
5920 rettv->vval.v_list = l;
5921 rettv->v_type = VAR_LIST;
5922 ++l->lv_refcount;
5923 return OK;
5924}
5925
5926/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 * Unreference a list: decrement the reference count and free it when it
5928 * becomes zero.
5929 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005930 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005934 if (l != NULL && --l->lv_refcount <= 0)
5935 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936}
5937
5938/*
5939 * Free a list, including all items it points to.
5940 * Ignores the reference count.
5941 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005942 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943list_free(l, recurse)
5944 list_T *l;
5945 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946{
Bram Moolenaar33570922005-01-25 22:26:29 +00005947 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005949 /* Remove the list from the list of lists for garbage collection. */
5950 if (l->lv_used_prev == NULL)
5951 first_list = l->lv_used_next;
5952 else
5953 l->lv_used_prev->lv_used_next = l->lv_used_next;
5954 if (l->lv_used_next != NULL)
5955 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5956
Bram Moolenaard9fba312005-06-26 22:34:35 +00005957 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005959 /* Remove the item before deleting it. */
5960 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005961 if (recurse || (item->li_tv.v_type != VAR_LIST
5962 && item->li_tv.v_type != VAR_DICT))
5963 clear_tv(&item->li_tv);
5964 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 }
5966 vim_free(l);
5967}
5968
5969/*
5970 * Allocate a list item.
5971 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005972 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973listitem_alloc()
5974{
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976}
5977
5978/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005979 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005981 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005985 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 vim_free(item);
5987}
5988
5989/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990 * Remove a list item from a List and free it. Also clears the value.
5991 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005992 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005993listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 list_T *l;
5995 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005996{
5997 list_remove(l, item, item);
5998 listitem_free(item);
5999}
6000
6001/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 * Get the number of items in a list.
6003 */
6004 static long
6005list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008 if (l == NULL)
6009 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006010 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011}
6012
6013/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 * Return TRUE when two lists have exactly the same values.
6015 */
6016 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006018 list_T *l1;
6019 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006020 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006021 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006022{
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006025 if (l1 == NULL || l2 == NULL)
6026 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006027 if (l1 == l2)
6028 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006029 if (list_len(l1) != list_len(l2))
6030 return FALSE;
6031
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006032 for (item1 = l1->lv_first, item2 = l2->lv_first;
6033 item1 != NULL && item2 != NULL;
6034 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006035 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036 return FALSE;
6037 return item1 == NULL && item2 == NULL;
6038}
6039
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006040#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6041 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006042/*
6043 * Return the dictitem that an entry in a hashtable points to.
6044 */
6045 dictitem_T *
6046dict_lookup(hi)
6047 hashitem_T *hi;
6048{
6049 return HI2DI(hi);
6050}
6051#endif
6052
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006053/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 * Return TRUE when two dictionaries have exactly the same key/values.
6055 */
6056 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 dict_T *d1;
6059 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 hashitem_T *hi;
6064 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006066
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006067 if (d1 == NULL || d2 == NULL)
6068 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006069 if (d1 == d2)
6070 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071 if (dict_len(d1) != dict_len(d2))
6072 return FALSE;
6073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006074 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006076 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 if (!HASHITEM_EMPTY(hi))
6078 {
6079 item2 = dict_find(d2, hi->hi_key, -1);
6080 if (item2 == NULL)
6081 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006083 return FALSE;
6084 --todo;
6085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086 }
6087 return TRUE;
6088}
6089
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090static int tv_equal_recurse_limit;
6091
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006094 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006095 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 */
6097 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 typval_T *tv1;
6100 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101 int ic; /* ignore case */
6102 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103{
6104 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006105 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006107 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 * recursiveness to a limit. We guess they are equal then.
6114 * A fixed limit has the problem of still taking an awful long time.
6115 * Reduce the limit every time running into it. That should work fine for
6116 * deeply linked structures that are not recursively linked and catch
6117 * recursiveness quickly. */
6118 if (!recursive)
6119 tv_equal_recurse_limit = 1000;
6120 if (recursive_cnt >= tv_equal_recurse_limit)
6121 {
6122 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006123 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006125
6126 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006128 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6131 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006132 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006135 ++recursive_cnt;
6136 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6137 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006138 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006139
6140 case VAR_FUNC:
6141 return (tv1->vval.v_string != NULL
6142 && tv2->vval.v_string != NULL
6143 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6144
6145 case VAR_NUMBER:
6146 return tv1->vval.v_number == tv2->vval.v_number;
6147
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006148#ifdef FEAT_FLOAT
6149 case VAR_FLOAT:
6150 return tv1->vval.v_float == tv2->vval.v_float;
6151#endif
6152
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006153 case VAR_STRING:
6154 s1 = get_tv_string_buf(tv1, buf1);
6155 s2 = get_tv_string_buf(tv2, buf2);
6156 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006157 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006158
6159 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006160 return TRUE;
6161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Locate item with index "n" in list "l" and return it.
6165 * A negative index is counted from the end; -1 is the last item.
6166 * Returns NULL when "n" is out of range.
6167 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006168 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 long n;
6172{
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 long idx;
6175
6176 if (l == NULL)
6177 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006178
6179 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006181 n = l->lv_len + n;
6182
6183 /* Check for index out of range. */
6184 if (n < 0 || n >= l->lv_len)
6185 return NULL;
6186
6187 /* When there is a cached index may start search from there. */
6188 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006190 if (n < l->lv_idx / 2)
6191 {
6192 /* closest to the start of the list */
6193 item = l->lv_first;
6194 idx = 0;
6195 }
6196 else if (n > (l->lv_idx + l->lv_len) / 2)
6197 {
6198 /* closest to the end of the list */
6199 item = l->lv_last;
6200 idx = l->lv_len - 1;
6201 }
6202 else
6203 {
6204 /* closest to the cached index */
6205 item = l->lv_idx_item;
6206 idx = l->lv_idx;
6207 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 }
6209 else
6210 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006211 if (n < l->lv_len / 2)
6212 {
6213 /* closest to the start of the list */
6214 item = l->lv_first;
6215 idx = 0;
6216 }
6217 else
6218 {
6219 /* closest to the end of the list */
6220 item = l->lv_last;
6221 idx = l->lv_len - 1;
6222 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006223 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006224
6225 while (n > idx)
6226 {
6227 /* search forward */
6228 item = item->li_next;
6229 ++idx;
6230 }
6231 while (n < idx)
6232 {
6233 /* search backward */
6234 item = item->li_prev;
6235 --idx;
6236 }
6237
6238 /* cache the used index */
6239 l->lv_idx = idx;
6240 l->lv_idx_item = item;
6241
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242 return item;
6243}
6244
6245/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006246 * Get list item "l[idx]" as a number.
6247 */
6248 static long
6249list_find_nr(l, idx, errorp)
6250 list_T *l;
6251 long idx;
6252 int *errorp; /* set to TRUE when something wrong */
6253{
6254 listitem_T *li;
6255
6256 li = list_find(l, idx);
6257 if (li == NULL)
6258 {
6259 if (errorp != NULL)
6260 *errorp = TRUE;
6261 return -1L;
6262 }
6263 return get_tv_number_chk(&li->li_tv, errorp);
6264}
6265
6266/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006267 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6268 */
6269 char_u *
6270list_find_str(l, idx)
6271 list_T *l;
6272 long idx;
6273{
6274 listitem_T *li;
6275
6276 li = list_find(l, idx - 1);
6277 if (li == NULL)
6278 {
6279 EMSGN(_(e_listidx), idx);
6280 return NULL;
6281 }
6282 return get_tv_string(&li->li_tv);
6283}
6284
6285/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006286 * Locate "item" list "l" and return its index.
6287 * Returns -1 when "item" is not in the list.
6288 */
6289 static long
6290list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 list_T *l;
6292 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006293{
6294 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006296
6297 if (l == NULL)
6298 return -1;
6299 idx = 0;
6300 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6301 ++idx;
6302 if (li == NULL)
6303 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006304 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006305}
6306
6307/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 * Append item "item" to the end of list "l".
6309 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006310 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 list_T *l;
6313 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314{
6315 if (l->lv_last == NULL)
6316 {
6317 /* empty list */
6318 l->lv_first = item;
6319 l->lv_last = item;
6320 item->li_prev = NULL;
6321 }
6322 else
6323 {
6324 l->lv_last->li_next = item;
6325 item->li_prev = l->lv_last;
6326 l->lv_last = item;
6327 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006328 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 item->li_next = NULL;
6330}
6331
6332/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006336 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006338 list_T *l;
6339 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006341 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342
Bram Moolenaar05159a02005-02-26 23:04:13 +00006343 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006344 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006345 copy_tv(tv, &li->li_tv);
6346 list_append(l, li);
6347 return OK;
6348}
6349
6350/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006351 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006352 * Return FAIL when out of memory.
6353 */
6354 int
6355list_append_dict(list, dict)
6356 list_T *list;
6357 dict_T *dict;
6358{
6359 listitem_T *li = listitem_alloc();
6360
6361 if (li == NULL)
6362 return FAIL;
6363 li->li_tv.v_type = VAR_DICT;
6364 li->li_tv.v_lock = 0;
6365 li->li_tv.vval.v_dict = dict;
6366 list_append(list, li);
6367 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 return OK;
6369}
6370
6371/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006373 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006374 * Returns FAIL when out of memory.
6375 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006376 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 list_T *l;
6379 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006380 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006381{
6382 listitem_T *li = listitem_alloc();
6383
6384 if (li == NULL)
6385 return FAIL;
6386 list_append(l, li);
6387 li->li_tv.v_type = VAR_STRING;
6388 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006389 if (str == NULL)
6390 li->li_tv.vval.v_string = NULL;
6391 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006392 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006393 return FAIL;
6394 return OK;
6395}
6396
6397/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006398 * Append "n" to list "l".
6399 * Returns FAIL when out of memory.
6400 */
6401 static int
6402list_append_number(l, n)
6403 list_T *l;
6404 varnumber_T n;
6405{
6406 listitem_T *li;
6407
6408 li = listitem_alloc();
6409 if (li == NULL)
6410 return FAIL;
6411 li->li_tv.v_type = VAR_NUMBER;
6412 li->li_tv.v_lock = 0;
6413 li->li_tv.vval.v_number = n;
6414 list_append(l, li);
6415 return OK;
6416}
6417
6418/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 * If "item" is NULL append at the end.
6421 * Return FAIL when out of memory.
6422 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006423 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006425 list_T *l;
6426 typval_T *tv;
6427 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428{
Bram Moolenaar33570922005-01-25 22:26:29 +00006429 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430
6431 if (ni == NULL)
6432 return FAIL;
6433 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006434 list_insert(l, ni, item);
6435 return OK;
6436}
6437
6438 void
6439list_insert(l, ni, item)
6440 list_T *l;
6441 listitem_T *ni;
6442 listitem_T *item;
6443{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006444 if (item == NULL)
6445 /* Append new item at end of list. */
6446 list_append(l, ni);
6447 else
6448 {
6449 /* Insert new item before existing item. */
6450 ni->li_prev = item->li_prev;
6451 ni->li_next = item;
6452 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006455 ++l->lv_idx;
6456 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006458 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006460 l->lv_idx_item = NULL;
6461 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465}
6466
6467/*
6468 * Extend "l1" with "l2".
6469 * If "bef" is NULL append at the end, otherwise insert before this item.
6470 * Returns FAIL when out of memory.
6471 */
6472 static int
6473list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 list_T *l1;
6475 list_T *l2;
6476 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477{
Bram Moolenaar33570922005-01-25 22:26:29 +00006478 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006479 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006481 /* We also quit the loop when we have inserted the original item count of
6482 * the list, avoid a hang when we extend a list with itself. */
6483 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6485 return FAIL;
6486 return OK;
6487}
6488
6489/*
6490 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6491 * Return FAIL when out of memory.
6492 */
6493 static int
6494list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 list_T *l1;
6496 list_T *l2;
6497 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498{
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006501 if (l1 == NULL || l2 == NULL)
6502 return FAIL;
6503
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 if (l == NULL)
6507 return FAIL;
6508 tv->v_type = VAR_LIST;
6509 tv->vval.v_list = l;
6510
6511 /* append all items from the second list */
6512 return list_extend(l, l2, NULL);
6513}
6514
6515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006516 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 * Returns NULL when out of memory.
6520 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006522list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526{
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 list_T *copy;
6528 listitem_T *item;
6529 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530
6531 if (orig == NULL)
6532 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533
6534 copy = list_alloc();
6535 if (copy != NULL)
6536 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006537 if (copyID != 0)
6538 {
6539 /* Do this before adding the items, because one of the items may
6540 * refer back to this list. */
6541 orig->lv_copyID = copyID;
6542 orig->lv_copylist = copy;
6543 }
6544 for (item = orig->lv_first; item != NULL && !got_int;
6545 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 {
6547 ni = listitem_alloc();
6548 if (ni == NULL)
6549 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006551 {
6552 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6553 {
6554 vim_free(ni);
6555 break;
6556 }
6557 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006559 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 list_append(copy, ni);
6561 }
6562 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006563 if (item != NULL)
6564 {
6565 list_unref(copy);
6566 copy = NULL;
6567 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 }
6569
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 return copy;
6571}
6572
6573/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006575 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006577 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006578list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 list_T *l;
6580 listitem_T *item;
6581 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585 /* notify watchers */
6586 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006588 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 list_fix_watch(l, ip);
6590 if (ip == item2)
6591 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593
6594 if (item2->li_next == NULL)
6595 l->lv_last = item->li_prev;
6596 else
6597 item2->li_next->li_prev = item->li_prev;
6598 if (item->li_prev == NULL)
6599 l->lv_first = item2->li_next;
6600 else
6601 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006602 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603}
6604
6605/*
6606 * Return an allocated string with the string representation of a list.
6607 * May return NULL.
6608 */
6609 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006610list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006612 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613{
6614 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615
6616 if (tv->vval.v_list == NULL)
6617 return NULL;
6618 ga_init2(&ga, (int)sizeof(char), 80);
6619 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006620 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006621 {
6622 vim_free(ga.ga_data);
6623 return NULL;
6624 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 ga_append(&ga, ']');
6626 ga_append(&ga, NUL);
6627 return (char_u *)ga.ga_data;
6628}
6629
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006630typedef struct join_S {
6631 char_u *s;
6632 char_u *tofree;
6633} join_T;
6634
6635 static int
6636list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6637 garray_T *gap; /* to store the result in */
6638 list_T *l;
6639 char_u *sep;
6640 int echo_style;
6641 int copyID;
6642 garray_T *join_gap; /* to keep each list item string */
6643{
6644 int i;
6645 join_T *p;
6646 int len;
6647 int sumlen = 0;
6648 int first = TRUE;
6649 char_u *tofree;
6650 char_u numbuf[NUMBUFLEN];
6651 listitem_T *item;
6652 char_u *s;
6653
6654 /* Stringify each item in the list. */
6655 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6656 {
6657 if (echo_style)
6658 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6659 else
6660 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6661 if (s == NULL)
6662 return FAIL;
6663
6664 len = (int)STRLEN(s);
6665 sumlen += len;
6666
6667 ga_grow(join_gap, 1);
6668 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6669 if (tofree != NULL || s != numbuf)
6670 {
6671 p->s = s;
6672 p->tofree = tofree;
6673 }
6674 else
6675 {
6676 p->s = vim_strnsave(s, len);
6677 p->tofree = p->s;
6678 }
6679
6680 line_breakcheck();
6681 }
6682
6683 /* Allocate result buffer with its total size, avoid re-allocation and
6684 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6685 if (join_gap->ga_len >= 2)
6686 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6687 if (ga_grow(gap, sumlen + 2) == FAIL)
6688 return FAIL;
6689
6690 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6691 {
6692 if (first)
6693 first = FALSE;
6694 else
6695 ga_concat(gap, sep);
6696 p = ((join_T *)join_gap->ga_data) + i;
6697
6698 if (p->s != NULL)
6699 ga_concat(gap, p->s);
6700 line_breakcheck();
6701 }
6702
6703 return OK;
6704}
6705
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006708 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006709 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006712list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006715 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006716 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006717 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006718{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719 garray_T join_ga;
6720 int retval;
6721 join_T *p;
6722 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6725 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6726
6727 /* Dispose each item in join_ga. */
6728 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006729 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 p = (join_T *)join_ga.ga_data;
6731 for (i = 0; i < join_ga.ga_len; ++i)
6732 {
6733 vim_free(p->tofree);
6734 ++p;
6735 }
6736 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006737 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738
6739 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006740}
6741
6742/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 * Garbage collection for lists and dictionaries.
6744 *
6745 * We use reference counts to be able to free most items right away when they
6746 * are no longer used. But for composite items it's possible that it becomes
6747 * unused while the reference count is > 0: When there is a recursive
6748 * reference. Example:
6749 * :let l = [1, 2, 3]
6750 * :let d = {9: l}
6751 * :let l[1] = d
6752 *
6753 * Since this is quite unusual we handle this with garbage collection: every
6754 * once in a while find out which lists and dicts are not referenced from any
6755 * variable.
6756 *
6757 * Here is a good reference text about garbage collection (refers to Python
6758 * but it applies to all reference-counting mechanisms):
6759 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006760 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006761
6762/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 * Do garbage collection for lists and dicts.
6764 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 int
6767garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006768{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006769 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006770 buf_T *buf;
6771 win_T *wp;
6772 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006773 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774 int did_free;
6775 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006776#ifdef FEAT_WINDOWS
6777 tabpage_T *tp;
6778#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006780 /* Only do this once. */
6781 want_garbage_collect = FALSE;
6782 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006783 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006784
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006785 /* We advance by two because we add one for items referenced through
6786 * previous_funccal. */
6787 current_copyID += COPYID_INC;
6788 copyID = current_copyID;
6789
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006790 /*
6791 * 1. Go through all accessible variables and mark all lists and dicts
6792 * with copyID.
6793 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006794
6795 /* Don't free variables in the previous_funccal list unless they are only
6796 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006797 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006798 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6799 {
6800 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6801 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6802 }
6803
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006804 /* script-local variables */
6805 for (i = 1; i <= ga_scripts.ga_len; ++i)
6806 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6807
6808 /* buffer-local variables */
6809 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006810 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811
6812 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006813 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006814 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006815#ifdef FEAT_AUTOCMD
6816 if (aucmd_win != NULL)
6817 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6818#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006819
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820#ifdef FEAT_WINDOWS
6821 /* tabpage-local variables */
6822 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006823 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006824#endif
6825
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826 /* global variables */
6827 set_ref_in_ht(&globvarht, copyID);
6828
6829 /* function-local variables */
6830 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6831 {
6832 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6833 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6834 }
6835
Bram Moolenaard812df62008-11-09 12:46:09 +00006836 /* v: vars */
6837 set_ref_in_ht(&vimvarht, copyID);
6838
Bram Moolenaar1dced572012-04-05 16:54:08 +02006839#ifdef FEAT_LUA
6840 set_ref_in_lua(copyID);
6841#endif
6842
Bram Moolenaardb913952012-06-29 12:54:53 +02006843#ifdef FEAT_PYTHON
6844 set_ref_in_python(copyID);
6845#endif
6846
6847#ifdef FEAT_PYTHON3
6848 set_ref_in_python3(copyID);
6849#endif
6850
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006851 /*
6852 * 2. Free lists and dictionaries that are not referenced.
6853 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 did_free = free_unref_items(copyID);
6855
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006856 /*
6857 * 3. Check if any funccal can be freed now.
6858 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 for (pfc = &previous_funccal; *pfc != NULL; )
6860 {
6861 if (can_free_funccal(*pfc, copyID))
6862 {
6863 fc = *pfc;
6864 *pfc = fc->caller;
6865 free_funccal(fc, TRUE);
6866 did_free = TRUE;
6867 did_free_funccal = TRUE;
6868 }
6869 else
6870 pfc = &(*pfc)->caller;
6871 }
6872 if (did_free_funccal)
6873 /* When a funccal was freed some more items might be garbage
6874 * collected, so run again. */
6875 (void)garbage_collect();
6876
6877 return did_free;
6878}
6879
6880/*
6881 * Free lists and dictionaries that are no longer referenced.
6882 */
6883 static int
6884free_unref_items(copyID)
6885 int copyID;
6886{
6887 dict_T *dd;
6888 list_T *ll;
6889 int did_free = FALSE;
6890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006892 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 */
6894 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006897 /* Free the Dictionary and ordinary items it contains, but don't
6898 * recurse into Lists and Dictionaries, they will be in the list
6899 * of dicts or list of lists. */
6900 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 did_free = TRUE;
6902
6903 /* restart, next dict may also have been freed */
6904 dd = first_dict;
6905 }
6906 else
6907 dd = dd->dv_used_next;
6908
6909 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006910 * Go through the list of lists and free items without the copyID.
6911 * But don't free a list that has a watcher (used in a for loop), these
6912 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 */
6914 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006915 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6916 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006918 /* Free the List and ordinary items it contains, but don't recurse
6919 * into Lists and Dictionaries, they will be in the list of dicts
6920 * or list of lists. */
6921 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 did_free = TRUE;
6923
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006924 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 ll = first_list;
6926 }
6927 else
6928 ll = ll->lv_used_next;
6929
6930 return did_free;
6931}
6932
6933/*
6934 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6935 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006936 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937set_ref_in_ht(ht, copyID)
6938 hashtab_T *ht;
6939 int copyID;
6940{
6941 int todo;
6942 hashitem_T *hi;
6943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006944 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 for (hi = ht->ht_array; todo > 0; ++hi)
6946 if (!HASHITEM_EMPTY(hi))
6947 {
6948 --todo;
6949 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6950 }
6951}
6952
6953/*
6954 * Mark all lists and dicts referenced through list "l" with "copyID".
6955 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006956 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957set_ref_in_list(l, copyID)
6958 list_T *l;
6959 int copyID;
6960{
6961 listitem_T *li;
6962
6963 for (li = l->lv_first; li != NULL; li = li->li_next)
6964 set_ref_in_item(&li->li_tv, copyID);
6965}
6966
6967/*
6968 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6969 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006970 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971set_ref_in_item(tv, copyID)
6972 typval_T *tv;
6973 int copyID;
6974{
6975 dict_T *dd;
6976 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977
6978 switch (tv->v_type)
6979 {
6980 case VAR_DICT:
6981 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006982 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 /* Didn't see this dict yet. */
6985 dd->dv_copyID = copyID;
6986 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006989
6990 case VAR_LIST:
6991 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006992 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 /* Didn't see this list yet. */
6995 ll->lv_copyID = copyID;
6996 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006997 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007001}
7002
7003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004 * Allocate an empty header for a dictionary.
7005 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007006 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007007dict_alloc()
7008{
Bram Moolenaar33570922005-01-25 22:26:29 +00007009 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007010
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007013 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007014 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 if (first_dict != NULL)
7016 first_dict->dv_used_prev = d;
7017 d->dv_used_next = first_dict;
7018 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007019 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020
Bram Moolenaar33570922005-01-25 22:26:29 +00007021 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007022 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007023 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007024 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007025 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007026 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007027 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028}
7029
7030/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007031 * Allocate an empty dict for a return value.
7032 * Returns OK or FAIL.
7033 */
7034 static int
7035rettv_dict_alloc(rettv)
7036 typval_T *rettv;
7037{
7038 dict_T *d = dict_alloc();
7039
7040 if (d == NULL)
7041 return FAIL;
7042
7043 rettv->vval.v_dict = d;
7044 rettv->v_type = VAR_DICT;
7045 ++d->dv_refcount;
7046 return OK;
7047}
7048
7049
7050/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051 * Unreference a Dictionary: decrement the reference count and free it when it
7052 * becomes zero.
7053 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007054 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007058 if (d != NULL && --d->dv_refcount <= 0)
7059 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060}
7061
7062/*
7063 * Free a Dictionary, including all items it contains.
7064 * Ignores the reference count.
7065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007066 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007067dict_free(d, recurse)
7068 dict_T *d;
7069 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007072 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007073 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075 /* Remove the dict from the list of dicts for garbage collection. */
7076 if (d->dv_used_prev == NULL)
7077 first_dict = d->dv_used_next;
7078 else
7079 d->dv_used_prev->dv_used_next = d->dv_used_next;
7080 if (d->dv_used_next != NULL)
7081 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7082
7083 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007084 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007085 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007086 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088 if (!HASHITEM_EMPTY(hi))
7089 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007090 /* Remove the item before deleting it, just in case there is
7091 * something recursive causing trouble. */
7092 di = HI2DI(hi);
7093 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007094 if (recurse || (di->di_tv.v_type != VAR_LIST
7095 && di->di_tv.v_type != VAR_DICT))
7096 clear_tv(&di->di_tv);
7097 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007098 --todo;
7099 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007102 vim_free(d);
7103}
7104
7105/*
7106 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107 * The "key" is copied to the new item.
7108 * Note that the value of the item "di_tv" still needs to be initialized!
7109 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007110 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007111 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112dictitem_alloc(key)
7113 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114{
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007117 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007118 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007119 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007121 di->di_flags = 0;
7122 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124}
7125
7126/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007127 * Make a copy of a Dictionary item.
7128 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007130dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007132{
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007134
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007135 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7136 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007137 if (di != NULL)
7138 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007139 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007140 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 copy_tv(&org->di_tv, &di->di_tv);
7142 }
7143 return di;
7144}
7145
7146/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147 * Remove item "item" from Dictionary "dict" and free it.
7148 */
7149 static void
7150dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 dict_T *dict;
7152 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007153{
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 if (HASHITEM_EMPTY(hi))
7158 EMSG2(_(e_intern2), "dictitem_remove()");
7159 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 dictitem_free(item);
7162}
7163
7164/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 * Free a dict item. Also clears the value.
7166 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007167 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007169 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007170{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 clear_tv(&item->di_tv);
7172 vim_free(item);
7173}
7174
7175/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7177 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179 * Returns NULL when out of memory.
7180 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007185 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007186{
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *copy;
7188 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007189 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191
7192 if (orig == NULL)
7193 return NULL;
7194
7195 copy = dict_alloc();
7196 if (copy != NULL)
7197 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 if (copyID != 0)
7199 {
7200 orig->dv_copyID = copyID;
7201 orig->dv_copydict = copy;
7202 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007203 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007204 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 --todo;
7209
7210 di = dictitem_alloc(hi->hi_key);
7211 if (di == NULL)
7212 break;
7213 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007214 {
7215 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7216 copyID) == FAIL)
7217 {
7218 vim_free(di);
7219 break;
7220 }
7221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 else
7223 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7224 if (dict_add(copy, di) == FAIL)
7225 {
7226 dictitem_free(di);
7227 break;
7228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007231
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007233 if (todo > 0)
7234 {
7235 dict_unref(copy);
7236 copy = NULL;
7237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007238 }
7239
7240 return copy;
7241}
7242
7243/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007245 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007247 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007249 dict_T *d;
7250 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253}
7254
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007256 * Add a number or string entry to dictionary "d".
7257 * When "str" is NULL use number "nr", otherwise use "str".
7258 * Returns FAIL when out of memory and when key already exists.
7259 */
7260 int
7261dict_add_nr_str(d, key, nr, str)
7262 dict_T *d;
7263 char *key;
7264 long nr;
7265 char_u *str;
7266{
7267 dictitem_T *item;
7268
7269 item = dictitem_alloc((char_u *)key);
7270 if (item == NULL)
7271 return FAIL;
7272 item->di_tv.v_lock = 0;
7273 if (str == NULL)
7274 {
7275 item->di_tv.v_type = VAR_NUMBER;
7276 item->di_tv.vval.v_number = nr;
7277 }
7278 else
7279 {
7280 item->di_tv.v_type = VAR_STRING;
7281 item->di_tv.vval.v_string = vim_strsave(str);
7282 }
7283 if (dict_add(d, item) == FAIL)
7284 {
7285 dictitem_free(item);
7286 return FAIL;
7287 }
7288 return OK;
7289}
7290
7291/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007292 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007293 * Returns FAIL when out of memory and when key already exists.
7294 */
7295 int
7296dict_add_list(d, key, list)
7297 dict_T *d;
7298 char *key;
7299 list_T *list;
7300{
7301 dictitem_T *item;
7302
7303 item = dictitem_alloc((char_u *)key);
7304 if (item == NULL)
7305 return FAIL;
7306 item->di_tv.v_lock = 0;
7307 item->di_tv.v_type = VAR_LIST;
7308 item->di_tv.vval.v_list = list;
7309 if (dict_add(d, item) == FAIL)
7310 {
7311 dictitem_free(item);
7312 return FAIL;
7313 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007314 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007315 return OK;
7316}
7317
7318/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 * Get the number of items in a Dictionary.
7320 */
7321 static long
7322dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007325 if (d == NULL)
7326 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007327 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Find item "key[len]" in Dictionary "d".
7332 * If "len" is negative use strlen(key).
7333 * Returns NULL when not found.
7334 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007335 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 char_u *key;
7339 int len;
7340{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341#define AKEYLEN 200
7342 char_u buf[AKEYLEN];
7343 char_u *akey;
7344 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (len < 0)
7348 akey = key;
7349 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007350 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 tofree = akey = vim_strnsave(key, len);
7352 if (akey == NULL)
7353 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007354 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 else
7356 {
7357 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007358 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007359 akey = buf;
7360 }
7361
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 vim_free(tofree);
7364 if (HASHITEM_EMPTY(hi))
7365 return NULL;
7366 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367}
7368
7369/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007370 * Get a string item from a dictionary.
7371 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007372 * Returns NULL if the entry doesn't exist or out of memory.
7373 */
7374 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007375get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376 dict_T *d;
7377 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007378 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007379{
7380 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007381 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007382
7383 di = dict_find(d, key, -1);
7384 if (di == NULL)
7385 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007386 s = get_tv_string(&di->di_tv);
7387 if (save && s != NULL)
7388 s = vim_strsave(s);
7389 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007390}
7391
7392/*
7393 * Get a number item from a dictionary.
7394 * Returns 0 if the entry doesn't exist or out of memory.
7395 */
7396 long
7397get_dict_number(d, key)
7398 dict_T *d;
7399 char_u *key;
7400{
7401 dictitem_T *di;
7402
7403 di = dict_find(d, key, -1);
7404 if (di == NULL)
7405 return 0;
7406 return get_tv_number(&di->di_tv);
7407}
7408
7409/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 * Return an allocated string with the string representation of a Dictionary.
7411 * May return NULL.
7412 */
7413 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007414dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007415 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007416 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417{
7418 garray_T ga;
7419 int first = TRUE;
7420 char_u *tofree;
7421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007423 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 return NULL;
7429 ga_init2(&ga, (int)sizeof(char), 80);
7430 ga_append(&ga, '{');
7431
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007432 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 --todo;
7438
7439 if (first)
7440 first = FALSE;
7441 else
7442 ga_concat(&ga, (char_u *)", ");
7443
7444 tofree = string_quote(hi->hi_key, FALSE);
7445 if (tofree != NULL)
7446 {
7447 ga_concat(&ga, tofree);
7448 vim_free(tofree);
7449 }
7450 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 if (s != NULL)
7453 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007455 if (s == NULL)
7456 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007459 if (todo > 0)
7460 {
7461 vim_free(ga.ga_data);
7462 return NULL;
7463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464
7465 ga_append(&ga, '}');
7466 ga_append(&ga, NUL);
7467 return (char_u *)ga.ga_data;
7468}
7469
7470/*
7471 * Allocate a variable for a Dictionary and fill it from "*arg".
7472 * Return OK or FAIL. Returns NOTDONE for {expr}.
7473 */
7474 static int
7475get_dict_tv(arg, rettv, evaluate)
7476 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 int evaluate;
7479{
Bram Moolenaar33570922005-01-25 22:26:29 +00007480 dict_T *d = NULL;
7481 typval_T tvkey;
7482 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007483 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007486 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 /*
7489 * First check if it's not a curly-braces thing: {expr}.
7490 * Must do this without evaluating, otherwise a function may be called
7491 * twice. Unfortunately this means we need to call eval1() twice for the
7492 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007495 if (*start != '}')
7496 {
7497 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7498 return FAIL;
7499 if (*start == '}')
7500 return NOTDONE;
7501 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502
7503 if (evaluate)
7504 {
7505 d = dict_alloc();
7506 if (d == NULL)
7507 return FAIL;
7508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 tvkey.v_type = VAR_UNKNOWN;
7510 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511
7512 *arg = skipwhite(*arg + 1);
7513 while (**arg != '}' && **arg != NUL)
7514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 goto failret;
7517 if (**arg != ':')
7518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007519 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521 goto failret;
7522 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007523 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007525 key = get_tv_string_buf_chk(&tvkey, buf);
7526 if (key == NULL || *key == NUL)
7527 {
7528 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7529 if (key != NULL)
7530 EMSG(_(e_emptykey));
7531 clear_tv(&tvkey);
7532 goto failret;
7533 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535
7536 *arg = skipwhite(*arg + 1);
7537 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7538 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007539 if (evaluate)
7540 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 goto failret;
7542 }
7543 if (evaluate)
7544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007546 if (item != NULL)
7547 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007548 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007549 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 clear_tv(&tv);
7551 goto failret;
7552 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 item = dictitem_alloc(key);
7554 clear_tv(&tvkey);
7555 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007558 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 if (dict_add(d, item) == FAIL)
7560 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 }
7562 }
7563
7564 if (**arg == '}')
7565 break;
7566 if (**arg != ',')
7567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007568 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 goto failret;
7570 }
7571 *arg = skipwhite(*arg + 1);
7572 }
7573
7574 if (**arg != '}')
7575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007576 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577failret:
7578 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007579 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 return FAIL;
7581 }
7582
7583 *arg = skipwhite(*arg + 1);
7584 if (evaluate)
7585 {
7586 rettv->v_type = VAR_DICT;
7587 rettv->vval.v_dict = d;
7588 ++d->dv_refcount;
7589 }
7590
7591 return OK;
7592}
7593
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 * Return a string with the string representation of a variable.
7596 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007597 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007598 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007600 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 */
7602 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007603echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007605 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007606 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007608{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 static int recurse = 0;
7610 char_u *r = NULL;
7611
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007614 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 *tofree = NULL;
7616 return NULL;
7617 }
7618 ++recurse;
7619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007620 switch (tv->v_type)
7621 {
7622 case VAR_FUNC:
7623 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 r = tv->vval.v_string;
7625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007627 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628 if (tv->vval.v_list == NULL)
7629 {
7630 *tofree = NULL;
7631 r = NULL;
7632 }
7633 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7634 {
7635 *tofree = NULL;
7636 r = (char_u *)"[...]";
7637 }
7638 else
7639 {
7640 tv->vval.v_list->lv_copyID = copyID;
7641 *tofree = list2string(tv, copyID);
7642 r = *tofree;
7643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007645
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007647 if (tv->vval.v_dict == NULL)
7648 {
7649 *tofree = NULL;
7650 r = NULL;
7651 }
7652 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7653 {
7654 *tofree = NULL;
7655 r = (char_u *)"{...}";
7656 }
7657 else
7658 {
7659 tv->vval.v_dict->dv_copyID = copyID;
7660 *tofree = dict2string(tv, copyID);
7661 r = *tofree;
7662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 case VAR_STRING:
7666 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 *tofree = NULL;
7668 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007671#ifdef FEAT_FLOAT
7672 case VAR_FLOAT:
7673 *tofree = NULL;
7674 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7675 r = numbuf;
7676 break;
7677#endif
7678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007683
7684 --recurse;
7685 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007686}
7687
7688/*
7689 * Return a string with the string representation of a variable.
7690 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7691 * "numbuf" is used for a number.
7692 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007693 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 */
7695 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007696tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 char_u **tofree;
7699 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007700 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007701{
7702 switch (tv->v_type)
7703 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 case VAR_FUNC:
7705 *tofree = string_quote(tv->vval.v_string, TRUE);
7706 return *tofree;
7707 case VAR_STRING:
7708 *tofree = string_quote(tv->vval.v_string, FALSE);
7709 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007710#ifdef FEAT_FLOAT
7711 case VAR_FLOAT:
7712 *tofree = NULL;
7713 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7714 return numbuf;
7715#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007720 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007723 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724}
7725
7726/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 * Return string "str" in ' quotes, doubling ' characters.
7728 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007730 */
7731 static char_u *
7732string_quote(str, function)
7733 char_u *str;
7734 int function;
7735{
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007737 char_u *p, *r, *s;
7738
Bram Moolenaar33570922005-01-25 22:26:29 +00007739 len = (function ? 13 : 3);
7740 if (str != NULL)
7741 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007742 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007743 for (p = str; *p != NUL; mb_ptr_adv(p))
7744 if (*p == '\'')
7745 ++len;
7746 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 s = r = alloc(len);
7748 if (r != NULL)
7749 {
7750 if (function)
7751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007753 r += 10;
7754 }
7755 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 if (str != NULL)
7758 for (p = str; *p != NUL; )
7759 {
7760 if (*p == '\'')
7761 *r++ = '\'';
7762 MB_COPY_CHAR(p, r);
7763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007765 if (function)
7766 *r++ = ')';
7767 *r++ = NUL;
7768 }
7769 return s;
7770}
7771
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007772#ifdef FEAT_FLOAT
7773/*
7774 * Convert the string "text" to a floating point number.
7775 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7776 * this always uses a decimal point.
7777 * Returns the length of the text that was consumed.
7778 */
7779 static int
7780string2float(text, value)
7781 char_u *text;
7782 float_T *value; /* result stored here */
7783{
7784 char *s = (char *)text;
7785 float_T f;
7786
7787 f = strtod(s, &s);
7788 *value = f;
7789 return (int)((char_u *)s - text);
7790}
7791#endif
7792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 * Get the value of an environment variable.
7795 * "arg" is pointing to the '$'. It is advanced to after the name.
7796 * If the environment variable was not set, silently assume it is empty.
7797 * Always return OK.
7798 */
7799 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 int evaluate;
7804{
7805 char_u *string = NULL;
7806 int len;
7807 int cc;
7808 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007809 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810
7811 ++*arg;
7812 name = *arg;
7813 len = get_env_len(arg);
7814 if (evaluate)
7815 {
7816 if (len != 0)
7817 {
7818 cc = name[len];
7819 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007820 /* first try vim_getenv(), fast for normal environment vars */
7821 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823 {
7824 if (!mustfree)
7825 string = vim_strsave(string);
7826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 else
7828 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007829 if (mustfree)
7830 vim_free(string);
7831
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 /* next try expanding things like $VIM and ${HOME} */
7833 string = expand_env_save(name - 1);
7834 if (string != NULL && *string == '$')
7835 {
7836 vim_free(string);
7837 string = NULL;
7838 }
7839 }
7840 name[len] = cc;
7841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842 rettv->v_type = VAR_STRING;
7843 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845
7846 return OK;
7847}
7848
7849/*
7850 * Array with names and number of arguments of all internal functions
7851 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7852 */
7853static struct fst
7854{
7855 char *f_name; /* function name */
7856 char f_min_argc; /* minimal number of arguments */
7857 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007859 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860} functions[] =
7861{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862#ifdef FEAT_FLOAT
7863 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007864 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007866 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007867 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"append", 2, 2, f_append},
7869 {"argc", 0, 0, f_argc},
7870 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007871 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007873 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007875 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007878 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"bufexists", 1, 1, f_bufexists},
7880 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7881 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7882 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7883 {"buflisted", 1, 1, f_buflisted},
7884 {"bufloaded", 1, 1, f_bufloaded},
7885 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007886 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"bufwinnr", 1, 1, f_bufwinnr},
7888 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007889 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007890 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007892#ifdef FEAT_FLOAT
7893 {"ceil", 1, 1, f_ceil},
7894#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007895 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007896 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007898 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007900#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007901 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007902 {"complete_add", 1, 1, f_complete_add},
7903 {"complete_check", 0, 0, f_complete_check},
7904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007907#ifdef FEAT_FLOAT
7908 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007909 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007913 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007914 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"delete", 1, 1, f_delete},
7916 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007917 {"diff_filler", 1, 1, f_diff_filler},
7918 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007919 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007921 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"eventhandler", 0, 0, f_eventhandler},
7923 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007924 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007926#ifdef FEAT_FLOAT
7927 {"exp", 1, 1, f_exp},
7928#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007929 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007930 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007931 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7933 {"filereadable", 1, 1, f_filereadable},
7934 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007935 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007936 {"finddir", 1, 3, f_finddir},
7937 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007938#ifdef FEAT_FLOAT
7939 {"float2nr", 1, 1, f_float2nr},
7940 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007941 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007943 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {"fnamemodify", 2, 2, f_fnamemodify},
7945 {"foldclosed", 1, 1, f_foldclosed},
7946 {"foldclosedend", 1, 1, f_foldclosedend},
7947 {"foldlevel", 1, 1, f_foldlevel},
7948 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007949 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007952 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007953 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007954 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007955 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {"getchar", 0, 1, f_getchar},
7957 {"getcharmod", 0, 0, f_getcharmod},
7958 {"getcmdline", 0, 0, f_getcmdline},
7959 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007960 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007962 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007963 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {"getfsize", 1, 1, f_getfsize},
7965 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007966 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007967 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007968 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007969 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007970 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007971 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007972 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007973 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007975 {"gettabvar", 2, 3, f_gettabvar},
7976 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"getwinposx", 0, 0, f_getwinposx},
7978 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007979 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007980 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007981 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007984 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007985 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7987 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7988 {"histadd", 2, 2, f_histadd},
7989 {"histdel", 1, 2, f_histdel},
7990 {"histget", 1, 2, f_histget},
7991 {"histnr", 1, 1, f_histnr},
7992 {"hlID", 1, 1, f_hlID},
7993 {"hlexists", 1, 1, f_hlexists},
7994 {"hostname", 0, 0, f_hostname},
7995 {"iconv", 3, 3, f_iconv},
7996 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007998 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008000 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {"inputrestore", 0, 0, f_inputrestore},
8002 {"inputsave", 0, 0, f_inputsave},
8003 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008004 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008005 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008007 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008010 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008012 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"libcall", 3, 3, f_libcall},
8014 {"libcallnr", 3, 3, f_libcallnr},
8015 {"line", 1, 1, f_line},
8016 {"line2byte", 1, 1, f_line2byte},
8017 {"lispindent", 1, 1, f_lispindent},
8018 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008019#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008020 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008021 {"log10", 1, 1, f_log10},
8022#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008023#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008024 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008025#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008027 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008028 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008029 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008030 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008031 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008032 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008033 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008034 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008035 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008036 {"max", 1, 1, f_max},
8037 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008038#ifdef vim_mkdir
8039 {"mkdir", 1, 3, f_mkdir},
8040#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008041 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008042#ifdef FEAT_MZSCHEME
8043 {"mzeval", 1, 1, f_mzeval},
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008046 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008047 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008048 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008049#ifdef FEAT_FLOAT
8050 {"pow", 2, 2, f_pow},
8051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008053 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008054 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008055#ifdef FEAT_PYTHON3
8056 {"py3eval", 1, 1, f_py3eval},
8057#endif
8058#ifdef FEAT_PYTHON
8059 {"pyeval", 1, 1, f_pyeval},
8060#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008062 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008063 {"reltime", 0, 2, f_reltime},
8064 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"remote_expr", 2, 3, f_remote_expr},
8066 {"remote_foreground", 1, 1, f_remote_foreground},
8067 {"remote_peek", 1, 2, f_remote_peek},
8068 {"remote_read", 1, 1, f_remote_read},
8069 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008070 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008072 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008074 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"round", 1, 1, f_round},
8077#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008078 {"screenattr", 2, 2, f_screenattr},
8079 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008080 {"screencol", 0, 0, f_screencol},
8081 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008082 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008083 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008084 {"searchpair", 3, 7, f_searchpair},
8085 {"searchpairpos", 3, 7, f_searchpairpos},
8086 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {"server2client", 2, 2, f_server2client},
8088 {"serverlist", 0, 0, f_serverlist},
8089 {"setbufvar", 3, 3, f_setbufvar},
8090 {"setcmdpos", 1, 1, f_setcmdpos},
8091 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008092 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008093 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008094 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008095 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008097 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008098 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008100#ifdef FEAT_CRYPT
8101 {"sha256", 1, 1, f_sha256},
8102#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008103 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008104 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#ifdef FEAT_FLOAT
8107 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008108 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008109#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008110 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008111 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008112 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008113 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008114 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008115#ifdef FEAT_FLOAT
8116 {"sqrt", 1, 1, f_sqrt},
8117 {"str2float", 1, 1, f_str2float},
8118#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008119 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008120 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008121 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122#ifdef HAVE_STRFTIME
8123 {"strftime", 1, 2, f_strftime},
8124#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"strlen", 1, 1, f_strlen},
8128 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008129 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008131 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"submatch", 1, 1, f_submatch},
8133 {"substitute", 4, 4, f_substitute},
8134 {"synID", 3, 3, f_synID},
8135 {"synIDattr", 2, 3, f_synIDattr},
8136 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008137 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008138 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008139 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008140 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008141 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008142 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008143 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008144 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008145#ifdef FEAT_FLOAT
8146 {"tan", 1, 1, f_tan},
8147 {"tanh", 1, 1, f_tanh},
8148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008150 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {"tolower", 1, 1, f_tolower},
8152 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008153 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154#ifdef FEAT_FLOAT
8155 {"trunc", 1, 1, f_trunc},
8156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008158 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008159 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008160 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008161 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"virtcol", 1, 1, f_virtcol},
8163 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008164 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"winbufnr", 1, 1, f_winbufnr},
8166 {"wincol", 0, 0, f_wincol},
8167 {"winheight", 1, 1, f_winheight},
8168 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008169 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008171 {"winrestview", 1, 1, f_winrestview},
8172 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008174 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008175 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176};
8177
8178#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8179
8180/*
8181 * Function given to ExpandGeneric() to obtain the list of internal
8182 * or user defined function names.
8183 */
8184 char_u *
8185get_function_name(xp, idx)
8186 expand_T *xp;
8187 int idx;
8188{
8189 static int intidx = -1;
8190 char_u *name;
8191
8192 if (idx == 0)
8193 intidx = -1;
8194 if (intidx < 0)
8195 {
8196 name = get_user_func_name(xp, idx);
8197 if (name != NULL)
8198 return name;
8199 }
8200 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8201 {
8202 STRCPY(IObuff, functions[intidx].f_name);
8203 STRCAT(IObuff, "(");
8204 if (functions[intidx].f_max_argc == 0)
8205 STRCAT(IObuff, ")");
8206 return IObuff;
8207 }
8208
8209 return NULL;
8210}
8211
8212/*
8213 * Function given to ExpandGeneric() to obtain the list of internal or
8214 * user defined variable or function names.
8215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 char_u *
8217get_expr_name(xp, idx)
8218 expand_T *xp;
8219 int idx;
8220{
8221 static int intidx = -1;
8222 char_u *name;
8223
8224 if (idx == 0)
8225 intidx = -1;
8226 if (intidx < 0)
8227 {
8228 name = get_function_name(xp, idx);
8229 if (name != NULL)
8230 return name;
8231 }
8232 return get_user_var_name(xp, ++intidx);
8233}
8234
8235#endif /* FEAT_CMDL_COMPL */
8236
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008237#if defined(EBCDIC) || defined(PROTO)
8238/*
8239 * Compare struct fst by function name.
8240 */
8241 static int
8242compare_func_name(s1, s2)
8243 const void *s1;
8244 const void *s2;
8245{
8246 struct fst *p1 = (struct fst *)s1;
8247 struct fst *p2 = (struct fst *)s2;
8248
8249 return STRCMP(p1->f_name, p2->f_name);
8250}
8251
8252/*
8253 * Sort the function table by function name.
8254 * The sorting of the table above is ASCII dependant.
8255 * On machines using EBCDIC we have to sort it.
8256 */
8257 static void
8258sortFunctions()
8259{
8260 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8261
8262 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8263}
8264#endif
8265
8266
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267/*
8268 * Find internal function in table above.
8269 * Return index, or -1 if not found
8270 */
8271 static int
8272find_internal_func(name)
8273 char_u *name; /* name of the function */
8274{
8275 int first = 0;
8276 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8277 int cmp;
8278 int x;
8279
8280 /*
8281 * Find the function name in the table. Binary search.
8282 */
8283 while (first <= last)
8284 {
8285 x = first + ((unsigned)(last - first) >> 1);
8286 cmp = STRCMP(name, functions[x].f_name);
8287 if (cmp < 0)
8288 last = x - 1;
8289 else if (cmp > 0)
8290 first = x + 1;
8291 else
8292 return x;
8293 }
8294 return -1;
8295}
8296
8297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8299 * name it contains, otherwise return "name".
8300 */
8301 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008302deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 char_u *name;
8304 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008305 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306{
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 int cc;
8309
8310 cc = name[*lenp];
8311 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008312 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008313 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008317 {
8318 *lenp = 0;
8319 return (char_u *)""; /* just in case */
8320 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008321 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008322 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323 }
8324
8325 return name;
8326}
8327
8328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 * Allocate a variable for the result of a function.
8330 * Return OK or FAIL.
8331 */
8332 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008333get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8334 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 char_u *name; /* name of the function */
8336 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 char_u **arg; /* argument, pointing to the '(' */
8339 linenr_T firstline; /* first line of range */
8340 linenr_T lastline; /* last line of range */
8341 int *doesrange; /* return: function handled range */
8342 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008343 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 char_u *argp;
8346 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008347 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 int argcount = 0; /* number of arguments found */
8349
8350 /*
8351 * Get the arguments.
8352 */
8353 argp = *arg;
8354 while (argcount < MAX_FUNC_ARGS)
8355 {
8356 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8357 if (*argp == ')' || *argp == ',' || *argp == NUL)
8358 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8360 {
8361 ret = FAIL;
8362 break;
8363 }
8364 ++argcount;
8365 if (*argp != ',')
8366 break;
8367 }
8368 if (*argp == ')')
8369 ++argp;
8370 else
8371 ret = FAIL;
8372
8373 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008374 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008377 {
8378 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008379 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008381 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008385 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387 *arg = skipwhite(argp);
8388 return ret;
8389}
8390
8391
8392/*
8393 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008394 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008395 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 */
8397 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008398call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008399 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008404 typval_T *argvars; /* vars for arguments, must have "argcount"
8405 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 linenr_T firstline; /* first line of range */
8407 linenr_T lastline; /* last line of range */
8408 int *doesrange; /* return: function handled range */
8409 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008410 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413#define ERROR_UNKNOWN 0
8414#define ERROR_TOOMANY 1
8415#define ERROR_TOOFEW 2
8416#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008417#define ERROR_DICT 4
8418#define ERROR_NONE 5
8419#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 int error = ERROR_NONE;
8421 int i;
8422 int llen;
8423 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424#define FLEN_FIXED 40
8425 char_u fname_buf[FLEN_FIXED + 1];
8426 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008427 char_u *name;
8428
8429 /* Make a copy of the name, if it comes from a funcref variable it could
8430 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008431 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008432 if (name == NULL)
8433 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
8435 /*
8436 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8437 * Change <SNR>123_name() to K_SNR 123_name().
8438 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 llen = eval_fname_script(name);
8441 if (llen > 0)
8442 {
8443 fname_buf[0] = K_SPECIAL;
8444 fname_buf[1] = KS_EXTRA;
8445 fname_buf[2] = (int)KE_SNR;
8446 i = 3;
8447 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8448 {
8449 if (current_SID <= 0)
8450 error = ERROR_SCRIPT;
8451 else
8452 {
8453 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8454 i = (int)STRLEN(fname_buf);
8455 }
8456 }
8457 if (i + STRLEN(name + llen) < FLEN_FIXED)
8458 {
8459 STRCPY(fname_buf + i, name + llen);
8460 fname = fname_buf;
8461 }
8462 else
8463 {
8464 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8465 if (fname == NULL)
8466 error = ERROR_OTHER;
8467 else
8468 {
8469 mch_memmove(fname, fname_buf, (size_t)i);
8470 STRCPY(fname + i, name + llen);
8471 }
8472 }
8473 }
8474 else
8475 fname = name;
8476
8477 *doesrange = FALSE;
8478
8479
8480 /* execute the function if no errors detected and executing */
8481 if (evaluate && error == ERROR_NONE)
8482 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008483 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 error = ERROR_UNKNOWN;
8486
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008487 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {
8489 /*
8490 * User defined function.
8491 */
8492 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008495 /* Trigger FuncUndefined event, may load the function. */
8496 if (fp == NULL
8497 && apply_autocmds(EVENT_FUNCUNDEFINED,
8498 fname, fname, TRUE, NULL)
8499 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008501 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 fp = find_func(fname);
8503 }
8504#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008505 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008506 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008507 {
8508 /* loaded a package, search for the function again */
8509 fp = find_func(fname);
8510 }
8511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (fp != NULL)
8513 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008514 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008516 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008518 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008520 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008521 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 else
8523 {
8524 /*
8525 * Call the user function.
8526 * Save and restore search patterns, script variables and
8527 * redo buffer.
8528 */
8529 save_search_patterns();
8530 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008531 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008534 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8535 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8536 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008537 /* Function was unreferenced while being used, free it
8538 * now. */
8539 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 restoreRedobuff();
8541 restore_search_patterns();
8542 error = ERROR_NONE;
8543 }
8544 }
8545 }
8546 else
8547 {
8548 /*
8549 * Find the function name in the table, call its implementation.
8550 */
8551 i = find_internal_func(fname);
8552 if (i >= 0)
8553 {
8554 if (argcount < functions[i].f_min_argc)
8555 error = ERROR_TOOFEW;
8556 else if (argcount > functions[i].f_max_argc)
8557 error = ERROR_TOOMANY;
8558 else
8559 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 error = ERROR_NONE;
8563 }
8564 }
8565 }
8566 /*
8567 * The function call (or "FuncUndefined" autocommand sequence) might
8568 * have been aborted by an error, an interrupt, or an explicitly thrown
8569 * exception that has not been caught so far. This situation can be
8570 * tested for by calling aborting(). For an error in an internal
8571 * function or for the "E132" error in call_user_func(), however, the
8572 * throw point at which the "force_abort" flag (temporarily reset by
8573 * emsg()) is normally updated has not been reached yet. We need to
8574 * update that flag first to make aborting() reliable.
8575 */
8576 update_force_abort();
8577 }
8578 if (error == ERROR_NONE)
8579 ret = OK;
8580
8581 /*
8582 * Report an error unless the argument evaluation or function call has been
8583 * cancelled due to an aborting error, an interrupt, or an exception.
8584 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008585 if (!aborting())
8586 {
8587 switch (error)
8588 {
8589 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008590 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008591 break;
8592 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008594 break;
8595 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008596 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008597 name);
8598 break;
8599 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008600 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008601 name);
8602 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008603 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008604 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008605 name);
8606 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008607 }
8608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 if (fname != name && fname != fname_buf)
8611 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008612 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
8614 return ret;
8615}
8616
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008617/*
8618 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008619 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008620 */
8621 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008622emsg_funcname(ermsg, name)
8623 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008624 char_u *name;
8625{
8626 char_u *p;
8627
8628 if (*name == K_SPECIAL)
8629 p = concat_str((char_u *)"<SNR>", name + 3);
8630 else
8631 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008632 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008633 if (p != name)
8634 vim_free(p);
8635}
8636
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008637/*
8638 * Return TRUE for a non-zero Number and a non-empty String.
8639 */
8640 static int
8641non_zero_arg(argvars)
8642 typval_T *argvars;
8643{
8644 return ((argvars[0].v_type == VAR_NUMBER
8645 && argvars[0].vval.v_number != 0)
8646 || (argvars[0].v_type == VAR_STRING
8647 && argvars[0].vval.v_string != NULL
8648 && *argvars[0].vval.v_string != NUL));
8649}
8650
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651/*********************************************
8652 * Implementation of the built-in functions
8653 */
8654
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008655#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008656static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8657
8658/*
8659 * Get the float value of "argvars[0]" into "f".
8660 * Returns FAIL when the argument is not a Number or Float.
8661 */
8662 static int
8663get_float_arg(argvars, f)
8664 typval_T *argvars;
8665 float_T *f;
8666{
8667 if (argvars[0].v_type == VAR_FLOAT)
8668 {
8669 *f = argvars[0].vval.v_float;
8670 return OK;
8671 }
8672 if (argvars[0].v_type == VAR_NUMBER)
8673 {
8674 *f = (float_T)argvars[0].vval.v_number;
8675 return OK;
8676 }
8677 EMSG(_("E808: Number or Float required"));
8678 return FAIL;
8679}
8680
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008681/*
8682 * "abs(expr)" function
8683 */
8684 static void
8685f_abs(argvars, rettv)
8686 typval_T *argvars;
8687 typval_T *rettv;
8688{
8689 if (argvars[0].v_type == VAR_FLOAT)
8690 {
8691 rettv->v_type = VAR_FLOAT;
8692 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8693 }
8694 else
8695 {
8696 varnumber_T n;
8697 int error = FALSE;
8698
8699 n = get_tv_number_chk(&argvars[0], &error);
8700 if (error)
8701 rettv->vval.v_number = -1;
8702 else if (n > 0)
8703 rettv->vval.v_number = n;
8704 else
8705 rettv->vval.v_number = -n;
8706 }
8707}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008708
8709/*
8710 * "acos()" function
8711 */
8712 static void
8713f_acos(argvars, rettv)
8714 typval_T *argvars;
8715 typval_T *rettv;
8716{
8717 float_T f;
8718
8719 rettv->v_type = VAR_FLOAT;
8720 if (get_float_arg(argvars, &f) == OK)
8721 rettv->vval.v_float = acos(f);
8722 else
8723 rettv->vval.v_float = 0.0;
8724}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008725#endif
8726
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 */
8730 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 typval_T *argvars;
8733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008740 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008741 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008742 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008743 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008744 }
8745 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746 EMSG(_(e_listreq));
8747}
8748
8749/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008750 * "and(expr, expr)" function
8751 */
8752 static void
8753f_and(argvars, rettv)
8754 typval_T *argvars;
8755 typval_T *rettv;
8756{
8757 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8758 & get_tv_number_chk(&argvars[1], NULL);
8759}
8760
8761/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762 * "append(lnum, string/list)" function
8763 */
8764 static void
8765f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 typval_T *argvars;
8767 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008768{
8769 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 list_T *l = NULL;
8772 listitem_T *li = NULL;
8773 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008774 long added = 0;
8775
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008776 /* When coming here from Insert mode, sync undo, so that this can be
8777 * undone separately from what was previously inserted. */
8778 if (u_sync_once == 2)
8779 {
8780 u_sync_once = 1; /* notify that u_sync() was called */
8781 u_sync(TRUE);
8782 }
8783
Bram Moolenaar0d660222005-01-07 21:51:51 +00008784 lnum = get_tv_lnum(argvars);
8785 if (lnum >= 0
8786 && lnum <= curbuf->b_ml.ml_line_count
8787 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008788 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008789 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008790 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008791 l = argvars[1].vval.v_list;
8792 if (l == NULL)
8793 return;
8794 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008795 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796 for (;;)
8797 {
8798 if (l == NULL)
8799 tv = &argvars[1]; /* append a string */
8800 else if (li == NULL)
8801 break; /* end of list */
8802 else
8803 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008804 line = get_tv_string_chk(tv);
8805 if (line == NULL) /* type error */
8806 {
8807 rettv->vval.v_number = 1; /* Failed */
8808 break;
8809 }
8810 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008811 ++added;
8812 if (l == NULL)
8813 break;
8814 li = li->li_next;
8815 }
8816
8817 appended_lines_mark(lnum, added);
8818 if (curwin->w_cursor.lnum > lnum)
8819 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008821 else
8822 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823}
8824
8825/*
8826 * "argc()" function
8827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008829f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834}
8835
8836/*
8837 * "argidx()" function
8838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008840f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008841 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008844 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845}
8846
8847/*
8848 * "argv(nr)" function
8849 */
8850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008851f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008852 typval_T *argvars;
8853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855 int idx;
8856
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008857 if (argvars[0].v_type != VAR_UNKNOWN)
8858 {
8859 idx = get_tv_number_chk(&argvars[0], NULL);
8860 if (idx >= 0 && idx < ARGCOUNT)
8861 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8862 else
8863 rettv->vval.v_string = NULL;
8864 rettv->v_type = VAR_STRING;
8865 }
8866 else if (rettv_list_alloc(rettv) == OK)
8867 for (idx = 0; idx < ARGCOUNT; ++idx)
8868 list_append_string(rettv->vval.v_list,
8869 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008873/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008874 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008875 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008876 static void
8877f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008879 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008881 float_T f;
8882
8883 rettv->v_type = VAR_FLOAT;
8884 if (get_float_arg(argvars, &f) == OK)
8885 rettv->vval.v_float = asin(f);
8886 else
8887 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008888}
8889
8890/*
8891 * "atan()" function
8892 */
8893 static void
8894f_atan(argvars, rettv)
8895 typval_T *argvars;
8896 typval_T *rettv;
8897{
8898 float_T f;
8899
8900 rettv->v_type = VAR_FLOAT;
8901 if (get_float_arg(argvars, &f) == OK)
8902 rettv->vval.v_float = atan(f);
8903 else
8904 rettv->vval.v_float = 0.0;
8905}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008906
8907/*
8908 * "atan2()" function
8909 */
8910 static void
8911f_atan2(argvars, rettv)
8912 typval_T *argvars;
8913 typval_T *rettv;
8914{
8915 float_T fx, fy;
8916
8917 rettv->v_type = VAR_FLOAT;
8918 if (get_float_arg(argvars, &fx) == OK
8919 && get_float_arg(&argvars[1], &fy) == OK)
8920 rettv->vval.v_float = atan2(fx, fy);
8921 else
8922 rettv->vval.v_float = 0.0;
8923}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#endif
8925
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926/*
8927 * "browse(save, title, initdir, default)" function
8928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
8934#ifdef FEAT_BROWSE
8935 int save;
8936 char_u *title;
8937 char_u *initdir;
8938 char_u *defname;
8939 char_u buf[NUMBUFLEN];
8940 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 save = get_tv_number_chk(&argvars[0], &error);
8944 title = get_tv_string_chk(&argvars[1]);
8945 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8946 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 if (error || title == NULL || initdir == NULL || defname == NULL)
8949 rettv->vval.v_string = NULL;
8950 else
8951 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008952 do_browse(save ? BROWSE_SAVE : 0,
8953 title, defname, NULL, initdir, NULL, curbuf);
8954#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008958}
8959
8960/*
8961 * "browsedir(title, initdir)" function
8962 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008966 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008967{
8968#ifdef FEAT_BROWSE
8969 char_u *title;
8970 char_u *initdir;
8971 char_u buf[NUMBUFLEN];
8972
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973 title = get_tv_string_chk(&argvars[0]);
8974 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008976 if (title == NULL || initdir == NULL)
8977 rettv->vval.v_string = NULL;
8978 else
8979 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008980 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985}
8986
Bram Moolenaar33570922005-01-25 22:26:29 +00008987static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008988
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989/*
8990 * Find a buffer by number or exact name.
8991 */
8992 static buf_T *
8993find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008998 if (avar->v_type == VAR_NUMBER)
8999 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009000 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009003 if (buf == NULL)
9004 {
9005 /* No full path name match, try a match with a URL or a "nofile"
9006 * buffer, these don't use the full path. */
9007 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9008 if (buf->b_fname != NULL
9009 && (path_with_url(buf->b_fname)
9010#ifdef FEAT_QUICKFIX
9011 || bt_nofile(buf)
9012#endif
9013 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009015 break;
9016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018 return buf;
9019}
9020
9021/*
9022 * "bufexists(expr)" function
9023 */
9024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009025f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009026 typval_T *argvars;
9027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030}
9031
9032/*
9033 * "buflisted(expr)" function
9034 */
9035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *argvars;
9038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
9040 buf_T *buf;
9041
9042 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044}
9045
9046/*
9047 * "bufloaded(expr)" function
9048 */
9049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009050f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009051 typval_T *argvars;
9052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
9054 buf_T *buf;
9055
9056 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
9063 * Get buffer by number or pattern.
9064 */
9065 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009066get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009068 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 int save_magic;
9072 char_u *save_cpo;
9073 buf_T *buf;
9074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 if (tv->v_type == VAR_NUMBER)
9076 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009077 if (tv->v_type != VAR_STRING)
9078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (name == NULL || *name == NUL)
9080 return curbuf;
9081 if (name[0] == '$' && name[1] == NUL)
9082 return lastbuf;
9083
9084 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9085 save_magic = p_magic;
9086 p_magic = TRUE;
9087 save_cpo = p_cpo;
9088 p_cpo = (char_u *)"";
9089
9090 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009091 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
9093 p_magic = save_magic;
9094 p_cpo = save_cpo;
9095
9096 /* If not found, try expanding the name, like done for bufexists(). */
9097 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099
9100 return buf;
9101}
9102
9103/*
9104 * "bufname(expr)" function
9105 */
9106 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009108 typval_T *argvars;
9109 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110{
9111 buf_T *buf;
9112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009115 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 --emsg_off;
9122}
9123
9124/*
9125 * "bufnr(expr)" function
9126 */
9127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *argvars;
9130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009133 int error = FALSE;
9134 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009138 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009139 --emsg_off;
9140
9141 /* If the buffer isn't found and the second argument is not zero create a
9142 * new buffer. */
9143 if (buf == NULL
9144 && argvars[1].v_type != VAR_UNKNOWN
9145 && get_tv_number_chk(&argvars[1], &error) != 0
9146 && !error
9147 && (name = get_tv_string_chk(&argvars[0])) != NULL
9148 && !error)
9149 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9150
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155}
9156
9157/*
9158 * "bufwinnr(nr)" function
9159 */
9160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 typval_T *argvars;
9163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165#ifdef FEAT_WINDOWS
9166 win_T *wp;
9167 int winnr = 0;
9168#endif
9169 buf_T *buf;
9170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009173 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#ifdef FEAT_WINDOWS
9175 for (wp = firstwin; wp; wp = wp->w_next)
9176 {
9177 ++winnr;
9178 if (wp->w_buffer == buf)
9179 break;
9180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#endif
9185 --emsg_off;
9186}
9187
9188/*
9189 * "byte2line(byte)" function
9190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009193 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195{
9196#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#else
9199 long boff = 0;
9200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009201 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009205 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 (linenr_T)0, &boff);
9207#endif
9208}
9209
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009210 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009211byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009212 typval_T *argvars;
9213 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009214 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009215{
9216#ifdef FEAT_MBYTE
9217 char_u *t;
9218#endif
9219 char_u *str;
9220 long idx;
9221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009222 str = get_tv_string_chk(&argvars[0]);
9223 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009224 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009225 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009226 return;
9227
9228#ifdef FEAT_MBYTE
9229 t = str;
9230 for ( ; idx > 0; idx--)
9231 {
9232 if (*t == NUL) /* EOL reached */
9233 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009234 if (enc_utf8 && comp)
9235 t += utf_ptr2len(t);
9236 else
9237 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009238 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009239 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009241 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009243#endif
9244}
9245
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009246/*
9247 * "byteidx()" function
9248 */
9249 static void
9250f_byteidx(argvars, rettv)
9251 typval_T *argvars;
9252 typval_T *rettv;
9253{
9254 byteidx(argvars, rettv, FALSE);
9255}
9256
9257/*
9258 * "byteidxcomp()" function
9259 */
9260 static void
9261f_byteidxcomp(argvars, rettv)
9262 typval_T *argvars;
9263 typval_T *rettv;
9264{
9265 byteidx(argvars, rettv, TRUE);
9266}
9267
Bram Moolenaardb913952012-06-29 12:54:53 +02009268 int
9269func_call(name, args, selfdict, rettv)
9270 char_u *name;
9271 typval_T *args;
9272 dict_T *selfdict;
9273 typval_T *rettv;
9274{
9275 listitem_T *item;
9276 typval_T argv[MAX_FUNC_ARGS + 1];
9277 int argc = 0;
9278 int dummy;
9279 int r = 0;
9280
9281 for (item = args->vval.v_list->lv_first; item != NULL;
9282 item = item->li_next)
9283 {
9284 if (argc == MAX_FUNC_ARGS)
9285 {
9286 EMSG(_("E699: Too many arguments"));
9287 break;
9288 }
9289 /* Make a copy of each argument. This is needed to be able to set
9290 * v_lock to VAR_FIXED in the copy without changing the original list.
9291 */
9292 copy_tv(&item->li_tv, &argv[argc++]);
9293 }
9294
9295 if (item == NULL)
9296 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9297 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9298 &dummy, TRUE, selfdict);
9299
9300 /* Free the arguments. */
9301 while (argc > 0)
9302 clear_tv(&argv[--argc]);
9303
9304 return r;
9305}
9306
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009307/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009308 * "call(func, arglist)" function
9309 */
9310 static void
9311f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009314{
9315 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009316 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009317
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009318 if (argvars[1].v_type != VAR_LIST)
9319 {
9320 EMSG(_(e_listreq));
9321 return;
9322 }
9323 if (argvars[1].vval.v_list == NULL)
9324 return;
9325
9326 if (argvars[0].v_type == VAR_FUNC)
9327 func = argvars[0].vval.v_string;
9328 else
9329 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009330 if (*func == NUL)
9331 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009332
Bram Moolenaare9a41262005-01-15 22:18:47 +00009333 if (argvars[2].v_type != VAR_UNKNOWN)
9334 {
9335 if (argvars[2].v_type != VAR_DICT)
9336 {
9337 EMSG(_(e_dictreq));
9338 return;
9339 }
9340 selfdict = argvars[2].vval.v_dict;
9341 }
9342
Bram Moolenaardb913952012-06-29 12:54:53 +02009343 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009344}
9345
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009346#ifdef FEAT_FLOAT
9347/*
9348 * "ceil({float})" function
9349 */
9350 static void
9351f_ceil(argvars, rettv)
9352 typval_T *argvars;
9353 typval_T *rettv;
9354{
9355 float_T f;
9356
9357 rettv->v_type = VAR_FLOAT;
9358 if (get_float_arg(argvars, &f) == OK)
9359 rettv->vval.v_float = ceil(f);
9360 else
9361 rettv->vval.v_float = 0.0;
9362}
9363#endif
9364
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009365/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009366 * "changenr()" function
9367 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009368 static void
9369f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009370 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009371 typval_T *rettv;
9372{
9373 rettv->vval.v_number = curbuf->b_u_seq_cur;
9374}
9375
9376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 * "char2nr(string)" function
9378 */
9379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009380f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *argvars;
9382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384#ifdef FEAT_MBYTE
9385 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009386 {
9387 int utf8 = 0;
9388
9389 if (argvars[1].v_type != VAR_UNKNOWN)
9390 utf8 = get_tv_number_chk(&argvars[1], NULL);
9391
9392 if (utf8)
9393 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9394 else
9395 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 else
9398#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009399 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400}
9401
9402/*
9403 * "cindent(lnum)" function
9404 */
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009407 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410#ifdef FEAT_CINDENT
9411 pos_T pos;
9412 linenr_T lnum;
9413
9414 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9417 {
9418 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 curwin->w_cursor = pos;
9421 }
9422 else
9423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425}
9426
9427/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009428 * "clearmatches()" function
9429 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009430 static void
9431f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009432 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009433 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009434{
9435#ifdef FEAT_SEARCH_EXTRA
9436 clear_matches(curwin);
9437#endif
9438}
9439
9440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * "col(string)" function
9442 */
9443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009444f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009445 typval_T *argvars;
9446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 colnr_T col = 0;
9449 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009450 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009452 fp = var2fpos(&argvars[0], FALSE, &fnum);
9453 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 {
9455 if (fp->col == MAXCOL)
9456 {
9457 /* '> can be MAXCOL, get the length of the line then */
9458 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009459 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 else
9461 col = MAXCOL;
9462 }
9463 else
9464 {
9465 col = fp->col + 1;
9466#ifdef FEAT_VIRTUALEDIT
9467 /* col(".") when the cursor is on the NUL at the end of the line
9468 * because of "coladd" can be seen as an extra column. */
9469 if (virtual_active() && fp == &curwin->w_cursor)
9470 {
9471 char_u *p = ml_get_cursor();
9472
9473 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9474 curwin->w_virtcol - curwin->w_cursor.coladd))
9475 {
9476# ifdef FEAT_MBYTE
9477 int l;
9478
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009479 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 col += l;
9481# else
9482 if (*p != NUL && p[1] == NUL)
9483 ++col;
9484# endif
9485 }
9486 }
9487#endif
9488 }
9489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491}
9492
Bram Moolenaar572cb562005-08-05 21:35:02 +00009493#if defined(FEAT_INS_EXPAND)
9494/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009495 * "complete()" function
9496 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009497 static void
9498f_complete(argvars, rettv)
9499 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009500 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009501{
9502 int startcol;
9503
9504 if ((State & INSERT) == 0)
9505 {
9506 EMSG(_("E785: complete() can only be used in Insert mode"));
9507 return;
9508 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009509
9510 /* Check for undo allowed here, because if something was already inserted
9511 * the line was already saved for undo and this check isn't done. */
9512 if (!undo_allowed())
9513 return;
9514
Bram Moolenaarade00832006-03-10 21:46:58 +00009515 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9516 {
9517 EMSG(_(e_invarg));
9518 return;
9519 }
9520
9521 startcol = get_tv_number_chk(&argvars[0], NULL);
9522 if (startcol <= 0)
9523 return;
9524
9525 set_completion(startcol - 1, argvars[1].vval.v_list);
9526}
9527
9528/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009529 * "complete_add()" function
9530 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009531 static void
9532f_complete_add(argvars, rettv)
9533 typval_T *argvars;
9534 typval_T *rettv;
9535{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009536 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009537}
9538
9539/*
9540 * "complete_check()" function
9541 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009542 static void
9543f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009544 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009545 typval_T *rettv;
9546{
9547 int saved = RedrawingDisabled;
9548
9549 RedrawingDisabled = 0;
9550 ins_compl_check_keys(0);
9551 rettv->vval.v_number = compl_interrupted;
9552 RedrawingDisabled = saved;
9553}
9554#endif
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556/*
9557 * "confirm(message, buttons[, default [, type]])" function
9558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009561 typval_T *argvars UNUSED;
9562 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9565 char_u *message;
9566 char_u *buttons = NULL;
9567 char_u buf[NUMBUFLEN];
9568 char_u buf2[NUMBUFLEN];
9569 int def = 1;
9570 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 char_u *typestr;
9572 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 message = get_tv_string_chk(&argvars[0]);
9575 if (message == NULL)
9576 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9580 if (buttons == NULL)
9581 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9588 if (typestr == NULL)
9589 error = TRUE;
9590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 switch (TOUPPER_ASC(*typestr))
9593 {
9594 case 'E': type = VIM_ERROR; break;
9595 case 'Q': type = VIM_QUESTION; break;
9596 case 'I': type = VIM_INFO; break;
9597 case 'W': type = VIM_WARNING; break;
9598 case 'G': type = VIM_GENERIC; break;
9599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 }
9601 }
9602 }
9603 }
9604
9605 if (buttons == NULL || *buttons == NUL)
9606 buttons = (char_u *)_("&Ok");
9607
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009608 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009610 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611#endif
9612}
9613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009614/*
9615 * "copy()" function
9616 */
9617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009618f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009619 typval_T *argvars;
9620 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009621{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009622 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009625#ifdef FEAT_FLOAT
9626/*
9627 * "cos()" function
9628 */
9629 static void
9630f_cos(argvars, rettv)
9631 typval_T *argvars;
9632 typval_T *rettv;
9633{
9634 float_T f;
9635
9636 rettv->v_type = VAR_FLOAT;
9637 if (get_float_arg(argvars, &f) == OK)
9638 rettv->vval.v_float = cos(f);
9639 else
9640 rettv->vval.v_float = 0.0;
9641}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009642
9643/*
9644 * "cosh()" function
9645 */
9646 static void
9647f_cosh(argvars, rettv)
9648 typval_T *argvars;
9649 typval_T *rettv;
9650{
9651 float_T f;
9652
9653 rettv->v_type = VAR_FLOAT;
9654 if (get_float_arg(argvars, &f) == OK)
9655 rettv->vval.v_float = cosh(f);
9656 else
9657 rettv->vval.v_float = 0.0;
9658}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009659#endif
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009662 * "count()" function
9663 */
9664 static void
9665f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 typval_T *argvars;
9667 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009668{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009669 long n = 0;
9670 int ic = FALSE;
9671
Bram Moolenaare9a41262005-01-15 22:18:47 +00009672 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009674 listitem_T *li;
9675 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009676 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009677
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 if ((l = argvars[0].vval.v_list) != NULL)
9679 {
9680 li = l->lv_first;
9681 if (argvars[2].v_type != VAR_UNKNOWN)
9682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009683 int error = FALSE;
9684
9685 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 if (argvars[3].v_type != VAR_UNKNOWN)
9687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 idx = get_tv_number_chk(&argvars[3], &error);
9689 if (!error)
9690 {
9691 li = list_find(l, idx);
9692 if (li == NULL)
9693 EMSGN(_(e_listidx), idx);
9694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009695 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009696 if (error)
9697 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009698 }
9699
9700 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009701 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009702 ++n;
9703 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 else if (argvars[0].v_type == VAR_DICT)
9706 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 int todo;
9708 dict_T *d;
9709 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009711 if ((d = argvars[0].vval.v_dict) != NULL)
9712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009713 int error = FALSE;
9714
Bram Moolenaare9a41262005-01-15 22:18:47 +00009715 if (argvars[2].v_type != VAR_UNKNOWN)
9716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009717 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009718 if (argvars[3].v_type != VAR_UNKNOWN)
9719 EMSG(_(e_invarg));
9720 }
9721
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009722 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009723 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009724 {
9725 if (!HASHITEM_EMPTY(hi))
9726 {
9727 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009728 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009729 ++n;
9730 }
9731 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009732 }
9733 }
9734 else
9735 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009736 rettv->vval.v_number = n;
9737}
9738
9739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9741 *
9742 * Checks the existence of a cscope connection.
9743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009746 typval_T *argvars UNUSED;
9747 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749#ifdef FEAT_CSCOPE
9750 int num = 0;
9751 char_u *dbpath = NULL;
9752 char_u *prepend = NULL;
9753 char_u buf[NUMBUFLEN];
9754
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009755 if (argvars[0].v_type != VAR_UNKNOWN
9756 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 num = (int)get_tv_number(&argvars[0]);
9759 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 }
9763
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#endif
9766}
9767
9768/*
9769 * "cursor(lnum, col)" function
9770 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009771 * Moves the cursor to the specified line and column.
9772 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009776 typval_T *argvars;
9777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009780#ifdef FEAT_VIRTUALEDIT
9781 long coladd = 0;
9782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009784 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785 if (argvars[1].v_type == VAR_UNKNOWN)
9786 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009787 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009788
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009789 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009790 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009791 line = pos.lnum;
9792 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009793#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009794 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009795#endif
9796 }
9797 else
9798 {
9799 line = get_tv_lnum(argvars);
9800 col = get_tv_number_chk(&argvars[1], NULL);
9801#ifdef FEAT_VIRTUALEDIT
9802 if (argvars[2].v_type != VAR_UNKNOWN)
9803 coladd = get_tv_number_chk(&argvars[2], NULL);
9804#endif
9805 }
9806 if (line < 0 || col < 0
9807#ifdef FEAT_VIRTUALEDIT
9808 || coladd < 0
9809#endif
9810 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (line > 0)
9813 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (col > 0)
9815 curwin->w_cursor.col = col - 1;
9816#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009817 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818#endif
9819
9820 /* Make sure the cursor is in a valid position. */
9821 check_cursor();
9822#ifdef FEAT_MBYTE
9823 /* Correct cursor for multi-byte character. */
9824 if (has_mbyte)
9825 mb_adjust_cursor();
9826#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009827
9828 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830}
9831
9832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009833 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 */
9835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009837 typval_T *argvars;
9838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009840 int noref = 0;
9841
9842 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009844 if (noref < 0 || noref > 1)
9845 EMSG(_(e_invarg));
9846 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009847 {
9848 current_copyID += COPYID_INC;
9849 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851}
9852
9853/*
9854 * "delete()" function
9855 */
9856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009858 typval_T *argvars;
9859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860{
9861 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865}
9866
9867/*
9868 * "did_filetype()" function
9869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009871f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009872 typval_T *argvars UNUSED;
9873 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874{
9875#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877#endif
9878}
9879
9880/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009881 * "diff_filler()" function
9882 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009885 typval_T *argvars UNUSED;
9886 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009887{
9888#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009890#endif
9891}
9892
9893/*
9894 * "diff_hlID()" function
9895 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009898 typval_T *argvars UNUSED;
9899 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009900{
9901#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009903 static linenr_T prev_lnum = 0;
9904 static int changedtick = 0;
9905 static int fnum = 0;
9906 static int change_start = 0;
9907 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009908 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009909 int filler_lines;
9910 int col;
9911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009912 if (lnum < 0) /* ignore type error in {lnum} arg */
9913 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009914 if (lnum != prev_lnum
9915 || changedtick != curbuf->b_changedtick
9916 || fnum != curbuf->b_fnum)
9917 {
9918 /* New line, buffer, change: need to get the values. */
9919 filler_lines = diff_check(curwin, lnum);
9920 if (filler_lines < 0)
9921 {
9922 if (filler_lines == -1)
9923 {
9924 change_start = MAXCOL;
9925 change_end = -1;
9926 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9927 hlID = HLF_ADD; /* added line */
9928 else
9929 hlID = HLF_CHD; /* changed line */
9930 }
9931 else
9932 hlID = HLF_ADD; /* added line */
9933 }
9934 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009935 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009936 prev_lnum = lnum;
9937 changedtick = curbuf->b_changedtick;
9938 fnum = curbuf->b_fnum;
9939 }
9940
9941 if (hlID == HLF_CHD || hlID == HLF_TXD)
9942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009943 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009944 if (col >= change_start && col <= change_end)
9945 hlID = HLF_TXD; /* changed text */
9946 else
9947 hlID = HLF_CHD; /* changed line */
9948 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009949 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009950#endif
9951}
9952
9953/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009954 * "empty({expr})" function
9955 */
9956 static void
9957f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *argvars;
9959 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009960{
9961 int n;
9962
9963 switch (argvars[0].v_type)
9964 {
9965 case VAR_STRING:
9966 case VAR_FUNC:
9967 n = argvars[0].vval.v_string == NULL
9968 || *argvars[0].vval.v_string == NUL;
9969 break;
9970 case VAR_NUMBER:
9971 n = argvars[0].vval.v_number == 0;
9972 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009973#ifdef FEAT_FLOAT
9974 case VAR_FLOAT:
9975 n = argvars[0].vval.v_float == 0.0;
9976 break;
9977#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009978 case VAR_LIST:
9979 n = argvars[0].vval.v_list == NULL
9980 || argvars[0].vval.v_list->lv_first == NULL;
9981 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009982 case VAR_DICT:
9983 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009985 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009986 default:
9987 EMSG2(_(e_intern2), "f_empty()");
9988 n = 0;
9989 }
9990
9991 rettv->vval.v_number = n;
9992}
9993
9994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 * "escape({string}, {chars})" function
9996 */
9997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009999 typval_T *argvars;
10000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001{
10002 char_u buf[NUMBUFLEN];
10003
Bram Moolenaar758711c2005-02-02 23:11:38 +000010004 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10005 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007}
10008
10009/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010010 * "eval()" function
10011 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010012 static void
10013f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010014 typval_T *argvars;
10015 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010016{
10017 char_u *s;
10018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 s = get_tv_string_chk(&argvars[0]);
10020 if (s != NULL)
10021 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010023 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10024 {
10025 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010026 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010027 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010028 else if (*s != NUL)
10029 EMSG(_(e_trailing));
10030}
10031
10032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 * "eventhandler()" function
10034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010036f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010040 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
10042
10043/*
10044 * "executable()" function
10045 */
10046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010048 typval_T *argvars;
10049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010051 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10052}
10053
10054/*
10055 * "exepath()" function
10056 */
10057 static void
10058f_exepath(argvars, rettv)
10059 typval_T *argvars;
10060 typval_T *rettv;
10061{
10062 char_u *p = NULL;
10063
10064 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10065 rettv->v_type = VAR_STRING;
10066 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067}
10068
10069/*
10070 * "exists()" function
10071 */
10072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010074 typval_T *argvars;
10075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076{
10077 char_u *p;
10078 char_u *name;
10079 int n = FALSE;
10080 int len = 0;
10081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 if (*p == '$') /* environment variable */
10084 {
10085 /* first try "normal" environment variables (fast) */
10086 if (mch_getenv(p + 1) != NULL)
10087 n = TRUE;
10088 else
10089 {
10090 /* try expanding things like $VIM and ${HOME} */
10091 p = expand_env_save(p);
10092 if (p != NULL && *p != '$')
10093 n = TRUE;
10094 vim_free(p);
10095 }
10096 }
10097 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010100 if (*skipwhite(p) != NUL)
10101 n = FALSE; /* trailing garbage */
10102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else if (*p == '*') /* internal or user defined function */
10104 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010105 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 }
10107 else if (*p == ':')
10108 {
10109 n = cmd_exists(p + 1);
10110 }
10111 else if (*p == '#')
10112 {
10113#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010114 if (p[1] == '#')
10115 n = autocmd_supported(p + 2);
10116 else
10117 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118#endif
10119 }
10120 else /* internal variable */
10121 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010122 char_u *tofree;
10123 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010125 /* get_name_len() takes care of expanding curly braces */
10126 name = p;
10127 len = get_name_len(&p, &tofree, TRUE, FALSE);
10128 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010130 if (tofree != NULL)
10131 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010132 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010133 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010135 /* handle d.key, l[idx], f(expr) */
10136 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10137 if (n)
10138 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 }
10140 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010141 if (*p != NUL)
10142 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010144 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 }
10146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148}
10149
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010150#ifdef FEAT_FLOAT
10151/*
10152 * "exp()" function
10153 */
10154 static void
10155f_exp(argvars, rettv)
10156 typval_T *argvars;
10157 typval_T *rettv;
10158{
10159 float_T f;
10160
10161 rettv->v_type = VAR_FLOAT;
10162 if (get_float_arg(argvars, &f) == OK)
10163 rettv->vval.v_float = exp(f);
10164 else
10165 rettv->vval.v_float = 0.0;
10166}
10167#endif
10168
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169/*
10170 * "expand()" function
10171 */
10172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010174 typval_T *argvars;
10175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176{
10177 char_u *s;
10178 int len;
10179 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010180 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010182 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010183 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010186 if (argvars[1].v_type != VAR_UNKNOWN
10187 && argvars[2].v_type != VAR_UNKNOWN
10188 && get_tv_number_chk(&argvars[2], &error)
10189 && !error)
10190 {
10191 rettv->v_type = VAR_LIST;
10192 rettv->vval.v_list = NULL;
10193 }
10194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010195 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 if (*s == '%' || *s == '#' || *s == '<')
10197 {
10198 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010199 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010201 if (rettv->v_type == VAR_LIST)
10202 {
10203 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10204 list_append_string(rettv->vval.v_list, result, -1);
10205 else
10206 vim_free(result);
10207 }
10208 else
10209 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 }
10211 else
10212 {
10213 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010214 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010215 if (argvars[1].v_type != VAR_UNKNOWN
10216 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010217 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 if (!error)
10219 {
10220 ExpandInit(&xpc);
10221 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010222 if (p_wic)
10223 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010224 if (rettv->v_type == VAR_STRING)
10225 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10226 options, WILD_ALL);
10227 else if (rettv_list_alloc(rettv) != FAIL)
10228 {
10229 int i;
10230
10231 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10232 for (i = 0; i < xpc.xp_numfiles; i++)
10233 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10234 ExpandCleanup(&xpc);
10235 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 }
10237 else
10238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 }
10240}
10241
10242/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010243 * Go over all entries in "d2" and add them to "d1".
10244 * When "action" is "error" then a duplicate key is an error.
10245 * When "action" is "force" then a duplicate key is overwritten.
10246 * Otherwise duplicate keys are ignored ("action" is "keep").
10247 */
10248 void
10249dict_extend(d1, d2, action)
10250 dict_T *d1;
10251 dict_T *d2;
10252 char_u *action;
10253{
10254 dictitem_T *di1;
10255 hashitem_T *hi2;
10256 int todo;
10257
10258 todo = (int)d2->dv_hashtab.ht_used;
10259 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10260 {
10261 if (!HASHITEM_EMPTY(hi2))
10262 {
10263 --todo;
10264 di1 = dict_find(d1, hi2->hi_key, -1);
10265 if (d1->dv_scope != 0)
10266 {
10267 /* Disallow replacing a builtin function in l: and g:.
10268 * Check the key to be valid when adding to any
10269 * scope. */
10270 if (d1->dv_scope == VAR_DEF_SCOPE
10271 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10272 && var_check_func_name(hi2->hi_key,
10273 di1 == NULL))
10274 break;
10275 if (!valid_varname(hi2->hi_key))
10276 break;
10277 }
10278 if (di1 == NULL)
10279 {
10280 di1 = dictitem_copy(HI2DI(hi2));
10281 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10282 dictitem_free(di1);
10283 }
10284 else if (*action == 'e')
10285 {
10286 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10287 break;
10288 }
10289 else if (*action == 'f' && HI2DI(hi2) != di1)
10290 {
10291 clear_tv(&di1->di_tv);
10292 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10293 }
10294 }
10295 }
10296}
10297
10298/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010299 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010300 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010301 */
10302 static void
10303f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010304 typval_T *argvars;
10305 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010307 char *arg_errmsg = N_("extend() argument");
10308
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010311 list_T *l1, *l2;
10312 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010315
Bram Moolenaare9a41262005-01-15 22:18:47 +000010316 l1 = argvars[0].vval.v_list;
10317 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010318 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010319 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 {
10321 if (argvars[2].v_type != VAR_UNKNOWN)
10322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 before = get_tv_number_chk(&argvars[2], &error);
10324 if (error)
10325 return; /* type error; errmsg already given */
10326
Bram Moolenaar758711c2005-02-02 23:11:38 +000010327 if (before == l1->lv_len)
10328 item = NULL;
10329 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010331 item = list_find(l1, before);
10332 if (item == NULL)
10333 {
10334 EMSGN(_(e_listidx), before);
10335 return;
10336 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010337 }
10338 }
10339 else
10340 item = NULL;
10341 list_extend(l1, l2, item);
10342
Bram Moolenaare9a41262005-01-15 22:18:47 +000010343 copy_tv(&argvars[0], rettv);
10344 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010345 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010346 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10347 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010348 dict_T *d1, *d2;
10349 char_u *action;
10350 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010351
10352 d1 = argvars[0].vval.v_dict;
10353 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010354 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010355 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 {
10357 /* Check the third argument. */
10358 if (argvars[2].v_type != VAR_UNKNOWN)
10359 {
10360 static char *(av[]) = {"keep", "force", "error"};
10361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010362 action = get_tv_string_chk(&argvars[2]);
10363 if (action == NULL)
10364 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010365 for (i = 0; i < 3; ++i)
10366 if (STRCMP(action, av[i]) == 0)
10367 break;
10368 if (i == 3)
10369 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010370 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010371 return;
10372 }
10373 }
10374 else
10375 action = (char_u *)"force";
10376
Bram Moolenaara9922d62013-05-30 13:01:18 +020010377 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010378
Bram Moolenaare9a41262005-01-15 22:18:47 +000010379 copy_tv(&argvars[0], rettv);
10380 }
10381 }
10382 else
10383 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010384}
10385
10386/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010387 * "feedkeys()" function
10388 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010389 static void
10390f_feedkeys(argvars, rettv)
10391 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010392 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010393{
10394 int remap = TRUE;
10395 char_u *keys, *flags;
10396 char_u nbuf[NUMBUFLEN];
10397 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010398 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010399
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010400 /* This is not allowed in the sandbox. If the commands would still be
10401 * executed in the sandbox it would be OK, but it probably happens later,
10402 * when "sandbox" is no longer set. */
10403 if (check_secure())
10404 return;
10405
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010406 keys = get_tv_string(&argvars[0]);
10407 if (*keys != NUL)
10408 {
10409 if (argvars[1].v_type != VAR_UNKNOWN)
10410 {
10411 flags = get_tv_string_buf(&argvars[1], nbuf);
10412 for ( ; *flags != NUL; ++flags)
10413 {
10414 switch (*flags)
10415 {
10416 case 'n': remap = FALSE; break;
10417 case 'm': remap = TRUE; break;
10418 case 't': typed = TRUE; break;
10419 }
10420 }
10421 }
10422
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010423 /* Need to escape K_SPECIAL and CSI before putting the string in the
10424 * typeahead buffer. */
10425 keys_esc = vim_strsave_escape_csi(keys);
10426 if (keys_esc != NULL)
10427 {
10428 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010429 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010430 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010431 if (vgetc_busy)
10432 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010433 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010434 }
10435}
10436
10437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 * "filereadable()" function
10439 */
10440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010442 typval_T *argvars;
10443 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010445 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 char_u *p;
10447 int n;
10448
Bram Moolenaarc236c162008-07-13 17:41:49 +000010449#ifndef O_NONBLOCK
10450# define O_NONBLOCK 0
10451#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010453 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10454 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 {
10456 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010457 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 }
10459 else
10460 n = FALSE;
10461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463}
10464
10465/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010466 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 * rights to write into.
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010474 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475}
10476
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010477static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
10479 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010480findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010481 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010482 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010483 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010484{
10485#ifdef FEAT_SEARCHPATH
10486 char_u *fname;
10487 char_u *fresult = NULL;
10488 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10489 char_u *p;
10490 char_u pathbuf[NUMBUFLEN];
10491 int count = 1;
10492 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010493 int error = FALSE;
10494#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010495
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010496 rettv->vval.v_string = NULL;
10497 rettv->v_type = VAR_STRING;
10498
10499#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010501
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010502 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10505 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010506 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 else
10508 {
10509 if (*p != NUL)
10510 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010513 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010515 }
10516
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010517 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10518 error = TRUE;
10519
10520 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 do
10523 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010524 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010525 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 fresult = find_file_in_path_option(first ? fname : NULL,
10527 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010528 0, first, path,
10529 find_what,
10530 curbuf->b_ffname,
10531 find_what == FINDFILE_DIR
10532 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010533 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010534
10535 if (fresult != NULL && rettv->v_type == VAR_LIST)
10536 list_append_string(rettv->vval.v_list, fresult, -1);
10537
10538 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010539 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010540
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010541 if (rettv->v_type == VAR_STRING)
10542 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010543#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010544}
10545
Bram Moolenaar33570922005-01-25 22:26:29 +000010546static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10547static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010548
10549/*
10550 * Implementation of map() and filter().
10551 */
10552 static void
10553filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010556 int map;
10557{
10558 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010560 listitem_T *li, *nli;
10561 list_T *l = NULL;
10562 dictitem_T *di;
10563 hashtab_T *ht;
10564 hashitem_T *hi;
10565 dict_T *d = NULL;
10566 typval_T save_val;
10567 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010569 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010570 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10571 char *arg_errmsg = (map ? N_("map() argument")
10572 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010573 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010574 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010575
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010577 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010578 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010579 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 return;
10581 }
10582 else if (argvars[0].v_type == VAR_DICT)
10583 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010584 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010585 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 return;
10587 }
10588 else
10589 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010590 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 return;
10592 }
10593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 expr = get_tv_string_buf_chk(&argvars[1], buf);
10595 /* On type errors, the preceding call has already displayed an error
10596 * message. Avoid a misleading error message for an empty string that
10597 * was not passed as argument. */
10598 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 prepare_vimvar(VV_VAL, &save_val);
10601 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010602
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010603 /* We reset "did_emsg" to be able to detect whether an error
10604 * occurred during evaluation of the expression. */
10605 save_did_emsg = did_emsg;
10606 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010607
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010608 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010609 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 vimvars[VV_KEY].vv_type = VAR_STRING;
10612
10613 ht = &d->dv_hashtab;
10614 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010615 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 if (!HASHITEM_EMPTY(hi))
10619 {
10620 --todo;
10621 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010622 if (tv_check_lock(di->di_tv.v_lock,
10623 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 break;
10625 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010626 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010627 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 break;
10629 if (!map && rem)
10630 dictitem_remove(d, di);
10631 clear_tv(&vimvars[VV_KEY].vv_tv);
10632 }
10633 }
10634 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010635 }
10636 else
10637 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010638 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 for (li = l->lv_first; li != NULL; li = nli)
10641 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010642 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010643 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010644 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010645 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010646 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010647 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010648 break;
10649 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010650 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010651 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010653 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010654
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010655 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010656 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010657
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010658 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660
10661 copy_tv(&argvars[0], rettv);
10662}
10663
10664 static int
10665filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010666 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010667 char_u *expr;
10668 int map;
10669 int *remp;
10670{
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010673 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674
Bram Moolenaar33570922005-01-25 22:26:29 +000010675 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010676 s = expr;
10677 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 if (*s != NUL) /* check for trailing chars after expr */
10680 {
10681 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010682 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010683 }
10684 if (map)
10685 {
10686 /* map(): replace the list item value */
10687 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010688 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010689 *tv = rettv;
10690 }
10691 else
10692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693 int error = FALSE;
10694
Bram Moolenaare9a41262005-01-15 22:18:47 +000010695 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010697 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 /* On type error, nothing has been removed; return FAIL to stop the
10699 * loop. The error message was given by get_tv_number_chk(). */
10700 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010701 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010702 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010703 retval = OK;
10704theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010705 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010706 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010707}
10708
10709/*
10710 * "filter()" function
10711 */
10712 static void
10713f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010714 typval_T *argvars;
10715 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010716{
10717 filter_map(argvars, rettv, FALSE);
10718}
10719
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010720/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010721 * "finddir({fname}[, {path}[, {count}]])" function
10722 */
10723 static void
10724f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010725 typval_T *argvars;
10726 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010727{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010728 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010729}
10730
10731/*
10732 * "findfile({fname}[, {path}[, {count}]])" function
10733 */
10734 static void
10735f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010736 typval_T *argvars;
10737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010738{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010739 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010740}
10741
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010742#ifdef FEAT_FLOAT
10743/*
10744 * "float2nr({float})" function
10745 */
10746 static void
10747f_float2nr(argvars, rettv)
10748 typval_T *argvars;
10749 typval_T *rettv;
10750{
10751 float_T f;
10752
10753 if (get_float_arg(argvars, &f) == OK)
10754 {
10755 if (f < -0x7fffffff)
10756 rettv->vval.v_number = -0x7fffffff;
10757 else if (f > 0x7fffffff)
10758 rettv->vval.v_number = 0x7fffffff;
10759 else
10760 rettv->vval.v_number = (varnumber_T)f;
10761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010762}
10763
10764/*
10765 * "floor({float})" function
10766 */
10767 static void
10768f_floor(argvars, rettv)
10769 typval_T *argvars;
10770 typval_T *rettv;
10771{
10772 float_T f;
10773
10774 rettv->v_type = VAR_FLOAT;
10775 if (get_float_arg(argvars, &f) == OK)
10776 rettv->vval.v_float = floor(f);
10777 else
10778 rettv->vval.v_float = 0.0;
10779}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010780
10781/*
10782 * "fmod()" function
10783 */
10784 static void
10785f_fmod(argvars, rettv)
10786 typval_T *argvars;
10787 typval_T *rettv;
10788{
10789 float_T fx, fy;
10790
10791 rettv->v_type = VAR_FLOAT;
10792 if (get_float_arg(argvars, &fx) == OK
10793 && get_float_arg(&argvars[1], &fy) == OK)
10794 rettv->vval.v_float = fmod(fx, fy);
10795 else
10796 rettv->vval.v_float = 0.0;
10797}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010798#endif
10799
Bram Moolenaar0d660222005-01-07 21:51:51 +000010800/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010801 * "fnameescape({string})" function
10802 */
10803 static void
10804f_fnameescape(argvars, rettv)
10805 typval_T *argvars;
10806 typval_T *rettv;
10807{
10808 rettv->vval.v_string = vim_strsave_fnameescape(
10809 get_tv_string(&argvars[0]), FALSE);
10810 rettv->v_type = VAR_STRING;
10811}
10812
10813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 * "fnamemodify({fname}, {mods})" function
10815 */
10816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *argvars;
10819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 char_u *fname;
10822 char_u *mods;
10823 int usedlen = 0;
10824 int len;
10825 char_u *fbuf = NULL;
10826 char_u buf[NUMBUFLEN];
10827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 fname = get_tv_string_chk(&argvars[0]);
10829 mods = get_tv_string_buf_chk(&argvars[1], buf);
10830 if (fname == NULL || mods == NULL)
10831 fname = NULL;
10832 else
10833 {
10834 len = (int)STRLEN(fname);
10835 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 vim_free(fbuf);
10844}
10845
Bram Moolenaar33570922005-01-25 22:26:29 +000010846static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847
10848/*
10849 * "foldclosed()" function
10850 */
10851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010853 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010855 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856{
10857#ifdef FEAT_FOLDING
10858 linenr_T lnum;
10859 linenr_T first, last;
10860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10863 {
10864 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10865 {
10866 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 return;
10871 }
10872 }
10873#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010878 * "foldclosed()" function
10879 */
10880 static void
10881f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010882 typval_T *argvars;
10883 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010884{
10885 foldclosed_both(argvars, rettv, FALSE);
10886}
10887
10888/*
10889 * "foldclosedend()" function
10890 */
10891 static void
10892f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010893 typval_T *argvars;
10894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010895{
10896 foldclosed_both(argvars, rettv, TRUE);
10897}
10898
10899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 * "foldlevel()" function
10901 */
10902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010904 typval_T *argvars UNUSED;
10905 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907#ifdef FEAT_FOLDING
10908 linenr_T lnum;
10909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914}
10915
10916/*
10917 * "foldtext()" function
10918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
10924#ifdef FEAT_FOLDING
10925 linenr_T lnum;
10926 char_u *s;
10927 char_u *r;
10928 int len;
10929 char *txt;
10930#endif
10931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 rettv->v_type = VAR_STRING;
10933 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010935 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10936 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10937 <= curbuf->b_ml.ml_line_count
10938 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 {
10940 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10942 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 {
10944 if (!linewhite(lnum))
10945 break;
10946 ++lnum;
10947 }
10948
10949 /* Find interesting text in this line. */
10950 s = skipwhite(ml_get(lnum));
10951 /* skip C comment-start */
10952 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010955 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010957 {
10958 s = skipwhite(ml_get(lnum + 1));
10959 if (*s == '*')
10960 s = skipwhite(s + 1);
10961 }
10962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 txt = _("+-%s%3ld lines: ");
10964 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010965 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 + 20 /* for %3ld */
10967 + STRLEN(s))); /* concatenated */
10968 if (r != NULL)
10969 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010970 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10971 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10972 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 len = (int)STRLEN(r);
10974 STRCAT(r, s);
10975 /* remove 'foldmarker' and 'commentstring' */
10976 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 }
10979 }
10980#endif
10981}
10982
10983/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010984 * "foldtextresult(lnum)" function
10985 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010988 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010990{
10991#ifdef FEAT_FOLDING
10992 linenr_T lnum;
10993 char_u *text;
10994 char_u buf[51];
10995 foldinfo_T foldinfo;
10996 int fold_count;
10997#endif
10998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999 rettv->v_type = VAR_STRING;
11000 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011001#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003 /* treat illegal types and illegal string values for {lnum} the same */
11004 if (lnum < 0)
11005 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011006 fold_count = foldedCount(curwin, lnum, &foldinfo);
11007 if (fold_count > 0)
11008 {
11009 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11010 &foldinfo, buf);
11011 if (text == buf)
11012 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011014 }
11015#endif
11016}
11017
11018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 * "foreground()" function
11020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011023 typval_T *argvars UNUSED;
11024 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026#ifdef FEAT_GUI
11027 if (gui.in_use)
11028 gui_mch_set_foreground();
11029#else
11030# ifdef WIN32
11031 win32_set_foreground();
11032# endif
11033#endif
11034}
11035
11036/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011037 * "function()" function
11038 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011041 typval_T *argvars;
11042 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043{
11044 char_u *s;
11045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011047 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011048 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011049 /* Don't check an autoload name for existence here. */
11050 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011051 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052 else
11053 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011054 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011055 {
11056 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011057 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011058
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011059 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11060 * also be called from another script. Using trans_function_name()
11061 * would also work, but some plugins depend on the name being
11062 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011063 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011064 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011065 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011066 if (rettv->vval.v_string != NULL)
11067 {
11068 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011069 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011070 }
11071 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011072 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011073 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075 }
11076}
11077
11078/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011079 * "garbagecollect()" function
11080 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011081 static void
11082f_garbagecollect(argvars, rettv)
11083 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011084 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011085{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011086 /* This is postponed until we are back at the toplevel, because we may be
11087 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11088 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011089
11090 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11091 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011092}
11093
11094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095 * "get()" function
11096 */
11097 static void
11098f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011101{
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 listitem_T *li;
11103 list_T *l;
11104 dictitem_T *di;
11105 dict_T *d;
11106 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011107
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011109 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 int error = FALSE;
11113
11114 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11115 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011116 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011119 else if (argvars[0].v_type == VAR_DICT)
11120 {
11121 if ((d = argvars[0].vval.v_dict) != NULL)
11122 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011123 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 if (di != NULL)
11125 tv = &di->di_tv;
11126 }
11127 }
11128 else
11129 EMSG2(_(e_listdictarg), "get()");
11130
11131 if (tv == NULL)
11132 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011134 copy_tv(&argvars[2], rettv);
11135 }
11136 else
11137 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138}
11139
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140static 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 +000011141
11142/*
11143 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 * Return a range (from start to end) of lines in rettv from the specified
11145 * buffer.
11146 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011147 */
11148 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011150 buf_T *buf;
11151 linenr_T start;
11152 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011153 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011154 typval_T *rettv;
11155{
11156 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011157
Bram Moolenaar959a1432013-12-14 12:17:38 +010011158 rettv->v_type = VAR_STRING;
11159 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011160 if (retlist && rettv_list_alloc(rettv) == FAIL)
11161 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011162
11163 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11164 return;
11165
11166 if (!retlist)
11167 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011168 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11169 p = ml_get_buf(buf, start, FALSE);
11170 else
11171 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011172 rettv->vval.v_string = vim_strsave(p);
11173 }
11174 else
11175 {
11176 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011177 return;
11178
11179 if (start < 1)
11180 start = 1;
11181 if (end > buf->b_ml.ml_line_count)
11182 end = buf->b_ml.ml_line_count;
11183 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011184 if (list_append_string(rettv->vval.v_list,
11185 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011186 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011187 }
11188}
11189
11190/*
11191 * "getbufline()" function
11192 */
11193 static void
11194f_getbufline(argvars, rettv)
11195 typval_T *argvars;
11196 typval_T *rettv;
11197{
11198 linenr_T lnum;
11199 linenr_T end;
11200 buf_T *buf;
11201
11202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11203 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011204 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011205 --emsg_off;
11206
Bram Moolenaar661b1822005-07-28 22:36:45 +000011207 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011208 if (argvars[2].v_type == VAR_UNKNOWN)
11209 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011210 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011211 end = get_tv_lnum_buf(&argvars[2], buf);
11212
Bram Moolenaar342337a2005-07-21 21:11:17 +000011213 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011214}
11215
Bram Moolenaar0d660222005-01-07 21:51:51 +000011216/*
11217 * "getbufvar()" function
11218 */
11219 static void
11220f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011221 typval_T *argvars;
11222 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011223{
11224 buf_T *buf;
11225 buf_T *save_curbuf;
11226 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011228 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11231 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011233 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011235 rettv->v_type = VAR_STRING;
11236 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237
11238 if (buf != NULL && varname != NULL)
11239 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011240 /* set curbuf to be our buf, temporarily */
11241 save_curbuf = curbuf;
11242 curbuf = buf;
11243
Bram Moolenaar0d660222005-01-07 21:51:51 +000011244 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011245 {
11246 if (get_option_tv(&varname, rettv, TRUE) == OK)
11247 done = TRUE;
11248 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011249 else if (STRCMP(varname, "changedtick") == 0)
11250 {
11251 rettv->v_type = VAR_NUMBER;
11252 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011253 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011254 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011255 else
11256 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011257 /* Look up the variable. */
11258 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11259 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11260 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011261 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011264 done = TRUE;
11265 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011267
11268 /* restore previous notion of curbuf */
11269 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011270 }
11271
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011272 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11273 /* use the default value */
11274 copy_tv(&argvars[2], rettv);
11275
Bram Moolenaar0d660222005-01-07 21:51:51 +000011276 --emsg_off;
11277}
11278
11279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 * "getchar()" function
11281 */
11282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *argvars;
11285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286{
11287 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011290 /* Position the cursor. Needed after a message that ends in a space. */
11291 windgoto(msg_row, msg_col);
11292
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 ++no_mapping;
11294 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011295 for (;;)
11296 {
11297 if (argvars[0].v_type == VAR_UNKNOWN)
11298 /* getchar(): blocking wait. */
11299 n = safe_vgetc();
11300 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11301 /* getchar(1): only check if char avail */
11302 n = vpeekc();
11303 else if (error || vpeekc() == NUL)
11304 /* illegal argument or getchar(0) and no char avail: return zero */
11305 n = 0;
11306 else
11307 /* getchar(0) and char avail: return char */
11308 n = safe_vgetc();
11309 if (n == K_IGNORE)
11310 continue;
11311 break;
11312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 --no_mapping;
11314 --allow_keys;
11315
Bram Moolenaar219b8702006-11-01 14:32:36 +000011316 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11317 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11318 vimvars[VV_MOUSE_COL].vv_nr = 0;
11319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 if (IS_SPECIAL(n) || mod_mask != 0)
11322 {
11323 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11324 int i = 0;
11325
11326 /* Turn a special key into three bytes, plus modifier. */
11327 if (mod_mask != 0)
11328 {
11329 temp[i++] = K_SPECIAL;
11330 temp[i++] = KS_MODIFIER;
11331 temp[i++] = mod_mask;
11332 }
11333 if (IS_SPECIAL(n))
11334 {
11335 temp[i++] = K_SPECIAL;
11336 temp[i++] = K_SECOND(n);
11337 temp[i++] = K_THIRD(n);
11338 }
11339#ifdef FEAT_MBYTE
11340 else if (has_mbyte)
11341 i += (*mb_char2bytes)(n, temp + i);
11342#endif
11343 else
11344 temp[i++] = n;
11345 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->v_type = VAR_STRING;
11347 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011348
11349#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011350 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011351 {
11352 int row = mouse_row;
11353 int col = mouse_col;
11354 win_T *win;
11355 linenr_T lnum;
11356# ifdef FEAT_WINDOWS
11357 win_T *wp;
11358# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011359 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011360
11361 if (row >= 0 && col >= 0)
11362 {
11363 /* Find the window at the mouse coordinates and compute the
11364 * text position. */
11365 win = mouse_find_win(&row, &col);
11366 (void)mouse_comp_pos(win, &row, &col, &lnum);
11367# ifdef FEAT_WINDOWS
11368 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011369 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011370# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011371 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011372 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11373 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11374 }
11375 }
11376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 }
11378}
11379
11380/*
11381 * "getcharmod()" function
11382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011385 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
11392 * "getcmdline()" function
11393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011399 rettv->v_type = VAR_STRING;
11400 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
11403/*
11404 * "getcmdpos()" function
11405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412}
11413
11414/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011415 * "getcmdtype()" function
11416 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011417 static void
11418f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011419 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011420 typval_T *rettv;
11421{
11422 rettv->v_type = VAR_STRING;
11423 rettv->vval.v_string = alloc(2);
11424 if (rettv->vval.v_string != NULL)
11425 {
11426 rettv->vval.v_string[0] = get_cmdline_type();
11427 rettv->vval.v_string[1] = NUL;
11428 }
11429}
11430
11431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 * "getcwd()" function
11433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011436 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011439 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011442 rettv->vval.v_string = NULL;
11443 cwd = alloc(MAXPATHL);
11444 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011446 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11447 {
11448 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011450 if (rettv->vval.v_string != NULL)
11451 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011453 }
11454 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 }
11456}
11457
11458/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011459 * "getfontname()" function
11460 */
11461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462f_getfontname(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 Moolenaar46c9c732004-12-12 11:37:09 +000011465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011468#ifdef FEAT_GUI
11469 if (gui.in_use)
11470 {
11471 GuiFont font;
11472 char_u *name = NULL;
11473
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011474 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011475 {
11476 /* Get the "Normal" font. Either the name saved by
11477 * hl_set_font_name() or from the font ID. */
11478 font = gui.norm_font;
11479 name = hl_get_font_name();
11480 }
11481 else
11482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011484 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11485 return;
11486 font = gui_mch_get_font(name, FALSE);
11487 if (font == NOFONT)
11488 return; /* Invalid font name, return empty string. */
11489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011491 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011492 gui_mch_free_font(font);
11493 }
11494#endif
11495}
11496
11497/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011498 * "getfperm({fname})" function
11499 */
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011504{
11505 char_u *fname;
11506 struct stat st;
11507 char_u *perm = NULL;
11508 char_u flags[] = "rwx";
11509 int i;
11510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011514 if (mch_stat((char *)fname, &st) >= 0)
11515 {
11516 perm = vim_strsave((char_u *)"---------");
11517 if (perm != NULL)
11518 {
11519 for (i = 0; i < 9; i++)
11520 {
11521 if (st.st_mode & (1 << (8 - i)))
11522 perm[i] = flags[i % 3];
11523 }
11524 }
11525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011527}
11528
11529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 * "getfsize({fname})" function
11531 */
11532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011534 typval_T *argvars;
11535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536{
11537 char_u *fname;
11538 struct stat st;
11539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543
11544 if (mch_stat((char *)fname, &st) >= 0)
11545 {
11546 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011547 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011550 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011551
11552 /* non-perfect check for overflow */
11553 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11554 rettv->vval.v_number = -2;
11555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556 }
11557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559}
11560
11561/*
11562 * "getftime({fname})" function
11563 */
11564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011566 typval_T *argvars;
11567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568{
11569 char_u *fname;
11570 struct stat st;
11571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573
11574 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578}
11579
11580/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011581 * "getftype({fname})" function
11582 */
11583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011587{
11588 char_u *fname;
11589 struct stat st;
11590 char_u *type = NULL;
11591 char *t;
11592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011595 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011596 if (mch_lstat((char *)fname, &st) >= 0)
11597 {
11598#ifdef S_ISREG
11599 if (S_ISREG(st.st_mode))
11600 t = "file";
11601 else if (S_ISDIR(st.st_mode))
11602 t = "dir";
11603# ifdef S_ISLNK
11604 else if (S_ISLNK(st.st_mode))
11605 t = "link";
11606# endif
11607# ifdef S_ISBLK
11608 else if (S_ISBLK(st.st_mode))
11609 t = "bdev";
11610# endif
11611# ifdef S_ISCHR
11612 else if (S_ISCHR(st.st_mode))
11613 t = "cdev";
11614# endif
11615# ifdef S_ISFIFO
11616 else if (S_ISFIFO(st.st_mode))
11617 t = "fifo";
11618# endif
11619# ifdef S_ISSOCK
11620 else if (S_ISSOCK(st.st_mode))
11621 t = "fifo";
11622# endif
11623 else
11624 t = "other";
11625#else
11626# ifdef S_IFMT
11627 switch (st.st_mode & S_IFMT)
11628 {
11629 case S_IFREG: t = "file"; break;
11630 case S_IFDIR: t = "dir"; break;
11631# ifdef S_IFLNK
11632 case S_IFLNK: t = "link"; break;
11633# endif
11634# ifdef S_IFBLK
11635 case S_IFBLK: t = "bdev"; break;
11636# endif
11637# ifdef S_IFCHR
11638 case S_IFCHR: t = "cdev"; break;
11639# endif
11640# ifdef S_IFIFO
11641 case S_IFIFO: t = "fifo"; break;
11642# endif
11643# ifdef S_IFSOCK
11644 case S_IFSOCK: t = "socket"; break;
11645# endif
11646 default: t = "other";
11647 }
11648# else
11649 if (mch_isdir(fname))
11650 t = "dir";
11651 else
11652 t = "file";
11653# endif
11654#endif
11655 type = vim_strsave((char_u *)t);
11656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011658}
11659
11660/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011661 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 */
11663 static void
11664f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667{
11668 linenr_T lnum;
11669 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011670 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671
11672 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011673 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011674 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011675 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011676 retlist = FALSE;
11677 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011678 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011681 retlist = TRUE;
11682 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011683
Bram Moolenaar342337a2005-07-21 21:11:17 +000011684 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011685}
11686
11687/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011688 * "getmatches()" function
11689 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011690 static void
11691f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011692 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011693 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011694{
11695#ifdef FEAT_SEARCH_EXTRA
11696 dict_T *dict;
11697 matchitem_T *cur = curwin->w_match_head;
11698
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011699 if (rettv_list_alloc(rettv) == OK)
11700 {
11701 while (cur != NULL)
11702 {
11703 dict = dict_alloc();
11704 if (dict == NULL)
11705 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011706 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11707 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11708 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11709 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11710 list_append_dict(rettv->vval.v_list, dict);
11711 cur = cur->next;
11712 }
11713 }
11714#endif
11715}
11716
11717/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011718 * "getpid()" function
11719 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011720 static void
11721f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011722 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011723 typval_T *rettv;
11724{
11725 rettv->vval.v_number = mch_get_pid();
11726}
11727
11728/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011729 * "getpos(string)" function
11730 */
11731 static void
11732f_getpos(argvars, rettv)
11733 typval_T *argvars;
11734 typval_T *rettv;
11735{
11736 pos_T *fp;
11737 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011738 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011739
11740 if (rettv_list_alloc(rettv) == OK)
11741 {
11742 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011743 fp = var2fpos(&argvars[0], TRUE, &fnum);
11744 if (fnum != -1)
11745 list_append_number(l, (varnumber_T)fnum);
11746 else
11747 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011748 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11749 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011750 list_append_number(l, (fp != NULL)
11751 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011752 : (varnumber_T)0);
11753 list_append_number(l,
11754#ifdef FEAT_VIRTUALEDIT
11755 (fp != NULL) ? (varnumber_T)fp->coladd :
11756#endif
11757 (varnumber_T)0);
11758 }
11759 else
11760 rettv->vval.v_number = FALSE;
11761}
11762
11763/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011764 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011765 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011766 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011767f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011768 typval_T *argvars UNUSED;
11769 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011770{
11771#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011772 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011773#endif
11774
Bram Moolenaar2641f772005-03-25 21:58:17 +000011775#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011776 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011777 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011778 wp = NULL;
11779 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11780 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011781 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011782 if (wp == NULL)
11783 return;
11784 }
11785
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011786 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011787 }
11788#endif
11789}
11790
11791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792 * "getreg()" function
11793 */
11794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011796 typval_T *argvars;
11797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798{
11799 char_u *strregname;
11800 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011801 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011804 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 strregname = get_tv_string_chk(&argvars[0]);
11807 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011808 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011812 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 regname = (strregname == NULL ? '"' : *strregname);
11814 if (regname == 0)
11815 regname = '"';
11816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011817 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 rettv->vval.v_string = error ? NULL :
11819 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820}
11821
11822/*
11823 * "getregtype()" function
11824 */
11825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *argvars;
11828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829{
11830 char_u *strregname;
11831 int regname;
11832 char_u buf[NUMBUFLEN + 2];
11833 long reglen = 0;
11834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011835 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 {
11837 strregname = get_tv_string_chk(&argvars[0]);
11838 if (strregname == NULL) /* type error; errmsg already given */
11839 {
11840 rettv->v_type = VAR_STRING;
11841 rettv->vval.v_string = NULL;
11842 return;
11843 }
11844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 else
11846 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011847 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848
11849 regname = (strregname == NULL ? '"' : *strregname);
11850 if (regname == 0)
11851 regname = '"';
11852
11853 buf[0] = NUL;
11854 buf[1] = NUL;
11855 switch (get_reg_type(regname, &reglen))
11856 {
11857 case MLINE: buf[0] = 'V'; break;
11858 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 case MBLOCK:
11860 buf[0] = Ctrl_V;
11861 sprintf((char *)buf + 1, "%ld", reglen + 1);
11862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->v_type = VAR_STRING;
11865 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866}
11867
11868/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011869 * "gettabvar()" function
11870 */
11871 static void
11872f_gettabvar(argvars, rettv)
11873 typval_T *argvars;
11874 typval_T *rettv;
11875{
11876 tabpage_T *tp;
11877 dictitem_T *v;
11878 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011879 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011880
11881 rettv->v_type = VAR_STRING;
11882 rettv->vval.v_string = NULL;
11883
11884 varname = get_tv_string_chk(&argvars[1]);
11885 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11886 if (tp != NULL && varname != NULL)
11887 {
11888 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011889 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011890 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011891 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011892 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011893 done = TRUE;
11894 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011895 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011896
11897 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011898 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011899}
11900
11901/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011902 * "gettabwinvar()" function
11903 */
11904 static void
11905f_gettabwinvar(argvars, rettv)
11906 typval_T *argvars;
11907 typval_T *rettv;
11908{
11909 getwinvar(argvars, rettv, 1);
11910}
11911
11912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 * "getwinposx()" function
11914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011916f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921#ifdef FEAT_GUI
11922 if (gui.in_use)
11923 {
11924 int x, y;
11925
11926 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 }
11929#endif
11930}
11931
11932/*
11933 * "getwinposy()" function
11934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011936f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011937 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941#ifdef FEAT_GUI
11942 if (gui.in_use)
11943 {
11944 int x, y;
11945
11946 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011947 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 }
11949#endif
11950}
11951
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011952/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011953 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011954 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011955 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011956find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011957 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011958 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011959{
11960#ifdef FEAT_WINDOWS
11961 win_T *wp;
11962#endif
11963 int nr;
11964
11965 nr = get_tv_number_chk(vp, NULL);
11966
11967#ifdef FEAT_WINDOWS
11968 if (nr < 0)
11969 return NULL;
11970 if (nr == 0)
11971 return curwin;
11972
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011973 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11974 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011975 if (--nr <= 0)
11976 break;
11977 return wp;
11978#else
11979 if (nr == 0 || nr == 1)
11980 return curwin;
11981 return NULL;
11982#endif
11983}
11984
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985/*
11986 * "getwinvar()" function
11987 */
11988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011989f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011990 typval_T *argvars;
11991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011993 getwinvar(argvars, rettv, 0);
11994}
11995
11996/*
11997 * getwinvar() and gettabwinvar()
11998 */
11999 static void
12000getwinvar(argvars, rettv, off)
12001 typval_T *argvars;
12002 typval_T *rettv;
12003 int off; /* 1 for gettabwinvar() */
12004{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 win_T *win, *oldcurwin;
12006 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012007 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012008 tabpage_T *tp = NULL;
12009 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012010 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012012#ifdef FEAT_WINDOWS
12013 if (off == 1)
12014 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12015 else
12016 tp = curtab;
12017#endif
12018 win = find_win_by_nr(&argvars[off], tp);
12019 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012022 rettv->v_type = VAR_STRING;
12023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
12025 if (win != NULL && varname != NULL)
12026 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012027 /* Set curwin to be our win, temporarily. Also set the tabpage,
12028 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012029 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012030
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012032 {
12033 if (get_option_tv(&varname, rettv, 1) == OK)
12034 done = TRUE;
12035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036 else
12037 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012038 /* Look up the variable. */
12039 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12040 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012044 done = TRUE;
12045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012047
12048 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012049 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 }
12051
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012052 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12053 /* use the default return value */
12054 copy_tv(&argvars[off + 2], rettv);
12055
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 --emsg_off;
12057}
12058
12059/*
12060 * "glob()" function
12061 */
12062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012063f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012064 typval_T *argvars;
12065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012067 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012069 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012071 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012072 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012073 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012074 if (argvars[1].v_type != VAR_UNKNOWN)
12075 {
12076 if (get_tv_number_chk(&argvars[1], &error))
12077 options |= WILD_KEEP_ALL;
12078 if (argvars[2].v_type != VAR_UNKNOWN
12079 && get_tv_number_chk(&argvars[2], &error))
12080 {
12081 rettv->v_type = VAR_LIST;
12082 rettv->vval.v_list = NULL;
12083 }
12084 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012085 if (!error)
12086 {
12087 ExpandInit(&xpc);
12088 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012089 if (p_wic)
12090 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012091 if (rettv->v_type == VAR_STRING)
12092 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012093 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012094 else if (rettv_list_alloc(rettv) != FAIL)
12095 {
12096 int i;
12097
12098 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12099 NULL, options, WILD_ALL_KEEP);
12100 for (i = 0; i < xpc.xp_numfiles; i++)
12101 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12102
12103 ExpandCleanup(&xpc);
12104 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012105 }
12106 else
12107 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108}
12109
12110/*
12111 * "globpath()" function
12112 */
12113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012115 typval_T *argvars;
12116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012118 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012120 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012121 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012123 /* When the optional second argument is non-zero, don't remove matches
12124 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12125 if (argvars[2].v_type != VAR_UNKNOWN
12126 && get_tv_number_chk(&argvars[2], &error))
12127 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012129 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012130 rettv->vval.v_string = NULL;
12131 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012132 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12133 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134}
12135
12136/*
12137 * "has()" function
12138 */
12139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012140f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012141 typval_T *argvars;
12142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143{
12144 int i;
12145 char_u *name;
12146 int n = FALSE;
12147 static char *(has_list[]) =
12148 {
12149#ifdef AMIGA
12150 "amiga",
12151# ifdef FEAT_ARP
12152 "arp",
12153# endif
12154#endif
12155#ifdef __BEOS__
12156 "beos",
12157#endif
12158#ifdef MSDOS
12159# ifdef DJGPP
12160 "dos32",
12161# else
12162 "dos16",
12163# endif
12164#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012165#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 "mac",
12167#endif
12168#if defined(MACOS_X_UNIX)
12169 "macunix",
12170#endif
12171#ifdef OS2
12172 "os2",
12173#endif
12174#ifdef __QNX__
12175 "qnx",
12176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177#ifdef UNIX
12178 "unix",
12179#endif
12180#ifdef VMS
12181 "vms",
12182#endif
12183#ifdef WIN16
12184 "win16",
12185#endif
12186#ifdef WIN32
12187 "win32",
12188#endif
12189#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12190 "win32unix",
12191#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012192#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 "win64",
12194#endif
12195#ifdef EBCDIC
12196 "ebcdic",
12197#endif
12198#ifndef CASE_INSENSITIVE_FILENAME
12199 "fname_case",
12200#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012201#ifdef HAVE_ACL
12202 "acl",
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef FEAT_ARABIC
12205 "arabic",
12206#endif
12207#ifdef FEAT_AUTOCMD
12208 "autocmd",
12209#endif
12210#ifdef FEAT_BEVAL
12211 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012212# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12213 "balloon_multiline",
12214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215#endif
12216#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12217 "builtin_terms",
12218# ifdef ALL_BUILTIN_TCAPS
12219 "all_builtin_terms",
12220# endif
12221#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012222#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12223 || defined(FEAT_GUI_W32) \
12224 || defined(FEAT_GUI_MOTIF))
12225 "browsefilter",
12226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227#ifdef FEAT_BYTEOFF
12228 "byte_offset",
12229#endif
12230#ifdef FEAT_CINDENT
12231 "cindent",
12232#endif
12233#ifdef FEAT_CLIENTSERVER
12234 "clientserver",
12235#endif
12236#ifdef FEAT_CLIPBOARD
12237 "clipboard",
12238#endif
12239#ifdef FEAT_CMDL_COMPL
12240 "cmdline_compl",
12241#endif
12242#ifdef FEAT_CMDHIST
12243 "cmdline_hist",
12244#endif
12245#ifdef FEAT_COMMENTS
12246 "comments",
12247#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012248#ifdef FEAT_CONCEAL
12249 "conceal",
12250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251#ifdef FEAT_CRYPT
12252 "cryptv",
12253#endif
12254#ifdef FEAT_CSCOPE
12255 "cscope",
12256#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012257#ifdef FEAT_CURSORBIND
12258 "cursorbind",
12259#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012260#ifdef CURSOR_SHAPE
12261 "cursorshape",
12262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263#ifdef DEBUG
12264 "debug",
12265#endif
12266#ifdef FEAT_CON_DIALOG
12267 "dialog_con",
12268#endif
12269#ifdef FEAT_GUI_DIALOG
12270 "dialog_gui",
12271#endif
12272#ifdef FEAT_DIFF
12273 "diff",
12274#endif
12275#ifdef FEAT_DIGRAPHS
12276 "digraphs",
12277#endif
12278#ifdef FEAT_DND
12279 "dnd",
12280#endif
12281#ifdef FEAT_EMACS_TAGS
12282 "emacs_tags",
12283#endif
12284 "eval", /* always present, of course! */
12285#ifdef FEAT_EX_EXTRA
12286 "ex_extra",
12287#endif
12288#ifdef FEAT_SEARCH_EXTRA
12289 "extra_search",
12290#endif
12291#ifdef FEAT_FKMAP
12292 "farsi",
12293#endif
12294#ifdef FEAT_SEARCHPATH
12295 "file_in_path",
12296#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012297#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012298 "filterpipe",
12299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300#ifdef FEAT_FIND_ID
12301 "find_in_path",
12302#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012303#ifdef FEAT_FLOAT
12304 "float",
12305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306#ifdef FEAT_FOLDING
12307 "folding",
12308#endif
12309#ifdef FEAT_FOOTER
12310 "footer",
12311#endif
12312#if !defined(USE_SYSTEM) && defined(UNIX)
12313 "fork",
12314#endif
12315#ifdef FEAT_GETTEXT
12316 "gettext",
12317#endif
12318#ifdef FEAT_GUI
12319 "gui",
12320#endif
12321#ifdef FEAT_GUI_ATHENA
12322# ifdef FEAT_GUI_NEXTAW
12323 "gui_neXtaw",
12324# else
12325 "gui_athena",
12326# endif
12327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328#ifdef FEAT_GUI_GTK
12329 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012332#ifdef FEAT_GUI_GNOME
12333 "gui_gnome",
12334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335#ifdef FEAT_GUI_MAC
12336 "gui_mac",
12337#endif
12338#ifdef FEAT_GUI_MOTIF
12339 "gui_motif",
12340#endif
12341#ifdef FEAT_GUI_PHOTON
12342 "gui_photon",
12343#endif
12344#ifdef FEAT_GUI_W16
12345 "gui_win16",
12346#endif
12347#ifdef FEAT_GUI_W32
12348 "gui_win32",
12349#endif
12350#ifdef FEAT_HANGULIN
12351 "hangul_input",
12352#endif
12353#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12354 "iconv",
12355#endif
12356#ifdef FEAT_INS_EXPAND
12357 "insert_expand",
12358#endif
12359#ifdef FEAT_JUMPLIST
12360 "jumplist",
12361#endif
12362#ifdef FEAT_KEYMAP
12363 "keymap",
12364#endif
12365#ifdef FEAT_LANGMAP
12366 "langmap",
12367#endif
12368#ifdef FEAT_LIBCALL
12369 "libcall",
12370#endif
12371#ifdef FEAT_LINEBREAK
12372 "linebreak",
12373#endif
12374#ifdef FEAT_LISP
12375 "lispindent",
12376#endif
12377#ifdef FEAT_LISTCMDS
12378 "listcmds",
12379#endif
12380#ifdef FEAT_LOCALMAP
12381 "localmap",
12382#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012383#ifdef FEAT_LUA
12384# ifndef DYNAMIC_LUA
12385 "lua",
12386# endif
12387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388#ifdef FEAT_MENU
12389 "menu",
12390#endif
12391#ifdef FEAT_SESSION
12392 "mksession",
12393#endif
12394#ifdef FEAT_MODIFY_FNAME
12395 "modify_fname",
12396#endif
12397#ifdef FEAT_MOUSE
12398 "mouse",
12399#endif
12400#ifdef FEAT_MOUSESHAPE
12401 "mouseshape",
12402#endif
12403#if defined(UNIX) || defined(VMS)
12404# ifdef FEAT_MOUSE_DEC
12405 "mouse_dec",
12406# endif
12407# ifdef FEAT_MOUSE_GPM
12408 "mouse_gpm",
12409# endif
12410# ifdef FEAT_MOUSE_JSB
12411 "mouse_jsbterm",
12412# endif
12413# ifdef FEAT_MOUSE_NET
12414 "mouse_netterm",
12415# endif
12416# ifdef FEAT_MOUSE_PTERM
12417 "mouse_pterm",
12418# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012419# ifdef FEAT_MOUSE_SGR
12420 "mouse_sgr",
12421# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012422# ifdef FEAT_SYSMOUSE
12423 "mouse_sysmouse",
12424# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012425# ifdef FEAT_MOUSE_URXVT
12426 "mouse_urxvt",
12427# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428# ifdef FEAT_MOUSE_XTERM
12429 "mouse_xterm",
12430# endif
12431#endif
12432#ifdef FEAT_MBYTE
12433 "multi_byte",
12434#endif
12435#ifdef FEAT_MBYTE_IME
12436 "multi_byte_ime",
12437#endif
12438#ifdef FEAT_MULTI_LANG
12439 "multi_lang",
12440#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012441#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012442#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012443 "mzscheme",
12444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446#ifdef FEAT_OLE
12447 "ole",
12448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449#ifdef FEAT_PATH_EXTRA
12450 "path_extra",
12451#endif
12452#ifdef FEAT_PERL
12453#ifndef DYNAMIC_PERL
12454 "perl",
12455#endif
12456#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012457#ifdef FEAT_PERSISTENT_UNDO
12458 "persistent_undo",
12459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460#ifdef FEAT_PYTHON
12461#ifndef DYNAMIC_PYTHON
12462 "python",
12463#endif
12464#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012465#ifdef FEAT_PYTHON3
12466#ifndef DYNAMIC_PYTHON3
12467 "python3",
12468#endif
12469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470#ifdef FEAT_POSTSCRIPT
12471 "postscript",
12472#endif
12473#ifdef FEAT_PRINTER
12474 "printer",
12475#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012476#ifdef FEAT_PROFILE
12477 "profile",
12478#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012479#ifdef FEAT_RELTIME
12480 "reltime",
12481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482#ifdef FEAT_QUICKFIX
12483 "quickfix",
12484#endif
12485#ifdef FEAT_RIGHTLEFT
12486 "rightleft",
12487#endif
12488#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12489 "ruby",
12490#endif
12491#ifdef FEAT_SCROLLBIND
12492 "scrollbind",
12493#endif
12494#ifdef FEAT_CMDL_INFO
12495 "showcmd",
12496 "cmdline_info",
12497#endif
12498#ifdef FEAT_SIGNS
12499 "signs",
12500#endif
12501#ifdef FEAT_SMARTINDENT
12502 "smartindent",
12503#endif
12504#ifdef FEAT_SNIFF
12505 "sniff",
12506#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012507#ifdef STARTUPTIME
12508 "startuptime",
12509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510#ifdef FEAT_STL_OPT
12511 "statusline",
12512#endif
12513#ifdef FEAT_SUN_WORKSHOP
12514 "sun_workshop",
12515#endif
12516#ifdef FEAT_NETBEANS_INTG
12517 "netbeans_intg",
12518#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012519#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012520 "spell",
12521#endif
12522#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012523 "syntax",
12524#endif
12525#if defined(USE_SYSTEM) || !defined(UNIX)
12526 "system",
12527#endif
12528#ifdef FEAT_TAG_BINS
12529 "tag_binary",
12530#endif
12531#ifdef FEAT_TAG_OLDSTATIC
12532 "tag_old_static",
12533#endif
12534#ifdef FEAT_TAG_ANYWHITE
12535 "tag_any_white",
12536#endif
12537#ifdef FEAT_TCL
12538# ifndef DYNAMIC_TCL
12539 "tcl",
12540# endif
12541#endif
12542#ifdef TERMINFO
12543 "terminfo",
12544#endif
12545#ifdef FEAT_TERMRESPONSE
12546 "termresponse",
12547#endif
12548#ifdef FEAT_TEXTOBJ
12549 "textobjects",
12550#endif
12551#ifdef HAVE_TGETENT
12552 "tgetent",
12553#endif
12554#ifdef FEAT_TITLE
12555 "title",
12556#endif
12557#ifdef FEAT_TOOLBAR
12558 "toolbar",
12559#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012560#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12561 "unnamedplus",
12562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563#ifdef FEAT_USR_CMDS
12564 "user-commands", /* was accidentally included in 5.4 */
12565 "user_commands",
12566#endif
12567#ifdef FEAT_VIMINFO
12568 "viminfo",
12569#endif
12570#ifdef FEAT_VERTSPLIT
12571 "vertsplit",
12572#endif
12573#ifdef FEAT_VIRTUALEDIT
12574 "virtualedit",
12575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577#ifdef FEAT_VISUALEXTRA
12578 "visualextra",
12579#endif
12580#ifdef FEAT_VREPLACE
12581 "vreplace",
12582#endif
12583#ifdef FEAT_WILDIGN
12584 "wildignore",
12585#endif
12586#ifdef FEAT_WILDMENU
12587 "wildmenu",
12588#endif
12589#ifdef FEAT_WINDOWS
12590 "windows",
12591#endif
12592#ifdef FEAT_WAK
12593 "winaltkeys",
12594#endif
12595#ifdef FEAT_WRITEBACKUP
12596 "writebackup",
12597#endif
12598#ifdef FEAT_XIM
12599 "xim",
12600#endif
12601#ifdef FEAT_XFONTSET
12602 "xfontset",
12603#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012604#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012605 "xpm",
12606 "xpm_w32", /* for backward compatibility */
12607#else
12608# if defined(HAVE_XPM)
12609 "xpm",
12610# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612#ifdef USE_XSMP
12613 "xsmp",
12614#endif
12615#ifdef USE_XSMP_INTERACT
12616 "xsmp_interact",
12617#endif
12618#ifdef FEAT_XCLIPBOARD
12619 "xterm_clipboard",
12620#endif
12621#ifdef FEAT_XTERM_SAVE
12622 "xterm_save",
12623#endif
12624#if defined(UNIX) && defined(FEAT_X11)
12625 "X11",
12626#endif
12627 NULL
12628 };
12629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 for (i = 0; has_list[i] != NULL; ++i)
12632 if (STRICMP(name, has_list[i]) == 0)
12633 {
12634 n = TRUE;
12635 break;
12636 }
12637
12638 if (n == FALSE)
12639 {
12640 if (STRNICMP(name, "patch", 5) == 0)
12641 n = has_patch(atoi((char *)name + 5));
12642 else if (STRICMP(name, "vim_starting") == 0)
12643 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012644#ifdef FEAT_MBYTE
12645 else if (STRICMP(name, "multi_byte_encoding") == 0)
12646 n = has_mbyte;
12647#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012648#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12649 else if (STRICMP(name, "balloon_multiline") == 0)
12650 n = multiline_balloon_available();
12651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652#ifdef DYNAMIC_TCL
12653 else if (STRICMP(name, "tcl") == 0)
12654 n = tcl_enabled(FALSE);
12655#endif
12656#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12657 else if (STRICMP(name, "iconv") == 0)
12658 n = iconv_enabled(FALSE);
12659#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012660#ifdef DYNAMIC_LUA
12661 else if (STRICMP(name, "lua") == 0)
12662 n = lua_enabled(FALSE);
12663#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012664#ifdef DYNAMIC_MZSCHEME
12665 else if (STRICMP(name, "mzscheme") == 0)
12666 n = mzscheme_enabled(FALSE);
12667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668#ifdef DYNAMIC_RUBY
12669 else if (STRICMP(name, "ruby") == 0)
12670 n = ruby_enabled(FALSE);
12671#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012672#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673#ifdef DYNAMIC_PYTHON
12674 else if (STRICMP(name, "python") == 0)
12675 n = python_enabled(FALSE);
12676#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012677#endif
12678#ifdef FEAT_PYTHON3
12679#ifdef DYNAMIC_PYTHON3
12680 else if (STRICMP(name, "python3") == 0)
12681 n = python3_enabled(FALSE);
12682#endif
12683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684#ifdef DYNAMIC_PERL
12685 else if (STRICMP(name, "perl") == 0)
12686 n = perl_enabled(FALSE);
12687#endif
12688#ifdef FEAT_GUI
12689 else if (STRICMP(name, "gui_running") == 0)
12690 n = (gui.in_use || gui.starting);
12691# ifdef FEAT_GUI_W32
12692 else if (STRICMP(name, "gui_win32s") == 0)
12693 n = gui_is_win32s();
12694# endif
12695# ifdef FEAT_BROWSE
12696 else if (STRICMP(name, "browse") == 0)
12697 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12698# endif
12699#endif
12700#ifdef FEAT_SYN_HL
12701 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012702 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703#endif
12704#if defined(WIN3264)
12705 else if (STRICMP(name, "win95") == 0)
12706 n = mch_windows95();
12707#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012708#ifdef FEAT_NETBEANS_INTG
12709 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012710 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 }
12713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012718 * "has_key()" function
12719 */
12720 static void
12721f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *argvars;
12723 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012724{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012725 if (argvars[0].v_type != VAR_DICT)
12726 {
12727 EMSG(_(e_dictreq));
12728 return;
12729 }
12730 if (argvars[0].vval.v_dict == NULL)
12731 return;
12732
12733 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012734 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012735}
12736
12737/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012738 * "haslocaldir()" function
12739 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012740 static void
12741f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012742 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012743 typval_T *rettv;
12744{
12745 rettv->vval.v_number = (curwin->w_localdir != NULL);
12746}
12747
12748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 * "hasmapto()" function
12750 */
12751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012753 typval_T *argvars;
12754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755{
12756 char_u *name;
12757 char_u *mode;
12758 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012759 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012762 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 mode = (char_u *)"nvo";
12764 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012767 if (argvars[2].v_type != VAR_UNKNOWN)
12768 abbr = get_tv_number(&argvars[2]);
12769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770
Bram Moolenaar2c932302006-03-18 21:42:09 +000012771 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775}
12776
12777/*
12778 * "histadd()" function
12779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012782 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784{
12785#ifdef FEAT_CMDHIST
12786 int histype;
12787 char_u *str;
12788 char_u buf[NUMBUFLEN];
12789#endif
12790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 if (check_restricted() || check_secure())
12793 return;
12794#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12796 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 if (histype >= 0)
12798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 if (*str != NUL)
12801 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012802 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 return;
12806 }
12807 }
12808#endif
12809}
12810
12811/*
12812 * "histdel()" function
12813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012816 typval_T *argvars UNUSED;
12817 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818{
12819#ifdef FEAT_CMDHIST
12820 int n;
12821 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012822 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12825 if (str == NULL)
12826 n = 0;
12827 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012829 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012830 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012832 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 else
12835 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 get_tv_string_buf(&argvars[1], buf));
12838 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839#endif
12840}
12841
12842/*
12843 * "histget()" function
12844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012847 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849{
12850#ifdef FEAT_CMDHIST
12851 int type;
12852 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012853 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012855 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12856 if (str == NULL)
12857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012859 {
12860 type = get_histtype(str);
12861 if (argvars[1].v_type == VAR_UNKNOWN)
12862 idx = get_history_idx(type);
12863 else
12864 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12865 /* -1 on type error */
12866 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872}
12873
12874/*
12875 * "histnr()" function
12876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012879 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881{
12882 int i;
12883
12884#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012885 char_u *history = get_tv_string_chk(&argvars[0]);
12886
12887 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888 if (i >= HIST_CMD && i < HIST_COUNT)
12889 i = get_history_idx(i);
12890 else
12891#endif
12892 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894}
12895
12896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897 * "highlightID(name)" function
12898 */
12899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012901 typval_T *argvars;
12902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905}
12906
12907/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012908 * "highlight_exists()" function
12909 */
12910 static void
12911f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012912 typval_T *argvars;
12913 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012914{
12915 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12916}
12917
12918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 * "hostname()" function
12920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925{
12926 char_u hostname[256];
12927
12928 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
12930 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931}
12932
12933/*
12934 * iconv() function
12935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012938 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940{
12941#ifdef FEAT_MBYTE
12942 char_u buf1[NUMBUFLEN];
12943 char_u buf2[NUMBUFLEN];
12944 char_u *from, *to, *str;
12945 vimconv_T vimconv;
12946#endif
12947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->v_type = VAR_STRING;
12949 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950
12951#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 str = get_tv_string(&argvars[0]);
12953 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12954 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 vimconv.vc_type = CONV_NONE;
12956 convert_setup(&vimconv, from, to);
12957
12958 /* If the encodings are equal, no conversion needed. */
12959 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963
12964 convert_setup(&vimconv, NULL, NULL);
12965 vim_free(from);
12966 vim_free(to);
12967#endif
12968}
12969
12970/*
12971 * "indent()" function
12972 */
12973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012975 typval_T *argvars;
12976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977{
12978 linenr_T lnum;
12979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012987/*
12988 * "index()" function
12989 */
12990 static void
12991f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012992 typval_T *argvars;
12993 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012994{
Bram Moolenaar33570922005-01-25 22:26:29 +000012995 list_T *l;
12996 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012997 long idx = 0;
12998 int ic = FALSE;
12999
13000 rettv->vval.v_number = -1;
13001 if (argvars[0].v_type != VAR_LIST)
13002 {
13003 EMSG(_(e_listreq));
13004 return;
13005 }
13006 l = argvars[0].vval.v_list;
13007 if (l != NULL)
13008 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013009 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013012 int error = FALSE;
13013
Bram Moolenaar758711c2005-02-02 23:11:38 +000013014 /* Start at specified item. Use the cached index that list_find()
13015 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013016 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013017 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013018 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013019 ic = get_tv_number_chk(&argvars[3], &error);
13020 if (error)
13021 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013022 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013023
Bram Moolenaar758711c2005-02-02 23:11:38 +000013024 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013025 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013026 {
13027 rettv->vval.v_number = idx;
13028 break;
13029 }
13030 }
13031}
13032
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033static int inputsecret_flag = 0;
13034
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013035static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13036
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013038 * This function is used by f_input() and f_inputdialog() functions. The third
13039 * argument to f_input() specifies the type of completion to use at the
13040 * prompt. The third argument to f_inputdialog() specifies the value to return
13041 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 */
13043 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013044get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *argvars;
13046 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013047 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013049 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 char_u *p = NULL;
13051 int c;
13052 char_u buf[NUMBUFLEN];
13053 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013055 int xp_type = EXPAND_NOTHING;
13056 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013059 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060
13061#ifdef NO_CONSOLE_INPUT
13062 /* While starting up, there is no place to enter text. */
13063 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065#endif
13066
13067 cmd_silent = FALSE; /* Want to see the prompt. */
13068 if (prompt != NULL)
13069 {
13070 /* Only the part of the message after the last NL is considered as
13071 * prompt for the command line */
13072 p = vim_strrchr(prompt, '\n');
13073 if (p == NULL)
13074 p = prompt;
13075 else
13076 {
13077 ++p;
13078 c = *p;
13079 *p = NUL;
13080 msg_start();
13081 msg_clr_eos();
13082 msg_puts_attr(prompt, echo_attr);
13083 msg_didout = FALSE;
13084 msg_starthere();
13085 *p = c;
13086 }
13087 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 if (argvars[1].v_type != VAR_UNKNOWN)
13090 {
13091 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13092 if (defstr != NULL)
13093 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013095 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013096 {
13097 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013098 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013099 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013100
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013101 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013102 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013103
Bram Moolenaar4463f292005-09-25 22:20:24 +000013104 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13105 if (xp_name == NULL)
13106 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013108 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013109
Bram Moolenaar4463f292005-09-25 22:20:24 +000013110 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13111 &xp_arg) == FAIL)
13112 return;
13113 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013114 }
13115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013116 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013117 {
13118# ifdef FEAT_EX_EXTRA
13119 int save_ex_normal_busy = ex_normal_busy;
13120 ex_normal_busy = 0;
13121# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013122 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013123 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13124 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013125# ifdef FEAT_EX_EXTRA
13126 ex_normal_busy = save_ex_normal_busy;
13127# endif
13128 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013129 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013130 && argvars[1].v_type != VAR_UNKNOWN
13131 && argvars[2].v_type != VAR_UNKNOWN)
13132 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13133 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013134
13135 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013137 /* since the user typed this, no need to wait for return */
13138 need_wait_return = FALSE;
13139 msg_didout = FALSE;
13140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013141 cmd_silent = cmd_silent_save;
13142}
13143
13144/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013145 * "input()" function
13146 * Also handles inputsecret() when inputsecret is set.
13147 */
13148 static void
13149f_input(argvars, rettv)
13150 typval_T *argvars;
13151 typval_T *rettv;
13152{
13153 get_user_input(argvars, rettv, FALSE);
13154}
13155
13156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 * "inputdialog()" function
13158 */
13159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013161 typval_T *argvars;
13162 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163{
13164#if defined(FEAT_GUI_TEXTDIALOG)
13165 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13166 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13167 {
13168 char_u *message;
13169 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013170 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013172 message = get_tv_string_chk(&argvars[0]);
13173 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013174 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013175 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 else
13177 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 if (message != NULL && defstr != NULL
13179 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013180 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 else
13183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013184 if (message != NULL && defstr != NULL
13185 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013186 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 rettv->vval.v_string = vim_strsave(
13188 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013190 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013192 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 }
13194 else
13195#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013196 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197}
13198
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013199/*
13200 * "inputlist()" function
13201 */
13202 static void
13203f_inputlist(argvars, rettv)
13204 typval_T *argvars;
13205 typval_T *rettv;
13206{
13207 listitem_T *li;
13208 int selected;
13209 int mouse_used;
13210
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013211#ifdef NO_CONSOLE_INPUT
13212 /* While starting up, there is no place to enter text. */
13213 if (no_console_input())
13214 return;
13215#endif
13216 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13217 {
13218 EMSG2(_(e_listarg), "inputlist()");
13219 return;
13220 }
13221
13222 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013223 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013224 lines_left = Rows; /* avoid more prompt */
13225 msg_scroll = TRUE;
13226 msg_clr_eos();
13227
13228 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13229 {
13230 msg_puts(get_tv_string(&li->li_tv));
13231 msg_putchar('\n');
13232 }
13233
13234 /* Ask for choice. */
13235 selected = prompt_for_number(&mouse_used);
13236 if (mouse_used)
13237 selected -= lines_left;
13238
13239 rettv->vval.v_number = selected;
13240}
13241
13242
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13244
13245/*
13246 * "inputrestore()" function
13247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013250 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
13253 if (ga_userinput.ga_len > 0)
13254 {
13255 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13257 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013258 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 }
13260 else if (p_verbose > 1)
13261 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013262 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 }
13265}
13266
13267/*
13268 * "inputsave()" function
13269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013272 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013275 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 if (ga_grow(&ga_userinput, 1) == OK)
13277 {
13278 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13279 + ga_userinput.ga_len);
13280 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013281 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 }
13283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285}
13286
13287/*
13288 * "inputsecret()" function
13289 */
13290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 typval_T *argvars;
13293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294{
13295 ++cmdline_star;
13296 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 --cmdline_star;
13299 --inputsecret_flag;
13300}
13301
13302/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013303 * "insert()" function
13304 */
13305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 typval_T *argvars;
13308 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013309{
13310 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 listitem_T *item;
13312 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013314
13315 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013316 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013317 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013318 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013319 {
13320 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 before = get_tv_number_chk(&argvars[2], &error);
13322 if (error)
13323 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013324
Bram Moolenaar758711c2005-02-02 23:11:38 +000013325 if (before == l->lv_len)
13326 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013327 else
13328 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013329 item = list_find(l, before);
13330 if (item == NULL)
13331 {
13332 EMSGN(_(e_listidx), before);
13333 l = NULL;
13334 }
13335 }
13336 if (l != NULL)
13337 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013338 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013339 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013340 }
13341 }
13342}
13343
13344/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013345 * "invert(expr)" function
13346 */
13347 static void
13348f_invert(argvars, rettv)
13349 typval_T *argvars;
13350 typval_T *rettv;
13351{
13352 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13353}
13354
13355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 * "isdirectory()" function
13357 */
13358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013360 typval_T *argvars;
13361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013366/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013367 * "islocked()" function
13368 */
13369 static void
13370f_islocked(argvars, rettv)
13371 typval_T *argvars;
13372 typval_T *rettv;
13373{
13374 lval_T lv;
13375 char_u *end;
13376 dictitem_T *di;
13377
13378 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013379 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13380 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013381 if (end != NULL && lv.ll_name != NULL)
13382 {
13383 if (*end != NUL)
13384 EMSG(_(e_trailing));
13385 else
13386 {
13387 if (lv.ll_tv == NULL)
13388 {
13389 if (check_changedtick(lv.ll_name))
13390 rettv->vval.v_number = 1; /* always locked */
13391 else
13392 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013393 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013394 if (di != NULL)
13395 {
13396 /* Consider a variable locked when:
13397 * 1. the variable itself is locked
13398 * 2. the value of the variable is locked.
13399 * 3. the List or Dict value is locked.
13400 */
13401 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13402 || tv_islocked(&di->di_tv));
13403 }
13404 }
13405 }
13406 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013407 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013408 else if (lv.ll_newkey != NULL)
13409 EMSG2(_(e_dictkey), lv.ll_newkey);
13410 else if (lv.ll_list != NULL)
13411 /* List item. */
13412 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13413 else
13414 /* Dictionary item. */
13415 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13416 }
13417 }
13418
13419 clear_lval(&lv);
13420}
13421
Bram Moolenaar33570922005-01-25 22:26:29 +000013422static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013423
13424/*
13425 * Turn a dict into a list:
13426 * "what" == 0: list of keys
13427 * "what" == 1: list of values
13428 * "what" == 2: list of items
13429 */
13430 static void
13431dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013432 typval_T *argvars;
13433 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013434 int what;
13435{
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 list_T *l2;
13437 dictitem_T *di;
13438 hashitem_T *hi;
13439 listitem_T *li;
13440 listitem_T *li2;
13441 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013442 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013443
Bram Moolenaar8c711452005-01-14 21:53:12 +000013444 if (argvars[0].v_type != VAR_DICT)
13445 {
13446 EMSG(_(e_dictreq));
13447 return;
13448 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013449 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013450 return;
13451
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013452 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013453 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013454
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013455 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013458 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013459 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013460 --todo;
13461 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013462
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013463 li = listitem_alloc();
13464 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013465 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013466 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013468 if (what == 0)
13469 {
13470 /* keys() */
13471 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013472 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013473 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13474 }
13475 else if (what == 1)
13476 {
13477 /* values() */
13478 copy_tv(&di->di_tv, &li->li_tv);
13479 }
13480 else
13481 {
13482 /* items() */
13483 l2 = list_alloc();
13484 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013485 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013486 li->li_tv.vval.v_list = l2;
13487 if (l2 == NULL)
13488 break;
13489 ++l2->lv_refcount;
13490
13491 li2 = listitem_alloc();
13492 if (li2 == NULL)
13493 break;
13494 list_append(l2, li2);
13495 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013496 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013497 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13498
13499 li2 = listitem_alloc();
13500 if (li2 == NULL)
13501 break;
13502 list_append(l2, li2);
13503 copy_tv(&di->di_tv, &li2->li_tv);
13504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013505 }
13506 }
13507}
13508
13509/*
13510 * "items(dict)" function
13511 */
13512 static void
13513f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013514 typval_T *argvars;
13515 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013516{
13517 dict_list(argvars, rettv, 2);
13518}
13519
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013521 * "join()" function
13522 */
13523 static void
13524f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013525 typval_T *argvars;
13526 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013527{
13528 garray_T ga;
13529 char_u *sep;
13530
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013531 if (argvars[0].v_type != VAR_LIST)
13532 {
13533 EMSG(_(e_listreq));
13534 return;
13535 }
13536 if (argvars[0].vval.v_list == NULL)
13537 return;
13538 if (argvars[1].v_type == VAR_UNKNOWN)
13539 sep = (char_u *)" ";
13540 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013541 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013542
13543 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013544
13545 if (sep != NULL)
13546 {
13547 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013548 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 ga_append(&ga, NUL);
13550 rettv->vval.v_string = (char_u *)ga.ga_data;
13551 }
13552 else
13553 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013554}
13555
13556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013557 * "keys()" function
13558 */
13559 static void
13560f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 typval_T *argvars;
13562 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013563{
13564 dict_list(argvars, rettv, 0);
13565}
13566
13567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 * "last_buffer_nr()" function.
13569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013572 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574{
13575 int n = 0;
13576 buf_T *buf;
13577
13578 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13579 if (n < buf->b_fnum)
13580 n = buf->b_fnum;
13581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583}
13584
13585/*
13586 * "len()" function
13587 */
13588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013590 typval_T *argvars;
13591 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013592{
13593 switch (argvars[0].v_type)
13594 {
13595 case VAR_STRING:
13596 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->vval.v_number = (varnumber_T)STRLEN(
13598 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013599 break;
13600 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013602 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013603 case VAR_DICT:
13604 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13605 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013606 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013607 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013608 break;
13609 }
13610}
13611
Bram Moolenaar33570922005-01-25 22:26:29 +000013612static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013613
13614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *argvars;
13617 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013618 int type;
13619{
13620#ifdef FEAT_LIBCALL
13621 char_u *string_in;
13622 char_u **string_result;
13623 int nr_result;
13624#endif
13625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013627 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013629
13630 if (check_restricted() || check_secure())
13631 return;
13632
13633#ifdef FEAT_LIBCALL
13634 /* The first two args must be strings, otherwise its meaningless */
13635 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13636 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013637 string_in = NULL;
13638 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013639 string_in = argvars[2].vval.v_string;
13640 if (type == VAR_NUMBER)
13641 string_result = NULL;
13642 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013644 if (mch_libcall(argvars[0].vval.v_string,
13645 argvars[1].vval.v_string,
13646 string_in,
13647 argvars[2].vval.v_number,
13648 string_result,
13649 &nr_result) == OK
13650 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013652 }
13653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
13656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013657 * "libcall()" function
13658 */
13659 static void
13660f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013661 typval_T *argvars;
13662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013663{
13664 libcall_common(argvars, rettv, VAR_STRING);
13665}
13666
13667/*
13668 * "libcallnr()" function
13669 */
13670 static void
13671f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013672 typval_T *argvars;
13673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013674{
13675 libcall_common(argvars, rettv, VAR_NUMBER);
13676}
13677
13678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 * "line(string)" function
13680 */
13681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *argvars;
13684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685{
13686 linenr_T lnum = 0;
13687 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013688 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013690 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 if (fp != NULL)
13692 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694}
13695
13696/*
13697 * "line2byte(lnum)" function
13698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013701 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703{
13704#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706#else
13707 linenr_T lnum;
13708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13714 if (rettv->vval.v_number >= 0)
13715 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716#endif
13717}
13718
13719/*
13720 * "lispindent(lnum)" function
13721 */
13722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013723f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013724 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726{
13727#ifdef FEAT_LISP
13728 pos_T pos;
13729 linenr_T lnum;
13730
13731 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13734 {
13735 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013736 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 curwin->w_cursor = pos;
13738 }
13739 else
13740#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742}
13743
13744/*
13745 * "localtime()" function
13746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013748f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013749 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013752 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753}
13754
Bram Moolenaar33570922005-01-25 22:26:29 +000013755static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756
13757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *argvars;
13760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 int exact;
13762{
13763 char_u *keys;
13764 char_u *which;
13765 char_u buf[NUMBUFLEN];
13766 char_u *keys_buf = NULL;
13767 char_u *rhs;
13768 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013769 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013770 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013771 mapblock_T *mp;
13772 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773
13774 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013775 rettv->v_type = VAR_STRING;
13776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013778 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 if (*keys == NUL)
13780 return;
13781
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013782 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013785 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013786 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013787 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013788 if (argvars[3].v_type != VAR_UNKNOWN)
13789 get_dict = get_tv_number(&argvars[3]);
13790 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 else
13793 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 if (which == NULL)
13795 return;
13796
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797 mode = get_map_mode(&which, 0);
13798
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013799 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013800 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013802
13803 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013805 /* Return a string. */
13806 if (rhs != NULL)
13807 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808
Bram Moolenaarbd743252010-10-20 21:23:33 +020013809 }
13810 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13811 {
13812 /* Return a dictionary. */
13813 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13814 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13815 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
Bram Moolenaarbd743252010-10-20 21:23:33 +020013817 dict_add_nr_str(dict, "lhs", 0L, lhs);
13818 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13819 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13820 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13821 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13822 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13823 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013824 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013825 dict_add_nr_str(dict, "mode", 0L, mapmode);
13826
13827 vim_free(lhs);
13828 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 }
13830}
13831
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013832#ifdef FEAT_FLOAT
13833/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013834 * "log()" function
13835 */
13836 static void
13837f_log(argvars, rettv)
13838 typval_T *argvars;
13839 typval_T *rettv;
13840{
13841 float_T f;
13842
13843 rettv->v_type = VAR_FLOAT;
13844 if (get_float_arg(argvars, &f) == OK)
13845 rettv->vval.v_float = log(f);
13846 else
13847 rettv->vval.v_float = 0.0;
13848}
13849
13850/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013851 * "log10()" function
13852 */
13853 static void
13854f_log10(argvars, rettv)
13855 typval_T *argvars;
13856 typval_T *rettv;
13857{
13858 float_T f;
13859
13860 rettv->v_type = VAR_FLOAT;
13861 if (get_float_arg(argvars, &f) == OK)
13862 rettv->vval.v_float = log10(f);
13863 else
13864 rettv->vval.v_float = 0.0;
13865}
13866#endif
13867
Bram Moolenaar1dced572012-04-05 16:54:08 +020013868#ifdef FEAT_LUA
13869/*
13870 * "luaeval()" function
13871 */
13872 static void
13873f_luaeval(argvars, rettv)
13874 typval_T *argvars;
13875 typval_T *rettv;
13876{
13877 char_u *str;
13878 char_u buf[NUMBUFLEN];
13879
13880 str = get_tv_string_buf(&argvars[0], buf);
13881 do_luaeval(str, argvars + 1, rettv);
13882}
13883#endif
13884
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 * "map()" function
13887 */
13888 static void
13889f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013890 typval_T *argvars;
13891 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892{
13893 filter_map(argvars, rettv, TRUE);
13894}
13895
13896/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013897 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 */
13899 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013900f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013901 typval_T *argvars;
13902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013904 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905}
13906
13907/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013908 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 */
13910 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013911f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013912 typval_T *argvars;
13913 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013915 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916}
13917
Bram Moolenaar33570922005-01-25 22:26:29 +000013918static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919
13920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013922 typval_T *argvars;
13923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 int type;
13925{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013926 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013927 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013928 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 char_u *pat;
13930 regmatch_T regmatch;
13931 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013932 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 char_u *save_cpo;
13934 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013935 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013936 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013937 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013938 list_T *l = NULL;
13939 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013940 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013941 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
13943 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13944 save_cpo = p_cpo;
13945 p_cpo = (char_u *)"";
13946
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013947 rettv->vval.v_number = -1;
13948 if (type == 3)
13949 {
13950 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013951 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013952 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013953 }
13954 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->v_type = VAR_STRING;
13957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013960 if (argvars[0].v_type == VAR_LIST)
13961 {
13962 if ((l = argvars[0].vval.v_list) == NULL)
13963 goto theend;
13964 li = l->lv_first;
13965 }
13966 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013967 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013968 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013969 len = (long)STRLEN(str);
13970 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013971
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13973 if (pat == NULL)
13974 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013975
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013976 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 int error = FALSE;
13979
13980 start = get_tv_number_chk(&argvars[2], &error);
13981 if (error)
13982 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013983 if (l != NULL)
13984 {
13985 li = list_find(l, start);
13986 if (li == NULL)
13987 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013988 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013989 }
13990 else
13991 {
13992 if (start < 0)
13993 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010013994 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013995 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013996 /* When "count" argument is there ignore matches before "start",
13997 * otherwise skip part of the string. Differs when pattern is "^"
13998 * or "\<". */
13999 if (argvars[3].v_type != VAR_UNKNOWN)
14000 startcol = start;
14001 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014002 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014003 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014004 len -= start;
14005 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014006 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014007
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014008 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 nth = get_tv_number_chk(&argvars[3], &error);
14010 if (error)
14011 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 }
14013
14014 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14015 if (regmatch.regprog != NULL)
14016 {
14017 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014018
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014019 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014020 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014021 if (l != NULL)
14022 {
14023 if (li == NULL)
14024 {
14025 match = FALSE;
14026 break;
14027 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014028 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014029 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014030 if (str == NULL)
14031 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014032 }
14033
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014034 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014035
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014036 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014037 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014038 if (l == NULL && !match)
14039 break;
14040
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014041 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014042 if (l != NULL)
14043 {
14044 li = li->li_next;
14045 ++idx;
14046 }
14047 else
14048 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014049#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014050 startcol = (colnr_T)(regmatch.startp[0]
14051 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014052#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014053 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014054#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014055 if (startcol > (colnr_T)len
14056 || str + startcol <= regmatch.startp[0])
14057 {
14058 match = FALSE;
14059 break;
14060 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014061 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014062 }
14063
14064 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014066 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014067 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014068 int i;
14069
14070 /* return list with matched string and submatches */
14071 for (i = 0; i < NSUBEXP; ++i)
14072 {
14073 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014074 {
14075 if (list_append_string(rettv->vval.v_list,
14076 (char_u *)"", 0) == FAIL)
14077 break;
14078 }
14079 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014080 regmatch.startp[i],
14081 (int)(regmatch.endp[i] - regmatch.startp[i]))
14082 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014083 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014084 }
14085 }
14086 else if (type == 2)
14087 {
14088 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014089 if (l != NULL)
14090 copy_tv(&li->li_tv, rettv);
14091 else
14092 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014094 }
14095 else if (l != NULL)
14096 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 else
14098 {
14099 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 (varnumber_T)(regmatch.startp[0] - str);
14102 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014103 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014105 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 }
14107 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014108 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 }
14110
14111theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014112 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 p_cpo = save_cpo;
14114}
14115
14116/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014117 * "match()" function
14118 */
14119 static void
14120f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014121 typval_T *argvars;
14122 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014123{
14124 find_some_match(argvars, rettv, 1);
14125}
14126
14127/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014128 * "matchadd()" function
14129 */
14130 static void
14131f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014132 typval_T *argvars UNUSED;
14133 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014134{
14135#ifdef FEAT_SEARCH_EXTRA
14136 char_u buf[NUMBUFLEN];
14137 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14138 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14139 int prio = 10; /* default priority */
14140 int id = -1;
14141 int error = FALSE;
14142
14143 rettv->vval.v_number = -1;
14144
14145 if (grp == NULL || pat == NULL)
14146 return;
14147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014148 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014149 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014150 if (argvars[3].v_type != VAR_UNKNOWN)
14151 id = get_tv_number_chk(&argvars[3], &error);
14152 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014153 if (error == TRUE)
14154 return;
14155 if (id >= 1 && id <= 3)
14156 {
14157 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14158 return;
14159 }
14160
14161 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14162#endif
14163}
14164
14165/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014166 * "matcharg()" function
14167 */
14168 static void
14169f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014170 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014171 typval_T *rettv;
14172{
14173 if (rettv_list_alloc(rettv) == OK)
14174 {
14175#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014176 int id = get_tv_number(&argvars[0]);
14177 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014178
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014179 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014180 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014181 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14182 {
14183 list_append_string(rettv->vval.v_list,
14184 syn_id2name(m->hlg_id), -1);
14185 list_append_string(rettv->vval.v_list, m->pattern, -1);
14186 }
14187 else
14188 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014189 list_append_string(rettv->vval.v_list, NULL, -1);
14190 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014191 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014192 }
14193#endif
14194 }
14195}
14196
14197/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014198 * "matchdelete()" function
14199 */
14200 static void
14201f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014202 typval_T *argvars UNUSED;
14203 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014204{
14205#ifdef FEAT_SEARCH_EXTRA
14206 rettv->vval.v_number = match_delete(curwin,
14207 (int)get_tv_number(&argvars[0]), TRUE);
14208#endif
14209}
14210
14211/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212 * "matchend()" function
14213 */
14214 static void
14215f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014216 typval_T *argvars;
14217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218{
14219 find_some_match(argvars, rettv, 0);
14220}
14221
14222/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014223 * "matchlist()" function
14224 */
14225 static void
14226f_matchlist(argvars, rettv)
14227 typval_T *argvars;
14228 typval_T *rettv;
14229{
14230 find_some_match(argvars, rettv, 3);
14231}
14232
14233/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014234 * "matchstr()" function
14235 */
14236 static void
14237f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 typval_T *argvars;
14239 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014240{
14241 find_some_match(argvars, rettv, 2);
14242}
14243
Bram Moolenaar33570922005-01-25 22:26:29 +000014244static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014245
14246 static void
14247max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014248 typval_T *argvars;
14249 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014250 int domax;
14251{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014252 long n = 0;
14253 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014255
14256 if (argvars[0].v_type == VAR_LIST)
14257 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014258 list_T *l;
14259 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014260
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014261 l = argvars[0].vval.v_list;
14262 if (l != NULL)
14263 {
14264 li = l->lv_first;
14265 if (li != NULL)
14266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014268 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014269 {
14270 li = li->li_next;
14271 if (li == NULL)
14272 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014274 if (domax ? i > n : i < n)
14275 n = i;
14276 }
14277 }
14278 }
14279 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014280 else if (argvars[0].v_type == VAR_DICT)
14281 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014282 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014283 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014284 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014285 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014286
14287 d = argvars[0].vval.v_dict;
14288 if (d != NULL)
14289 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014290 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014291 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014293 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014295 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014297 if (first)
14298 {
14299 n = i;
14300 first = FALSE;
14301 }
14302 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014303 n = i;
14304 }
14305 }
14306 }
14307 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014308 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014309 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014311}
14312
14313/*
14314 * "max()" function
14315 */
14316 static void
14317f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014318 typval_T *argvars;
14319 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014320{
14321 max_min(argvars, rettv, TRUE);
14322}
14323
14324/*
14325 * "min()" function
14326 */
14327 static void
14328f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014331{
14332 max_min(argvars, rettv, FALSE);
14333}
14334
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014335static int mkdir_recurse __ARGS((char_u *dir, int prot));
14336
14337/*
14338 * Create the directory in which "dir" is located, and higher levels when
14339 * needed.
14340 */
14341 static int
14342mkdir_recurse(dir, prot)
14343 char_u *dir;
14344 int prot;
14345{
14346 char_u *p;
14347 char_u *updir;
14348 int r = FAIL;
14349
14350 /* Get end of directory name in "dir".
14351 * We're done when it's "/" or "c:/". */
14352 p = gettail_sep(dir);
14353 if (p <= get_past_head(dir))
14354 return OK;
14355
14356 /* If the directory exists we're done. Otherwise: create it.*/
14357 updir = vim_strnsave(dir, (int)(p - dir));
14358 if (updir == NULL)
14359 return FAIL;
14360 if (mch_isdir(updir))
14361 r = OK;
14362 else if (mkdir_recurse(updir, prot) == OK)
14363 r = vim_mkdir_emsg(updir, prot);
14364 vim_free(updir);
14365 return r;
14366}
14367
14368#ifdef vim_mkdir
14369/*
14370 * "mkdir()" function
14371 */
14372 static void
14373f_mkdir(argvars, rettv)
14374 typval_T *argvars;
14375 typval_T *rettv;
14376{
14377 char_u *dir;
14378 char_u buf[NUMBUFLEN];
14379 int prot = 0755;
14380
14381 rettv->vval.v_number = FAIL;
14382 if (check_restricted() || check_secure())
14383 return;
14384
14385 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014386 if (*dir == NUL)
14387 rettv->vval.v_number = FAIL;
14388 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014389 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014390 if (*gettail(dir) == NUL)
14391 /* remove trailing slashes */
14392 *gettail_sep(dir) = NUL;
14393
14394 if (argvars[1].v_type != VAR_UNKNOWN)
14395 {
14396 if (argvars[2].v_type != VAR_UNKNOWN)
14397 prot = get_tv_number_chk(&argvars[2], NULL);
14398 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14399 mkdir_recurse(dir, prot);
14400 }
14401 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014402 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014403}
14404#endif
14405
Bram Moolenaar0d660222005-01-07 21:51:51 +000014406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 * "mode()" function
14408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014411 typval_T *argvars;
14412 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014414 char_u buf[3];
14415
14416 buf[1] = NUL;
14417 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 if (VIsual_active)
14420 {
14421 if (VIsual_select)
14422 buf[0] = VIsual_mode + 's' - 'v';
14423 else
14424 buf[0] = VIsual_mode;
14425 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014426 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014427 || State == CONFIRM)
14428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014430 if (State == ASKMORE)
14431 buf[1] = 'm';
14432 else if (State == CONFIRM)
14433 buf[1] = '?';
14434 }
14435 else if (State == EXTERNCMD)
14436 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 else if (State & INSERT)
14438 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014439#ifdef FEAT_VREPLACE
14440 if (State & VREPLACE_FLAG)
14441 {
14442 buf[0] = 'R';
14443 buf[1] = 'v';
14444 }
14445 else
14446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 if (State & REPLACE_FLAG)
14448 buf[0] = 'R';
14449 else
14450 buf[0] = 'i';
14451 }
14452 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014455 if (exmode_active)
14456 buf[1] = 'v';
14457 }
14458 else if (exmode_active)
14459 {
14460 buf[0] = 'c';
14461 buf[1] = 'e';
14462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014466 if (finish_op)
14467 buf[1] = 'o';
14468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014470 /* Clear out the minor mode when the argument is not a non-zero number or
14471 * non-empty string. */
14472 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014473 buf[1] = NUL;
14474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_string = vim_strsave(buf);
14476 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477}
14478
Bram Moolenaar429fa852013-04-15 12:27:36 +020014479#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014480/*
14481 * "mzeval()" function
14482 */
14483 static void
14484f_mzeval(argvars, rettv)
14485 typval_T *argvars;
14486 typval_T *rettv;
14487{
14488 char_u *str;
14489 char_u buf[NUMBUFLEN];
14490
14491 str = get_tv_string_buf(&argvars[0], buf);
14492 do_mzeval(str, rettv);
14493}
Bram Moolenaar75676462013-01-30 14:55:42 +010014494
14495 void
14496mzscheme_call_vim(name, args, rettv)
14497 char_u *name;
14498 typval_T *args;
14499 typval_T *rettv;
14500{
14501 typval_T argvars[3];
14502
14503 argvars[0].v_type = VAR_STRING;
14504 argvars[0].vval.v_string = name;
14505 copy_tv(args, &argvars[1]);
14506 argvars[2].v_type = VAR_UNKNOWN;
14507 f_call(argvars, rettv);
14508 clear_tv(&argvars[1]);
14509}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014510#endif
14511
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014513 * "nextnonblank()" function
14514 */
14515 static void
14516f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 typval_T *argvars;
14518 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014519{
14520 linenr_T lnum;
14521
14522 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 {
14526 lnum = 0;
14527 break;
14528 }
14529 if (*skipwhite(ml_get(lnum)) != NUL)
14530 break;
14531 }
14532 rettv->vval.v_number = lnum;
14533}
14534
14535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 * "nr2char()" function
14537 */
14538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014540 typval_T *argvars;
14541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542{
14543 char_u buf[NUMBUFLEN];
14544
14545#ifdef FEAT_MBYTE
14546 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014547 {
14548 int utf8 = 0;
14549
14550 if (argvars[1].v_type != VAR_UNKNOWN)
14551 utf8 = get_tv_number_chk(&argvars[1], NULL);
14552 if (utf8)
14553 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14554 else
14555 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 else
14558#endif
14559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 buf[1] = NUL;
14562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->v_type = VAR_STRING;
14564 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014565}
14566
14567/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014568 * "or(expr, expr)" function
14569 */
14570 static void
14571f_or(argvars, rettv)
14572 typval_T *argvars;
14573 typval_T *rettv;
14574{
14575 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14576 | get_tv_number_chk(&argvars[1], NULL);
14577}
14578
14579/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014580 * "pathshorten()" function
14581 */
14582 static void
14583f_pathshorten(argvars, rettv)
14584 typval_T *argvars;
14585 typval_T *rettv;
14586{
14587 char_u *p;
14588
14589 rettv->v_type = VAR_STRING;
14590 p = get_tv_string_chk(&argvars[0]);
14591 if (p == NULL)
14592 rettv->vval.v_string = NULL;
14593 else
14594 {
14595 p = vim_strsave(p);
14596 rettv->vval.v_string = p;
14597 if (p != NULL)
14598 shorten_dir(p);
14599 }
14600}
14601
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014602#ifdef FEAT_FLOAT
14603/*
14604 * "pow()" function
14605 */
14606 static void
14607f_pow(argvars, rettv)
14608 typval_T *argvars;
14609 typval_T *rettv;
14610{
14611 float_T fx, fy;
14612
14613 rettv->v_type = VAR_FLOAT;
14614 if (get_float_arg(argvars, &fx) == OK
14615 && get_float_arg(&argvars[1], &fy) == OK)
14616 rettv->vval.v_float = pow(fx, fy);
14617 else
14618 rettv->vval.v_float = 0.0;
14619}
14620#endif
14621
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014622/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014623 * "prevnonblank()" function
14624 */
14625 static void
14626f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 typval_T *argvars;
14628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014629{
14630 linenr_T lnum;
14631
14632 lnum = get_tv_lnum(argvars);
14633 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14634 lnum = 0;
14635 else
14636 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14637 --lnum;
14638 rettv->vval.v_number = lnum;
14639}
14640
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014641#ifdef HAVE_STDARG_H
14642/* This dummy va_list is here because:
14643 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14644 * - locally in the function results in a "used before set" warning
14645 * - using va_start() to initialize it gives "function with fixed args" error */
14646static va_list ap;
14647#endif
14648
Bram Moolenaar8c711452005-01-14 21:53:12 +000014649/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014650 * "printf()" function
14651 */
14652 static void
14653f_printf(argvars, rettv)
14654 typval_T *argvars;
14655 typval_T *rettv;
14656{
14657 rettv->v_type = VAR_STRING;
14658 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014659#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014660 {
14661 char_u buf[NUMBUFLEN];
14662 int len;
14663 char_u *s;
14664 int saved_did_emsg = did_emsg;
14665 char *fmt;
14666
14667 /* Get the required length, allocate the buffer and do it for real. */
14668 did_emsg = FALSE;
14669 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014670 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014671 if (!did_emsg)
14672 {
14673 s = alloc(len + 1);
14674 if (s != NULL)
14675 {
14676 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014677 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014678 }
14679 }
14680 did_emsg |= saved_did_emsg;
14681 }
14682#endif
14683}
14684
14685/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014686 * "pumvisible()" function
14687 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014688 static void
14689f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014690 typval_T *argvars UNUSED;
14691 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014692{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014693#ifdef FEAT_INS_EXPAND
14694 if (pum_visible())
14695 rettv->vval.v_number = 1;
14696#endif
14697}
14698
Bram Moolenaardb913952012-06-29 12:54:53 +020014699#ifdef FEAT_PYTHON3
14700/*
14701 * "py3eval()" function
14702 */
14703 static void
14704f_py3eval(argvars, rettv)
14705 typval_T *argvars;
14706 typval_T *rettv;
14707{
14708 char_u *str;
14709 char_u buf[NUMBUFLEN];
14710
14711 str = get_tv_string_buf(&argvars[0], buf);
14712 do_py3eval(str, rettv);
14713}
14714#endif
14715
14716#ifdef FEAT_PYTHON
14717/*
14718 * "pyeval()" function
14719 */
14720 static void
14721f_pyeval(argvars, rettv)
14722 typval_T *argvars;
14723 typval_T *rettv;
14724{
14725 char_u *str;
14726 char_u buf[NUMBUFLEN];
14727
14728 str = get_tv_string_buf(&argvars[0], buf);
14729 do_pyeval(str, rettv);
14730}
14731#endif
14732
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014733/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734 * "range()" function
14735 */
14736 static void
14737f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 typval_T *argvars;
14739 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014740{
14741 long start;
14742 long end;
14743 long stride = 1;
14744 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014745 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014747 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014748 if (argvars[1].v_type == VAR_UNKNOWN)
14749 {
14750 end = start - 1;
14751 start = 0;
14752 }
14753 else
14754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014755 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014757 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014758 }
14759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 if (error)
14761 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014763 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014764 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014765 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014766 else
14767 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014768 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014769 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014770 if (list_append_number(rettv->vval.v_list,
14771 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014772 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014773 }
14774}
14775
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014776/*
14777 * "readfile()" function
14778 */
14779 static void
14780f_readfile(argvars, rettv)
14781 typval_T *argvars;
14782 typval_T *rettv;
14783{
14784 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014785 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786 char_u *fname;
14787 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014788 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14789 int io_size = sizeof(buf);
14790 int readlen; /* size of last fread() */
14791 char_u *prev = NULL; /* previously read bytes, if any */
14792 long prevlen = 0; /* length of data in prev */
14793 long prevsize = 0; /* size of prev buffer */
14794 long maxline = MAXLNUM;
14795 long cnt = 0;
14796 char_u *p; /* position in buf */
14797 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014798
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014799 if (argvars[1].v_type != VAR_UNKNOWN)
14800 {
14801 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14802 binary = TRUE;
14803 if (argvars[2].v_type != VAR_UNKNOWN)
14804 maxline = get_tv_number(&argvars[2]);
14805 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014806
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014807 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014808 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014809
14810 /* Always open the file in binary mode, library functions have a mind of
14811 * their own about CR-LF conversion. */
14812 fname = get_tv_string(&argvars[0]);
14813 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14814 {
14815 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14816 return;
14817 }
14818
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014819 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014820 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014821 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014822
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 /* This for loop processes what was read, but is also entered at end
14824 * of file so that either:
14825 * - an incomplete line gets written
14826 * - a "binary" file gets an empty line at the end if it ends in a
14827 * newline. */
14828 for (p = buf, start = buf;
14829 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14830 ++p)
14831 {
14832 if (*p == '\n' || readlen <= 0)
14833 {
14834 listitem_T *li;
14835 char_u *s = NULL;
14836 long_u len = p - start;
14837
14838 /* Finished a line. Remove CRs before NL. */
14839 if (readlen > 0 && !binary)
14840 {
14841 while (len > 0 && start[len - 1] == '\r')
14842 --len;
14843 /* removal may cross back to the "prev" string */
14844 if (len == 0)
14845 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14846 --prevlen;
14847 }
14848 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014849 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014850 else
14851 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014852 /* Change "prev" buffer to be the right size. This way
14853 * the bytes are only copied once, and very long lines are
14854 * allocated only once. */
14855 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014856 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014857 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014858 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014859 prev = NULL; /* the list will own the string */
14860 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014861 }
14862 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014863 if (s == NULL)
14864 {
14865 do_outofmem_msg((long_u) prevlen + len + 1);
14866 failed = TRUE;
14867 break;
14868 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014869
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014870 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014871 {
14872 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014873 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014874 break;
14875 }
14876 li->li_tv.v_type = VAR_STRING;
14877 li->li_tv.v_lock = 0;
14878 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014879 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014880
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014881 start = p + 1; /* step over newline */
14882 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014883 break;
14884 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014885 else if (*p == NUL)
14886 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014887#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014888 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14889 * when finding the BF and check the previous two bytes. */
14890 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014891 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014892 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14893 * + 1, these may be in the "prev" string. */
14894 char_u back1 = p >= buf + 1 ? p[-1]
14895 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14896 char_u back2 = p >= buf + 2 ? p[-2]
14897 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14898 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014899
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014900 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014901 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014902 char_u *dest = p - 2;
14903
14904 /* Usually a BOM is at the beginning of a file, and so at
14905 * the beginning of a line; then we can just step over it.
14906 */
14907 if (start == dest)
14908 start = p + 1;
14909 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014910 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 /* have to shuffle buf to close gap */
14912 int adjust_prevlen = 0;
14913
14914 if (dest < buf)
14915 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014916 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014917 dest = buf;
14918 }
14919 if (readlen > p - buf + 1)
14920 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14921 readlen -= 3 - adjust_prevlen;
14922 prevlen -= adjust_prevlen;
14923 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014924 }
14925 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014926 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014927#endif
14928 } /* for */
14929
14930 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14931 break;
14932 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014933 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014934 /* There's part of a line in buf, store it in "prev". */
14935 if (p - start + prevlen >= prevsize)
14936 {
14937 /* need bigger "prev" buffer */
14938 char_u *newprev;
14939
14940 /* A common use case is ordinary text files and "prev" gets a
14941 * fragment of a line, so the first allocation is made
14942 * small, to avoid repeatedly 'allocing' large and
14943 * 'reallocing' small. */
14944 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014945 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014946 else
14947 {
14948 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014949 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014950 prevsize = grow50pc > growmin ? grow50pc : growmin;
14951 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014952 newprev = prev == NULL ? alloc(prevsize)
14953 : vim_realloc(prev, prevsize);
14954 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014955 {
14956 do_outofmem_msg((long_u)prevsize);
14957 failed = TRUE;
14958 break;
14959 }
14960 prev = newprev;
14961 }
14962 /* Add the line part to end of "prev". */
14963 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014964 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014965 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014966 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014967
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014968 /*
14969 * For a negative line count use only the lines at the end of the file,
14970 * free the rest.
14971 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014972 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014973 while (cnt > -maxline)
14974 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014975 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014976 --cnt;
14977 }
14978
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014979 if (failed)
14980 {
14981 list_free(rettv->vval.v_list, TRUE);
14982 /* readfile doc says an empty list is returned on error */
14983 rettv->vval.v_list = list_alloc();
14984 }
14985
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014986 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014987 fclose(fd);
14988}
14989
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014990#if defined(FEAT_RELTIME)
14991static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14992
14993/*
14994 * Convert a List to proftime_T.
14995 * Return FAIL when there is something wrong.
14996 */
14997 static int
14998list2proftime(arg, tm)
14999 typval_T *arg;
15000 proftime_T *tm;
15001{
15002 long n1, n2;
15003 int error = FALSE;
15004
15005 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15006 || arg->vval.v_list->lv_len != 2)
15007 return FAIL;
15008 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15009 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15010# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015011 tm->HighPart = n1;
15012 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015013# else
15014 tm->tv_sec = n1;
15015 tm->tv_usec = n2;
15016# endif
15017 return error ? FAIL : OK;
15018}
15019#endif /* FEAT_RELTIME */
15020
15021/*
15022 * "reltime()" function
15023 */
15024 static void
15025f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015026 typval_T *argvars UNUSED;
15027 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015028{
15029#ifdef FEAT_RELTIME
15030 proftime_T res;
15031 proftime_T start;
15032
15033 if (argvars[0].v_type == VAR_UNKNOWN)
15034 {
15035 /* No arguments: get current time. */
15036 profile_start(&res);
15037 }
15038 else if (argvars[1].v_type == VAR_UNKNOWN)
15039 {
15040 if (list2proftime(&argvars[0], &res) == FAIL)
15041 return;
15042 profile_end(&res);
15043 }
15044 else
15045 {
15046 /* Two arguments: compute the difference. */
15047 if (list2proftime(&argvars[0], &start) == FAIL
15048 || list2proftime(&argvars[1], &res) == FAIL)
15049 return;
15050 profile_sub(&res, &start);
15051 }
15052
15053 if (rettv_list_alloc(rettv) == OK)
15054 {
15055 long n1, n2;
15056
15057# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015058 n1 = res.HighPart;
15059 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015060# else
15061 n1 = res.tv_sec;
15062 n2 = res.tv_usec;
15063# endif
15064 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15065 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15066 }
15067#endif
15068}
15069
15070/*
15071 * "reltimestr()" function
15072 */
15073 static void
15074f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015075 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015076 typval_T *rettv;
15077{
15078#ifdef FEAT_RELTIME
15079 proftime_T tm;
15080#endif
15081
15082 rettv->v_type = VAR_STRING;
15083 rettv->vval.v_string = NULL;
15084#ifdef FEAT_RELTIME
15085 if (list2proftime(&argvars[0], &tm) == OK)
15086 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15087#endif
15088}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015089
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15091static void make_connection __ARGS((void));
15092static int check_connection __ARGS((void));
15093
15094 static void
15095make_connection()
15096{
15097 if (X_DISPLAY == NULL
15098# ifdef FEAT_GUI
15099 && !gui.in_use
15100# endif
15101 )
15102 {
15103 x_force_connect = TRUE;
15104 setup_term_clip();
15105 x_force_connect = FALSE;
15106 }
15107}
15108
15109 static int
15110check_connection()
15111{
15112 make_connection();
15113 if (X_DISPLAY == NULL)
15114 {
15115 EMSG(_("E240: No connection to Vim server"));
15116 return FAIL;
15117 }
15118 return OK;
15119}
15120#endif
15121
15122#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015123static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124
15125 static void
15126remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 typval_T *argvars;
15128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129 int expr;
15130{
15131 char_u *server_name;
15132 char_u *keys;
15133 char_u *r = NULL;
15134 char_u buf[NUMBUFLEN];
15135# ifdef WIN32
15136 HWND w;
15137# else
15138 Window w;
15139# endif
15140
15141 if (check_restricted() || check_secure())
15142 return;
15143
15144# ifdef FEAT_X11
15145 if (check_connection() == FAIL)
15146 return;
15147# endif
15148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015149 server_name = get_tv_string_chk(&argvars[0]);
15150 if (server_name == NULL)
15151 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 keys = get_tv_string_buf(&argvars[1], buf);
15153# ifdef WIN32
15154 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15155# else
15156 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15157 < 0)
15158# endif
15159 {
15160 if (r != NULL)
15161 EMSG(r); /* sending worked but evaluation failed */
15162 else
15163 EMSG2(_("E241: Unable to send to %s"), server_name);
15164 return;
15165 }
15166
15167 rettv->vval.v_string = r;
15168
15169 if (argvars[2].v_type != VAR_UNKNOWN)
15170 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015171 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015172 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015173 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015175 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 v.di_tv.v_type = VAR_STRING;
15177 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 idvar = get_tv_string_chk(&argvars[2]);
15179 if (idvar != NULL)
15180 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015182 }
15183}
15184#endif
15185
15186/*
15187 * "remote_expr()" function
15188 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189 static void
15190f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193{
15194 rettv->v_type = VAR_STRING;
15195 rettv->vval.v_string = NULL;
15196#ifdef FEAT_CLIENTSERVER
15197 remote_common(argvars, rettv, TRUE);
15198#endif
15199}
15200
15201/*
15202 * "remote_foreground()" function
15203 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204 static void
15205f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015206 typval_T *argvars UNUSED;
15207 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015208{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209#ifdef FEAT_CLIENTSERVER
15210# ifdef WIN32
15211 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 {
15213 char_u *server_name = get_tv_string_chk(&argvars[0]);
15214
15215 if (server_name != NULL)
15216 serverForeground(server_name);
15217 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015218# else
15219 /* Send a foreground() expression to the server. */
15220 argvars[1].v_type = VAR_STRING;
15221 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15222 argvars[2].v_type = VAR_UNKNOWN;
15223 remote_common(argvars, rettv, TRUE);
15224 vim_free(argvars[1].vval.v_string);
15225# endif
15226#endif
15227}
15228
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229 static void
15230f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015231 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015232 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233{
15234#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 char_u *s = NULL;
15237# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015238 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015240 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241
15242 if (check_restricted() || check_secure())
15243 {
15244 rettv->vval.v_number = -1;
15245 return;
15246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 serverid = get_tv_string_chk(&argvars[0]);
15248 if (serverid == NULL)
15249 {
15250 rettv->vval.v_number = -1;
15251 return; /* type error; errmsg already given */
15252 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015254 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015255 if (n == 0)
15256 rettv->vval.v_number = -1;
15257 else
15258 {
15259 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15260 rettv->vval.v_number = (s != NULL);
15261 }
15262# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 if (check_connection() == FAIL)
15264 return;
15265
15266 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015267 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268# endif
15269
15270 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 char_u *retvar;
15273
Bram Moolenaar33570922005-01-25 22:26:29 +000015274 v.di_tv.v_type = VAR_STRING;
15275 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015276 retvar = get_tv_string_chk(&argvars[1]);
15277 if (retvar != NULL)
15278 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015279 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280 }
15281#else
15282 rettv->vval.v_number = -1;
15283#endif
15284}
15285
Bram Moolenaar0d660222005-01-07 21:51:51 +000015286 static void
15287f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015288 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015289 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290{
15291 char_u *r = NULL;
15292
15293#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015294 char_u *serverid = get_tv_string_chk(&argvars[0]);
15295
15296 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015297 {
15298# ifdef WIN32
15299 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015300 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015301
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015302 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015303 if (n != 0)
15304 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15305 if (r == NULL)
15306# else
15307 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015308 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015309# endif
15310 EMSG(_("E277: Unable to read a server reply"));
15311 }
15312#endif
15313 rettv->v_type = VAR_STRING;
15314 rettv->vval.v_string = r;
15315}
15316
15317/*
15318 * "remote_send()" function
15319 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320 static void
15321f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015322 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015323 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015324{
15325 rettv->v_type = VAR_STRING;
15326 rettv->vval.v_string = NULL;
15327#ifdef FEAT_CLIENTSERVER
15328 remote_common(argvars, rettv, FALSE);
15329#endif
15330}
15331
15332/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015334 */
15335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015337 typval_T *argvars;
15338 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015339{
Bram Moolenaar33570922005-01-25 22:26:29 +000015340 list_T *l;
15341 listitem_T *item, *item2;
15342 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015343 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015345 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015346 dict_T *d;
15347 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015348 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015349
Bram Moolenaar8c711452005-01-14 21:53:12 +000015350 if (argvars[0].v_type == VAR_DICT)
15351 {
15352 if (argvars[2].v_type != VAR_UNKNOWN)
15353 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015354 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015355 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015357 key = get_tv_string_chk(&argvars[1]);
15358 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 di = dict_find(d, key, -1);
15361 if (di == NULL)
15362 EMSG2(_(e_dictkey), key);
15363 else
15364 {
15365 *rettv = di->di_tv;
15366 init_tv(&di->di_tv);
15367 dictitem_remove(d, di);
15368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015369 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015370 }
15371 }
15372 else if (argvars[0].v_type != VAR_LIST)
15373 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015374 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015375 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015377 int error = FALSE;
15378
15379 idx = get_tv_number_chk(&argvars[1], &error);
15380 if (error)
15381 ; /* type error: do nothing, errmsg already given */
15382 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015383 EMSGN(_(e_listidx), idx);
15384 else
15385 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015386 if (argvars[2].v_type == VAR_UNKNOWN)
15387 {
15388 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015389 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015390 *rettv = item->li_tv;
15391 vim_free(item);
15392 }
15393 else
15394 {
15395 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015396 end = get_tv_number_chk(&argvars[2], &error);
15397 if (error)
15398 ; /* type error: do nothing */
15399 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015400 EMSGN(_(e_listidx), end);
15401 else
15402 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015403 int cnt = 0;
15404
15405 for (li = item; li != NULL; li = li->li_next)
15406 {
15407 ++cnt;
15408 if (li == item2)
15409 break;
15410 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015411 if (li == NULL) /* didn't find "item2" after "item" */
15412 EMSG(_(e_invrange));
15413 else
15414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015415 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015416 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015417 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015418 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015419 l->lv_first = item;
15420 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015421 item->li_prev = NULL;
15422 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015423 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015424 }
15425 }
15426 }
15427 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015428 }
15429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
15432/*
15433 * "rename({from}, {to})" function
15434 */
15435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015437 typval_T *argvars;
15438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439{
15440 char_u buf[NUMBUFLEN];
15441
15442 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15446 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447}
15448
15449/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015450 * "repeat()" function
15451 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015452 static void
15453f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015454 typval_T *argvars;
15455 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015456{
15457 char_u *p;
15458 int n;
15459 int slen;
15460 int len;
15461 char_u *r;
15462 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015463
15464 n = get_tv_number(&argvars[1]);
15465 if (argvars[0].v_type == VAR_LIST)
15466 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015467 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015468 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015469 if (list_extend(rettv->vval.v_list,
15470 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015471 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015472 }
15473 else
15474 {
15475 p = get_tv_string(&argvars[0]);
15476 rettv->v_type = VAR_STRING;
15477 rettv->vval.v_string = NULL;
15478
15479 slen = (int)STRLEN(p);
15480 len = slen * n;
15481 if (len <= 0)
15482 return;
15483
15484 r = alloc(len + 1);
15485 if (r != NULL)
15486 {
15487 for (i = 0; i < n; i++)
15488 mch_memmove(r + i * slen, p, (size_t)slen);
15489 r[len] = NUL;
15490 }
15491
15492 rettv->vval.v_string = r;
15493 }
15494}
15495
15496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 * "resolve()" function
15498 */
15499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015500f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015501 typval_T *argvars;
15502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503{
15504 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015505#ifdef HAVE_READLINK
15506 char_u *buf = NULL;
15507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015509 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510#ifdef FEAT_SHORTCUT
15511 {
15512 char_u *v = NULL;
15513
15514 v = mch_resolve_shortcut(p);
15515 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015516 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 }
15520#else
15521# ifdef HAVE_READLINK
15522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 char_u *cpy;
15524 int len;
15525 char_u *remain = NULL;
15526 char_u *q;
15527 int is_relative_to_current = FALSE;
15528 int has_trailing_pathsep = FALSE;
15529 int limit = 100;
15530
15531 p = vim_strsave(p);
15532
15533 if (p[0] == '.' && (vim_ispathsep(p[1])
15534 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15535 is_relative_to_current = TRUE;
15536
15537 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015538 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015541 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543
15544 q = getnextcomp(p);
15545 if (*q != NUL)
15546 {
15547 /* Separate the first path component in "p", and keep the
15548 * remainder (beginning with the path separator). */
15549 remain = vim_strsave(q - 1);
15550 q[-1] = NUL;
15551 }
15552
Bram Moolenaard9462e32011-04-11 21:35:11 +020015553 buf = alloc(MAXPATHL + 1);
15554 if (buf == NULL)
15555 goto fail;
15556
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 for (;;)
15558 {
15559 for (;;)
15560 {
15561 len = readlink((char *)p, (char *)buf, MAXPATHL);
15562 if (len <= 0)
15563 break;
15564 buf[len] = NUL;
15565
15566 if (limit-- == 0)
15567 {
15568 vim_free(p);
15569 vim_free(remain);
15570 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 goto fail;
15573 }
15574
15575 /* Ensure that the result will have a trailing path separator
15576 * if the argument has one. */
15577 if (remain == NULL && has_trailing_pathsep)
15578 add_pathsep(buf);
15579
15580 /* Separate the first path component in the link value and
15581 * concatenate the remainders. */
15582 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15583 if (*q != NUL)
15584 {
15585 if (remain == NULL)
15586 remain = vim_strsave(q - 1);
15587 else
15588 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015589 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 if (cpy != NULL)
15591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 vim_free(remain);
15593 remain = cpy;
15594 }
15595 }
15596 q[-1] = NUL;
15597 }
15598
15599 q = gettail(p);
15600 if (q > p && *q == NUL)
15601 {
15602 /* Ignore trailing path separator. */
15603 q[-1] = NUL;
15604 q = gettail(p);
15605 }
15606 if (q > p && !mch_isFullName(buf))
15607 {
15608 /* symlink is relative to directory of argument */
15609 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15610 if (cpy != NULL)
15611 {
15612 STRCPY(cpy, p);
15613 STRCPY(gettail(cpy), buf);
15614 vim_free(p);
15615 p = cpy;
15616 }
15617 }
15618 else
15619 {
15620 vim_free(p);
15621 p = vim_strsave(buf);
15622 }
15623 }
15624
15625 if (remain == NULL)
15626 break;
15627
15628 /* Append the first path component of "remain" to "p". */
15629 q = getnextcomp(remain + 1);
15630 len = q - remain - (*q != NUL);
15631 cpy = vim_strnsave(p, STRLEN(p) + len);
15632 if (cpy != NULL)
15633 {
15634 STRNCAT(cpy, remain, len);
15635 vim_free(p);
15636 p = cpy;
15637 }
15638 /* Shorten "remain". */
15639 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015640 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 else
15642 {
15643 vim_free(remain);
15644 remain = NULL;
15645 }
15646 }
15647
15648 /* If the result is a relative path name, make it explicitly relative to
15649 * the current directory if and only if the argument had this form. */
15650 if (!vim_ispathsep(*p))
15651 {
15652 if (is_relative_to_current
15653 && *p != NUL
15654 && !(p[0] == '.'
15655 && (p[1] == NUL
15656 || vim_ispathsep(p[1])
15657 || (p[1] == '.'
15658 && (p[2] == NUL
15659 || vim_ispathsep(p[2]))))))
15660 {
15661 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015662 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 if (cpy != NULL)
15664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 vim_free(p);
15666 p = cpy;
15667 }
15668 }
15669 else if (!is_relative_to_current)
15670 {
15671 /* Strip leading "./". */
15672 q = p;
15673 while (q[0] == '.' && vim_ispathsep(q[1]))
15674 q += 2;
15675 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015676 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 }
15678 }
15679
15680 /* Ensure that the result will have no trailing path separator
15681 * if the argument had none. But keep "/" or "//". */
15682 if (!has_trailing_pathsep)
15683 {
15684 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015685 if (after_pathsep(p, q))
15686 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 }
15688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015689 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 }
15691# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693# endif
15694#endif
15695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015696 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697
15698#ifdef HAVE_READLINK
15699fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015700 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015702 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703}
15704
15705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 */
15708 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015709f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 typval_T *argvars;
15711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712{
Bram Moolenaar33570922005-01-25 22:26:29 +000015713 list_T *l;
15714 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716 if (argvars[0].v_type != VAR_LIST)
15717 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015718 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015719 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015720 {
15721 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015722 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015723 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 while (li != NULL)
15725 {
15726 ni = li->li_prev;
15727 list_append(l, li);
15728 li = ni;
15729 }
15730 rettv->vval.v_list = l;
15731 rettv->v_type = VAR_LIST;
15732 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015733 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735}
15736
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015737#define SP_NOMOVE 0x01 /* don't move cursor */
15738#define SP_REPEAT 0x02 /* repeat to find outer pair */
15739#define SP_RETCOUNT 0x04 /* return matchcount */
15740#define SP_SETPCMARK 0x08 /* set previous context mark */
15741#define SP_START 0x10 /* accept match at start position */
15742#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15743#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015744
Bram Moolenaar33570922005-01-25 22:26:29 +000015745static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015746
15747/*
15748 * Get flags for a search function.
15749 * Possibly sets "p_ws".
15750 * Returns BACKWARD, FORWARD or zero (for an error).
15751 */
15752 static int
15753get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015754 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 int *flagsp;
15756{
15757 int dir = FORWARD;
15758 char_u *flags;
15759 char_u nbuf[NUMBUFLEN];
15760 int mask;
15761
15762 if (varp->v_type != VAR_UNKNOWN)
15763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 flags = get_tv_string_buf_chk(varp, nbuf);
15765 if (flags == NULL)
15766 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015767 while (*flags != NUL)
15768 {
15769 switch (*flags)
15770 {
15771 case 'b': dir = BACKWARD; break;
15772 case 'w': p_ws = TRUE; break;
15773 case 'W': p_ws = FALSE; break;
15774 default: mask = 0;
15775 if (flagsp != NULL)
15776 switch (*flags)
15777 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015778 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015779 case 'e': mask = SP_END; break;
15780 case 'm': mask = SP_RETCOUNT; break;
15781 case 'n': mask = SP_NOMOVE; break;
15782 case 'p': mask = SP_SUBPAT; break;
15783 case 'r': mask = SP_REPEAT; break;
15784 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015785 }
15786 if (mask == 0)
15787 {
15788 EMSG2(_(e_invarg2), flags);
15789 dir = 0;
15790 }
15791 else
15792 *flagsp |= mask;
15793 }
15794 if (dir == 0)
15795 break;
15796 ++flags;
15797 }
15798 }
15799 return dir;
15800}
15801
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015803 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015805 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015806search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015807 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015808 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015809 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015811 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 char_u *pat;
15813 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015814 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 int save_p_ws = p_ws;
15816 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015817 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015818 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015819 proftime_T tm;
15820#ifdef FEAT_RELTIME
15821 long time_limit = 0;
15822#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015823 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015824 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015826 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015827 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015828 if (dir == 0)
15829 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015830 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015831 if (flags & SP_START)
15832 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015833 if (flags & SP_END)
15834 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015835
Bram Moolenaar76929292008-01-06 19:07:36 +000015836 /* Optional arguments: line number to stop searching and timeout. */
15837 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015838 {
15839 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15840 if (lnum_stop < 0)
15841 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015842#ifdef FEAT_RELTIME
15843 if (argvars[3].v_type != VAR_UNKNOWN)
15844 {
15845 time_limit = get_tv_number_chk(&argvars[3], NULL);
15846 if (time_limit < 0)
15847 goto theend;
15848 }
15849#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015850 }
15851
Bram Moolenaar76929292008-01-06 19:07:36 +000015852#ifdef FEAT_RELTIME
15853 /* Set the time limit, if there is one. */
15854 profile_setlimit(time_limit, &tm);
15855#endif
15856
Bram Moolenaar231334e2005-07-25 20:46:57 +000015857 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015858 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015859 * Check to make sure only those flags are set.
15860 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15861 * flags cannot be set. Check for that condition also.
15862 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015863 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015864 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015865 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015866 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015867 goto theend;
15868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015870 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015871 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015872 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015873 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015875 if (flags & SP_SUBPAT)
15876 retval = subpatnum;
15877 else
15878 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015879 if (flags & SP_SETPCMARK)
15880 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015882 if (match_pos != NULL)
15883 {
15884 /* Store the match cursor position */
15885 match_pos->lnum = pos.lnum;
15886 match_pos->col = pos.col + 1;
15887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 /* "/$" will put the cursor after the end of the line, may need to
15889 * correct that here */
15890 check_cursor();
15891 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015892
15893 /* If 'n' flag is used: restore cursor position. */
15894 if (flags & SP_NOMOVE)
15895 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015896 else
15897 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015898theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015900
15901 return retval;
15902}
15903
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015904#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015905
15906/*
15907 * round() is not in C90, use ceil() or floor() instead.
15908 */
15909 float_T
15910vim_round(f)
15911 float_T f;
15912{
15913 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15914}
15915
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015916/*
15917 * "round({float})" function
15918 */
15919 static void
15920f_round(argvars, rettv)
15921 typval_T *argvars;
15922 typval_T *rettv;
15923{
15924 float_T f;
15925
15926 rettv->v_type = VAR_FLOAT;
15927 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015928 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015929 else
15930 rettv->vval.v_float = 0.0;
15931}
15932#endif
15933
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015934/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015935 * "screenattr()" function
15936 */
15937 static void
15938f_screenattr(argvars, rettv)
15939 typval_T *argvars UNUSED;
15940 typval_T *rettv;
15941{
15942 int row;
15943 int col;
15944 int c;
15945
15946 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15947 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15948 if (row < 0 || row >= screen_Rows
15949 || col < 0 || col >= screen_Columns)
15950 c = -1;
15951 else
15952 c = ScreenAttrs[LineOffset[row] + col];
15953 rettv->vval.v_number = c;
15954}
15955
15956/*
15957 * "screenchar()" function
15958 */
15959 static void
15960f_screenchar(argvars, rettv)
15961 typval_T *argvars UNUSED;
15962 typval_T *rettv;
15963{
15964 int row;
15965 int col;
15966 int off;
15967 int c;
15968
15969 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15970 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15971 if (row < 0 || row >= screen_Rows
15972 || col < 0 || col >= screen_Columns)
15973 c = -1;
15974 else
15975 {
15976 off = LineOffset[row] + col;
15977#ifdef FEAT_MBYTE
15978 if (enc_utf8 && ScreenLinesUC[off] != 0)
15979 c = ScreenLinesUC[off];
15980 else
15981#endif
15982 c = ScreenLines[off];
15983 }
15984 rettv->vval.v_number = c;
15985}
15986
15987/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015988 * "screencol()" function
15989 *
15990 * First column is 1 to be consistent with virtcol().
15991 */
15992 static void
15993f_screencol(argvars, rettv)
15994 typval_T *argvars UNUSED;
15995 typval_T *rettv;
15996{
15997 rettv->vval.v_number = screen_screencol() + 1;
15998}
15999
16000/*
16001 * "screenrow()" function
16002 */
16003 static void
16004f_screenrow(argvars, rettv)
16005 typval_T *argvars UNUSED;
16006 typval_T *rettv;
16007{
16008 rettv->vval.v_number = screen_screenrow() + 1;
16009}
16010
16011/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016012 * "search()" function
16013 */
16014 static void
16015f_search(argvars, rettv)
16016 typval_T *argvars;
16017 typval_T *rettv;
16018{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016019 int flags = 0;
16020
16021 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022}
16023
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016025 * "searchdecl()" function
16026 */
16027 static void
16028f_searchdecl(argvars, rettv)
16029 typval_T *argvars;
16030 typval_T *rettv;
16031{
16032 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016033 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016034 int error = FALSE;
16035 char_u *name;
16036
16037 rettv->vval.v_number = 1; /* default: FAIL */
16038
16039 name = get_tv_string_chk(&argvars[0]);
16040 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016041 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016042 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016043 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16044 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16045 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016046 if (!error && name != NULL)
16047 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016048 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016049}
16050
16051/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016052 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016053 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016054 static int
16055searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016056 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016057 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058{
16059 char_u *spat, *mpat, *epat;
16060 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 int dir;
16063 int flags = 0;
16064 char_u nbuf1[NUMBUFLEN];
16065 char_u nbuf2[NUMBUFLEN];
16066 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016067 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016068 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016069 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016072 spat = get_tv_string_chk(&argvars[0]);
16073 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16074 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16075 if (spat == NULL || mpat == NULL || epat == NULL)
16076 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 /* Handle the optional fourth argument: flags */
16079 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016080 if (dir == 0)
16081 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016082
16083 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016084 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16085 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016086 if ((flags & (SP_END | SP_SUBPAT)) != 0
16087 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016088 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016089 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016090 goto theend;
16091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016093 /* Using 'r' implies 'W', otherwise it doesn't work. */
16094 if (flags & SP_REPEAT)
16095 p_ws = FALSE;
16096
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016097 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016098 if (argvars[3].v_type == VAR_UNKNOWN
16099 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 skip = (char_u *)"";
16101 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016103 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016104 if (argvars[5].v_type != VAR_UNKNOWN)
16105 {
16106 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16107 if (lnum_stop < 0)
16108 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016109#ifdef FEAT_RELTIME
16110 if (argvars[6].v_type != VAR_UNKNOWN)
16111 {
16112 time_limit = get_tv_number_chk(&argvars[6], NULL);
16113 if (time_limit < 0)
16114 goto theend;
16115 }
16116#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016117 }
16118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016119 if (skip == NULL)
16120 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016122 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016123 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016124
16125theend:
16126 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127
16128 return retval;
16129}
16130
16131/*
16132 * "searchpair()" function
16133 */
16134 static void
16135f_searchpair(argvars, rettv)
16136 typval_T *argvars;
16137 typval_T *rettv;
16138{
16139 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16140}
16141
16142/*
16143 * "searchpairpos()" function
16144 */
16145 static void
16146f_searchpairpos(argvars, rettv)
16147 typval_T *argvars;
16148 typval_T *rettv;
16149{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016150 pos_T match_pos;
16151 int lnum = 0;
16152 int col = 0;
16153
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016154 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016155 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016156
16157 if (searchpair_cmn(argvars, &match_pos) > 0)
16158 {
16159 lnum = match_pos.lnum;
16160 col = match_pos.col;
16161 }
16162
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016163 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16164 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016165}
16166
16167/*
16168 * Search for a start/middle/end thing.
16169 * Used by searchpair(), see its documentation for the details.
16170 * Returns 0 or -1 for no match,
16171 */
16172 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016173do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16174 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016175 char_u *spat; /* start pattern */
16176 char_u *mpat; /* middle pattern */
16177 char_u *epat; /* end pattern */
16178 int dir; /* BACKWARD or FORWARD */
16179 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016180 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016181 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016182 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016183 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016184{
16185 char_u *save_cpo;
16186 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16187 long retval = 0;
16188 pos_T pos;
16189 pos_T firstpos;
16190 pos_T foundpos;
16191 pos_T save_cursor;
16192 pos_T save_pos;
16193 int n;
16194 int r;
16195 int nest = 1;
16196 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016197 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016198 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016199
16200 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16201 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016202 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016203
Bram Moolenaar76929292008-01-06 19:07:36 +000016204#ifdef FEAT_RELTIME
16205 /* Set the time limit, if there is one. */
16206 profile_setlimit(time_limit, &tm);
16207#endif
16208
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016209 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16210 * start/middle/end (pat3, for the top pair). */
16211 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16212 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16213 if (pat2 == NULL || pat3 == NULL)
16214 goto theend;
16215 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16216 if (*mpat == NUL)
16217 STRCPY(pat3, pat2);
16218 else
16219 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16220 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016221 if (flags & SP_START)
16222 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 save_cursor = curwin->w_cursor;
16225 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016226 clearpos(&firstpos);
16227 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228 pat = pat3;
16229 for (;;)
16230 {
16231 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016232 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16234 /* didn't find it or found the first match again: FAIL */
16235 break;
16236
16237 if (firstpos.lnum == 0)
16238 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016239 if (equalpos(pos, foundpos))
16240 {
16241 /* Found the same position again. Can happen with a pattern that
16242 * has "\zs" at the end and searching backwards. Advance one
16243 * character and try again. */
16244 if (dir == BACKWARD)
16245 decl(&pos);
16246 else
16247 incl(&pos);
16248 }
16249 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016251 /* clear the start flag to avoid getting stuck here */
16252 options &= ~SEARCH_START;
16253
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 /* If the skip pattern matches, ignore this match. */
16255 if (*skip != NUL)
16256 {
16257 save_pos = curwin->w_cursor;
16258 curwin->w_cursor = pos;
16259 r = eval_to_bool(skip, &err, NULL, FALSE);
16260 curwin->w_cursor = save_pos;
16261 if (err)
16262 {
16263 /* Evaluating {skip} caused an error, break here. */
16264 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016265 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 break;
16267 }
16268 if (r)
16269 continue;
16270 }
16271
16272 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16273 {
16274 /* Found end when searching backwards or start when searching
16275 * forward: nested pair. */
16276 ++nest;
16277 pat = pat2; /* nested, don't search for middle */
16278 }
16279 else
16280 {
16281 /* Found end when searching forward or start when searching
16282 * backward: end of (nested) pair; or found middle in outer pair. */
16283 if (--nest == 1)
16284 pat = pat3; /* outer level, search for middle */
16285 }
16286
16287 if (nest == 0)
16288 {
16289 /* Found the match: return matchcount or line number. */
16290 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016291 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016293 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016294 if (flags & SP_SETPCMARK)
16295 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 curwin->w_cursor = pos;
16297 if (!(flags & SP_REPEAT))
16298 break;
16299 nest = 1; /* search for next unmatched */
16300 }
16301 }
16302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016303 if (match_pos != NULL)
16304 {
16305 /* Store the match cursor position */
16306 match_pos->lnum = curwin->w_cursor.lnum;
16307 match_pos->col = curwin->w_cursor.col + 1;
16308 }
16309
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016311 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 curwin->w_cursor = save_cursor;
16313
16314theend:
16315 vim_free(pat2);
16316 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016317 if (p_cpo == empty_option)
16318 p_cpo = save_cpo;
16319 else
16320 /* Darn, evaluating the {skip} expression changed the value. */
16321 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016322
16323 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324}
16325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016326/*
16327 * "searchpos()" function
16328 */
16329 static void
16330f_searchpos(argvars, rettv)
16331 typval_T *argvars;
16332 typval_T *rettv;
16333{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016334 pos_T match_pos;
16335 int lnum = 0;
16336 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016337 int n;
16338 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016339
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016340 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016341 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016342
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016343 n = search_cmn(argvars, &match_pos, &flags);
16344 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016345 {
16346 lnum = match_pos.lnum;
16347 col = match_pos.col;
16348 }
16349
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016350 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16351 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016352 if (flags & SP_SUBPAT)
16353 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016354}
16355
16356
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 static void
16358f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016359 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362#ifdef FEAT_CLIENTSERVER
16363 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016364 char_u *server = get_tv_string_chk(&argvars[0]);
16365 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 if (server == NULL || reply == NULL)
16369 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370 if (check_restricted() || check_secure())
16371 return;
16372# ifdef FEAT_X11
16373 if (check_connection() == FAIL)
16374 return;
16375# endif
16376
16377 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379 EMSG(_("E258: Unable to send to client"));
16380 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 rettv->vval.v_number = 0;
16383#else
16384 rettv->vval.v_number = -1;
16385#endif
16386}
16387
Bram Moolenaar0d660222005-01-07 21:51:51 +000016388 static void
16389f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016391 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392{
16393 char_u *r = NULL;
16394
16395#ifdef FEAT_CLIENTSERVER
16396# ifdef WIN32
16397 r = serverGetVimNames();
16398# else
16399 make_connection();
16400 if (X_DISPLAY != NULL)
16401 r = serverGetVimNames(X_DISPLAY);
16402# endif
16403#endif
16404 rettv->v_type = VAR_STRING;
16405 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406}
16407
16408/*
16409 * "setbufvar()" function
16410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016413 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016414 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415{
16416 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016419 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 char_u nbuf[NUMBUFLEN];
16421
16422 if (check_restricted() || check_secure())
16423 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16425 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016426 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016427 varp = &argvars[2];
16428
16429 if (buf != NULL && varname != NULL && varp != NULL)
16430 {
16431 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433
16434 if (*varname == '&')
16435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016436 long numval;
16437 char_u *strval;
16438 int error = FALSE;
16439
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 numval = get_tv_number_chk(varp, &error);
16442 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016443 if (!error && strval != NULL)
16444 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 }
16446 else
16447 {
16448 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16449 if (bufvarname != NULL)
16450 {
16451 STRCPY(bufvarname, "b:");
16452 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016453 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 vim_free(bufvarname);
16455 }
16456 }
16457
16458 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016459 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461}
16462
16463/*
16464 * "setcmdpos()" function
16465 */
16466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016467f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 typval_T *argvars;
16469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016471 int pos = (int)get_tv_number(&argvars[0]) - 1;
16472
16473 if (pos >= 0)
16474 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475}
16476
16477/*
16478 * "setline()" function
16479 */
16480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016481f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016482 typval_T *argvars;
16483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016484{
16485 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016486 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016487 list_T *l = NULL;
16488 listitem_T *li = NULL;
16489 long added = 0;
16490 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016492 lnum = get_tv_lnum(&argvars[0]);
16493 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016494 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016495 l = argvars[1].vval.v_list;
16496 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016498 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016499 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016500
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016501 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016502 for (;;)
16503 {
16504 if (l != NULL)
16505 {
16506 /* list argument, get next string */
16507 if (li == NULL)
16508 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016509 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016510 li = li->li_next;
16511 }
16512
16513 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016514 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016515 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016516
16517 /* When coming here from Insert mode, sync undo, so that this can be
16518 * undone separately from what was previously inserted. */
16519 if (u_sync_once == 2)
16520 {
16521 u_sync_once = 1; /* notify that u_sync() was called */
16522 u_sync(TRUE);
16523 }
16524
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016525 if (lnum <= curbuf->b_ml.ml_line_count)
16526 {
16527 /* existing line, replace it */
16528 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16529 {
16530 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016531 if (lnum == curwin->w_cursor.lnum)
16532 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016533 rettv->vval.v_number = 0; /* OK */
16534 }
16535 }
16536 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16537 {
16538 /* lnum is one past the last line, append the line */
16539 ++added;
16540 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16541 rettv->vval.v_number = 0; /* OK */
16542 }
16543
16544 if (l == NULL) /* only one string argument */
16545 break;
16546 ++lnum;
16547 }
16548
16549 if (added > 0)
16550 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551}
16552
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016553static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16554
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016556 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016557 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016558 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016559set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016560 win_T *wp UNUSED;
16561 typval_T *list_arg UNUSED;
16562 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016563 typval_T *rettv;
16564{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016565#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016566 char_u *act;
16567 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016568#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016569
Bram Moolenaar2641f772005-03-25 21:58:17 +000016570 rettv->vval.v_number = -1;
16571
16572#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016573 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016574 EMSG(_(e_listreq));
16575 else
16576 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016577 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016578
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016579 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016580 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016581 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016582 if (act == NULL)
16583 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016584 if (*act == 'a' || *act == 'r')
16585 action = *act;
16586 }
16587
Bram Moolenaar81484f42012-12-05 15:16:47 +010016588 if (l != NULL && set_errorlist(wp, l, action,
16589 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016590 rettv->vval.v_number = 0;
16591 }
16592#endif
16593}
16594
16595/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016596 * "setloclist()" function
16597 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016598 static void
16599f_setloclist(argvars, rettv)
16600 typval_T *argvars;
16601 typval_T *rettv;
16602{
16603 win_T *win;
16604
16605 rettv->vval.v_number = -1;
16606
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016607 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016608 if (win != NULL)
16609 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16610}
16611
16612/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016613 * "setmatches()" function
16614 */
16615 static void
16616f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016617 typval_T *argvars UNUSED;
16618 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016619{
16620#ifdef FEAT_SEARCH_EXTRA
16621 list_T *l;
16622 listitem_T *li;
16623 dict_T *d;
16624
16625 rettv->vval.v_number = -1;
16626 if (argvars[0].v_type != VAR_LIST)
16627 {
16628 EMSG(_(e_listreq));
16629 return;
16630 }
16631 if ((l = argvars[0].vval.v_list) != NULL)
16632 {
16633
16634 /* To some extent make sure that we are dealing with a list from
16635 * "getmatches()". */
16636 li = l->lv_first;
16637 while (li != NULL)
16638 {
16639 if (li->li_tv.v_type != VAR_DICT
16640 || (d = li->li_tv.vval.v_dict) == NULL)
16641 {
16642 EMSG(_(e_invarg));
16643 return;
16644 }
16645 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16646 && dict_find(d, (char_u *)"pattern", -1) != NULL
16647 && dict_find(d, (char_u *)"priority", -1) != NULL
16648 && dict_find(d, (char_u *)"id", -1) != NULL))
16649 {
16650 EMSG(_(e_invarg));
16651 return;
16652 }
16653 li = li->li_next;
16654 }
16655
16656 clear_matches(curwin);
16657 li = l->lv_first;
16658 while (li != NULL)
16659 {
16660 d = li->li_tv.vval.v_dict;
16661 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16662 get_dict_string(d, (char_u *)"pattern", FALSE),
16663 (int)get_dict_number(d, (char_u *)"priority"),
16664 (int)get_dict_number(d, (char_u *)"id"));
16665 li = li->li_next;
16666 }
16667 rettv->vval.v_number = 0;
16668 }
16669#endif
16670}
16671
16672/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016673 * "setpos()" function
16674 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016675 static void
16676f_setpos(argvars, rettv)
16677 typval_T *argvars;
16678 typval_T *rettv;
16679{
16680 pos_T pos;
16681 int fnum;
16682 char_u *name;
16683
Bram Moolenaar08250432008-02-13 11:42:46 +000016684 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016685 name = get_tv_string_chk(argvars);
16686 if (name != NULL)
16687 {
16688 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16689 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016690 if (--pos.col < 0)
16691 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016692 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016693 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016694 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016695 if (fnum == curbuf->b_fnum)
16696 {
16697 curwin->w_cursor = pos;
16698 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016699 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016700 }
16701 else
16702 EMSG(_(e_invarg));
16703 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016704 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16705 {
16706 /* set mark */
16707 if (setmark_pos(name[1], &pos, fnum) == OK)
16708 rettv->vval.v_number = 0;
16709 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016710 else
16711 EMSG(_(e_invarg));
16712 }
16713 }
16714}
16715
16716/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016717 * "setqflist()" function
16718 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016719 static void
16720f_setqflist(argvars, rettv)
16721 typval_T *argvars;
16722 typval_T *rettv;
16723{
16724 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16725}
16726
16727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 * "setreg()" function
16729 */
16730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016731f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016732 typval_T *argvars;
16733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734{
16735 int regname;
16736 char_u *strregname;
16737 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016738 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 int append;
16740 char_u yank_type;
16741 long block_len;
16742
16743 block_len = -1;
16744 yank_type = MAUTO;
16745 append = FALSE;
16746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016747 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016748 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 if (strregname == NULL)
16751 return; /* type error; errmsg already given */
16752 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 if (regname == 0 || regname == '@')
16754 regname = '"';
16755 else if (regname == '=')
16756 return;
16757
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016758 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016760 stropt = get_tv_string_chk(&argvars[2]);
16761 if (stropt == NULL)
16762 return; /* type error */
16763 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764 switch (*stropt)
16765 {
16766 case 'a': case 'A': /* append */
16767 append = TRUE;
16768 break;
16769 case 'v': case 'c': /* character-wise selection */
16770 yank_type = MCHAR;
16771 break;
16772 case 'V': case 'l': /* line-wise selection */
16773 yank_type = MLINE;
16774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 case 'b': case Ctrl_V: /* block-wise selection */
16776 yank_type = MBLOCK;
16777 if (VIM_ISDIGIT(stropt[1]))
16778 {
16779 ++stropt;
16780 block_len = getdigits(&stropt) - 1;
16781 --stropt;
16782 }
16783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 }
16785 }
16786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 strval = get_tv_string_chk(&argvars[1]);
16788 if (strval != NULL)
16789 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792}
16793
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016794/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016795 * "settabvar()" function
16796 */
16797 static void
16798f_settabvar(argvars, rettv)
16799 typval_T *argvars;
16800 typval_T *rettv;
16801{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016802#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016803 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016804 tabpage_T *tp;
16805#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016806 char_u *varname, *tabvarname;
16807 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016808
16809 rettv->vval.v_number = 0;
16810
16811 if (check_restricted() || check_secure())
16812 return;
16813
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016814#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016815 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016816#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016817 varname = get_tv_string_chk(&argvars[1]);
16818 varp = &argvars[2];
16819
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016820 if (varname != NULL && varp != NULL
16821#ifdef FEAT_WINDOWS
16822 && tp != NULL
16823#endif
16824 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016825 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016826#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016827 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016828 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016829#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016830
16831 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16832 if (tabvarname != NULL)
16833 {
16834 STRCPY(tabvarname, "t:");
16835 STRCPY(tabvarname + 2, varname);
16836 set_var(tabvarname, varp, TRUE);
16837 vim_free(tabvarname);
16838 }
16839
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016840#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016841 /* Restore current tabpage */
16842 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016843 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016844#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016845 }
16846}
16847
16848/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016849 * "settabwinvar()" function
16850 */
16851 static void
16852f_settabwinvar(argvars, rettv)
16853 typval_T *argvars;
16854 typval_T *rettv;
16855{
16856 setwinvar(argvars, rettv, 1);
16857}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858
16859/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016860 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016864 typval_T *argvars;
16865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016867 setwinvar(argvars, rettv, 0);
16868}
16869
16870/*
16871 * "setwinvar()" and "settabwinvar()" functions
16872 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016873
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016874 static void
16875setwinvar(argvars, rettv, off)
16876 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016877 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016878 int off;
16879{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 win_T *win;
16881#ifdef FEAT_WINDOWS
16882 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016883 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884#endif
16885 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016886 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016888 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889
16890 if (check_restricted() || check_secure())
16891 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016892
16893#ifdef FEAT_WINDOWS
16894 if (off == 1)
16895 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16896 else
16897 tp = curtab;
16898#endif
16899 win = find_win_by_nr(&argvars[off], tp);
16900 varname = get_tv_string_chk(&argvars[off + 1]);
16901 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902
16903 if (win != NULL && varname != NULL && varp != NULL)
16904 {
16905#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016906 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016907 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908#endif
16909
16910 if (*varname == '&')
16911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912 long numval;
16913 char_u *strval;
16914 int error = FALSE;
16915
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 numval = get_tv_number_chk(varp, &error);
16918 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016919 if (!error && strval != NULL)
16920 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 }
16922 else
16923 {
16924 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16925 if (winvarname != NULL)
16926 {
16927 STRCPY(winvarname, "w:");
16928 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016929 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 vim_free(winvarname);
16931 }
16932 }
16933
16934#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016935 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936#endif
16937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938}
16939
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016940#ifdef FEAT_CRYPT
16941/*
16942 * "sha256({string})" function
16943 */
16944 static void
16945f_sha256(argvars, rettv)
16946 typval_T *argvars;
16947 typval_T *rettv;
16948{
16949 char_u *p;
16950
16951 p = get_tv_string(&argvars[0]);
16952 rettv->vval.v_string = vim_strsave(
16953 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16954 rettv->v_type = VAR_STRING;
16955}
16956#endif /* FEAT_CRYPT */
16957
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016959 * "shellescape({string})" function
16960 */
16961 static void
16962f_shellescape(argvars, rettv)
16963 typval_T *argvars;
16964 typval_T *rettv;
16965{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016966 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010016967 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016968 rettv->v_type = VAR_STRING;
16969}
16970
16971/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016972 * shiftwidth() function
16973 */
16974 static void
16975f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016976 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016977 typval_T *rettv;
16978{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016979 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016980}
16981
16982/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 */
16985 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016987 typval_T *argvars;
16988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 p = get_tv_string(&argvars[0]);
16993 rettv->vval.v_string = vim_strsave(p);
16994 simplify_filename(rettv->vval.v_string); /* simplify in place */
16995 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996}
16997
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016998#ifdef FEAT_FLOAT
16999/*
17000 * "sin()" function
17001 */
17002 static void
17003f_sin(argvars, rettv)
17004 typval_T *argvars;
17005 typval_T *rettv;
17006{
17007 float_T f;
17008
17009 rettv->v_type = VAR_FLOAT;
17010 if (get_float_arg(argvars, &f) == OK)
17011 rettv->vval.v_float = sin(f);
17012 else
17013 rettv->vval.v_float = 0.0;
17014}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017015
17016/*
17017 * "sinh()" function
17018 */
17019 static void
17020f_sinh(argvars, rettv)
17021 typval_T *argvars;
17022 typval_T *rettv;
17023{
17024 float_T f;
17025
17026 rettv->v_type = VAR_FLOAT;
17027 if (get_float_arg(argvars, &f) == OK)
17028 rettv->vval.v_float = sinh(f);
17029 else
17030 rettv->vval.v_float = 0.0;
17031}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017032#endif
17033
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034static int
17035#ifdef __BORLANDC__
17036 _RTLENTRYF
17037#endif
17038 item_compare __ARGS((const void *s1, const void *s2));
17039static int
17040#ifdef __BORLANDC__
17041 _RTLENTRYF
17042#endif
17043 item_compare2 __ARGS((const void *s1, const void *s2));
17044
17045static int item_compare_ic;
17046static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017047static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048static int item_compare_func_err;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017049static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050#define ITEM_COMPARE_FAIL 999
17051
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017053 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 static int
17056#ifdef __BORLANDC__
17057_RTLENTRYF
17058#endif
17059item_compare(s1, s2)
17060 const void *s1;
17061 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 char_u *p1, *p2;
17064 char_u *tofree1, *tofree2;
17065 int res;
17066 char_u numbuf1[NUMBUFLEN];
17067 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017069 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17070 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017071 if (p1 == NULL)
17072 p1 = (char_u *)"";
17073 if (p2 == NULL)
17074 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075 if (item_compare_ic)
17076 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 res = STRCMP(p1, p2);
17079 vim_free(tofree1);
17080 vim_free(tofree2);
17081 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082}
17083
17084 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085#ifdef __BORLANDC__
17086_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088item_compare2(s1, s2)
17089 const void *s1;
17090 const void *s2;
17091{
17092 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017093 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017094 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 /* shortcut after failure in previous call; compare all items equal */
17098 if (item_compare_func_err)
17099 return 0;
17100
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017101 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17102 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017103 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17104 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105
17106 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017107 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017108 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17109 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 clear_tv(&argv[0]);
17111 clear_tv(&argv[1]);
17112
17113 if (res == FAIL)
17114 res = ITEM_COMPARE_FAIL;
17115 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017116 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17117 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017118 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119 clear_tv(&rettv);
17120 return res;
17121}
17122
17123/*
17124 * "sort({list})" function
17125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017127do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017128 typval_T *argvars;
17129 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017130 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131{
Bram Moolenaar33570922005-01-25 22:26:29 +000017132 list_T *l;
17133 listitem_T *li;
17134 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 long len;
17136 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017139 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 else
17141 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017142 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017143 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017144 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 return;
17146 rettv->vval.v_list = l;
17147 rettv->v_type = VAR_LIST;
17148 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 len = list_len(l);
17151 if (len <= 1)
17152 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154 item_compare_ic = FALSE;
17155 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017156 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 if (argvars[1].v_type != VAR_UNKNOWN)
17158 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017159 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017161 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 else
17163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017164 int error = FALSE;
17165
17166 i = get_tv_number_chk(&argvars[1], &error);
17167 if (error)
17168 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169 if (i == 1)
17170 item_compare_ic = TRUE;
17171 else
17172 item_compare_func = get_tv_string(&argvars[1]);
17173 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017174
17175 if (argvars[2].v_type != VAR_UNKNOWN)
17176 {
17177 /* optional third argument: {dict} */
17178 if (argvars[2].v_type != VAR_DICT)
17179 {
17180 EMSG(_(e_dictreq));
17181 return;
17182 }
17183 item_compare_selfdict = argvars[2].vval.v_dict;
17184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017188 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 if (ptrs == NULL)
17190 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
Bram Moolenaar327aa022014-03-25 18:24:23 +010017192 i = 0;
17193 if (sort)
17194 {
17195 /* sort(): ptrs will be the list to sort */
17196 for (li = l->lv_first; li != NULL; li = li->li_next)
17197 ptrs[i++] = li;
17198
17199 item_compare_func_err = FALSE;
17200 /* test the compare function */
17201 if (item_compare_func != NULL
17202 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017203 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017204 EMSG(_("E702: Sort compare function failed"));
17205 else
17206 {
17207 /* Sort the array with item pointers. */
17208 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
17209 item_compare_func == NULL ? item_compare : item_compare2);
17210
17211 if (!item_compare_func_err)
17212 {
17213 /* Clear the List and append the items in sorted order. */
17214 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17215 l->lv_len = 0;
17216 for (i = 0; i < len; ++i)
17217 list_append(l, ptrs[i]);
17218 }
17219 }
17220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017223 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17224
17225 /* f_uniq(): ptrs will be a stack of items to remove */
17226 item_compare_func_err = FALSE;
17227 item_compare_func_ptr = item_compare_func
17228 ? item_compare2 : item_compare;
17229
17230 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17231 li = li->li_next)
17232 {
17233 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17234 == 0)
17235 ptrs[i++] = li;
17236 if (item_compare_func_err)
17237 {
17238 EMSG(_("E882: Uniq compare function failed"));
17239 break;
17240 }
17241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017243 if (!item_compare_func_err)
17244 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017245 while (--i >= 0)
17246 {
17247 li = ptrs[i]->li_next;
17248 ptrs[i]->li_next = li->li_next;
17249 if (li->li_next != NULL)
17250 li->li_next->li_prev = ptrs[i];
17251 else
17252 l->lv_last = ptrs[i];
17253 list_fix_watch(l, li);
17254 listitem_free(li);
17255 l->lv_len--;
17256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258 }
17259
17260 vim_free(ptrs);
17261 }
17262}
17263
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017264/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017265 * "sort({list})" function
17266 */
17267 static void
17268f_sort(argvars, rettv)
17269 typval_T *argvars;
17270 typval_T *rettv;
17271{
17272 do_sort_uniq(argvars, rettv, TRUE);
17273}
17274
17275/*
17276 * "uniq({list})" function
17277 */
17278 static void
17279f_uniq(argvars, rettv)
17280 typval_T *argvars;
17281 typval_T *rettv;
17282{
17283 do_sort_uniq(argvars, rettv, FALSE);
17284}
17285
17286/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017287 * "soundfold({word})" function
17288 */
17289 static void
17290f_soundfold(argvars, rettv)
17291 typval_T *argvars;
17292 typval_T *rettv;
17293{
17294 char_u *s;
17295
17296 rettv->v_type = VAR_STRING;
17297 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017298#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017299 rettv->vval.v_string = eval_soundfold(s);
17300#else
17301 rettv->vval.v_string = vim_strsave(s);
17302#endif
17303}
17304
17305/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017306 * "spellbadword()" function
17307 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017308 static void
17309f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017310 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017311 typval_T *rettv;
17312{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017313 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017314 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017315 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017317 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017318 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017319
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017320#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017321 if (argvars[0].v_type == VAR_UNKNOWN)
17322 {
17323 /* Find the start and length of the badly spelled word. */
17324 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17325 if (len != 0)
17326 word = ml_get_cursor();
17327 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017328 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017329 {
17330 char_u *str = get_tv_string_chk(&argvars[0]);
17331 int capcol = -1;
17332
17333 if (str != NULL)
17334 {
17335 /* Check the argument for spelling. */
17336 while (*str != NUL)
17337 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017338 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017339 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017340 {
17341 word = str;
17342 break;
17343 }
17344 str += len;
17345 }
17346 }
17347 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017348#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017349
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017350 list_append_string(rettv->vval.v_list, word, len);
17351 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017352 attr == HLF_SPB ? "bad" :
17353 attr == HLF_SPR ? "rare" :
17354 attr == HLF_SPL ? "local" :
17355 attr == HLF_SPC ? "caps" :
17356 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017357}
17358
17359/*
17360 * "spellsuggest()" function
17361 */
17362 static void
17363f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017364 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017365 typval_T *rettv;
17366{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017367#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017368 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017369 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017370 int maxcount;
17371 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017372 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017373 listitem_T *li;
17374 int need_capital = FALSE;
17375#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017376
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017377 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017378 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017379
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017380#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017381 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017382 {
17383 str = get_tv_string(&argvars[0]);
17384 if (argvars[1].v_type != VAR_UNKNOWN)
17385 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017386 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017387 if (maxcount <= 0)
17388 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017389 if (argvars[2].v_type != VAR_UNKNOWN)
17390 {
17391 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17392 if (typeerr)
17393 return;
17394 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017395 }
17396 else
17397 maxcount = 25;
17398
Bram Moolenaar4770d092006-01-12 23:22:24 +000017399 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017400
17401 for (i = 0; i < ga.ga_len; ++i)
17402 {
17403 str = ((char_u **)ga.ga_data)[i];
17404
17405 li = listitem_alloc();
17406 if (li == NULL)
17407 vim_free(str);
17408 else
17409 {
17410 li->li_tv.v_type = VAR_STRING;
17411 li->li_tv.v_lock = 0;
17412 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017413 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017414 }
17415 }
17416 ga_clear(&ga);
17417 }
17418#endif
17419}
17420
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017422f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017423 typval_T *argvars;
17424 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425{
17426 char_u *str;
17427 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017428 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017429 regmatch_T regmatch;
17430 char_u patbuf[NUMBUFLEN];
17431 char_u *save_cpo;
17432 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017433 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017434 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017435 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017436
17437 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17438 save_cpo = p_cpo;
17439 p_cpo = (char_u *)"";
17440
17441 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017442 if (argvars[1].v_type != VAR_UNKNOWN)
17443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017444 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17445 if (pat == NULL)
17446 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017448 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017449 }
17450 if (pat == NULL || *pat == NUL)
17451 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017452
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017453 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017455 if (typeerr)
17456 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457
Bram Moolenaar0d660222005-01-07 21:51:51 +000017458 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17459 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017461 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017462 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017464 if (*str == NUL)
17465 match = FALSE; /* empty item at the end */
17466 else
17467 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017468 if (match)
17469 end = regmatch.startp[0];
17470 else
17471 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017472 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17473 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017474 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017475 if (list_append_string(rettv->vval.v_list, str,
17476 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017477 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017478 }
17479 if (!match)
17480 break;
17481 /* Advance to just after the match. */
17482 if (regmatch.endp[0] > str)
17483 col = 0;
17484 else
17485 {
17486 /* Don't get stuck at the same match. */
17487#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017488 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017489#else
17490 col = 1;
17491#endif
17492 }
17493 str = regmatch.endp[0];
17494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495
Bram Moolenaar473de612013-06-08 18:19:48 +020017496 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498
Bram Moolenaar0d660222005-01-07 21:51:51 +000017499 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500}
17501
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017502#ifdef FEAT_FLOAT
17503/*
17504 * "sqrt()" function
17505 */
17506 static void
17507f_sqrt(argvars, rettv)
17508 typval_T *argvars;
17509 typval_T *rettv;
17510{
17511 float_T f;
17512
17513 rettv->v_type = VAR_FLOAT;
17514 if (get_float_arg(argvars, &f) == OK)
17515 rettv->vval.v_float = sqrt(f);
17516 else
17517 rettv->vval.v_float = 0.0;
17518}
17519
17520/*
17521 * "str2float()" function
17522 */
17523 static void
17524f_str2float(argvars, rettv)
17525 typval_T *argvars;
17526 typval_T *rettv;
17527{
17528 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17529
17530 if (*p == '+')
17531 p = skipwhite(p + 1);
17532 (void)string2float(p, &rettv->vval.v_float);
17533 rettv->v_type = VAR_FLOAT;
17534}
17535#endif
17536
Bram Moolenaar2c932302006-03-18 21:42:09 +000017537/*
17538 * "str2nr()" function
17539 */
17540 static void
17541f_str2nr(argvars, rettv)
17542 typval_T *argvars;
17543 typval_T *rettv;
17544{
17545 int base = 10;
17546 char_u *p;
17547 long n;
17548
17549 if (argvars[1].v_type != VAR_UNKNOWN)
17550 {
17551 base = get_tv_number(&argvars[1]);
17552 if (base != 8 && base != 10 && base != 16)
17553 {
17554 EMSG(_(e_invarg));
17555 return;
17556 }
17557 }
17558
17559 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017560 if (*p == '+')
17561 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017562 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17563 rettv->vval.v_number = n;
17564}
17565
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566#ifdef HAVE_STRFTIME
17567/*
17568 * "strftime({format}[, {time}])" function
17569 */
17570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017571f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017572 typval_T *argvars;
17573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574{
17575 char_u result_buf[256];
17576 struct tm *curtime;
17577 time_t seconds;
17578 char_u *p;
17579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017583 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584 seconds = time(NULL);
17585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 curtime = localtime(&seconds);
17588 /* MSVC returns NULL for an invalid value of seconds. */
17589 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017590 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017591 else
17592 {
17593# ifdef FEAT_MBYTE
17594 vimconv_T conv;
17595 char_u *enc;
17596
17597 conv.vc_type = CONV_NONE;
17598 enc = enc_locale();
17599 convert_setup(&conv, p_enc, enc);
17600 if (conv.vc_type != CONV_NONE)
17601 p = string_convert(&conv, p, NULL);
17602# endif
17603 if (p != NULL)
17604 (void)strftime((char *)result_buf, sizeof(result_buf),
17605 (char *)p, curtime);
17606 else
17607 result_buf[0] = NUL;
17608
17609# ifdef FEAT_MBYTE
17610 if (conv.vc_type != CONV_NONE)
17611 vim_free(p);
17612 convert_setup(&conv, enc, p_enc);
17613 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 else
17616# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017617 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618
17619# ifdef FEAT_MBYTE
17620 /* Release conversion descriptors */
17621 convert_setup(&conv, NULL, NULL);
17622 vim_free(enc);
17623# endif
17624 }
17625}
17626#endif
17627
17628/*
17629 * "stridx()" function
17630 */
17631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017632f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017633 typval_T *argvars;
17634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635{
17636 char_u buf[NUMBUFLEN];
17637 char_u *needle;
17638 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017639 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017641 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017643 needle = get_tv_string_chk(&argvars[1]);
17644 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017645 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646 if (needle == NULL || haystack == NULL)
17647 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 if (argvars[2].v_type != VAR_UNKNOWN)
17650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017651 int error = FALSE;
17652
17653 start_idx = get_tv_number_chk(&argvars[2], &error);
17654 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017655 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017656 if (start_idx >= 0)
17657 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017658 }
17659
17660 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17661 if (pos != NULL)
17662 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663}
17664
17665/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017666 * "string()" function
17667 */
17668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017669f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017670 typval_T *argvars;
17671 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017672{
17673 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017674 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017677 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017678 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017679 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681}
17682
17683/*
17684 * "strlen()" function
17685 */
17686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017687f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017688 typval_T *argvars;
17689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017691 rettv->vval.v_number = (varnumber_T)(STRLEN(
17692 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693}
17694
17695/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017696 * "strchars()" function
17697 */
17698 static void
17699f_strchars(argvars, rettv)
17700 typval_T *argvars;
17701 typval_T *rettv;
17702{
17703 char_u *s = get_tv_string(&argvars[0]);
17704#ifdef FEAT_MBYTE
17705 varnumber_T len = 0;
17706
17707 while (*s != NUL)
17708 {
17709 mb_cptr2char_adv(&s);
17710 ++len;
17711 }
17712 rettv->vval.v_number = len;
17713#else
17714 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17715#endif
17716}
17717
17718/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017719 * "strdisplaywidth()" function
17720 */
17721 static void
17722f_strdisplaywidth(argvars, rettv)
17723 typval_T *argvars;
17724 typval_T *rettv;
17725{
17726 char_u *s = get_tv_string(&argvars[0]);
17727 int col = 0;
17728
17729 if (argvars[1].v_type != VAR_UNKNOWN)
17730 col = get_tv_number(&argvars[1]);
17731
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017732 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017733}
17734
17735/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017736 * "strwidth()" function
17737 */
17738 static void
17739f_strwidth(argvars, rettv)
17740 typval_T *argvars;
17741 typval_T *rettv;
17742{
17743 char_u *s = get_tv_string(&argvars[0]);
17744
17745 rettv->vval.v_number = (varnumber_T)(
17746#ifdef FEAT_MBYTE
17747 mb_string2cells(s, -1)
17748#else
17749 STRLEN(s)
17750#endif
17751 );
17752}
17753
17754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 * "strpart()" function
17756 */
17757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017759 typval_T *argvars;
17760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
17762 char_u *p;
17763 int n;
17764 int len;
17765 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 slen = (int)STRLEN(p);
17770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771 n = get_tv_number_chk(&argvars[1], &error);
17772 if (error)
17773 len = 0;
17774 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017775 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 else
17777 len = slen - n; /* default len: all bytes that are available. */
17778
17779 /*
17780 * Only return the overlap between the specified part and the actual
17781 * string.
17782 */
17783 if (n < 0)
17784 {
17785 len += n;
17786 n = 0;
17787 }
17788 else if (n > slen)
17789 n = slen;
17790 if (len < 0)
17791 len = 0;
17792 else if (n + len > slen)
17793 len = slen - n;
17794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017795 rettv->v_type = VAR_STRING;
17796 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797}
17798
17799/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017800 * "strridx()" function
17801 */
17802 static void
17803f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017804 typval_T *argvars;
17805 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017806{
17807 char_u buf[NUMBUFLEN];
17808 char_u *needle;
17809 char_u *haystack;
17810 char_u *rest;
17811 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017812 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 needle = get_tv_string_chk(&argvars[1]);
17815 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816
17817 rettv->vval.v_number = -1;
17818 if (needle == NULL || haystack == NULL)
17819 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017820
17821 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017822 if (argvars[2].v_type != VAR_UNKNOWN)
17823 {
17824 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017825 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017826 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017827 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017828 }
17829 else
17830 end_idx = haystack_len;
17831
Bram Moolenaar0d660222005-01-07 21:51:51 +000017832 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017833 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017834 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017835 lastmatch = haystack + end_idx;
17836 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017838 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017839 for (rest = haystack; *rest != '\0'; ++rest)
17840 {
17841 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017842 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017843 break;
17844 lastmatch = rest;
17845 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017846 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017847
17848 if (lastmatch == NULL)
17849 rettv->vval.v_number = -1;
17850 else
17851 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17852}
17853
17854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 * "strtrans()" function
17856 */
17857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017858f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017859 typval_T *argvars;
17860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017862 rettv->v_type = VAR_STRING;
17863 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864}
17865
17866/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017867 * "submatch()" function
17868 */
17869 static void
17870f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017871 typval_T *argvars;
17872 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017873{
17874 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017875 rettv->vval.v_string =
17876 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017877}
17878
17879/*
17880 * "substitute()" function
17881 */
17882 static void
17883f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017884 typval_T *argvars;
17885 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017886{
17887 char_u patbuf[NUMBUFLEN];
17888 char_u subbuf[NUMBUFLEN];
17889 char_u flagsbuf[NUMBUFLEN];
17890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017891 char_u *str = get_tv_string_chk(&argvars[0]);
17892 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17893 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17894 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17895
Bram Moolenaar0d660222005-01-07 21:51:51 +000017896 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017897 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17898 rettv->vval.v_string = NULL;
17899 else
17900 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017901}
17902
17903/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017904 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017907f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017908 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910{
17911 int id = 0;
17912#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017913 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 long col;
17915 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017916 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017918 lnum = get_tv_lnum(argvars); /* -1 on type error */
17919 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17920 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017922 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017923 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017924 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925#endif
17926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017927 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928}
17929
17930/*
17931 * "synIDattr(id, what [, mode])" function
17932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017934f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017936 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937{
17938 char_u *p = NULL;
17939#ifdef FEAT_SYN_HL
17940 int id;
17941 char_u *what;
17942 char_u *mode;
17943 char_u modebuf[NUMBUFLEN];
17944 int modec;
17945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017946 id = get_tv_number(&argvars[0]);
17947 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017948 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017950 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017952 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 modec = 0; /* replace invalid with current */
17954 }
17955 else
17956 {
17957#ifdef FEAT_GUI
17958 if (gui.in_use)
17959 modec = 'g';
17960 else
17961#endif
17962 if (t_colors > 1)
17963 modec = 'c';
17964 else
17965 modec = 't';
17966 }
17967
17968
17969 switch (TOLOWER_ASC(what[0]))
17970 {
17971 case 'b':
17972 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17973 p = highlight_color(id, what, modec);
17974 else /* bold */
17975 p = highlight_has_attr(id, HL_BOLD, modec);
17976 break;
17977
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017978 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 p = highlight_color(id, what, modec);
17980 break;
17981
17982 case 'i':
17983 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17984 p = highlight_has_attr(id, HL_INVERSE, modec);
17985 else /* italic */
17986 p = highlight_has_attr(id, HL_ITALIC, modec);
17987 break;
17988
17989 case 'n': /* name */
17990 p = get_highlight_name(NULL, id - 1);
17991 break;
17992
17993 case 'r': /* reverse */
17994 p = highlight_has_attr(id, HL_INVERSE, modec);
17995 break;
17996
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017997 case 's':
17998 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17999 p = highlight_color(id, what, modec);
18000 else /* standout */
18001 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 break;
18003
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018004 case 'u':
18005 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18006 /* underline */
18007 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18008 else
18009 /* undercurl */
18010 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 break;
18012 }
18013
18014 if (p != NULL)
18015 p = vim_strsave(p);
18016#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 rettv->v_type = VAR_STRING;
18018 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019}
18020
18021/*
18022 * "synIDtrans(id)" function
18023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018026 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028{
18029 int id;
18030
18031#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
18034 if (id > 0)
18035 id = syn_get_final_id(id);
18036 else
18037#endif
18038 id = 0;
18039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018040 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041}
18042
18043/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018044 * "synconcealed(lnum, col)" function
18045 */
18046 static void
18047f_synconcealed(argvars, rettv)
18048 typval_T *argvars UNUSED;
18049 typval_T *rettv;
18050{
18051#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18052 long lnum;
18053 long col;
18054 int syntax_flags = 0;
18055 int cchar;
18056 int matchid = 0;
18057 char_u str[NUMBUFLEN];
18058#endif
18059
18060 rettv->v_type = VAR_LIST;
18061 rettv->vval.v_list = NULL;
18062
18063#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18064 lnum = get_tv_lnum(argvars); /* -1 on type error */
18065 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18066
18067 vim_memset(str, NUL, sizeof(str));
18068
18069 if (rettv_list_alloc(rettv) != FAIL)
18070 {
18071 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18072 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18073 && curwin->w_p_cole > 0)
18074 {
18075 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18076 syntax_flags = get_syntax_info(&matchid);
18077
18078 /* get the conceal character */
18079 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18080 {
18081 cchar = syn_get_sub_char();
18082 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18083 cchar = lcs_conceal;
18084 if (cchar != NUL)
18085 {
18086# ifdef FEAT_MBYTE
18087 if (has_mbyte)
18088 (*mb_char2bytes)(cchar, str);
18089 else
18090# endif
18091 str[0] = cchar;
18092 }
18093 }
18094 }
18095
18096 list_append_number(rettv->vval.v_list,
18097 (syntax_flags & HL_CONCEAL) != 0);
18098 /* -1 to auto-determine strlen */
18099 list_append_string(rettv->vval.v_list, str, -1);
18100 list_append_number(rettv->vval.v_list, matchid);
18101 }
18102#endif
18103}
18104
18105/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018106 * "synstack(lnum, col)" function
18107 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018108 static void
18109f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018110 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018111 typval_T *rettv;
18112{
18113#ifdef FEAT_SYN_HL
18114 long lnum;
18115 long col;
18116 int i;
18117 int id;
18118#endif
18119
18120 rettv->v_type = VAR_LIST;
18121 rettv->vval.v_list = NULL;
18122
18123#ifdef FEAT_SYN_HL
18124 lnum = get_tv_lnum(argvars); /* -1 on type error */
18125 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18126
18127 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018128 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018129 && rettv_list_alloc(rettv) != FAIL)
18130 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018131 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018132 for (i = 0; ; ++i)
18133 {
18134 id = syn_get_stack_item(i);
18135 if (id < 0)
18136 break;
18137 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18138 break;
18139 }
18140 }
18141#endif
18142}
18143
18144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 * "system()" function
18146 */
18147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018149 typval_T *argvars;
18150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018152 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018154 char_u *infile = NULL;
18155 char_u buf[NUMBUFLEN];
18156 int err = FALSE;
18157 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018159 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018160 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018161
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018162 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018163 {
18164 /*
18165 * Write the string to a temp file, to be used for input of the shell
18166 * command.
18167 */
18168 if ((infile = vim_tempname('i')) == NULL)
18169 {
18170 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018171 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018172 }
18173
18174 fd = mch_fopen((char *)infile, WRITEBIN);
18175 if (fd == NULL)
18176 {
18177 EMSG2(_(e_notopen), infile);
18178 goto done;
18179 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018180 p = get_tv_string_buf_chk(&argvars[1], buf);
18181 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018182 {
18183 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018184 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018185 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018186 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18187 err = TRUE;
18188 if (fclose(fd) != 0)
18189 err = TRUE;
18190 if (err)
18191 {
18192 EMSG(_("E677: Error writing temp file"));
18193 goto done;
18194 }
18195 }
18196
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018197 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18198 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018199
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200#ifdef USE_CR
18201 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018202 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 {
18204 char_u *s;
18205
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018206 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 {
18208 if (*s == CAR)
18209 *s = NL;
18210 }
18211 }
18212#else
18213# ifdef USE_CRNL
18214 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018215 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 {
18217 char_u *s, *d;
18218
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018219 d = res;
18220 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 {
18222 if (s[0] == CAR && s[1] == NL)
18223 ++s;
18224 *d++ = *s;
18225 }
18226 *d = NUL;
18227 }
18228# endif
18229#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018230
18231done:
18232 if (infile != NULL)
18233 {
18234 mch_remove(infile);
18235 vim_free(infile);
18236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018237 rettv->v_type = VAR_STRING;
18238 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239}
18240
18241/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018242 * "tabpagebuflist()" function
18243 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018244 static void
18245f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018246 typval_T *argvars UNUSED;
18247 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018248{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018249#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018250 tabpage_T *tp;
18251 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018252
18253 if (argvars[0].v_type == VAR_UNKNOWN)
18254 wp = firstwin;
18255 else
18256 {
18257 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18258 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018259 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018260 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018261 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018262 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018263 for (; wp != NULL; wp = wp->w_next)
18264 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018265 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018266 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018267 }
18268#endif
18269}
18270
18271
18272/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018273 * "tabpagenr()" function
18274 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018275 static void
18276f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018277 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018278 typval_T *rettv;
18279{
18280 int nr = 1;
18281#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018282 char_u *arg;
18283
18284 if (argvars[0].v_type != VAR_UNKNOWN)
18285 {
18286 arg = get_tv_string_chk(&argvars[0]);
18287 nr = 0;
18288 if (arg != NULL)
18289 {
18290 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018291 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018292 else
18293 EMSG2(_(e_invexpr2), arg);
18294 }
18295 }
18296 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018297 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018298#endif
18299 rettv->vval.v_number = nr;
18300}
18301
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018302
18303#ifdef FEAT_WINDOWS
18304static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18305
18306/*
18307 * Common code for tabpagewinnr() and winnr().
18308 */
18309 static int
18310get_winnr(tp, argvar)
18311 tabpage_T *tp;
18312 typval_T *argvar;
18313{
18314 win_T *twin;
18315 int nr = 1;
18316 win_T *wp;
18317 char_u *arg;
18318
18319 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18320 if (argvar->v_type != VAR_UNKNOWN)
18321 {
18322 arg = get_tv_string_chk(argvar);
18323 if (arg == NULL)
18324 nr = 0; /* type error; errmsg already given */
18325 else if (STRCMP(arg, "$") == 0)
18326 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18327 else if (STRCMP(arg, "#") == 0)
18328 {
18329 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18330 if (twin == NULL)
18331 nr = 0;
18332 }
18333 else
18334 {
18335 EMSG2(_(e_invexpr2), arg);
18336 nr = 0;
18337 }
18338 }
18339
18340 if (nr > 0)
18341 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18342 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018343 {
18344 if (wp == NULL)
18345 {
18346 /* didn't find it in this tabpage */
18347 nr = 0;
18348 break;
18349 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018350 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018351 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018352 return nr;
18353}
18354#endif
18355
18356/*
18357 * "tabpagewinnr()" function
18358 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018359 static void
18360f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018361 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018362 typval_T *rettv;
18363{
18364 int nr = 1;
18365#ifdef FEAT_WINDOWS
18366 tabpage_T *tp;
18367
18368 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18369 if (tp == NULL)
18370 nr = 0;
18371 else
18372 nr = get_winnr(tp, &argvars[1]);
18373#endif
18374 rettv->vval.v_number = nr;
18375}
18376
18377
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018378/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018379 * "tagfiles()" function
18380 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018381 static void
18382f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018383 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018384 typval_T *rettv;
18385{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018386 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018387 tagname_T tn;
18388 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018389
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018390 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018391 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018392 fname = alloc(MAXPATHL);
18393 if (fname == NULL)
18394 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018396 for (first = TRUE; ; first = FALSE)
18397 if (get_tagfname(&tn, first, fname) == FAIL
18398 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018399 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018400 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018401 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018402}
18403
18404/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018405 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018406 */
18407 static void
18408f_taglist(argvars, rettv)
18409 typval_T *argvars;
18410 typval_T *rettv;
18411{
18412 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018413
18414 tag_pattern = get_tv_string(&argvars[0]);
18415
18416 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018417 if (*tag_pattern == NUL)
18418 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018419
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018420 if (rettv_list_alloc(rettv) == OK)
18421 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018422}
18423
18424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 * "tempname()" function
18426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018428f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431{
18432 static int x = 'A';
18433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018434 rettv->v_type = VAR_STRING;
18435 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436
18437 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18438 * names. Skip 'I' and 'O', they are used for shell redirection. */
18439 do
18440 {
18441 if (x == 'Z')
18442 x = '0';
18443 else if (x == '9')
18444 x = 'A';
18445 else
18446 {
18447#ifdef EBCDIC
18448 if (x == 'I')
18449 x = 'J';
18450 else if (x == 'R')
18451 x = 'S';
18452 else
18453#endif
18454 ++x;
18455 }
18456 } while (x == 'I' || x == 'O');
18457}
18458
18459/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018460 * "test(list)" function: Just checking the walls...
18461 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018462 static void
18463f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018464 typval_T *argvars UNUSED;
18465 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018466{
18467 /* Used for unit testing. Change the code below to your liking. */
18468#if 0
18469 listitem_T *li;
18470 list_T *l;
18471 char_u *bad, *good;
18472
18473 if (argvars[0].v_type != VAR_LIST)
18474 return;
18475 l = argvars[0].vval.v_list;
18476 if (l == NULL)
18477 return;
18478 li = l->lv_first;
18479 if (li == NULL)
18480 return;
18481 bad = get_tv_string(&li->li_tv);
18482 li = li->li_next;
18483 if (li == NULL)
18484 return;
18485 good = get_tv_string(&li->li_tv);
18486 rettv->vval.v_number = test_edit_score(bad, good);
18487#endif
18488}
18489
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018490#ifdef FEAT_FLOAT
18491/*
18492 * "tan()" function
18493 */
18494 static void
18495f_tan(argvars, rettv)
18496 typval_T *argvars;
18497 typval_T *rettv;
18498{
18499 float_T f;
18500
18501 rettv->v_type = VAR_FLOAT;
18502 if (get_float_arg(argvars, &f) == OK)
18503 rettv->vval.v_float = tan(f);
18504 else
18505 rettv->vval.v_float = 0.0;
18506}
18507
18508/*
18509 * "tanh()" function
18510 */
18511 static void
18512f_tanh(argvars, rettv)
18513 typval_T *argvars;
18514 typval_T *rettv;
18515{
18516 float_T f;
18517
18518 rettv->v_type = VAR_FLOAT;
18519 if (get_float_arg(argvars, &f) == OK)
18520 rettv->vval.v_float = tanh(f);
18521 else
18522 rettv->vval.v_float = 0.0;
18523}
18524#endif
18525
Bram Moolenaard52d9742005-08-21 22:20:28 +000018526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 * "tolower(string)" function
18528 */
18529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018531 typval_T *argvars;
18532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533{
18534 char_u *p;
18535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018536 p = vim_strsave(get_tv_string(&argvars[0]));
18537 rettv->v_type = VAR_STRING;
18538 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539
18540 if (p != NULL)
18541 while (*p != NUL)
18542 {
18543#ifdef FEAT_MBYTE
18544 int l;
18545
18546 if (enc_utf8)
18547 {
18548 int c, lc;
18549
18550 c = utf_ptr2char(p);
18551 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018552 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018553 /* TODO: reallocate string when byte count changes. */
18554 if (utf_char2len(lc) == l)
18555 utf_char2bytes(lc, p);
18556 p += l;
18557 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018558 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 p += l; /* skip multi-byte character */
18560 else
18561#endif
18562 {
18563 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18564 ++p;
18565 }
18566 }
18567}
18568
18569/*
18570 * "toupper(string)" function
18571 */
18572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018574 typval_T *argvars;
18575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018578 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579}
18580
18581/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018582 * "tr(string, fromstr, tostr)" function
18583 */
18584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018585f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018586 typval_T *argvars;
18587 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018588{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018589 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018590 char_u *fromstr;
18591 char_u *tostr;
18592 char_u *p;
18593#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018594 int inlen;
18595 int fromlen;
18596 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018597 int idx;
18598 char_u *cpstr;
18599 int cplen;
18600 int first = TRUE;
18601#endif
18602 char_u buf[NUMBUFLEN];
18603 char_u buf2[NUMBUFLEN];
18604 garray_T ga;
18605
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018606 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018607 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18608 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018609
18610 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 rettv->v_type = VAR_STRING;
18612 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018613 if (fromstr == NULL || tostr == NULL)
18614 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018615 ga_init2(&ga, (int)sizeof(char), 80);
18616
18617#ifdef FEAT_MBYTE
18618 if (!has_mbyte)
18619#endif
18620 /* not multi-byte: fromstr and tostr must be the same length */
18621 if (STRLEN(fromstr) != STRLEN(tostr))
18622 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018623#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018624error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018625#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018626 EMSG2(_(e_invarg2), fromstr);
18627 ga_clear(&ga);
18628 return;
18629 }
18630
18631 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018632 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018633 {
18634#ifdef FEAT_MBYTE
18635 if (has_mbyte)
18636 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018637 inlen = (*mb_ptr2len)(in_str);
18638 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018639 cplen = inlen;
18640 idx = 0;
18641 for (p = fromstr; *p != NUL; p += fromlen)
18642 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018643 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018644 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018645 {
18646 for (p = tostr; *p != NUL; p += tolen)
18647 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018648 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018649 if (idx-- == 0)
18650 {
18651 cplen = tolen;
18652 cpstr = p;
18653 break;
18654 }
18655 }
18656 if (*p == NUL) /* tostr is shorter than fromstr */
18657 goto error;
18658 break;
18659 }
18660 ++idx;
18661 }
18662
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018663 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018664 {
18665 /* Check that fromstr and tostr have the same number of
18666 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018667 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018668 first = FALSE;
18669 for (p = tostr; *p != NUL; p += tolen)
18670 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018671 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018672 --idx;
18673 }
18674 if (idx != 0)
18675 goto error;
18676 }
18677
18678 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018679 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018680 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018681
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018682 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018683 }
18684 else
18685#endif
18686 {
18687 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018688 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018689 if (p != NULL)
18690 ga_append(&ga, tostr[p - fromstr]);
18691 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018692 ga_append(&ga, *in_str);
18693 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018694 }
18695 }
18696
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018697 /* add a terminating NUL */
18698 ga_grow(&ga, 1);
18699 ga_append(&ga, NUL);
18700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018701 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018702}
18703
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018704#ifdef FEAT_FLOAT
18705/*
18706 * "trunc({float})" function
18707 */
18708 static void
18709f_trunc(argvars, rettv)
18710 typval_T *argvars;
18711 typval_T *rettv;
18712{
18713 float_T f;
18714
18715 rettv->v_type = VAR_FLOAT;
18716 if (get_float_arg(argvars, &f) == OK)
18717 /* trunc() is not in C90, use floor() or ceil() instead. */
18718 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18719 else
18720 rettv->vval.v_float = 0.0;
18721}
18722#endif
18723
Bram Moolenaar8299df92004-07-10 09:47:34 +000018724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 * "type(expr)" function
18726 */
18727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018728f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018729 typval_T *argvars;
18730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018732 int n;
18733
18734 switch (argvars[0].v_type)
18735 {
18736 case VAR_NUMBER: n = 0; break;
18737 case VAR_STRING: n = 1; break;
18738 case VAR_FUNC: n = 2; break;
18739 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018740 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018741#ifdef FEAT_FLOAT
18742 case VAR_FLOAT: n = 5; break;
18743#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018744 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18745 }
18746 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747}
18748
18749/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018750 * "undofile(name)" function
18751 */
18752 static void
18753f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018754 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018755 typval_T *rettv;
18756{
18757 rettv->v_type = VAR_STRING;
18758#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018759 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018760 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018761
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018762 if (*fname == NUL)
18763 {
18764 /* If there is no file name there will be no undo file. */
18765 rettv->vval.v_string = NULL;
18766 }
18767 else
18768 {
18769 char_u *ffname = FullName_save(fname, FALSE);
18770
18771 if (ffname != NULL)
18772 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18773 vim_free(ffname);
18774 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018775 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018776#else
18777 rettv->vval.v_string = NULL;
18778#endif
18779}
18780
18781/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018782 * "undotree()" function
18783 */
18784 static void
18785f_undotree(argvars, rettv)
18786 typval_T *argvars UNUSED;
18787 typval_T *rettv;
18788{
18789 if (rettv_dict_alloc(rettv) == OK)
18790 {
18791 dict_T *dict = rettv->vval.v_dict;
18792 list_T *list;
18793
Bram Moolenaar730cde92010-06-27 05:18:54 +020018794 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018795 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018796 dict_add_nr_str(dict, "save_last",
18797 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018798 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18799 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018800 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018801
18802 list = list_alloc();
18803 if (list != NULL)
18804 {
18805 u_eval_tree(curbuf->b_u_oldhead, list);
18806 dict_add_list(dict, "entries", list);
18807 }
18808 }
18809}
18810
18811/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018812 * "values(dict)" function
18813 */
18814 static void
18815f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 typval_T *argvars;
18817 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018818{
18819 dict_list(argvars, rettv, 1);
18820}
18821
18822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 * "virtcol(string)" function
18824 */
18825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018826f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 typval_T *argvars;
18828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829{
18830 colnr_T vcol = 0;
18831 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018832 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018834 fp = var2fpos(&argvars[0], FALSE, &fnum);
18835 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18836 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 {
18838 getvvcol(curwin, fp, NULL, NULL, &vcol);
18839 ++vcol;
18840 }
18841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
18845/*
18846 * "visualmode()" function
18847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018850 typval_T *argvars UNUSED;
18851 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 char_u str[2];
18854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 str[0] = curbuf->b_visual_mode_eval;
18857 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859
18860 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018861 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863}
18864
18865/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018866 * "wildmenumode()" function
18867 */
18868 static void
18869f_wildmenumode(argvars, rettv)
18870 typval_T *argvars UNUSED;
18871 typval_T *rettv UNUSED;
18872{
18873#ifdef FEAT_WILDMENU
18874 if (wild_menu_showing)
18875 rettv->vval.v_number = 1;
18876#endif
18877}
18878
18879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 * "winbufnr(nr)" function
18881 */
18882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018883f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 typval_T *argvars;
18885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886{
18887 win_T *wp;
18888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018889 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018893 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894}
18895
18896/*
18897 * "wincol()" function
18898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018900f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018901 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903{
18904 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018905 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906}
18907
18908/*
18909 * "winheight(nr)" function
18910 */
18911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018912f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018913 typval_T *argvars;
18914 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915{
18916 win_T *wp;
18917
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018918 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018920 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018922 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923}
18924
18925/*
18926 * "winline()" function
18927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018929f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018930 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932{
18933 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018934 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935}
18936
18937/*
18938 * "winnr()" function
18939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018941f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018942 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944{
18945 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018946
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018948 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018950 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951}
18952
18953/*
18954 * "winrestcmd()" function
18955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018957f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018958 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960{
18961#ifdef FEAT_WINDOWS
18962 win_T *wp;
18963 int winnr = 1;
18964 garray_T ga;
18965 char_u buf[50];
18966
18967 ga_init2(&ga, (int)sizeof(char), 70);
18968 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18969 {
18970 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18971 ga_concat(&ga, buf);
18972# ifdef FEAT_VERTSPLIT
18973 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18974 ga_concat(&ga, buf);
18975# endif
18976 ++winnr;
18977 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018978 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018980 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985}
18986
18987/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018988 * "winrestview()" function
18989 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018990 static void
18991f_winrestview(argvars, rettv)
18992 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018993 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018994{
18995 dict_T *dict;
18996
18997 if (argvars[0].v_type != VAR_DICT
18998 || (dict = argvars[0].vval.v_dict) == NULL)
18999 EMSG(_(e_invarg));
19000 else
19001 {
19002 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19003 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
19004#ifdef FEAT_VIRTUALEDIT
19005 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
19006#endif
19007 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019008 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019009
Bram Moolenaar6f11a412006-09-06 20:16:42 +000019010 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019011#ifdef FEAT_DIFF
19012 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
19013#endif
19014 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19015 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
19016
19017 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019018 win_new_height(curwin, curwin->w_height);
19019# ifdef FEAT_VERTSPLIT
19020 win_new_width(curwin, W_WIDTH(curwin));
19021# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019022 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019023
19024 if (curwin->w_topline == 0)
19025 curwin->w_topline = 1;
19026 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19027 curwin->w_topline = curbuf->b_ml.ml_line_count;
19028#ifdef FEAT_DIFF
19029 check_topfill(curwin, TRUE);
19030#endif
19031 }
19032}
19033
19034/*
19035 * "winsaveview()" function
19036 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019037 static void
19038f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019039 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019040 typval_T *rettv;
19041{
19042 dict_T *dict;
19043
Bram Moolenaara800b422010-06-27 01:15:55 +020019044 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019045 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019046 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019047
19048 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19049 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19050#ifdef FEAT_VIRTUALEDIT
19051 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19052#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019053 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019054 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19055
19056 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19057#ifdef FEAT_DIFF
19058 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19059#endif
19060 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19061 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19062}
19063
19064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065 * "winwidth(nr)" function
19066 */
19067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019068f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019069 typval_T *argvars;
19070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071{
19072 win_T *wp;
19073
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019074 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077 else
19078#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019079 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082#endif
19083}
19084
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019086 * "writefile()" function
19087 */
19088 static void
19089f_writefile(argvars, rettv)
19090 typval_T *argvars;
19091 typval_T *rettv;
19092{
19093 int binary = FALSE;
19094 char_u *fname;
19095 FILE *fd;
19096 listitem_T *li;
19097 char_u *s;
19098 int ret = 0;
19099 int c;
19100
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019101 if (check_restricted() || check_secure())
19102 return;
19103
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019104 if (argvars[0].v_type != VAR_LIST)
19105 {
19106 EMSG2(_(e_listarg), "writefile()");
19107 return;
19108 }
19109 if (argvars[0].vval.v_list == NULL)
19110 return;
19111
19112 if (argvars[2].v_type != VAR_UNKNOWN
19113 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19114 binary = TRUE;
19115
19116 /* Always open the file in binary mode, library functions have a mind of
19117 * their own about CR-LF conversion. */
19118 fname = get_tv_string(&argvars[1]);
19119 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19120 {
19121 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19122 ret = -1;
19123 }
19124 else
19125 {
19126 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19127 li = li->li_next)
19128 {
19129 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19130 {
19131 if (*s == '\n')
19132 c = putc(NUL, fd);
19133 else
19134 c = putc(*s, fd);
19135 if (c == EOF)
19136 {
19137 ret = -1;
19138 break;
19139 }
19140 }
19141 if (!binary || li->li_next != NULL)
19142 if (putc('\n', fd) == EOF)
19143 {
19144 ret = -1;
19145 break;
19146 }
19147 if (ret < 0)
19148 {
19149 EMSG(_(e_write));
19150 break;
19151 }
19152 }
19153 fclose(fd);
19154 }
19155
19156 rettv->vval.v_number = ret;
19157}
19158
19159/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019160 * "xor(expr, expr)" function
19161 */
19162 static void
19163f_xor(argvars, rettv)
19164 typval_T *argvars;
19165 typval_T *rettv;
19166{
19167 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19168 ^ get_tv_number_chk(&argvars[1], NULL);
19169}
19170
19171
19172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019174 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 */
19176 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019177var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019179 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019180 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019182 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019184 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185
Bram Moolenaara5525202006-03-02 22:52:09 +000019186 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019187 if (varp->v_type == VAR_LIST)
19188 {
19189 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019190 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019191 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019192 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019193
19194 l = varp->vval.v_list;
19195 if (l == NULL)
19196 return NULL;
19197
19198 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019199 pos.lnum = list_find_nr(l, 0L, &error);
19200 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019201 return NULL; /* invalid line number */
19202
19203 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019204 pos.col = list_find_nr(l, 1L, &error);
19205 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019206 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019207 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019208
19209 /* We accept "$" for the column number: last column. */
19210 li = list_find(l, 1L);
19211 if (li != NULL && li->li_tv.v_type == VAR_STRING
19212 && li->li_tv.vval.v_string != NULL
19213 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19214 pos.col = len + 1;
19215
Bram Moolenaara5525202006-03-02 22:52:09 +000019216 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019217 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019218 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019219 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019220
Bram Moolenaara5525202006-03-02 22:52:09 +000019221#ifdef FEAT_VIRTUALEDIT
19222 /* Get the virtual offset. Defaults to zero. */
19223 pos.coladd = list_find_nr(l, 2L, &error);
19224 if (error)
19225 pos.coladd = 0;
19226#endif
19227
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019228 return &pos;
19229 }
19230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019231 name = get_tv_string_chk(varp);
19232 if (name == NULL)
19233 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019234 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019236 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19237 {
19238 if (VIsual_active)
19239 return &VIsual;
19240 return &curwin->w_cursor;
19241 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019242 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019244 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19246 return NULL;
19247 return pp;
19248 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019249
19250#ifdef FEAT_VIRTUALEDIT
19251 pos.coladd = 0;
19252#endif
19253
Bram Moolenaar477933c2007-07-17 14:32:23 +000019254 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019255 {
19256 pos.col = 0;
19257 if (name[1] == '0') /* "w0": first visible line */
19258 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019259 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019260 pos.lnum = curwin->w_topline;
19261 return &pos;
19262 }
19263 else if (name[1] == '$') /* "w$": last visible line */
19264 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019265 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019266 pos.lnum = curwin->w_botline - 1;
19267 return &pos;
19268 }
19269 }
19270 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019272 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 {
19274 pos.lnum = curbuf->b_ml.ml_line_count;
19275 pos.col = 0;
19276 }
19277 else
19278 {
19279 pos.lnum = curwin->w_cursor.lnum;
19280 pos.col = (colnr_T)STRLEN(ml_get_curline());
19281 }
19282 return &pos;
19283 }
19284 return NULL;
19285}
19286
19287/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019288 * Convert list in "arg" into a position and optional file number.
19289 * When "fnump" is NULL there is no file number, only 3 items.
19290 * Note that the column is passed on as-is, the caller may want to decrement
19291 * it to use 1 for the first column.
19292 * Return FAIL when conversion is not possible, doesn't check the position for
19293 * validity.
19294 */
19295 static int
19296list2fpos(arg, posp, fnump)
19297 typval_T *arg;
19298 pos_T *posp;
19299 int *fnump;
19300{
19301 list_T *l = arg->vval.v_list;
19302 long i = 0;
19303 long n;
19304
Bram Moolenaarbde35262006-07-23 20:12:24 +000019305 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19306 * when "fnump" isn't NULL and "coladd" is optional. */
19307 if (arg->v_type != VAR_LIST
19308 || l == NULL
19309 || l->lv_len < (fnump == NULL ? 2 : 3)
19310 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019311 return FAIL;
19312
19313 if (fnump != NULL)
19314 {
19315 n = list_find_nr(l, i++, NULL); /* fnum */
19316 if (n < 0)
19317 return FAIL;
19318 if (n == 0)
19319 n = curbuf->b_fnum; /* current buffer */
19320 *fnump = n;
19321 }
19322
19323 n = list_find_nr(l, i++, NULL); /* lnum */
19324 if (n < 0)
19325 return FAIL;
19326 posp->lnum = n;
19327
19328 n = list_find_nr(l, i++, NULL); /* col */
19329 if (n < 0)
19330 return FAIL;
19331 posp->col = n;
19332
19333#ifdef FEAT_VIRTUALEDIT
19334 n = list_find_nr(l, i, NULL);
19335 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019336 posp->coladd = 0;
19337 else
19338 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019339#endif
19340
19341 return OK;
19342}
19343
19344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 * Get the length of an environment variable name.
19346 * Advance "arg" to the first character after the name.
19347 * Return 0 for error.
19348 */
19349 static int
19350get_env_len(arg)
19351 char_u **arg;
19352{
19353 char_u *p;
19354 int len;
19355
19356 for (p = *arg; vim_isIDc(*p); ++p)
19357 ;
19358 if (p == *arg) /* no name found */
19359 return 0;
19360
19361 len = (int)(p - *arg);
19362 *arg = p;
19363 return len;
19364}
19365
19366/*
19367 * Get the length of the name of a function or internal variable.
19368 * "arg" is advanced to the first non-white character after the name.
19369 * Return 0 if something is wrong.
19370 */
19371 static int
19372get_id_len(arg)
19373 char_u **arg;
19374{
19375 char_u *p;
19376 int len;
19377
19378 /* Find the end of the name. */
19379 for (p = *arg; eval_isnamec(*p); ++p)
19380 ;
19381 if (p == *arg) /* no name found */
19382 return 0;
19383
19384 len = (int)(p - *arg);
19385 *arg = skipwhite(p);
19386
19387 return len;
19388}
19389
19390/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019391 * Get the length of the name of a variable or function.
19392 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019394 * Return -1 if curly braces expansion failed.
19395 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 * If the name contains 'magic' {}'s, expand them and return the
19397 * expanded name in an allocated string via 'alias' - caller must free.
19398 */
19399 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019400get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 char_u **arg;
19402 char_u **alias;
19403 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019404 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405{
19406 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 char_u *p;
19408 char_u *expr_start;
19409 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410
19411 *alias = NULL; /* default to no alias */
19412
19413 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19414 && (*arg)[2] == (int)KE_SNR)
19415 {
19416 /* hard coded <SNR>, already translated */
19417 *arg += 3;
19418 return get_id_len(arg) + 3;
19419 }
19420 len = eval_fname_script(*arg);
19421 if (len > 0)
19422 {
19423 /* literal "<SID>", "s:" or "<SNR>" */
19424 *arg += len;
19425 }
19426
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019430 p = find_name_end(*arg, &expr_start, &expr_end,
19431 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 if (expr_start != NULL)
19433 {
19434 char_u *temp_string;
19435
19436 if (!evaluate)
19437 {
19438 len += (int)(p - *arg);
19439 *arg = skipwhite(p);
19440 return len;
19441 }
19442
19443 /*
19444 * Include any <SID> etc in the expanded string:
19445 * Thus the -len here.
19446 */
19447 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19448 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019449 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 *alias = temp_string;
19451 *arg = skipwhite(p);
19452 return (int)STRLEN(temp_string);
19453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454
19455 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019456 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 EMSG2(_(e_invexpr2), *arg);
19458
19459 return len;
19460}
19461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462/*
19463 * Find the end of a variable or function name, taking care of magic braces.
19464 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19465 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019466 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467 * Return a pointer to just after the name. Equal to "arg" if there is no
19468 * valid name.
19469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019471find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 char_u *arg;
19473 char_u **expr_start;
19474 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019475 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 int mb_nest = 0;
19478 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 char_u *p;
19480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483 *expr_start = NULL;
19484 *expr_end = NULL;
19485 }
19486
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019487 /* Quick check for valid starting character. */
19488 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19489 return arg;
19490
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019491 for (p = arg; *p != NUL
19492 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019493 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019494 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019495 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019496 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019497 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019498 if (*p == '\'')
19499 {
19500 /* skip over 'string' to avoid counting [ and ] inside it. */
19501 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19502 ;
19503 if (*p == NUL)
19504 break;
19505 }
19506 else if (*p == '"')
19507 {
19508 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19509 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19510 if (*p == '\\' && p[1] != NUL)
19511 ++p;
19512 if (*p == NUL)
19513 break;
19514 }
19515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019516 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 if (*p == '[')
19519 ++br_nest;
19520 else if (*p == ']')
19521 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019524 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526 if (*p == '{')
19527 {
19528 mb_nest++;
19529 if (expr_start != NULL && *expr_start == NULL)
19530 *expr_start = p;
19531 }
19532 else if (*p == '}')
19533 {
19534 mb_nest--;
19535 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19536 *expr_end = p;
19537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 }
19540
19541 return p;
19542}
19543
19544/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019545 * Expands out the 'magic' {}'s in a variable/function name.
19546 * Note that this can call itself recursively, to deal with
19547 * constructs like foo{bar}{baz}{bam}
19548 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19549 * "in_start" ^
19550 * "expr_start" ^
19551 * "expr_end" ^
19552 * "in_end" ^
19553 *
19554 * Returns a new allocated string, which the caller must free.
19555 * Returns NULL for failure.
19556 */
19557 static char_u *
19558make_expanded_name(in_start, expr_start, expr_end, in_end)
19559 char_u *in_start;
19560 char_u *expr_start;
19561 char_u *expr_end;
19562 char_u *in_end;
19563{
19564 char_u c1;
19565 char_u *retval = NULL;
19566 char_u *temp_result;
19567 char_u *nextcmd = NULL;
19568
19569 if (expr_end == NULL || in_end == NULL)
19570 return NULL;
19571 *expr_start = NUL;
19572 *expr_end = NUL;
19573 c1 = *in_end;
19574 *in_end = NUL;
19575
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019576 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019577 if (temp_result != NULL && nextcmd == NULL)
19578 {
19579 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19580 + (in_end - expr_end) + 1));
19581 if (retval != NULL)
19582 {
19583 STRCPY(retval, in_start);
19584 STRCAT(retval, temp_result);
19585 STRCAT(retval, expr_end + 1);
19586 }
19587 }
19588 vim_free(temp_result);
19589
19590 *in_end = c1; /* put char back for error messages */
19591 *expr_start = '{';
19592 *expr_end = '}';
19593
19594 if (retval != NULL)
19595 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019596 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019597 if (expr_start != NULL)
19598 {
19599 /* Further expansion! */
19600 temp_result = make_expanded_name(retval, expr_start,
19601 expr_end, temp_result);
19602 vim_free(retval);
19603 retval = temp_result;
19604 }
19605 }
19606
19607 return retval;
19608}
19609
19610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019612 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 */
19614 static int
19615eval_isnamec(c)
19616 int c;
19617{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019618 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19619}
19620
19621/*
19622 * Return TRUE if character "c" can be used as the first character in a
19623 * variable or function name (excluding '{' and '}').
19624 */
19625 static int
19626eval_isnamec1(c)
19627 int c;
19628{
19629 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630}
19631
19632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 * Set number v: variable to "val".
19634 */
19635 void
19636set_vim_var_nr(idx, val)
19637 int idx;
19638 long val;
19639{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019640 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641}
19642
19643/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019644 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 */
19646 long
19647get_vim_var_nr(idx)
19648 int idx;
19649{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019650 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651}
19652
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019653/*
19654 * Get string v: variable value. Uses a static buffer, can only be used once.
19655 */
19656 char_u *
19657get_vim_var_str(idx)
19658 int idx;
19659{
19660 return get_tv_string(&vimvars[idx].vv_tv);
19661}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019662
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019664 * Get List v: variable value. Caller must take care of reference count when
19665 * needed.
19666 */
19667 list_T *
19668get_vim_var_list(idx)
19669 int idx;
19670{
19671 return vimvars[idx].vv_list;
19672}
19673
19674/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019675 * Set v:char to character "c".
19676 */
19677 void
19678set_vim_var_char(c)
19679 int c;
19680{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019681 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019682
19683#ifdef FEAT_MBYTE
19684 if (has_mbyte)
19685 buf[(*mb_char2bytes)(c, buf)] = NUL;
19686 else
19687#endif
19688 {
19689 buf[0] = c;
19690 buf[1] = NUL;
19691 }
19692 set_vim_var_string(VV_CHAR, buf, -1);
19693}
19694
19695/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019696 * Set v:count to "count" and v:count1 to "count1".
19697 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 */
19699 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019700set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701 long count;
19702 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019703 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019705 if (set_prevcount)
19706 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019707 vimvars[VV_COUNT].vv_nr = count;
19708 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709}
19710
19711/*
19712 * Set string v: variable to a copy of "val".
19713 */
19714 void
19715set_vim_var_string(idx, val, len)
19716 int idx;
19717 char_u *val;
19718 int len; /* length of "val" to use or -1 (whole string) */
19719{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019720 /* Need to do this (at least) once, since we can't initialize a union.
19721 * Will always be invoked when "v:progname" is set. */
19722 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19723
Bram Moolenaare9a41262005-01-15 22:18:47 +000019724 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019726 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019728 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019730 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731}
19732
19733/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019734 * Set List v: variable to "val".
19735 */
19736 void
19737set_vim_var_list(idx, val)
19738 int idx;
19739 list_T *val;
19740{
19741 list_unref(vimvars[idx].vv_list);
19742 vimvars[idx].vv_list = val;
19743 if (val != NULL)
19744 ++val->lv_refcount;
19745}
19746
19747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 * Set v:register if needed.
19749 */
19750 void
19751set_reg_var(c)
19752 int c;
19753{
19754 char_u regname;
19755
19756 if (c == 0 || c == ' ')
19757 regname = '"';
19758 else
19759 regname = c;
19760 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019761 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 set_vim_var_string(VV_REG, &regname, 1);
19763}
19764
19765/*
19766 * Get or set v:exception. If "oldval" == NULL, return the current value.
19767 * Otherwise, restore the value to "oldval" and return NULL.
19768 * Must always be called in pairs to save and restore v:exception! Does not
19769 * take care of memory allocations.
19770 */
19771 char_u *
19772v_exception(oldval)
19773 char_u *oldval;
19774{
19775 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019776 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777
Bram Moolenaare9a41262005-01-15 22:18:47 +000019778 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 return NULL;
19780}
19781
19782/*
19783 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19784 * Otherwise, restore the value to "oldval" and return NULL.
19785 * Must always be called in pairs to save and restore v:throwpoint! Does not
19786 * take care of memory allocations.
19787 */
19788 char_u *
19789v_throwpoint(oldval)
19790 char_u *oldval;
19791{
19792 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019793 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794
Bram Moolenaare9a41262005-01-15 22:18:47 +000019795 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 return NULL;
19797}
19798
19799#if defined(FEAT_AUTOCMD) || defined(PROTO)
19800/*
19801 * Set v:cmdarg.
19802 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19803 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19804 * Must always be called in pairs!
19805 */
19806 char_u *
19807set_cmdarg(eap, oldarg)
19808 exarg_T *eap;
19809 char_u *oldarg;
19810{
19811 char_u *oldval;
19812 char_u *newval;
19813 unsigned len;
19814
Bram Moolenaare9a41262005-01-15 22:18:47 +000019815 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019816 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019818 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019819 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019820 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 }
19822
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019823 if (eap->force_bin == FORCE_BIN)
19824 len = 6;
19825 else if (eap->force_bin == FORCE_NOBIN)
19826 len = 8;
19827 else
19828 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019829
19830 if (eap->read_edit)
19831 len += 7;
19832
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019833 if (eap->force_ff != 0)
19834 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19835# ifdef FEAT_MBYTE
19836 if (eap->force_enc != 0)
19837 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019838 if (eap->bad_char != 0)
19839 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019840# endif
19841
19842 newval = alloc(len + 1);
19843 if (newval == NULL)
19844 return NULL;
19845
19846 if (eap->force_bin == FORCE_BIN)
19847 sprintf((char *)newval, " ++bin");
19848 else if (eap->force_bin == FORCE_NOBIN)
19849 sprintf((char *)newval, " ++nobin");
19850 else
19851 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019852
19853 if (eap->read_edit)
19854 STRCAT(newval, " ++edit");
19855
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019856 if (eap->force_ff != 0)
19857 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19858 eap->cmd + eap->force_ff);
19859# ifdef FEAT_MBYTE
19860 if (eap->force_enc != 0)
19861 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19862 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019863 if (eap->bad_char == BAD_KEEP)
19864 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19865 else if (eap->bad_char == BAD_DROP)
19866 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19867 else if (eap->bad_char != 0)
19868 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019869# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019870 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019871 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872}
19873#endif
19874
19875/*
19876 * Get the value of internal variable "name".
19877 * Return OK or FAIL.
19878 */
19879 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019880get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 char_u *name;
19882 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019884 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019885 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886{
19887 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019888 typval_T *tv = NULL;
19889 typval_T atv;
19890 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892
19893 /* truncate the name, so that we can use strcmp() */
19894 cc = name[len];
19895 name[len] = NUL;
19896
19897 /*
19898 * Check for "b:changedtick".
19899 */
19900 if (STRCMP(name, "b:changedtick") == 0)
19901 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019902 atv.v_type = VAR_NUMBER;
19903 atv.vval.v_number = curbuf->b_changedtick;
19904 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905 }
19906
19907 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 * Check for user-defined variables.
19909 */
19910 else
19911 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019912 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019913 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019914 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 }
19916
Bram Moolenaare9a41262005-01-15 22:18:47 +000019917 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019919 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 ret = FAIL;
19922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019924 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925
19926 name[len] = cc;
19927
19928 return ret;
19929}
19930
19931/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019932 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19933 * Also handle function call with Funcref variable: func(expr)
19934 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19935 */
19936 static int
19937handle_subscript(arg, rettv, evaluate, verbose)
19938 char_u **arg;
19939 typval_T *rettv;
19940 int evaluate; /* do more than finding the end */
19941 int verbose; /* give error messages */
19942{
19943 int ret = OK;
19944 dict_T *selfdict = NULL;
19945 char_u *s;
19946 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019947 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019948
19949 while (ret == OK
19950 && (**arg == '['
19951 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019952 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019953 && !vim_iswhite(*(*arg - 1)))
19954 {
19955 if (**arg == '(')
19956 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019957 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019958 if (evaluate)
19959 {
19960 functv = *rettv;
19961 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019962
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019963 /* Invoke the function. Recursive! */
19964 s = functv.vval.v_string;
19965 }
19966 else
19967 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019968 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019969 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19970 &len, evaluate, selfdict);
19971
19972 /* Clear the funcref afterwards, so that deleting it while
19973 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019974 if (evaluate)
19975 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019976
19977 /* Stop the expression evaluation when immediately aborting on
19978 * error, or when an interrupt occurred or an exception was thrown
19979 * but not caught. */
19980 if (aborting())
19981 {
19982 if (ret == OK)
19983 clear_tv(rettv);
19984 ret = FAIL;
19985 }
19986 dict_unref(selfdict);
19987 selfdict = NULL;
19988 }
19989 else /* **arg == '[' || **arg == '.' */
19990 {
19991 dict_unref(selfdict);
19992 if (rettv->v_type == VAR_DICT)
19993 {
19994 selfdict = rettv->vval.v_dict;
19995 if (selfdict != NULL)
19996 ++selfdict->dv_refcount;
19997 }
19998 else
19999 selfdict = NULL;
20000 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20001 {
20002 clear_tv(rettv);
20003 ret = FAIL;
20004 }
20005 }
20006 }
20007 dict_unref(selfdict);
20008 return ret;
20009}
20010
20011/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020012 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020013 * value).
20014 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020015 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020017{
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020019}
20020
20021/*
20022 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 * The string "s" must have been allocated, it is consumed.
20024 * Return NULL for out of memory, the variable otherwise.
20025 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020026 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020027alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 char_u *s;
20029{
Bram Moolenaar33570922005-01-25 22:26:29 +000020030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020032 rettv = alloc_tv();
20033 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 rettv->v_type = VAR_STRING;
20036 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
20038 else
20039 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020040 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041}
20042
20043/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020044 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020046 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020047free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020048 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049{
20050 if (varp != NULL)
20051 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020052 switch (varp->v_type)
20053 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020054 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020055 func_unref(varp->vval.v_string);
20056 /*FALLTHROUGH*/
20057 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020058 vim_free(varp->vval.v_string);
20059 break;
20060 case VAR_LIST:
20061 list_unref(varp->vval.v_list);
20062 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020063 case VAR_DICT:
20064 dict_unref(varp->vval.v_dict);
20065 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020066 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020067#ifdef FEAT_FLOAT
20068 case VAR_FLOAT:
20069#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020070 case VAR_UNKNOWN:
20071 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020072 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020073 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020074 break;
20075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 vim_free(varp);
20077 }
20078}
20079
20080/*
20081 * Free the memory for a variable value and set the value to NULL or 0.
20082 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020083 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020085 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086{
20087 if (varp != NULL)
20088 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020089 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020092 func_unref(varp->vval.v_string);
20093 /*FALLTHROUGH*/
20094 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020095 vim_free(varp->vval.v_string);
20096 varp->vval.v_string = NULL;
20097 break;
20098 case VAR_LIST:
20099 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020100 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020101 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020102 case VAR_DICT:
20103 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020104 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020105 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020107 varp->vval.v_number = 0;
20108 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020109#ifdef FEAT_FLOAT
20110 case VAR_FLOAT:
20111 varp->vval.v_float = 0.0;
20112 break;
20113#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 case VAR_UNKNOWN:
20115 break;
20116 default:
20117 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020119 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 }
20121}
20122
20123/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020124 * Set the value of a variable to NULL without freeing items.
20125 */
20126 static void
20127init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129{
20130 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020131 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020132}
20133
20134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 * Get the number value of a variable.
20136 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020137 * For incompatible types, return 0.
20138 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20139 * caller of incompatible types: it sets *denote to TRUE if "denote"
20140 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 */
20142 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020144 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020146 int error = FALSE;
20147
20148 return get_tv_number_chk(varp, &error); /* return 0L on error */
20149}
20150
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020151 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020152get_tv_number_chk(varp, denote)
20153 typval_T *varp;
20154 int *denote;
20155{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020156 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020158 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020160 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020161 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020162#ifdef FEAT_FLOAT
20163 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020164 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020165 break;
20166#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020167 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020168 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020169 break;
20170 case VAR_STRING:
20171 if (varp->vval.v_string != NULL)
20172 vim_str2nr(varp->vval.v_string, NULL, NULL,
20173 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020174 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020175 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020176 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020177 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020178 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020179 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020180 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020181 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020182 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020183 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020185 if (denote == NULL) /* useful for values that must be unsigned */
20186 n = -1;
20187 else
20188 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020189 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190}
20191
20192/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020193 * Get the lnum from the first argument.
20194 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020195 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 */
20197 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200{
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202 linenr_T lnum;
20203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020204 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 if (lnum == 0) /* no valid number, try using line() */
20206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020207 rettv.v_type = VAR_NUMBER;
20208 f_line(argvars, &rettv);
20209 lnum = rettv.vval.v_number;
20210 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 }
20212 return lnum;
20213}
20214
20215/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020216 * Get the lnum from the first argument.
20217 * Also accepts "$", then "buf" is used.
20218 * Returns 0 on error.
20219 */
20220 static linenr_T
20221get_tv_lnum_buf(argvars, buf)
20222 typval_T *argvars;
20223 buf_T *buf;
20224{
20225 if (argvars[0].v_type == VAR_STRING
20226 && argvars[0].vval.v_string != NULL
20227 && argvars[0].vval.v_string[0] == '$'
20228 && buf != NULL)
20229 return buf->b_ml.ml_line_count;
20230 return get_tv_number_chk(&argvars[0], NULL);
20231}
20232
20233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 * Get the string value of a variable.
20235 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020236 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20237 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 * If the String variable has never been set, return an empty string.
20239 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020240 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20241 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 */
20243 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020244get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246{
20247 static char_u mybuf[NUMBUFLEN];
20248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250}
20251
20252 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020254 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020255 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020257 char_u *res = get_tv_string_buf_chk(varp, buf);
20258
20259 return res != NULL ? res : (char_u *)"";
20260}
20261
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020262 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020263get_tv_string_chk(varp)
20264 typval_T *varp;
20265{
20266 static char_u mybuf[NUMBUFLEN];
20267
20268 return get_tv_string_buf_chk(varp, mybuf);
20269}
20270
20271 static char_u *
20272get_tv_string_buf_chk(varp, buf)
20273 typval_T *varp;
20274 char_u *buf;
20275{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020276 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020278 case VAR_NUMBER:
20279 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20280 return buf;
20281 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020282 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020283 break;
20284 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020285 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020286 break;
20287 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020288 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020289 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020290#ifdef FEAT_FLOAT
20291 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020292 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020293 break;
20294#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020295 case VAR_STRING:
20296 if (varp->vval.v_string != NULL)
20297 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020298 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020299 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020300 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020303 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304}
20305
20306/*
20307 * Find variable "name" in the list of variables.
20308 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020309 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020310 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020311 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020313 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020314find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020316 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020317 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020320 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321
Bram Moolenaara7043832005-01-21 11:56:39 +000020322 ht = find_var_ht(name, &varname);
20323 if (htp != NULL)
20324 *htp = ht;
20325 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020327 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328}
20329
20330/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020331 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020332 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020333 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020334 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020335find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020336 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020337 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020338 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020339 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020340{
Bram Moolenaar33570922005-01-25 22:26:29 +000020341 hashitem_T *hi;
20342
20343 if (*varname == NUL)
20344 {
20345 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020346 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020347 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020348 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020349 case 'g': return &globvars_var;
20350 case 'v': return &vimvars_var;
20351 case 'b': return &curbuf->b_bufvar;
20352 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020353#ifdef FEAT_WINDOWS
20354 case 't': return &curtab->tp_winvar;
20355#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020356 case 'l': return current_funccal == NULL
20357 ? NULL : &current_funccal->l_vars_var;
20358 case 'a': return current_funccal == NULL
20359 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 }
20361 return NULL;
20362 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020363
20364 hi = hash_find(ht, varname);
20365 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020366 {
20367 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020368 * worked find the variable again. Don't auto-load a script if it was
20369 * loaded already, otherwise it would be loaded every time when
20370 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020371 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020372 {
20373 /* Note: script_autoload() may make "hi" invalid. It must either
20374 * be obtained again or not used. */
20375 if (!script_autoload(varname, FALSE) || aborting())
20376 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020377 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020378 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020379 if (HASHITEM_EMPTY(hi))
20380 return NULL;
20381 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020383}
20384
20385/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020387 * Set "varname" to the start of name without ':'.
20388 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020389 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020390find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 char_u *name;
20392 char_u **varname;
20393{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020394 hashitem_T *hi;
20395
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 if (name[1] != ':')
20397 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020398 /* The name must not start with a colon or #. */
20399 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 return NULL;
20401 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020402
20403 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020404 hi = hash_find(&compat_hashtab, name);
20405 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020406 return &compat_hashtab;
20407
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 return &globvarht; /* global variable */
20410 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 }
20412 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020413 if (*name == 'g') /* global variable */
20414 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020415 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20416 */
20417 if (vim_strchr(name + 2, ':') != NULL
20418 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020419 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020421 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020423 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020424#ifdef FEAT_WINDOWS
20425 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020426 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020427#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020428 if (*name == 'v') /* v: variable */
20429 return &vimvarht;
20430 if (*name == 'a' && current_funccal != NULL) /* function argument */
20431 return &current_funccal->l_avars.dv_hashtab;
20432 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20433 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 if (*name == 's' /* script variable */
20435 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20436 return &SCRIPT_VARS(current_SID);
20437 return NULL;
20438}
20439
20440/*
20441 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020442 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 * Returns NULL when it doesn't exist.
20444 */
20445 char_u *
20446get_var_value(name)
20447 char_u *name;
20448{
Bram Moolenaar33570922005-01-25 22:26:29 +000020449 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020451 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 if (v == NULL)
20453 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020454 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455}
20456
20457/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020458 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 * sourcing this script and when executing functions defined in the script.
20460 */
20461 void
20462new_script_vars(id)
20463 scid_T id;
20464{
Bram Moolenaara7043832005-01-21 11:56:39 +000020465 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020466 hashtab_T *ht;
20467 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020468
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20470 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020471 /* Re-allocating ga_data means that an ht_array pointing to
20472 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020474 for (i = 1; i <= ga_scripts.ga_len; ++i)
20475 {
20476 ht = &SCRIPT_VARS(i);
20477 if (ht->ht_mask == HT_INIT_SIZE - 1)
20478 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020479 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020481 }
20482
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 while (ga_scripts.ga_len < id)
20484 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020485 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020486 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020487 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 }
20490 }
20491}
20492
20493/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20495 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 */
20497 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020498init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020499 dict_T *dict;
20500 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020501 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502{
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020504 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020505 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020506 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020507 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020508 dict_var->di_tv.vval.v_dict = dict;
20509 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020510 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020511 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20512 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513}
20514
20515/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020516 * Unreference a dictionary initialized by init_var_dict().
20517 */
20518 void
20519unref_var_dict(dict)
20520 dict_T *dict;
20521{
20522 /* Now the dict needs to be freed if no one else is using it, go back to
20523 * normal reference counting. */
20524 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20525 dict_unref(dict);
20526}
20527
20528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020530 * Frees all allocated variables and the value they contain.
20531 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020532 */
20533 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020534vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020535 hashtab_T *ht;
20536{
20537 vars_clear_ext(ht, TRUE);
20538}
20539
20540/*
20541 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20542 */
20543 static void
20544vars_clear_ext(ht, free_val)
20545 hashtab_T *ht;
20546 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020547{
Bram Moolenaara7043832005-01-21 11:56:39 +000020548 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020549 hashitem_T *hi;
20550 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551
Bram Moolenaar33570922005-01-25 22:26:29 +000020552 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020553 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020554 for (hi = ht->ht_array; todo > 0; ++hi)
20555 {
20556 if (!HASHITEM_EMPTY(hi))
20557 {
20558 --todo;
20559
Bram Moolenaar33570922005-01-25 22:26:29 +000020560 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020561 * ht_array might change then. hash_clear() takes care of it
20562 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020563 v = HI2DI(hi);
20564 if (free_val)
20565 clear_tv(&v->di_tv);
20566 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20567 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020568 }
20569 }
20570 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020571 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572}
20573
Bram Moolenaara7043832005-01-21 11:56:39 +000020574/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020575 * Delete a variable from hashtab "ht" at item "hi".
20576 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020579delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020580 hashtab_T *ht;
20581 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582{
Bram Moolenaar33570922005-01-25 22:26:29 +000020583 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020584
20585 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020586 clear_tv(&di->di_tv);
20587 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588}
20589
20590/*
20591 * List the value of one internal variable.
20592 */
20593 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020594list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020595 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020597 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020599 char_u *tofree;
20600 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020601 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020602
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020603 current_copyID += COPYID_INC;
20604 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020605 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020606 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608}
20609
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020611list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 char_u *prefix;
20613 char_u *name;
20614 int type;
20615 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020616 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617{
Bram Moolenaar31859182007-08-14 20:41:13 +000020618 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20619 msg_start();
20620 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 if (name != NULL) /* "a:" vars don't have a name stored */
20622 msg_puts(name);
20623 msg_putchar(' ');
20624 msg_advance(22);
20625 if (type == VAR_NUMBER)
20626 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020627 else if (type == VAR_FUNC)
20628 msg_putchar('*');
20629 else if (type == VAR_LIST)
20630 {
20631 msg_putchar('[');
20632 if (*string == '[')
20633 ++string;
20634 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020635 else if (type == VAR_DICT)
20636 {
20637 msg_putchar('{');
20638 if (*string == '{')
20639 ++string;
20640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 else
20642 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020643
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020645
20646 if (type == VAR_FUNC)
20647 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020648 if (*first)
20649 {
20650 msg_clr_eos();
20651 *first = FALSE;
20652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653}
20654
20655/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020656 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 * If the variable already exists, the value is updated.
20658 * Otherwise the variable is created.
20659 */
20660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020663 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020664 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665{
Bram Moolenaar33570922005-01-25 22:26:29 +000020666 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020667 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020668 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020670 ht = find_var_ht(name, &varname);
20671 if (ht == NULL || *varname == NUL)
20672 {
20673 EMSG2(_(e_illvar), name);
20674 return;
20675 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020676 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020677
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020678 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20679 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020680
Bram Moolenaar33570922005-01-25 22:26:29 +000020681 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020683 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020684 if (var_check_ro(v->di_flags, name)
20685 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020686 return;
20687 if (v->di_tv.v_type != tv->v_type
20688 && !((v->di_tv.v_type == VAR_STRING
20689 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020690 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020691 || tv->v_type == VAR_NUMBER))
20692#ifdef FEAT_FLOAT
20693 && !((v->di_tv.v_type == VAR_NUMBER
20694 || v->di_tv.v_type == VAR_FLOAT)
20695 && (tv->v_type == VAR_NUMBER
20696 || tv->v_type == VAR_FLOAT))
20697#endif
20698 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020699 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020700 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020701 return;
20702 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020703
20704 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020705 * Handle setting internal v: variables separately: we don't change
20706 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020707 */
20708 if (ht == &vimvarht)
20709 {
20710 if (v->di_tv.v_type == VAR_STRING)
20711 {
20712 vim_free(v->di_tv.vval.v_string);
20713 if (copy || tv->v_type != VAR_STRING)
20714 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20715 else
20716 {
20717 /* Take over the string to avoid an extra alloc/free. */
20718 v->di_tv.vval.v_string = tv->vval.v_string;
20719 tv->vval.v_string = NULL;
20720 }
20721 }
20722 else if (v->di_tv.v_type != VAR_NUMBER)
20723 EMSG2(_(e_intern2), "set_var()");
20724 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020725 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020726 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020727 if (STRCMP(varname, "searchforward") == 0)
20728 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020729#ifdef FEAT_SEARCH_EXTRA
20730 else if (STRCMP(varname, "hlsearch") == 0)
20731 {
20732 no_hlsearch = !v->di_tv.vval.v_number;
20733 redraw_all_later(SOME_VALID);
20734 }
20735#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020736 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020737 return;
20738 }
20739
20740 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 }
20742 else /* add a new variable */
20743 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020744 /* Can't add "v:" variable. */
20745 if (ht == &vimvarht)
20746 {
20747 EMSG2(_(e_illvar), name);
20748 return;
20749 }
20750
Bram Moolenaar92124a32005-06-17 22:03:40 +000020751 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020752 if (!valid_varname(varname))
20753 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020754
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020755 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20756 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020757 if (v == NULL)
20758 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020759 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020760 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020762 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 return;
20764 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020765 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020767
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020768 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020769 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020770 else
20771 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020772 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020773 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020774 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776}
20777
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020778/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020779 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020780 * Also give an error message.
20781 */
20782 static int
20783var_check_ro(flags, name)
20784 int flags;
20785 char_u *name;
20786{
20787 if (flags & DI_FLAGS_RO)
20788 {
20789 EMSG2(_(e_readonlyvar), name);
20790 return TRUE;
20791 }
20792 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20793 {
20794 EMSG2(_(e_readonlysbx), name);
20795 return TRUE;
20796 }
20797 return FALSE;
20798}
20799
20800/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020801 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20802 * Also give an error message.
20803 */
20804 static int
20805var_check_fixed(flags, name)
20806 int flags;
20807 char_u *name;
20808{
20809 if (flags & DI_FLAGS_FIX)
20810 {
20811 EMSG2(_("E795: Cannot delete variable %s"), name);
20812 return TRUE;
20813 }
20814 return FALSE;
20815}
20816
20817/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020818 * Check if a funcref is assigned to a valid variable name.
20819 * Return TRUE and give an error if not.
20820 */
20821 static int
20822var_check_func_name(name, new_var)
20823 char_u *name; /* points to start of variable name */
20824 int new_var; /* TRUE when creating the variable */
20825{
20826 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20827 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20828 ? name[2] : name[0]))
20829 {
20830 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20831 name);
20832 return TRUE;
20833 }
20834 /* Don't allow hiding a function. When "v" is not NULL we might be
20835 * assigning another function to the same var, the type is checked
20836 * below. */
20837 if (new_var && function_exists(name))
20838 {
20839 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20840 name);
20841 return TRUE;
20842 }
20843 return FALSE;
20844}
20845
20846/*
20847 * Check if a variable name is valid.
20848 * Return FALSE and give an error if not.
20849 */
20850 static int
20851valid_varname(varname)
20852 char_u *varname;
20853{
20854 char_u *p;
20855
20856 for (p = varname; *p != NUL; ++p)
20857 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20858 && *p != AUTOLOAD_CHAR)
20859 {
20860 EMSG2(_(e_illvar), varname);
20861 return FALSE;
20862 }
20863 return TRUE;
20864}
20865
20866/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020867 * Return TRUE if typeval "tv" is set to be locked (immutable).
20868 * Also give an error message, using "name".
20869 */
20870 static int
20871tv_check_lock(lock, name)
20872 int lock;
20873 char_u *name;
20874{
20875 if (lock & VAR_LOCKED)
20876 {
20877 EMSG2(_("E741: Value is locked: %s"),
20878 name == NULL ? (char_u *)_("Unknown") : name);
20879 return TRUE;
20880 }
20881 if (lock & VAR_FIXED)
20882 {
20883 EMSG2(_("E742: Cannot change value of %s"),
20884 name == NULL ? (char_u *)_("Unknown") : name);
20885 return TRUE;
20886 }
20887 return FALSE;
20888}
20889
20890/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020891 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020892 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020893 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020894 * It is OK for "from" and "to" to point to the same item. This is used to
20895 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020896 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020897 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 typval_T *from;
20900 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020902 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020903 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020904 switch (from->v_type)
20905 {
20906 case VAR_NUMBER:
20907 to->vval.v_number = from->vval.v_number;
20908 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020909#ifdef FEAT_FLOAT
20910 case VAR_FLOAT:
20911 to->vval.v_float = from->vval.v_float;
20912 break;
20913#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020914 case VAR_STRING:
20915 case VAR_FUNC:
20916 if (from->vval.v_string == NULL)
20917 to->vval.v_string = NULL;
20918 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020919 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020920 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020921 if (from->v_type == VAR_FUNC)
20922 func_ref(to->vval.v_string);
20923 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020924 break;
20925 case VAR_LIST:
20926 if (from->vval.v_list == NULL)
20927 to->vval.v_list = NULL;
20928 else
20929 {
20930 to->vval.v_list = from->vval.v_list;
20931 ++to->vval.v_list->lv_refcount;
20932 }
20933 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020934 case VAR_DICT:
20935 if (from->vval.v_dict == NULL)
20936 to->vval.v_dict = NULL;
20937 else
20938 {
20939 to->vval.v_dict = from->vval.v_dict;
20940 ++to->vval.v_dict->dv_refcount;
20941 }
20942 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020943 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020945 break;
20946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947}
20948
20949/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020950 * Make a copy of an item.
20951 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020952 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20953 * reference to an already copied list/dict can be used.
20954 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020955 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020956 static int
20957item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020958 typval_T *from;
20959 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020960 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020961 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020962{
20963 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020964 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020965
Bram Moolenaar33570922005-01-25 22:26:29 +000020966 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020967 {
20968 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020969 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020970 }
20971 ++recurse;
20972
20973 switch (from->v_type)
20974 {
20975 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020976#ifdef FEAT_FLOAT
20977 case VAR_FLOAT:
20978#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020979 case VAR_STRING:
20980 case VAR_FUNC:
20981 copy_tv(from, to);
20982 break;
20983 case VAR_LIST:
20984 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020985 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020986 if (from->vval.v_list == NULL)
20987 to->vval.v_list = NULL;
20988 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20989 {
20990 /* use the copy made earlier */
20991 to->vval.v_list = from->vval.v_list->lv_copylist;
20992 ++to->vval.v_list->lv_refcount;
20993 }
20994 else
20995 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20996 if (to->vval.v_list == NULL)
20997 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020998 break;
20999 case VAR_DICT:
21000 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021001 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021002 if (from->vval.v_dict == NULL)
21003 to->vval.v_dict = NULL;
21004 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21005 {
21006 /* use the copy made earlier */
21007 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21008 ++to->vval.v_dict->dv_refcount;
21009 }
21010 else
21011 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21012 if (to->vval.v_dict == NULL)
21013 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021014 break;
21015 default:
21016 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021017 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021018 }
21019 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021020 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021021}
21022
21023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 * ":echo expr1 ..." print each argument separated with a space, add a
21025 * newline at the end.
21026 * ":echon expr1 ..." print each argument plain.
21027 */
21028 void
21029ex_echo(eap)
21030 exarg_T *eap;
21031{
21032 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021033 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021034 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 char_u *p;
21036 int needclr = TRUE;
21037 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021038 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039
21040 if (eap->skip)
21041 ++emsg_skip;
21042 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021044 /* If eval1() causes an error message the text from the command may
21045 * still need to be cleared. E.g., "echo 22,44". */
21046 need_clr_eos = needclr;
21047
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021049 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 {
21051 /*
21052 * Report the invalid expression unless the expression evaluation
21053 * has been cancelled due to an aborting error, an interrupt, or an
21054 * exception.
21055 */
21056 if (!aborting())
21057 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021058 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 break;
21060 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021061 need_clr_eos = FALSE;
21062
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 if (!eap->skip)
21064 {
21065 if (atstart)
21066 {
21067 atstart = FALSE;
21068 /* Call msg_start() after eval1(), evaluating the expression
21069 * may cause a message to appear. */
21070 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021071 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021072 /* Mark the saved text as finishing the line, so that what
21073 * follows is displayed on a new line when scrolling back
21074 * at the more prompt. */
21075 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 }
21079 else if (eap->cmdidx == CMD_echo)
21080 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021081 current_copyID += COPYID_INC;
21082 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021083 if (p != NULL)
21084 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021086 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021088 if (*p != TAB && needclr)
21089 {
21090 /* remove any text still there from the command */
21091 msg_clr_eos();
21092 needclr = FALSE;
21093 }
21094 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095 }
21096 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021097 {
21098#ifdef FEAT_MBYTE
21099 if (has_mbyte)
21100 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021101 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021102
21103 (void)msg_outtrans_len_attr(p, i, echo_attr);
21104 p += i - 1;
21105 }
21106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021108 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021111 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 arg = skipwhite(arg);
21115 }
21116 eap->nextcmd = check_nextcmd(arg);
21117
21118 if (eap->skip)
21119 --emsg_skip;
21120 else
21121 {
21122 /* remove text that may still be there from the command */
21123 if (needclr)
21124 msg_clr_eos();
21125 if (eap->cmdidx == CMD_echo)
21126 msg_end();
21127 }
21128}
21129
21130/*
21131 * ":echohl {name}".
21132 */
21133 void
21134ex_echohl(eap)
21135 exarg_T *eap;
21136{
21137 int id;
21138
21139 id = syn_name2id(eap->arg);
21140 if (id == 0)
21141 echo_attr = 0;
21142 else
21143 echo_attr = syn_id2attr(id);
21144}
21145
21146/*
21147 * ":execute expr1 ..." execute the result of an expression.
21148 * ":echomsg expr1 ..." Print a message
21149 * ":echoerr expr1 ..." Print an error
21150 * Each gets spaces around each argument and a newline at the end for
21151 * echo commands
21152 */
21153 void
21154ex_execute(eap)
21155 exarg_T *eap;
21156{
21157 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021158 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 int ret = OK;
21160 char_u *p;
21161 garray_T ga;
21162 int len;
21163 int save_did_emsg;
21164
21165 ga_init2(&ga, 1, 80);
21166
21167 if (eap->skip)
21168 ++emsg_skip;
21169 while (*arg != NUL && *arg != '|' && *arg != '\n')
21170 {
21171 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021172 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 {
21174 /*
21175 * Report the invalid expression unless the expression evaluation
21176 * has been cancelled due to an aborting error, an interrupt, or an
21177 * exception.
21178 */
21179 if (!aborting())
21180 EMSG2(_(e_invexpr2), p);
21181 ret = FAIL;
21182 break;
21183 }
21184
21185 if (!eap->skip)
21186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 len = (int)STRLEN(p);
21189 if (ga_grow(&ga, len + 2) == FAIL)
21190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 ret = FAIL;
21193 break;
21194 }
21195 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 ga.ga_len += len;
21199 }
21200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021201 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 arg = skipwhite(arg);
21203 }
21204
21205 if (ret != FAIL && ga.ga_data != NULL)
21206 {
21207 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021210 out_flush();
21211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 else if (eap->cmdidx == CMD_echoerr)
21213 {
21214 /* We don't want to abort following commands, restore did_emsg. */
21215 save_did_emsg = did_emsg;
21216 EMSG((char_u *)ga.ga_data);
21217 if (!force_abort)
21218 did_emsg = save_did_emsg;
21219 }
21220 else if (eap->cmdidx == CMD_execute)
21221 do_cmdline((char_u *)ga.ga_data,
21222 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21223 }
21224
21225 ga_clear(&ga);
21226
21227 if (eap->skip)
21228 --emsg_skip;
21229
21230 eap->nextcmd = check_nextcmd(arg);
21231}
21232
21233/*
21234 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21235 * "arg" points to the "&" or '+' when called, to "option" when returning.
21236 * Returns NULL when no option name found. Otherwise pointer to the char
21237 * after the option name.
21238 */
21239 static char_u *
21240find_option_end(arg, opt_flags)
21241 char_u **arg;
21242 int *opt_flags;
21243{
21244 char_u *p = *arg;
21245
21246 ++p;
21247 if (*p == 'g' && p[1] == ':')
21248 {
21249 *opt_flags = OPT_GLOBAL;
21250 p += 2;
21251 }
21252 else if (*p == 'l' && p[1] == ':')
21253 {
21254 *opt_flags = OPT_LOCAL;
21255 p += 2;
21256 }
21257 else
21258 *opt_flags = 0;
21259
21260 if (!ASCII_ISALPHA(*p))
21261 return NULL;
21262 *arg = p;
21263
21264 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21265 p += 4; /* termcap option */
21266 else
21267 while (ASCII_ISALPHA(*p))
21268 ++p;
21269 return p;
21270}
21271
21272/*
21273 * ":function"
21274 */
21275 void
21276ex_function(eap)
21277 exarg_T *eap;
21278{
21279 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021280 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 int j;
21282 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021284 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 char_u *name = NULL;
21286 char_u *p;
21287 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021288 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 garray_T newargs;
21290 garray_T newlines;
21291 int varargs = FALSE;
21292 int mustend = FALSE;
21293 int flags = 0;
21294 ufunc_T *fp;
21295 int indent;
21296 int nesting;
21297 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021298 dictitem_T *v;
21299 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021300 static int func_nr = 0; /* number for nameless function */
21301 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021302 hashtab_T *ht;
21303 int todo;
21304 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021305 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306
21307 /*
21308 * ":function" without argument: list functions.
21309 */
21310 if (ends_excmd(*eap->arg))
21311 {
21312 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021313 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021314 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021315 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021316 {
21317 if (!HASHITEM_EMPTY(hi))
21318 {
21319 --todo;
21320 fp = HI2UF(hi);
21321 if (!isdigit(*fp->uf_name))
21322 list_func_head(fp, FALSE);
21323 }
21324 }
21325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 eap->nextcmd = check_nextcmd(eap->arg);
21327 return;
21328 }
21329
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021330 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021331 * ":function /pat": list functions matching pattern.
21332 */
21333 if (*eap->arg == '/')
21334 {
21335 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21336 if (!eap->skip)
21337 {
21338 regmatch_T regmatch;
21339
21340 c = *p;
21341 *p = NUL;
21342 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21343 *p = c;
21344 if (regmatch.regprog != NULL)
21345 {
21346 regmatch.rm_ic = p_ic;
21347
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021348 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021349 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21350 {
21351 if (!HASHITEM_EMPTY(hi))
21352 {
21353 --todo;
21354 fp = HI2UF(hi);
21355 if (!isdigit(*fp->uf_name)
21356 && vim_regexec(&regmatch, fp->uf_name, 0))
21357 list_func_head(fp, FALSE);
21358 }
21359 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021360 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021361 }
21362 }
21363 if (*p == '/')
21364 ++p;
21365 eap->nextcmd = check_nextcmd(p);
21366 return;
21367 }
21368
21369 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021370 * Get the function name. There are these situations:
21371 * func normal function name
21372 * "name" == func, "fudi.fd_dict" == NULL
21373 * dict.func new dictionary entry
21374 * "name" == NULL, "fudi.fd_dict" set,
21375 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21376 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021377 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021378 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21379 * dict.func existing dict entry that's not a Funcref
21380 * "name" == NULL, "fudi.fd_dict" set,
21381 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021384 name = trans_function_name(&p, eap->skip, 0, &fudi);
21385 paren = (vim_strchr(p, '(') != NULL);
21386 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 {
21388 /*
21389 * Return on an invalid expression in braces, unless the expression
21390 * evaluation has been cancelled due to an aborting error, an
21391 * interrupt, or an exception.
21392 */
21393 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021395 if (!eap->skip && fudi.fd_newkey != NULL)
21396 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021397 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 else
21401 eap->skip = TRUE;
21402 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021403
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 /* An error in a function call during evaluation of an expression in magic
21405 * braces should not cause the function not to be defined. */
21406 saved_did_emsg = did_emsg;
21407 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408
21409 /*
21410 * ":function func" with only function name: list function.
21411 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021412 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 {
21414 if (!ends_excmd(*skipwhite(p)))
21415 {
21416 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021417 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 }
21419 eap->nextcmd = check_nextcmd(p);
21420 if (eap->nextcmd != NULL)
21421 *p = NUL;
21422 if (!eap->skip && !got_int)
21423 {
21424 fp = find_func(name);
21425 if (fp != NULL)
21426 {
21427 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021428 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021430 if (FUNCLINE(fp, j) == NULL)
21431 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 msg_putchar('\n');
21433 msg_outnum((long)(j + 1));
21434 if (j < 9)
21435 msg_putchar(' ');
21436 if (j < 99)
21437 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021438 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 out_flush(); /* show a line at a time */
21440 ui_breakcheck();
21441 }
21442 if (!got_int)
21443 {
21444 msg_putchar('\n');
21445 msg_puts((char_u *)" endfunction");
21446 }
21447 }
21448 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021449 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021451 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 }
21453
21454 /*
21455 * ":function name(arg1, arg2)" Define function.
21456 */
21457 p = skipwhite(p);
21458 if (*p != '(')
21459 {
21460 if (!eap->skip)
21461 {
21462 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021463 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 }
21465 /* attempt to continue by skipping some text */
21466 if (vim_strchr(p, '(') != NULL)
21467 p = vim_strchr(p, '(');
21468 }
21469 p = skipwhite(p + 1);
21470
21471 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21472 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21473
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021474 if (!eap->skip)
21475 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021476 /* Check the name of the function. Unless it's a dictionary function
21477 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021478 if (name != NULL)
21479 arg = name;
21480 else
21481 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021482 if (arg != NULL && (fudi.fd_di == NULL
21483 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021484 {
21485 if (*arg == K_SPECIAL)
21486 j = 3;
21487 else
21488 j = 0;
21489 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21490 : eval_isnamec(arg[j])))
21491 ++j;
21492 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021493 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021494 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021495 /* Disallow using the g: dict. */
21496 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21497 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021498 }
21499
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 /*
21501 * Isolate the arguments: "arg1, arg2, ...)"
21502 */
21503 while (*p != ')')
21504 {
21505 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21506 {
21507 varargs = TRUE;
21508 p += 3;
21509 mustend = TRUE;
21510 }
21511 else
21512 {
21513 arg = p;
21514 while (ASCII_ISALNUM(*p) || *p == '_')
21515 ++p;
21516 if (arg == p || isdigit(*arg)
21517 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21518 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21519 {
21520 if (!eap->skip)
21521 EMSG2(_("E125: Illegal argument: %s"), arg);
21522 break;
21523 }
21524 if (ga_grow(&newargs, 1) == FAIL)
21525 goto erret;
21526 c = *p;
21527 *p = NUL;
21528 arg = vim_strsave(arg);
21529 if (arg == NULL)
21530 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021531
21532 /* Check for duplicate argument name. */
21533 for (i = 0; i < newargs.ga_len; ++i)
21534 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21535 {
21536 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010021537 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021538 goto erret;
21539 }
21540
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21542 *p = c;
21543 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 if (*p == ',')
21545 ++p;
21546 else
21547 mustend = TRUE;
21548 }
21549 p = skipwhite(p);
21550 if (mustend && *p != ')')
21551 {
21552 if (!eap->skip)
21553 EMSG2(_(e_invarg2), eap->arg);
21554 break;
21555 }
21556 }
21557 ++p; /* skip the ')' */
21558
Bram Moolenaare9a41262005-01-15 22:18:47 +000021559 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560 for (;;)
21561 {
21562 p = skipwhite(p);
21563 if (STRNCMP(p, "range", 5) == 0)
21564 {
21565 flags |= FC_RANGE;
21566 p += 5;
21567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021568 else if (STRNCMP(p, "dict", 4) == 0)
21569 {
21570 flags |= FC_DICT;
21571 p += 4;
21572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 else if (STRNCMP(p, "abort", 5) == 0)
21574 {
21575 flags |= FC_ABORT;
21576 p += 5;
21577 }
21578 else
21579 break;
21580 }
21581
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021582 /* When there is a line break use what follows for the function body.
21583 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21584 if (*p == '\n')
21585 line_arg = p + 1;
21586 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 EMSG(_(e_trailing));
21588
21589 /*
21590 * Read the body of the function, until ":endfunction" is found.
21591 */
21592 if (KeyTyped)
21593 {
21594 /* Check if the function already exists, don't let the user type the
21595 * whole function before telling him it doesn't work! For a script we
21596 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021597 if (!eap->skip && !eap->forceit)
21598 {
21599 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21600 EMSG(_(e_funcdict));
21601 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021602 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021605 if (!eap->skip && did_emsg)
21606 goto erret;
21607
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 msg_putchar('\n'); /* don't overwrite the function name */
21609 cmdline_row = msg_row;
21610 }
21611
21612 indent = 2;
21613 nesting = 0;
21614 for (;;)
21615 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021616 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021617 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021618 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021619 saved_wait_return = FALSE;
21620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021622 sourcing_lnum_off = sourcing_lnum;
21623
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021624 if (line_arg != NULL)
21625 {
21626 /* Use eap->arg, split up in parts by line breaks. */
21627 theline = line_arg;
21628 p = vim_strchr(theline, '\n');
21629 if (p == NULL)
21630 line_arg += STRLEN(line_arg);
21631 else
21632 {
21633 *p = NUL;
21634 line_arg = p + 1;
21635 }
21636 }
21637 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 theline = getcmdline(':', 0L, indent);
21639 else
21640 theline = eap->getline(':', eap->cookie, indent);
21641 if (KeyTyped)
21642 lines_left = Rows - 1;
21643 if (theline == NULL)
21644 {
21645 EMSG(_("E126: Missing :endfunction"));
21646 goto erret;
21647 }
21648
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021649 /* Detect line continuation: sourcing_lnum increased more than one. */
21650 if (sourcing_lnum > sourcing_lnum_off + 1)
21651 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21652 else
21653 sourcing_lnum_off = 0;
21654
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 if (skip_until != NULL)
21656 {
21657 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21658 * don't check for ":endfunc". */
21659 if (STRCMP(theline, skip_until) == 0)
21660 {
21661 vim_free(skip_until);
21662 skip_until = NULL;
21663 }
21664 }
21665 else
21666 {
21667 /* skip ':' and blanks*/
21668 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21669 ;
21670
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021671 /* Check for "endfunction". */
21672 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021674 if (line_arg == NULL)
21675 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 break;
21677 }
21678
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021679 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 * at "end". */
21681 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21682 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021683 else if (STRNCMP(p, "if", 2) == 0
21684 || STRNCMP(p, "wh", 2) == 0
21685 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 || STRNCMP(p, "try", 3) == 0)
21687 indent += 2;
21688
21689 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021690 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021692 if (*p == '!')
21693 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 p += eval_fname_script(p);
21695 if (ASCII_ISALPHA(*p))
21696 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021697 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 if (*skipwhite(p) == '(')
21699 {
21700 ++nesting;
21701 indent += 2;
21702 }
21703 }
21704 }
21705
21706 /* Check for ":append" or ":insert". */
21707 p = skip_range(p, NULL);
21708 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21709 || (p[0] == 'i'
21710 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21711 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21712 skip_until = vim_strsave((char_u *)".");
21713
21714 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21715 arg = skipwhite(skiptowhite(p));
21716 if (arg[0] == '<' && arg[1] =='<'
21717 && ((p[0] == 'p' && p[1] == 'y'
21718 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21719 || (p[0] == 'p' && p[1] == 'e'
21720 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21721 || (p[0] == 't' && p[1] == 'c'
21722 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021723 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21724 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21726 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021727 || (p[0] == 'm' && p[1] == 'z'
21728 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 ))
21730 {
21731 /* ":python <<" continues until a dot, like ":append" */
21732 p = skipwhite(arg + 2);
21733 if (*p == NUL)
21734 skip_until = vim_strsave((char_u *)".");
21735 else
21736 skip_until = vim_strsave(p);
21737 }
21738 }
21739
21740 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021741 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021742 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021743 if (line_arg == NULL)
21744 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021746 }
21747
21748 /* Copy the line to newly allocated memory. get_one_sourceline()
21749 * allocates 250 bytes per line, this saves 80% on average. The cost
21750 * is an extra alloc/free. */
21751 p = vim_strsave(theline);
21752 if (p != NULL)
21753 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021754 if (line_arg == NULL)
21755 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021756 theline = p;
21757 }
21758
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021759 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21760
21761 /* Add NULL lines for continuation lines, so that the line count is
21762 * equal to the index in the growarray. */
21763 while (sourcing_lnum_off-- > 0)
21764 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021765
21766 /* Check for end of eap->arg. */
21767 if (line_arg != NULL && *line_arg == NUL)
21768 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 }
21770
21771 /* Don't define the function when skipping commands or when an error was
21772 * detected. */
21773 if (eap->skip || did_emsg)
21774 goto erret;
21775
21776 /*
21777 * If there are no errors, add the function
21778 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021779 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021780 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021781 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021783 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021784 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021785 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021786 goto erret;
21787 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021788
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021789 fp = find_func(name);
21790 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021792 if (!eap->forceit)
21793 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021794 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021795 goto erret;
21796 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021797 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021798 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021799 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 name);
21801 goto erret;
21802 }
21803 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021804 ga_clear_strings(&(fp->uf_args));
21805 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021806 vim_free(name);
21807 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 }
21810 else
21811 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021812 char numbuf[20];
21813
21814 fp = NULL;
21815 if (fudi.fd_newkey == NULL && !eap->forceit)
21816 {
21817 EMSG(_(e_funcdict));
21818 goto erret;
21819 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021820 if (fudi.fd_di == NULL)
21821 {
21822 /* Can't add a function to a locked dictionary */
21823 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21824 goto erret;
21825 }
21826 /* Can't change an existing function if it is locked */
21827 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21828 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021829
21830 /* Give the function a sequential number. Can only be used with a
21831 * Funcref! */
21832 vim_free(name);
21833 sprintf(numbuf, "%d", ++func_nr);
21834 name = vim_strsave((char_u *)numbuf);
21835 if (name == NULL)
21836 goto erret;
21837 }
21838
21839 if (fp == NULL)
21840 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021841 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021842 {
21843 int slen, plen;
21844 char_u *scriptname;
21845
21846 /* Check that the autoload name matches the script name. */
21847 j = FAIL;
21848 if (sourcing_name != NULL)
21849 {
21850 scriptname = autoload_name(name);
21851 if (scriptname != NULL)
21852 {
21853 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021854 plen = (int)STRLEN(p);
21855 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021856 if (slen > plen && fnamecmp(p,
21857 sourcing_name + slen - plen) == 0)
21858 j = OK;
21859 vim_free(scriptname);
21860 }
21861 }
21862 if (j == FAIL)
21863 {
21864 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21865 goto erret;
21866 }
21867 }
21868
21869 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 if (fp == NULL)
21871 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021872
21873 if (fudi.fd_dict != NULL)
21874 {
21875 if (fudi.fd_di == NULL)
21876 {
21877 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021878 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021879 if (fudi.fd_di == NULL)
21880 {
21881 vim_free(fp);
21882 goto erret;
21883 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021884 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21885 {
21886 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021887 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021888 goto erret;
21889 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021890 }
21891 else
21892 /* overwrite existing dict entry */
21893 clear_tv(&fudi.fd_di->di_tv);
21894 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021895 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021896 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021897 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021898
21899 /* behave like "dict" was used */
21900 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021901 }
21902
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021904 STRCPY(fp->uf_name, name);
21905 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021907 fp->uf_args = newargs;
21908 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021909#ifdef FEAT_PROFILE
21910 fp->uf_tml_count = NULL;
21911 fp->uf_tml_total = NULL;
21912 fp->uf_tml_self = NULL;
21913 fp->uf_profiling = FALSE;
21914 if (prof_def_func())
21915 func_do_profile(fp);
21916#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021917 fp->uf_varargs = varargs;
21918 fp->uf_flags = flags;
21919 fp->uf_calls = 0;
21920 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021921 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922
21923erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 ga_clear_strings(&newargs);
21925 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021926ret_free:
21927 vim_free(skip_until);
21928 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021931 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932}
21933
21934/*
21935 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021936 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021938 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021939 * TFN_INT: internal function name OK
21940 * TFN_QUIET: be quiet
21941 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 * Advances "pp" to just after the function name (if no error).
21943 */
21944 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021945trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 char_u **pp;
21947 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021948 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021949 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021951 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 char_u *start;
21953 char_u *end;
21954 int lead;
21955 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021957 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021958
21959 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021960 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021962
21963 /* Check for hard coded <SNR>: already translated function ID (from a user
21964 * command). */
21965 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21966 && (*pp)[2] == (int)KE_SNR)
21967 {
21968 *pp += 3;
21969 len = get_id_len(pp) + 3;
21970 return vim_strnsave(start, len);
21971 }
21972
21973 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21974 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021976 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021978
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021979 /* Note that TFN_ flags use the same values as GLV_ flags. */
21980 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021981 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021982 if (end == start)
21983 {
21984 if (!skip)
21985 EMSG(_("E129: Function name required"));
21986 goto theend;
21987 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021988 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021989 {
21990 /*
21991 * Report an invalid expression in braces, unless the expression
21992 * evaluation has been cancelled due to an aborting error, an
21993 * interrupt, or an exception.
21994 */
21995 if (!aborting())
21996 {
21997 if (end != NULL)
21998 EMSG2(_(e_invarg2), start);
21999 }
22000 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022001 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022002 goto theend;
22003 }
22004
22005 if (lv.ll_tv != NULL)
22006 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022007 if (fdp != NULL)
22008 {
22009 fdp->fd_dict = lv.ll_dict;
22010 fdp->fd_newkey = lv.ll_newkey;
22011 lv.ll_newkey = NULL;
22012 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022014 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22015 {
22016 name = vim_strsave(lv.ll_tv->vval.v_string);
22017 *pp = end;
22018 }
22019 else
22020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022021 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22022 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022023 EMSG(_(e_funcref));
22024 else
22025 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022026 name = NULL;
22027 }
22028 goto theend;
22029 }
22030
22031 if (lv.ll_name == NULL)
22032 {
22033 /* Error found, but continue after the function name. */
22034 *pp = end;
22035 goto theend;
22036 }
22037
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022038 /* Check if the name is a Funcref. If so, use the value. */
22039 if (lv.ll_exp_name != NULL)
22040 {
22041 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022042 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022043 if (name == lv.ll_exp_name)
22044 name = NULL;
22045 }
22046 else
22047 {
22048 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022049 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022050 if (name == *pp)
22051 name = NULL;
22052 }
22053 if (name != NULL)
22054 {
22055 name = vim_strsave(name);
22056 *pp = end;
22057 goto theend;
22058 }
22059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022060 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022061 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022062 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022063 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22064 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22065 {
22066 /* When there was "s:" already or the name expanded to get a
22067 * leading "s:" then remove it. */
22068 lv.ll_name += 2;
22069 len -= 2;
22070 lead = 2;
22071 }
22072 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022073 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022074 {
22075 if (lead == 2) /* skip over "s:" */
22076 lv.ll_name += 2;
22077 len = (int)(end - lv.ll_name);
22078 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022079
22080 /*
22081 * Copy the function name to allocated memory.
22082 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22083 * Accept <SNR>123_name() outside a script.
22084 */
22085 if (skip)
22086 lead = 0; /* do nothing */
22087 else if (lead > 0)
22088 {
22089 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022090 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22091 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022092 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022093 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022094 if (current_SID <= 0)
22095 {
22096 EMSG(_(e_usingsid));
22097 goto theend;
22098 }
22099 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22100 lead += (int)STRLEN(sid_buf);
22101 }
22102 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022103 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022104 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022105 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022106 goto theend;
22107 }
22108 name = alloc((unsigned)(len + lead + 1));
22109 if (name != NULL)
22110 {
22111 if (lead > 0)
22112 {
22113 name[0] = K_SPECIAL;
22114 name[1] = KS_EXTRA;
22115 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022116 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022117 STRCPY(name + 3, sid_buf);
22118 }
22119 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22120 name[len + lead] = NUL;
22121 }
22122 *pp = end;
22123
22124theend:
22125 clear_lval(&lv);
22126 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127}
22128
22129/*
22130 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22131 * Return 2 if "p" starts with "s:".
22132 * Return 0 otherwise.
22133 */
22134 static int
22135eval_fname_script(p)
22136 char_u *p;
22137{
22138 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22139 || STRNICMP(p + 1, "SNR>", 4) == 0))
22140 return 5;
22141 if (p[0] == 's' && p[1] == ':')
22142 return 2;
22143 return 0;
22144}
22145
22146/*
22147 * Return TRUE if "p" starts with "<SID>" or "s:".
22148 * Only works if eval_fname_script() returned non-zero for "p"!
22149 */
22150 static int
22151eval_fname_sid(p)
22152 char_u *p;
22153{
22154 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22155}
22156
22157/*
22158 * List the head of the function: "name(arg1, arg2)".
22159 */
22160 static void
22161list_func_head(fp, indent)
22162 ufunc_T *fp;
22163 int indent;
22164{
22165 int j;
22166
22167 msg_start();
22168 if (indent)
22169 MSG_PUTS(" ");
22170 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022171 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 {
22173 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022174 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 }
22176 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022177 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022179 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 {
22181 if (j)
22182 MSG_PUTS(", ");
22183 msg_puts(FUNCARG(fp, j));
22184 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022185 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 {
22187 if (j)
22188 MSG_PUTS(", ");
22189 MSG_PUTS("...");
22190 }
22191 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022192 if (fp->uf_flags & FC_ABORT)
22193 MSG_PUTS(" abort");
22194 if (fp->uf_flags & FC_RANGE)
22195 MSG_PUTS(" range");
22196 if (fp->uf_flags & FC_DICT)
22197 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022198 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022199 if (p_verbose > 0)
22200 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201}
22202
22203/*
22204 * Find a function by name, return pointer to it in ufuncs.
22205 * Return NULL for unknown function.
22206 */
22207 static ufunc_T *
22208find_func(name)
22209 char_u *name;
22210{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022211 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022213 hi = hash_find(&func_hashtab, name);
22214 if (!HASHITEM_EMPTY(hi))
22215 return HI2UF(hi);
22216 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217}
22218
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022219#if defined(EXITFREE) || defined(PROTO)
22220 void
22221free_all_functions()
22222{
22223 hashitem_T *hi;
22224
22225 /* Need to start all over every time, because func_free() may change the
22226 * hash table. */
22227 while (func_hashtab.ht_used > 0)
22228 for (hi = func_hashtab.ht_array; ; ++hi)
22229 if (!HASHITEM_EMPTY(hi))
22230 {
22231 func_free(HI2UF(hi));
22232 break;
22233 }
22234}
22235#endif
22236
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022237 int
22238translated_function_exists(name)
22239 char_u *name;
22240{
22241 if (builtin_function(name))
22242 return find_internal_func(name) >= 0;
22243 return find_func(name) != NULL;
22244}
22245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022246/*
22247 * Return TRUE if a function "name" exists.
22248 */
22249 static int
22250function_exists(name)
22251 char_u *name;
22252{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022253 char_u *nm = name;
22254 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022255 int n = FALSE;
22256
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022257 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22258 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022259 nm = skipwhite(nm);
22260
22261 /* Only accept "funcname", "funcname ", "funcname (..." and
22262 * "funcname(...", not "funcname!...". */
22263 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022264 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022265 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022266 return n;
22267}
22268
Bram Moolenaara1544c02013-05-30 12:35:52 +020022269 char_u *
22270get_expanded_name(name, check)
22271 char_u *name;
22272 int check;
22273{
22274 char_u *nm = name;
22275 char_u *p;
22276
22277 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22278
22279 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022280 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022281 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022282
Bram Moolenaara1544c02013-05-30 12:35:52 +020022283 vim_free(p);
22284 return NULL;
22285}
22286
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022287/*
22288 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022289 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022290 */
22291 static int
22292builtin_function(name)
22293 char_u *name;
22294{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022295 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22296 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022297}
22298
Bram Moolenaar05159a02005-02-26 23:04:13 +000022299#if defined(FEAT_PROFILE) || defined(PROTO)
22300/*
22301 * Start profiling function "fp".
22302 */
22303 static void
22304func_do_profile(fp)
22305 ufunc_T *fp;
22306{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022307 int len = fp->uf_lines.ga_len;
22308
22309 if (len == 0)
22310 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022311 fp->uf_tm_count = 0;
22312 profile_zero(&fp->uf_tm_self);
22313 profile_zero(&fp->uf_tm_total);
22314 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022315 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022316 if (fp->uf_tml_total == NULL)
22317 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022318 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022319 if (fp->uf_tml_self == NULL)
22320 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022321 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022322 fp->uf_tml_idx = -1;
22323 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22324 || fp->uf_tml_self == NULL)
22325 return; /* out of memory */
22326
22327 fp->uf_profiling = TRUE;
22328}
22329
22330/*
22331 * Dump the profiling results for all functions in file "fd".
22332 */
22333 void
22334func_dump_profile(fd)
22335 FILE *fd;
22336{
22337 hashitem_T *hi;
22338 int todo;
22339 ufunc_T *fp;
22340 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022341 ufunc_T **sorttab;
22342 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022343
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022344 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022345 if (todo == 0)
22346 return; /* nothing to dump */
22347
Bram Moolenaar73830342005-02-28 22:48:19 +000022348 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22349
Bram Moolenaar05159a02005-02-26 23:04:13 +000022350 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22351 {
22352 if (!HASHITEM_EMPTY(hi))
22353 {
22354 --todo;
22355 fp = HI2UF(hi);
22356 if (fp->uf_profiling)
22357 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022358 if (sorttab != NULL)
22359 sorttab[st_len++] = fp;
22360
Bram Moolenaar05159a02005-02-26 23:04:13 +000022361 if (fp->uf_name[0] == K_SPECIAL)
22362 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22363 else
22364 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22365 if (fp->uf_tm_count == 1)
22366 fprintf(fd, "Called 1 time\n");
22367 else
22368 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22369 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22370 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22371 fprintf(fd, "\n");
22372 fprintf(fd, "count total (s) self (s)\n");
22373
22374 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22375 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022376 if (FUNCLINE(fp, i) == NULL)
22377 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022378 prof_func_line(fd, fp->uf_tml_count[i],
22379 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022380 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22381 }
22382 fprintf(fd, "\n");
22383 }
22384 }
22385 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022386
22387 if (sorttab != NULL && st_len > 0)
22388 {
22389 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22390 prof_total_cmp);
22391 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22392 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22393 prof_self_cmp);
22394 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22395 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022396
22397 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022398}
Bram Moolenaar73830342005-02-28 22:48:19 +000022399
22400 static void
22401prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22402 FILE *fd;
22403 ufunc_T **sorttab;
22404 int st_len;
22405 char *title;
22406 int prefer_self; /* when equal print only self time */
22407{
22408 int i;
22409 ufunc_T *fp;
22410
22411 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22412 fprintf(fd, "count total (s) self (s) function\n");
22413 for (i = 0; i < 20 && i < st_len; ++i)
22414 {
22415 fp = sorttab[i];
22416 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22417 prefer_self);
22418 if (fp->uf_name[0] == K_SPECIAL)
22419 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22420 else
22421 fprintf(fd, " %s()\n", fp->uf_name);
22422 }
22423 fprintf(fd, "\n");
22424}
22425
22426/*
22427 * Print the count and times for one function or function line.
22428 */
22429 static void
22430prof_func_line(fd, count, total, self, prefer_self)
22431 FILE *fd;
22432 int count;
22433 proftime_T *total;
22434 proftime_T *self;
22435 int prefer_self; /* when equal print only self time */
22436{
22437 if (count > 0)
22438 {
22439 fprintf(fd, "%5d ", count);
22440 if (prefer_self && profile_equal(total, self))
22441 fprintf(fd, " ");
22442 else
22443 fprintf(fd, "%s ", profile_msg(total));
22444 if (!prefer_self && profile_equal(total, self))
22445 fprintf(fd, " ");
22446 else
22447 fprintf(fd, "%s ", profile_msg(self));
22448 }
22449 else
22450 fprintf(fd, " ");
22451}
22452
22453/*
22454 * Compare function for total time sorting.
22455 */
22456 static int
22457#ifdef __BORLANDC__
22458_RTLENTRYF
22459#endif
22460prof_total_cmp(s1, s2)
22461 const void *s1;
22462 const void *s2;
22463{
22464 ufunc_T *p1, *p2;
22465
22466 p1 = *(ufunc_T **)s1;
22467 p2 = *(ufunc_T **)s2;
22468 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22469}
22470
22471/*
22472 * Compare function for self time sorting.
22473 */
22474 static int
22475#ifdef __BORLANDC__
22476_RTLENTRYF
22477#endif
22478prof_self_cmp(s1, s2)
22479 const void *s1;
22480 const void *s2;
22481{
22482 ufunc_T *p1, *p2;
22483
22484 p1 = *(ufunc_T **)s1;
22485 p2 = *(ufunc_T **)s2;
22486 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22487}
22488
Bram Moolenaar05159a02005-02-26 23:04:13 +000022489#endif
22490
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022491/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022492 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022493 * Return TRUE if a package was loaded.
22494 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022495 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022496script_autoload(name, reload)
22497 char_u *name;
22498 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022499{
22500 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022501 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022502 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022503 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022504
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022505 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022506 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022507 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022508 return FALSE;
22509
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022510 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022512 /* Find the name in the list of previously loaded package names. Skip
22513 * "autoload/", it's always the same. */
22514 for (i = 0; i < ga_loaded.ga_len; ++i)
22515 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22516 break;
22517 if (!reload && i < ga_loaded.ga_len)
22518 ret = FALSE; /* was loaded already */
22519 else
22520 {
22521 /* Remember the name if it wasn't loaded already. */
22522 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22523 {
22524 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22525 tofree = NULL;
22526 }
22527
22528 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022529 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022530 ret = TRUE;
22531 }
22532
22533 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022534 return ret;
22535}
22536
22537/*
22538 * Return the autoload script name for a function or variable name.
22539 * Returns NULL when out of memory.
22540 */
22541 static char_u *
22542autoload_name(name)
22543 char_u *name;
22544{
22545 char_u *p;
22546 char_u *scriptname;
22547
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022548 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022549 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22550 if (scriptname == NULL)
22551 return FALSE;
22552 STRCPY(scriptname, "autoload/");
22553 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022554 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022555 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022556 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022557 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022558 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022559}
22560
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22562
22563/*
22564 * Function given to ExpandGeneric() to obtain the list of user defined
22565 * function names.
22566 */
22567 char_u *
22568get_user_func_name(xp, idx)
22569 expand_T *xp;
22570 int idx;
22571{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022572 static long_u done;
22573 static hashitem_T *hi;
22574 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575
22576 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 done = 0;
22579 hi = func_hashtab.ht_array;
22580 }
22581 if (done < func_hashtab.ht_used)
22582 {
22583 if (done++ > 0)
22584 ++hi;
22585 while (HASHITEM_EMPTY(hi))
22586 ++hi;
22587 fp = HI2UF(hi);
22588
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022589 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022590 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022591
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022592 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22593 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594
22595 cat_func_name(IObuff, fp);
22596 if (xp->xp_context != EXPAND_USER_FUNC)
22597 {
22598 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022599 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 STRCAT(IObuff, ")");
22601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 return IObuff;
22603 }
22604 return NULL;
22605}
22606
22607#endif /* FEAT_CMDL_COMPL */
22608
22609/*
22610 * Copy the function name of "fp" to buffer "buf".
22611 * "buf" must be able to hold the function name plus three bytes.
22612 * Takes care of script-local function names.
22613 */
22614 static void
22615cat_func_name(buf, fp)
22616 char_u *buf;
22617 ufunc_T *fp;
22618{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022619 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 {
22621 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022622 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 }
22624 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022625 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626}
22627
22628/*
22629 * ":delfunction {name}"
22630 */
22631 void
22632ex_delfunction(eap)
22633 exarg_T *eap;
22634{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022635 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 char_u *p;
22637 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022638 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639
22640 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022641 name = trans_function_name(&p, eap->skip, 0, &fudi);
22642 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022644 {
22645 if (fudi.fd_dict != NULL && !eap->skip)
22646 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 if (!ends_excmd(*skipwhite(p)))
22650 {
22651 vim_free(name);
22652 EMSG(_(e_trailing));
22653 return;
22654 }
22655 eap->nextcmd = check_nextcmd(p);
22656 if (eap->nextcmd != NULL)
22657 *p = NUL;
22658
22659 if (!eap->skip)
22660 fp = find_func(name);
22661 vim_free(name);
22662
22663 if (!eap->skip)
22664 {
22665 if (fp == NULL)
22666 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022667 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 return;
22669 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022670 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 {
22672 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22673 return;
22674 }
22675
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022676 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022678 /* Delete the dict item that refers to the function, it will
22679 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022680 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022682 else
22683 func_free(fp);
22684 }
22685}
22686
22687/*
22688 * Free a function and remove it from the list of functions.
22689 */
22690 static void
22691func_free(fp)
22692 ufunc_T *fp;
22693{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022694 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022695
22696 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022697 ga_clear_strings(&(fp->uf_args));
22698 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022699#ifdef FEAT_PROFILE
22700 vim_free(fp->uf_tml_count);
22701 vim_free(fp->uf_tml_total);
22702 vim_free(fp->uf_tml_self);
22703#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022704
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022705 /* remove the function from the function hashtable */
22706 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22707 if (HASHITEM_EMPTY(hi))
22708 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022709 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022710 hash_remove(&func_hashtab, hi);
22711
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022712 vim_free(fp);
22713}
22714
22715/*
22716 * Unreference a Function: decrement the reference count and free it when it
22717 * becomes zero. Only for numbered functions.
22718 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022719 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022720func_unref(name)
22721 char_u *name;
22722{
22723 ufunc_T *fp;
22724
22725 if (name != NULL && isdigit(*name))
22726 {
22727 fp = find_func(name);
22728 if (fp == NULL)
22729 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022730 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022731 {
22732 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022733 * when "uf_calls" becomes zero. */
22734 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022735 func_free(fp);
22736 }
22737 }
22738}
22739
22740/*
22741 * Count a reference to a Function.
22742 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022743 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744func_ref(name)
22745 char_u *name;
22746{
22747 ufunc_T *fp;
22748
22749 if (name != NULL && isdigit(*name))
22750 {
22751 fp = find_func(name);
22752 if (fp == NULL)
22753 EMSG2(_(e_intern2), "func_ref()");
22754 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022755 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 }
22757}
22758
22759/*
22760 * Call a user function.
22761 */
22762 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022763call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 ufunc_T *fp; /* pointer to function */
22765 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 typval_T *argvars; /* arguments */
22767 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 linenr_T firstline; /* first line of range */
22769 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022770 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771{
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 char_u *save_sourcing_name;
22773 linenr_T save_sourcing_lnum;
22774 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022775 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022776 int save_did_emsg;
22777 static int depth = 0;
22778 dictitem_T *v;
22779 int fixvar_idx = 0; /* index in fixvar[] */
22780 int i;
22781 int ai;
22782 char_u numbuf[NUMBUFLEN];
22783 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022784#ifdef FEAT_PROFILE
22785 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022786 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788
22789 /* If depth of calling is getting too high, don't execute the function */
22790 if (depth >= p_mfd)
22791 {
22792 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022793 rettv->v_type = VAR_NUMBER;
22794 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 return;
22796 }
22797 ++depth;
22798
22799 line_breakcheck(); /* check for CTRL-C hit */
22800
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022801 fc = (funccall_T *)alloc(sizeof(funccall_T));
22802 fc->caller = current_funccal;
22803 current_funccal = fc;
22804 fc->func = fp;
22805 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022806 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022807 fc->linenr = 0;
22808 fc->returned = FALSE;
22809 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022811 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22812 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022815 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022816 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22817 * each argument variable and saves a lot of time.
22818 */
22819 /*
22820 * Init l: variables.
22821 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022822 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022823 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022824 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022825 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22826 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022827 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022828 name = v->di_key;
22829 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022831 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022833 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 v->di_tv.vval.v_dict = selfdict;
22835 ++selfdict->dv_refcount;
22836 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022837
Bram Moolenaar33570922005-01-25 22:26:29 +000022838 /*
22839 * Init a: variables.
22840 * Set a:0 to "argcount".
22841 * Set a:000 to a list with room for the "..." arguments.
22842 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022843 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022844 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022845 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022846 /* Use "name" to avoid a warning from some compiler that checks the
22847 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022848 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022849 name = v->di_key;
22850 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022852 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022853 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022854 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022855 v->di_tv.vval.v_list = &fc->l_varlist;
22856 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22857 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22858 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022859
22860 /*
22861 * Set a:firstline to "firstline" and a:lastline to "lastline".
22862 * Set a:name to named arguments.
22863 * Set a:N to the "..." arguments.
22864 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022865 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022866 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022867 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022868 (varnumber_T)lastline);
22869 for (i = 0; i < argcount; ++i)
22870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022871 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 if (ai < 0)
22873 /* named argument a:name */
22874 name = FUNCARG(fp, i);
22875 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022876 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 /* "..." argument a:1, a:2, etc. */
22878 sprintf((char *)numbuf, "%d", ai + 1);
22879 name = numbuf;
22880 }
22881 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22882 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022883 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022884 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22885 }
22886 else
22887 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022888 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22889 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 if (v == NULL)
22891 break;
22892 v->di_flags = DI_FLAGS_RO;
22893 }
22894 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022895 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022896
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022897 /* Note: the values are copied directly to avoid alloc/free.
22898 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022899 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022900 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022901
22902 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22903 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022904 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22905 fc->l_listitems[ai].li_tv = argvars[i];
22906 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022907 }
22908 }
22909
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 /* Don't redraw while executing the function. */
22911 ++RedrawingDisabled;
22912 save_sourcing_name = sourcing_name;
22913 save_sourcing_lnum = sourcing_lnum;
22914 sourcing_lnum = 1;
22915 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022916 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917 if (sourcing_name != NULL)
22918 {
22919 if (save_sourcing_name != NULL
22920 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22921 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22922 else
22923 STRCPY(sourcing_name, "function ");
22924 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22925
22926 if (p_verbose >= 12)
22927 {
22928 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022929 verbose_enter_scroll();
22930
Bram Moolenaar555b2802005-05-19 21:08:39 +000022931 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 if (p_verbose >= 14)
22933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022935 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022936 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022937 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938
22939 msg_puts((char_u *)"(");
22940 for (i = 0; i < argcount; ++i)
22941 {
22942 if (i > 0)
22943 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022944 if (argvars[i].v_type == VAR_NUMBER)
22945 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 else
22947 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022948 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22949 if (s != NULL)
22950 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022951 if (vim_strsize(s) > MSG_BUF_CLEN)
22952 {
22953 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22954 s = buf;
22955 }
22956 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022957 vim_free(tofree);
22958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 }
22960 }
22961 msg_puts((char_u *)")");
22962 }
22963 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022964
22965 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 --no_wait_return;
22967 }
22968 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022969#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022970 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022971 {
22972 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22973 func_do_profile(fp);
22974 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022975 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022976 {
22977 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022978 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022979 profile_zero(&fp->uf_tm_children);
22980 }
22981 script_prof_save(&wait_start);
22982 }
22983#endif
22984
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022986 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 save_did_emsg = did_emsg;
22988 did_emsg = FALSE;
22989
22990 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022991 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22993
22994 --RedrawingDisabled;
22995
22996 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022997 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022999 clear_tv(rettv);
23000 rettv->v_type = VAR_NUMBER;
23001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 }
23003
Bram Moolenaar05159a02005-02-26 23:04:13 +000023004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023005 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023006 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023007 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023008 profile_end(&call_start);
23009 profile_sub_wait(&wait_start, &call_start);
23010 profile_add(&fp->uf_tm_total, &call_start);
23011 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023012 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023013 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023014 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23015 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023016 }
23017 }
23018#endif
23019
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 /* when being verbose, mention the return value */
23021 if (p_verbose >= 12)
23022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023024 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023027 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023028 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023029 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023030 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023033 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023034 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023035 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023036 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023037
Bram Moolenaar555b2802005-05-19 21:08:39 +000023038 /* The value may be very long. Skip the middle part, so that we
23039 * have some idea how it starts and ends. smsg() would always
23040 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023041 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023042 if (s != NULL)
23043 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023044 if (vim_strsize(s) > MSG_BUF_CLEN)
23045 {
23046 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23047 s = buf;
23048 }
23049 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023050 vim_free(tofree);
23051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 }
23053 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023054
23055 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 --no_wait_return;
23057 }
23058
23059 vim_free(sourcing_name);
23060 sourcing_name = save_sourcing_name;
23061 sourcing_lnum = save_sourcing_lnum;
23062 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023063#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023064 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023065 script_prof_restore(&wait_start);
23066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067
23068 if (p_verbose >= 12 && sourcing_name != NULL)
23069 {
23070 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023071 verbose_enter_scroll();
23072
Bram Moolenaar555b2802005-05-19 21:08:39 +000023073 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023075
23076 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 --no_wait_return;
23078 }
23079
23080 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023081 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023083
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023084 /* If the a:000 list and the l: and a: dicts are not referenced we can
23085 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023086 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23087 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23088 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23089 {
23090 free_funccal(fc, FALSE);
23091 }
23092 else
23093 {
23094 hashitem_T *hi;
23095 listitem_T *li;
23096 int todo;
23097
23098 /* "fc" is still in use. This can happen when returning "a:000" or
23099 * assigning "l:" to a global variable.
23100 * Link "fc" in the list for garbage collection later. */
23101 fc->caller = previous_funccal;
23102 previous_funccal = fc;
23103
23104 /* Make a copy of the a: variables, since we didn't do that above. */
23105 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23106 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23107 {
23108 if (!HASHITEM_EMPTY(hi))
23109 {
23110 --todo;
23111 v = HI2DI(hi);
23112 copy_tv(&v->di_tv, &v->di_tv);
23113 }
23114 }
23115
23116 /* Make a copy of the a:000 items, since we didn't do that above. */
23117 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23118 copy_tv(&li->li_tv, &li->li_tv);
23119 }
23120}
23121
23122/*
23123 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023124 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023125 */
23126 static int
23127can_free_funccal(fc, copyID)
23128 funccall_T *fc;
23129 int copyID;
23130{
23131 return (fc->l_varlist.lv_copyID != copyID
23132 && fc->l_vars.dv_copyID != copyID
23133 && fc->l_avars.dv_copyID != copyID);
23134}
23135
23136/*
23137 * Free "fc" and what it contains.
23138 */
23139 static void
23140free_funccal(fc, free_val)
23141 funccall_T *fc;
23142 int free_val; /* a: vars were allocated */
23143{
23144 listitem_T *li;
23145
23146 /* The a: variables typevals may not have been allocated, only free the
23147 * allocated variables. */
23148 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23149
23150 /* free all l: variables */
23151 vars_clear(&fc->l_vars.dv_hashtab);
23152
23153 /* Free the a:000 variables if they were allocated. */
23154 if (free_val)
23155 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23156 clear_tv(&li->li_tv);
23157
23158 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159}
23160
23161/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 * Add a number variable "name" to dict "dp" with value "nr".
23163 */
23164 static void
23165add_nr_var(dp, v, name, nr)
23166 dict_T *dp;
23167 dictitem_T *v;
23168 char *name;
23169 varnumber_T nr;
23170{
23171 STRCPY(v->di_key, name);
23172 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23173 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23174 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023175 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023176 v->di_tv.vval.v_number = nr;
23177}
23178
23179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 * ":return [expr]"
23181 */
23182 void
23183ex_return(eap)
23184 exarg_T *eap;
23185{
23186 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023187 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 int returning = FALSE;
23189
23190 if (current_funccal == NULL)
23191 {
23192 EMSG(_("E133: :return not inside a function"));
23193 return;
23194 }
23195
23196 if (eap->skip)
23197 ++emsg_skip;
23198
23199 eap->nextcmd = NULL;
23200 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023201 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 {
23203 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023204 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023206 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 }
23208 /* It's safer to return also on error. */
23209 else if (!eap->skip)
23210 {
23211 /*
23212 * Return unless the expression evaluation has been cancelled due to an
23213 * aborting error, an interrupt, or an exception.
23214 */
23215 if (!aborting())
23216 returning = do_return(eap, FALSE, TRUE, NULL);
23217 }
23218
23219 /* When skipping or the return gets pending, advance to the next command
23220 * in this line (!returning). Otherwise, ignore the rest of the line.
23221 * Following lines will be ignored by get_func_line(). */
23222 if (returning)
23223 eap->nextcmd = NULL;
23224 else if (eap->nextcmd == NULL) /* no argument */
23225 eap->nextcmd = check_nextcmd(arg);
23226
23227 if (eap->skip)
23228 --emsg_skip;
23229}
23230
23231/*
23232 * Return from a function. Possibly makes the return pending. Also called
23233 * for a pending return at the ":endtry" or after returning from an extra
23234 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023236 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 * FALSE when the return gets pending.
23238 */
23239 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023240do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 exarg_T *eap;
23242 int reanimate;
23243 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023244 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245{
23246 int idx;
23247 struct condstack *cstack = eap->cstack;
23248
23249 if (reanimate)
23250 /* Undo the return. */
23251 current_funccal->returned = FALSE;
23252
23253 /*
23254 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23255 * not in its finally clause (which then is to be executed next) is found.
23256 * In this case, make the ":return" pending for execution at the ":endtry".
23257 * Otherwise, return normally.
23258 */
23259 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23260 if (idx >= 0)
23261 {
23262 cstack->cs_pending[idx] = CSTP_RETURN;
23263
23264 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023265 /* A pending return again gets pending. "rettv" points to an
23266 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023268 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 else
23270 {
23271 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023272 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023274 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023276 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 {
23278 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023279 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023280 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 else
23282 EMSG(_(e_outofmem));
23283 }
23284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023285 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286
23287 if (reanimate)
23288 {
23289 /* The pending return value could be overwritten by a ":return"
23290 * without argument in a finally clause; reset the default
23291 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023292 current_funccal->rettv->v_type = VAR_NUMBER;
23293 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 }
23295 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023296 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 }
23298 else
23299 {
23300 current_funccal->returned = TRUE;
23301
23302 /* If the return is carried out now, store the return value. For
23303 * a return immediately after reanimation, the value is already
23304 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023305 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023307 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023310 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 }
23312 }
23313
23314 return idx < 0;
23315}
23316
23317/*
23318 * Free the variable with a pending return value.
23319 */
23320 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023321discard_pending_return(rettv)
23322 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323{
Bram Moolenaar33570922005-01-25 22:26:29 +000023324 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325}
23326
23327/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023328 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 * is an allocated string. Used by report_pending() for verbose messages.
23330 */
23331 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023332get_return_cmd(rettv)
23333 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023335 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023336 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023337 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023339 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023340 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023341 if (s == NULL)
23342 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023343
23344 STRCPY(IObuff, ":return ");
23345 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23346 if (STRLEN(s) + 8 >= IOSIZE)
23347 STRCPY(IObuff + IOSIZE - 4, "...");
23348 vim_free(tofree);
23349 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350}
23351
23352/*
23353 * Get next function line.
23354 * Called by do_cmdline() to get the next line.
23355 * Returns allocated string, or NULL for end of function.
23356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 char_u *
23358get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023359 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023361 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362{
Bram Moolenaar33570922005-01-25 22:26:29 +000023363 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023364 ufunc_T *fp = fcp->func;
23365 char_u *retval;
23366 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367
23368 /* If breakpoints have been added/deleted need to check for it. */
23369 if (fcp->dbg_tick != debug_tick)
23370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023371 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 sourcing_lnum);
23373 fcp->dbg_tick = debug_tick;
23374 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023375#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023376 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023377 func_line_end(cookie);
23378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379
Bram Moolenaar05159a02005-02-26 23:04:13 +000023380 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023381 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23382 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383 retval = NULL;
23384 else
23385 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023386 /* Skip NULL lines (continuation lines). */
23387 while (fcp->linenr < gap->ga_len
23388 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23389 ++fcp->linenr;
23390 if (fcp->linenr >= gap->ga_len)
23391 retval = NULL;
23392 else
23393 {
23394 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23395 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023396#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023397 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023398 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023399#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 }
23402
23403 /* Did we encounter a breakpoint? */
23404 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23405 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023406 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023408 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 sourcing_lnum);
23410 fcp->dbg_tick = debug_tick;
23411 }
23412
23413 return retval;
23414}
23415
Bram Moolenaar05159a02005-02-26 23:04:13 +000023416#if defined(FEAT_PROFILE) || defined(PROTO)
23417/*
23418 * Called when starting to read a function line.
23419 * "sourcing_lnum" must be correct!
23420 * When skipping lines it may not actually be executed, but we won't find out
23421 * until later and we need to store the time now.
23422 */
23423 void
23424func_line_start(cookie)
23425 void *cookie;
23426{
23427 funccall_T *fcp = (funccall_T *)cookie;
23428 ufunc_T *fp = fcp->func;
23429
23430 if (fp->uf_profiling && sourcing_lnum >= 1
23431 && sourcing_lnum <= fp->uf_lines.ga_len)
23432 {
23433 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023434 /* Skip continuation lines. */
23435 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23436 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023437 fp->uf_tml_execed = FALSE;
23438 profile_start(&fp->uf_tml_start);
23439 profile_zero(&fp->uf_tml_children);
23440 profile_get_wait(&fp->uf_tml_wait);
23441 }
23442}
23443
23444/*
23445 * Called when actually executing a function line.
23446 */
23447 void
23448func_line_exec(cookie)
23449 void *cookie;
23450{
23451 funccall_T *fcp = (funccall_T *)cookie;
23452 ufunc_T *fp = fcp->func;
23453
23454 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23455 fp->uf_tml_execed = TRUE;
23456}
23457
23458/*
23459 * Called when done with a function line.
23460 */
23461 void
23462func_line_end(cookie)
23463 void *cookie;
23464{
23465 funccall_T *fcp = (funccall_T *)cookie;
23466 ufunc_T *fp = fcp->func;
23467
23468 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23469 {
23470 if (fp->uf_tml_execed)
23471 {
23472 ++fp->uf_tml_count[fp->uf_tml_idx];
23473 profile_end(&fp->uf_tml_start);
23474 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023475 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023476 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23477 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023478 }
23479 fp->uf_tml_idx = -1;
23480 }
23481}
23482#endif
23483
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484/*
23485 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023486 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 */
23488 int
23489func_has_ended(cookie)
23490 void *cookie;
23491{
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493
23494 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23495 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023496 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 || fcp->returned);
23498}
23499
23500/*
23501 * return TRUE if cookie indicates a function which "abort"s on errors.
23502 */
23503 int
23504func_has_abort(cookie)
23505 void *cookie;
23506{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023507 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508}
23509
23510#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23511typedef enum
23512{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023513 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23514 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23515 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516} var_flavour_T;
23517
23518static var_flavour_T var_flavour __ARGS((char_u *varname));
23519
23520 static var_flavour_T
23521var_flavour(varname)
23522 char_u *varname;
23523{
23524 char_u *p = varname;
23525
23526 if (ASCII_ISUPPER(*p))
23527 {
23528 while (*(++p))
23529 if (ASCII_ISLOWER(*p))
23530 return VAR_FLAVOUR_SESSION;
23531 return VAR_FLAVOUR_VIMINFO;
23532 }
23533 else
23534 return VAR_FLAVOUR_DEFAULT;
23535}
23536#endif
23537
23538#if defined(FEAT_VIMINFO) || defined(PROTO)
23539/*
23540 * Restore global vars that start with a capital from the viminfo file
23541 */
23542 int
23543read_viminfo_varlist(virp, writing)
23544 vir_T *virp;
23545 int writing;
23546{
23547 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023548 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023549 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550
23551 if (!writing && (find_viminfo_parameter('!') != NULL))
23552 {
23553 tab = vim_strchr(virp->vir_line + 1, '\t');
23554 if (tab != NULL)
23555 {
23556 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023557 switch (*tab)
23558 {
23559 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023560#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023561 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023562#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023563 case 'D': type = VAR_DICT; break;
23564 case 'L': type = VAR_LIST; break;
23565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566
23567 tab = vim_strchr(tab, '\t');
23568 if (tab != NULL)
23569 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023570 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023571 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023572 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023574#ifdef FEAT_FLOAT
23575 else if (type == VAR_FLOAT)
23576 (void)string2float(tab + 1, &tv.vval.v_float);
23577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023579 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023580 if (type == VAR_DICT || type == VAR_LIST)
23581 {
23582 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23583
23584 if (etv == NULL)
23585 /* Failed to parse back the dict or list, use it as a
23586 * string. */
23587 tv.v_type = VAR_STRING;
23588 else
23589 {
23590 vim_free(tv.vval.v_string);
23591 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023592 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023593 }
23594 }
23595
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023596 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023597
23598 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023599 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023600 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23601 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 }
23603 }
23604 }
23605
23606 return viminfo_readline(virp);
23607}
23608
23609/*
23610 * Write global vars that start with a capital to the viminfo file
23611 */
23612 void
23613write_viminfo_varlist(fp)
23614 FILE *fp;
23615{
Bram Moolenaar33570922005-01-25 22:26:29 +000023616 hashitem_T *hi;
23617 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023618 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023619 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023620 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023621 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023622 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023623
23624 if (find_viminfo_parameter('!') == NULL)
23625 return;
23626
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023627 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023628
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023629 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023630 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023632 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023634 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023635 this_var = HI2DI(hi);
23636 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023637 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023638 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023639 {
23640 case VAR_STRING: s = "STR"; break;
23641 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023642#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023643 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023644#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023645 case VAR_DICT: s = "DIC"; break;
23646 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023647 default: continue;
23648 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023649 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023650 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023651 if (p != NULL)
23652 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023653 vim_free(tofree);
23654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 }
23656 }
23657}
23658#endif
23659
23660#if defined(FEAT_SESSION) || defined(PROTO)
23661 int
23662store_session_globals(fd)
23663 FILE *fd;
23664{
Bram Moolenaar33570922005-01-25 22:26:29 +000023665 hashitem_T *hi;
23666 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023667 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 char_u *p, *t;
23669
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023670 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023671 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023675 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023676 this_var = HI2DI(hi);
23677 if ((this_var->di_tv.v_type == VAR_NUMBER
23678 || this_var->di_tv.v_type == VAR_STRING)
23679 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023680 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023681 /* Escape special characters with a backslash. Turn a LF and
23682 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023683 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023684 (char_u *)"\\\"\n\r");
23685 if (p == NULL) /* out of memory */
23686 break;
23687 for (t = p; *t != NUL; ++t)
23688 if (*t == '\n')
23689 *t = 'n';
23690 else if (*t == '\r')
23691 *t = 'r';
23692 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023693 this_var->di_key,
23694 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23695 : ' ',
23696 p,
23697 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23698 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023699 || put_eol(fd) == FAIL)
23700 {
23701 vim_free(p);
23702 return FAIL;
23703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 vim_free(p);
23705 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023706#ifdef FEAT_FLOAT
23707 else if (this_var->di_tv.v_type == VAR_FLOAT
23708 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23709 {
23710 float_T f = this_var->di_tv.vval.v_float;
23711 int sign = ' ';
23712
23713 if (f < 0)
23714 {
23715 f = -f;
23716 sign = '-';
23717 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023718 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023719 this_var->di_key, sign, f) < 0)
23720 || put_eol(fd) == FAIL)
23721 return FAIL;
23722 }
23723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 }
23725 }
23726 return OK;
23727}
23728#endif
23729
Bram Moolenaar661b1822005-07-28 22:36:45 +000023730/*
23731 * Display script name where an item was last set.
23732 * Should only be invoked when 'verbose' is non-zero.
23733 */
23734 void
23735last_set_msg(scriptID)
23736 scid_T scriptID;
23737{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023738 char_u *p;
23739
Bram Moolenaar661b1822005-07-28 22:36:45 +000023740 if (scriptID != 0)
23741 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023742 p = home_replace_save(NULL, get_scriptname(scriptID));
23743 if (p != NULL)
23744 {
23745 verbose_enter();
23746 MSG_PUTS(_("\n\tLast set from "));
23747 MSG_PUTS(p);
23748 vim_free(p);
23749 verbose_leave();
23750 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023751 }
23752}
23753
Bram Moolenaard812df62008-11-09 12:46:09 +000023754/*
23755 * List v:oldfiles in a nice way.
23756 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023757 void
23758ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023759 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023760{
23761 list_T *l = vimvars[VV_OLDFILES].vv_list;
23762 listitem_T *li;
23763 int nr = 0;
23764
23765 if (l == NULL)
23766 msg((char_u *)_("No old files"));
23767 else
23768 {
23769 msg_start();
23770 msg_scroll = TRUE;
23771 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23772 {
23773 msg_outnum((long)++nr);
23774 MSG_PUTS(": ");
23775 msg_outtrans(get_tv_string(&li->li_tv));
23776 msg_putchar('\n');
23777 out_flush(); /* output one line at a time */
23778 ui_breakcheck();
23779 }
23780 /* Assume "got_int" was set to truncate the listing. */
23781 got_int = FALSE;
23782
23783#ifdef FEAT_BROWSE_CMD
23784 if (cmdmod.browse)
23785 {
23786 quit_more = FALSE;
23787 nr = prompt_for_number(FALSE);
23788 msg_starthere();
23789 if (nr > 0)
23790 {
23791 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23792 (long)nr);
23793
23794 if (p != NULL)
23795 {
23796 p = expand_env_save(p);
23797 eap->arg = p;
23798 eap->cmdidx = CMD_edit;
23799 cmdmod.browse = FALSE;
23800 do_exedit(eap, NULL);
23801 vim_free(p);
23802 }
23803 }
23804 }
23805#endif
23806 }
23807}
23808
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809#endif /* FEAT_EVAL */
23810
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023812#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813
23814#ifdef WIN3264
23815/*
23816 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23817 */
23818static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23819static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23820static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23821
23822/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023823 * Get the short path (8.3) for the filename in "fnamep".
23824 * Only works for a valid file name.
23825 * When the path gets longer "fnamep" is changed and the allocated buffer
23826 * is put in "bufp".
23827 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23828 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 */
23830 static int
23831get_short_pathname(fnamep, bufp, fnamelen)
23832 char_u **fnamep;
23833 char_u **bufp;
23834 int *fnamelen;
23835{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023836 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 char_u *newbuf;
23838
23839 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 l = GetShortPathName(*fnamep, *fnamep, len);
23841 if (l > len - 1)
23842 {
23843 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023844 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 newbuf = vim_strnsave(*fnamep, l);
23846 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023847 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848
23849 vim_free(*bufp);
23850 *fnamep = *bufp = newbuf;
23851
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023852 /* Really should always succeed, as the buffer is big enough. */
23853 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 }
23855
23856 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023857 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858}
23859
23860/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023861 * Get the short path (8.3) for the filename in "fname". The converted
23862 * path is returned in "bufp".
23863 *
23864 * Some of the directories specified in "fname" may not exist. This function
23865 * will shorten the existing directories at the beginning of the path and then
23866 * append the remaining non-existing path.
23867 *
23868 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023869 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023870 * bufp - Pointer to an allocated buffer for the filename.
23871 * fnamelen - Length of the filename pointed to by fname
23872 *
23873 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 */
23875 static int
23876shortpath_for_invalid_fname(fname, bufp, fnamelen)
23877 char_u **fname;
23878 char_u **bufp;
23879 int *fnamelen;
23880{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023881 char_u *short_fname, *save_fname, *pbuf_unused;
23882 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023884 int old_len, len;
23885 int new_len, sfx_len;
23886 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887
23888 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023889 old_len = *fnamelen;
23890 save_fname = vim_strnsave(*fname, old_len);
23891 pbuf_unused = NULL;
23892 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023894 endp = save_fname + old_len - 1; /* Find the end of the copy */
23895 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023897 /*
23898 * Try shortening the supplied path till it succeeds by removing one
23899 * directory at a time from the tail of the path.
23900 */
23901 len = 0;
23902 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023904 /* go back one path-separator */
23905 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23906 --endp;
23907 if (endp <= save_fname)
23908 break; /* processed the complete path */
23909
23910 /*
23911 * Replace the path separator with a NUL and try to shorten the
23912 * resulting path.
23913 */
23914 ch = *endp;
23915 *endp = 0;
23916 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023917 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023918 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23919 {
23920 retval = FAIL;
23921 goto theend;
23922 }
23923 *endp = ch; /* preserve the string */
23924
23925 if (len > 0)
23926 break; /* successfully shortened the path */
23927
23928 /* failed to shorten the path. Skip the path separator */
23929 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 }
23931
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023932 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023934 /*
23935 * Succeeded in shortening the path. Now concatenate the shortened
23936 * path with the remaining path at the tail.
23937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023939 /* Compute the length of the new path. */
23940 sfx_len = (int)(save_endp - endp) + 1;
23941 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023943 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023945 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023947 /* There is not enough space in the currently allocated string,
23948 * copy it to a buffer big enough. */
23949 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023951 {
23952 retval = FAIL;
23953 goto theend;
23954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 }
23956 else
23957 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023958 /* Transfer short_fname to the main buffer (it's big enough),
23959 * unless get_short_pathname() did its work in-place. */
23960 *fname = *bufp = save_fname;
23961 if (short_fname != save_fname)
23962 vim_strncpy(save_fname, short_fname, len);
23963 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023965
23966 /* concat the not-shortened part of the path */
23967 vim_strncpy(*fname + len, endp, sfx_len);
23968 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023970
23971theend:
23972 vim_free(pbuf_unused);
23973 vim_free(save_fname);
23974
23975 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976}
23977
23978/*
23979 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023980 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981 */
23982 static int
23983shortpath_for_partial(fnamep, bufp, fnamelen)
23984 char_u **fnamep;
23985 char_u **bufp;
23986 int *fnamelen;
23987{
23988 int sepcount, len, tflen;
23989 char_u *p;
23990 char_u *pbuf, *tfname;
23991 int hasTilde;
23992
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023993 /* Count up the path separators from the RHS.. so we know which part
23994 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023996 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 if (vim_ispathsep(*p))
23998 ++sepcount;
23999
24000 /* Need full path first (use expand_env() to remove a "~/") */
24001 hasTilde = (**fnamep == '~');
24002 if (hasTilde)
24003 pbuf = tfname = expand_env_save(*fnamep);
24004 else
24005 pbuf = tfname = FullName_save(*fnamep, FALSE);
24006
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024007 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024009 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24010 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011
24012 if (len == 0)
24013 {
24014 /* Don't have a valid filename, so shorten the rest of the
24015 * path if we can. This CAN give us invalid 8.3 filenames, but
24016 * there's not a lot of point in guessing what it might be.
24017 */
24018 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024019 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24020 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021 }
24022
24023 /* Count the paths backward to find the beginning of the desired string. */
24024 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024025 {
24026#ifdef FEAT_MBYTE
24027 if (has_mbyte)
24028 p -= mb_head_off(tfname, p);
24029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 if (vim_ispathsep(*p))
24031 {
24032 if (sepcount == 0 || (hasTilde && sepcount == 1))
24033 break;
24034 else
24035 sepcount --;
24036 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038 if (hasTilde)
24039 {
24040 --p;
24041 if (p >= tfname)
24042 *p = '~';
24043 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024044 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024045 }
24046 else
24047 ++p;
24048
24049 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24050 vim_free(*bufp);
24051 *fnamelen = (int)STRLEN(p);
24052 *bufp = pbuf;
24053 *fnamep = p;
24054
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024055 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056}
24057#endif /* WIN3264 */
24058
24059/*
24060 * Adjust a filename, according to a string of modifiers.
24061 * *fnamep must be NUL terminated when called. When returning, the length is
24062 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024063 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 * When there is an error, *fnamep is set to NULL.
24065 */
24066 int
24067modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24068 char_u *src; /* string with modifiers */
24069 int *usedlen; /* characters after src that are used */
24070 char_u **fnamep; /* file name so far */
24071 char_u **bufp; /* buffer for allocated file name or NULL */
24072 int *fnamelen; /* length of fnamep */
24073{
24074 int valid = 0;
24075 char_u *tail;
24076 char_u *s, *p, *pbuf;
24077 char_u dirname[MAXPATHL];
24078 int c;
24079 int has_fullname = 0;
24080#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024081 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082 int has_shortname = 0;
24083#endif
24084
24085repeat:
24086 /* ":p" - full path/file_name */
24087 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24088 {
24089 has_fullname = 1;
24090
24091 valid |= VALID_PATH;
24092 *usedlen += 2;
24093
24094 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24095 if ((*fnamep)[0] == '~'
24096#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24097 && ((*fnamep)[1] == '/'
24098# ifdef BACKSLASH_IN_FILENAME
24099 || (*fnamep)[1] == '\\'
24100# endif
24101 || (*fnamep)[1] == NUL)
24102
24103#endif
24104 )
24105 {
24106 *fnamep = expand_env_save(*fnamep);
24107 vim_free(*bufp); /* free any allocated file name */
24108 *bufp = *fnamep;
24109 if (*fnamep == NULL)
24110 return -1;
24111 }
24112
24113 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024114 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 {
24116 if (vim_ispathsep(*p)
24117 && p[1] == '.'
24118 && (p[2] == NUL
24119 || vim_ispathsep(p[2])
24120 || (p[2] == '.'
24121 && (p[3] == NUL || vim_ispathsep(p[3])))))
24122 break;
24123 }
24124
24125 /* FullName_save() is slow, don't use it when not needed. */
24126 if (*p != NUL || !vim_isAbsName(*fnamep))
24127 {
24128 *fnamep = FullName_save(*fnamep, *p != NUL);
24129 vim_free(*bufp); /* free any allocated file name */
24130 *bufp = *fnamep;
24131 if (*fnamep == NULL)
24132 return -1;
24133 }
24134
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024135#ifdef WIN3264
24136# if _WIN32_WINNT >= 0x0500
24137 if (vim_strchr(*fnamep, '~') != NULL)
24138 {
24139 /* Expand 8.3 filename to full path. Needed to make sure the same
24140 * file does not have two different names.
24141 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24142 p = alloc(_MAX_PATH + 1);
24143 if (p != NULL)
24144 {
24145 if (GetLongPathName(*fnamep, p, MAXPATHL))
24146 {
24147 vim_free(*bufp);
24148 *bufp = *fnamep = p;
24149 }
24150 else
24151 vim_free(p);
24152 }
24153 }
24154# endif
24155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 /* Append a path separator to a directory. */
24157 if (mch_isdir(*fnamep))
24158 {
24159 /* Make room for one or two extra characters. */
24160 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24161 vim_free(*bufp); /* free any allocated file name */
24162 *bufp = *fnamep;
24163 if (*fnamep == NULL)
24164 return -1;
24165 add_pathsep(*fnamep);
24166 }
24167 }
24168
24169 /* ":." - path relative to the current directory */
24170 /* ":~" - path relative to the home directory */
24171 /* ":8" - shortname path - postponed till after */
24172 while (src[*usedlen] == ':'
24173 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24174 {
24175 *usedlen += 2;
24176 if (c == '8')
24177 {
24178#ifdef WIN3264
24179 has_shortname = 1; /* Postpone this. */
24180#endif
24181 continue;
24182 }
24183 pbuf = NULL;
24184 /* Need full path first (use expand_env() to remove a "~/") */
24185 if (!has_fullname)
24186 {
24187 if (c == '.' && **fnamep == '~')
24188 p = pbuf = expand_env_save(*fnamep);
24189 else
24190 p = pbuf = FullName_save(*fnamep, FALSE);
24191 }
24192 else
24193 p = *fnamep;
24194
24195 has_fullname = 0;
24196
24197 if (p != NULL)
24198 {
24199 if (c == '.')
24200 {
24201 mch_dirname(dirname, MAXPATHL);
24202 s = shorten_fname(p, dirname);
24203 if (s != NULL)
24204 {
24205 *fnamep = s;
24206 if (pbuf != NULL)
24207 {
24208 vim_free(*bufp); /* free any allocated file name */
24209 *bufp = pbuf;
24210 pbuf = NULL;
24211 }
24212 }
24213 }
24214 else
24215 {
24216 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24217 /* Only replace it when it starts with '~' */
24218 if (*dirname == '~')
24219 {
24220 s = vim_strsave(dirname);
24221 if (s != NULL)
24222 {
24223 *fnamep = s;
24224 vim_free(*bufp);
24225 *bufp = s;
24226 }
24227 }
24228 }
24229 vim_free(pbuf);
24230 }
24231 }
24232
24233 tail = gettail(*fnamep);
24234 *fnamelen = (int)STRLEN(*fnamep);
24235
24236 /* ":h" - head, remove "/file_name", can be repeated */
24237 /* Don't remove the first "/" or "c:\" */
24238 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24239 {
24240 valid |= VALID_HEAD;
24241 *usedlen += 2;
24242 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024243 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024244 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 *fnamelen = (int)(tail - *fnamep);
24246#ifdef VMS
24247 if (*fnamelen > 0)
24248 *fnamelen += 1; /* the path separator is part of the path */
24249#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024250 if (*fnamelen == 0)
24251 {
24252 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24253 p = vim_strsave((char_u *)".");
24254 if (p == NULL)
24255 return -1;
24256 vim_free(*bufp);
24257 *bufp = *fnamep = tail = p;
24258 *fnamelen = 1;
24259 }
24260 else
24261 {
24262 while (tail > s && !after_pathsep(s, tail))
24263 mb_ptr_back(*fnamep, tail);
24264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 }
24266
24267 /* ":8" - shortname */
24268 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24269 {
24270 *usedlen += 2;
24271#ifdef WIN3264
24272 has_shortname = 1;
24273#endif
24274 }
24275
24276#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024277 /*
24278 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 */
24280 if (has_shortname)
24281 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024282 /* Copy the string if it is shortened by :h and when it wasn't copied
24283 * yet, because we are going to change it in place. Avoids changing
24284 * the buffer name for "%:8". */
24285 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 {
24287 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024288 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289 return -1;
24290 vim_free(*bufp);
24291 *bufp = *fnamep = p;
24292 }
24293
24294 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024295 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024296 if (!has_fullname && !vim_isAbsName(*fnamep))
24297 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024298 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 return -1;
24300 }
24301 else
24302 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024303 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304
Bram Moolenaardc935552011-08-17 15:23:23 +020024305 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024307 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 return -1;
24309
24310 if (l == 0)
24311 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024312 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024313 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024314 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 return -1;
24316 }
24317 *fnamelen = l;
24318 }
24319 }
24320#endif /* WIN3264 */
24321
24322 /* ":t" - tail, just the basename */
24323 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24324 {
24325 *usedlen += 2;
24326 *fnamelen -= (int)(tail - *fnamep);
24327 *fnamep = tail;
24328 }
24329
24330 /* ":e" - extension, can be repeated */
24331 /* ":r" - root, without extension, can be repeated */
24332 while (src[*usedlen] == ':'
24333 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24334 {
24335 /* find a '.' in the tail:
24336 * - for second :e: before the current fname
24337 * - otherwise: The last '.'
24338 */
24339 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24340 s = *fnamep - 2;
24341 else
24342 s = *fnamep + *fnamelen - 1;
24343 for ( ; s > tail; --s)
24344 if (s[0] == '.')
24345 break;
24346 if (src[*usedlen + 1] == 'e') /* :e */
24347 {
24348 if (s > tail)
24349 {
24350 *fnamelen += (int)(*fnamep - (s + 1));
24351 *fnamep = s + 1;
24352#ifdef VMS
24353 /* cut version from the extension */
24354 s = *fnamep + *fnamelen - 1;
24355 for ( ; s > *fnamep; --s)
24356 if (s[0] == ';')
24357 break;
24358 if (s > *fnamep)
24359 *fnamelen = s - *fnamep;
24360#endif
24361 }
24362 else if (*fnamep <= tail)
24363 *fnamelen = 0;
24364 }
24365 else /* :r */
24366 {
24367 if (s > tail) /* remove one extension */
24368 *fnamelen = (int)(s - *fnamep);
24369 }
24370 *usedlen += 2;
24371 }
24372
24373 /* ":s?pat?foo?" - substitute */
24374 /* ":gs?pat?foo?" - global substitute */
24375 if (src[*usedlen] == ':'
24376 && (src[*usedlen + 1] == 's'
24377 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24378 {
24379 char_u *str;
24380 char_u *pat;
24381 char_u *sub;
24382 int sep;
24383 char_u *flags;
24384 int didit = FALSE;
24385
24386 flags = (char_u *)"";
24387 s = src + *usedlen + 2;
24388 if (src[*usedlen + 1] == 'g')
24389 {
24390 flags = (char_u *)"g";
24391 ++s;
24392 }
24393
24394 sep = *s++;
24395 if (sep)
24396 {
24397 /* find end of pattern */
24398 p = vim_strchr(s, sep);
24399 if (p != NULL)
24400 {
24401 pat = vim_strnsave(s, (int)(p - s));
24402 if (pat != NULL)
24403 {
24404 s = p + 1;
24405 /* find end of substitution */
24406 p = vim_strchr(s, sep);
24407 if (p != NULL)
24408 {
24409 sub = vim_strnsave(s, (int)(p - s));
24410 str = vim_strnsave(*fnamep, *fnamelen);
24411 if (sub != NULL && str != NULL)
24412 {
24413 *usedlen = (int)(p + 1 - src);
24414 s = do_string_sub(str, pat, sub, flags);
24415 if (s != NULL)
24416 {
24417 *fnamep = s;
24418 *fnamelen = (int)STRLEN(s);
24419 vim_free(*bufp);
24420 *bufp = s;
24421 didit = TRUE;
24422 }
24423 }
24424 vim_free(sub);
24425 vim_free(str);
24426 }
24427 vim_free(pat);
24428 }
24429 }
24430 /* after using ":s", repeat all the modifiers */
24431 if (didit)
24432 goto repeat;
24433 }
24434 }
24435
Bram Moolenaar26df0922014-02-23 23:39:13 +010024436 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
24437 {
24438 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
24439 if (p == NULL)
24440 return -1;
24441 vim_free(*bufp);
24442 *bufp = *fnamep = p;
24443 *fnamelen = (int)STRLEN(p);
24444 *usedlen += 2;
24445 }
24446
Bram Moolenaar071d4272004-06-13 20:20:40 +000024447 return valid;
24448}
24449
24450/*
24451 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24452 * "flags" can be "g" to do a global substitute.
24453 * Returns an allocated string, NULL for error.
24454 */
24455 char_u *
24456do_string_sub(str, pat, sub, flags)
24457 char_u *str;
24458 char_u *pat;
24459 char_u *sub;
24460 char_u *flags;
24461{
24462 int sublen;
24463 regmatch_T regmatch;
24464 int i;
24465 int do_all;
24466 char_u *tail;
24467 garray_T ga;
24468 char_u *ret;
24469 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010024470 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471
24472 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24473 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024474 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475
24476 ga_init2(&ga, 1, 200);
24477
24478 do_all = (flags[0] == 'g');
24479
24480 regmatch.rm_ic = p_ic;
24481 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24482 if (regmatch.regprog != NULL)
24483 {
24484 tail = str;
24485 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24486 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010024487 /* Skip empty match except for first match. */
24488 if (regmatch.startp[0] == regmatch.endp[0])
24489 {
24490 if (zero_width == regmatch.startp[0])
24491 {
24492 /* avoid getting stuck on a match with an empty string */
24493 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24494 ++ga.ga_len;
24495 continue;
24496 }
24497 zero_width = regmatch.startp[0];
24498 }
24499
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 /*
24501 * Get some space for a temporary buffer to do the substitution
24502 * into. It will contain:
24503 * - The text up to where the match is.
24504 * - The substituted text.
24505 * - The text after the match.
24506 */
24507 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24508 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24509 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24510 {
24511 ga_clear(&ga);
24512 break;
24513 }
24514
24515 /* copy the text up to where the match is */
24516 i = (int)(regmatch.startp[0] - tail);
24517 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24518 /* add the substituted text */
24519 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24520 + ga.ga_len + i, TRUE, TRUE, FALSE);
24521 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024522 tail = regmatch.endp[0];
24523 if (*tail == NUL)
24524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525 if (!do_all)
24526 break;
24527 }
24528
24529 if (ga.ga_data != NULL)
24530 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24531
Bram Moolenaar473de612013-06-08 18:19:48 +020024532 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 }
24534
24535 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24536 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024537 if (p_cpo == empty_option)
24538 p_cpo = save_cpo;
24539 else
24540 /* Darn, evaluating {sub} expression changed the value. */
24541 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542
24543 return ret;
24544}
24545
24546#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */