blob: 7b7302f74ab9dc3f26baaca79643e96110c3a3f8 [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 Moolenaar33570922005-01-25 22:26:29 +0000364};
365
366/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367#define vv_type vv_di.di_tv.v_type
368#define vv_nr vv_di.di_tv.vval.v_number
369#define vv_float vv_di.di_tv.vval.v_float
370#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000371#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000372#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000373
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200374static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000375#define vimvarht vimvardict.dv_hashtab
376
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
378static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
380static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
381static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000382static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
383static void list_glob_vars __ARGS((int *first));
384static void list_buf_vars __ARGS((int *first));
385static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_vim_vars __ARGS((int *first));
390static void list_script_vars __ARGS((int *first));
391static void list_func_vars __ARGS((int *first));
392static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
394static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100395static 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 +0000396static void clear_lval __ARGS((lval_T *lp));
397static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
398static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
400static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
401static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
402static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
403static void item_lock __ARGS((typval_T *tv, int deep, int lock));
404static int tv_islocked __ARGS((typval_T *tv));
405
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
407static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000412static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
413static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000415static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000420static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100422static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
423static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
424static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000431static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100432static 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 +0000433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000434static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200435static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#ifdef FEAT_FLOAT
446static int string2float __ARGS((char_u *text, float_T *value));
447#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100450static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451static 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 +0200452static 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 +0000453static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000454static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200458static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100461static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100480static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100482static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
485static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
486#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000487static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000490static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000491static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000492#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000493static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000494static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200501static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
506static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517#ifdef FEAT_FLOAT
518static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000533static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000542static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000544static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000558static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000559static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000560static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000561static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200564static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000565static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000573static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000587static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100592static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000594static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000606#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200607static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#ifdef FEAT_LUA
611static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000618static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000621static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000625#ifdef vim_mkdir
626static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100629#ifdef FEAT_MZSCHEME
630static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100634static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000635static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000636#ifdef FEAT_FLOAT
637static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000640static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000641static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200642#ifdef FEAT_PYTHON3
643static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
645#ifdef FEAT_PYTHON
646static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200665static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100667static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100687#ifdef FEAT_CRYPT
688static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
689#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000690static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200691static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200695static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000698static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000699static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000706static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200707static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708#ifdef HAVE_STRFTIME
709static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
711static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200717static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000724static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200725static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000728static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000730static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000731static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000733static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200734#ifdef FEAT_FLOAT
735static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000741#ifdef FEAT_FLOAT
742static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200745static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200746static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100750static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100775static 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 +0000776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
787static 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 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static 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 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200852 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
853 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200854 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000855 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000857
858 for (i = 0; i < VV_LEN; ++i)
859 {
860 p = &vimvars[i];
861 STRCPY(p->vv_di.di_key, p->vv_name);
862 if (p->vv_flags & VV_RO)
863 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
864 else if (p->vv_flags & VV_RO_SBX)
865 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
866 else
867 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000868
869 /* add to v: scope dict, unless the value is not always available */
870 if (p->vv_type != VAR_UNKNOWN)
871 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 /* add to compat scope dict */
874 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000876 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100877 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100914# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100916# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917
918 /* global variables */
919 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000921 /* autoloaded script names */
922 ga_clear_strings(&ga_loaded);
923
Bram Moolenaarcca74132013-09-25 21:00:28 +0200924 /* Script-local variables. First clear all the variables and in a second
925 * loop free the scriptvar_T, because a variable in one script might hold
926 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200929 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001155 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001568call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001573 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 long n;
1578 int len;
1579 int i;
1580 int doesrange;
1581 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001584 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 for (i = 0; i < argc; i++)
1589 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001590 /* Pass a NULL or empty argument as an empty string */
1591 if (argv[i] == NULL || *argv[i] == NUL)
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 continue;
1596 }
1597
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 if (str_arg_only)
1599 len = 0;
1600 else
1601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (len != 0 && len == (int)STRLEN(argv[i]))
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_NUMBER;
1606 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 else
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_STRING;
1611 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614
1615 if (safe)
1616 {
1617 save_funccalp = save_funccal();
1618 ++sandbox;
1619 }
1620
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1622 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (safe)
1626 {
1627 --sandbox;
1628 restore_funccal(save_funccalp);
1629 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 vim_free(argvars);
1631
1632 if (ret == FAIL)
1633 clear_tv(rettv);
1634
1635 return ret;
1636}
1637
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001638/*
1639 * Call vimL function "func" and return the result as a number.
1640 * Returns -1 when calling the function fails.
1641 * Uses argv[argc] for the function arguments.
1642 */
1643 long
1644call_func_retnr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
1651 long retval;
1652
1653 /* All arguments are passed as strings, no conversion to number. */
1654 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1655 return -1;
1656
1657 retval = get_tv_number_chk(&rettv, NULL);
1658 clear_tv(&rettv);
1659 return retval;
1660}
1661
1662#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1663 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1664
Bram Moolenaar4f688582007-07-24 12:34:30 +00001665# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 * Call vimL function "func" and return the result as a string.
1668 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Uses argv[argc] for the function arguments.
1670 */
1671 void *
1672call_func_retstr(func, argc, argv, safe)
1673 char_u *func;
1674 int argc;
1675 char_u **argv;
1676 int safe; /* use the sandbox */
1677{
1678 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 return NULL;
1684
1685 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return retval;
1688}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001689# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001694 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 */
1696 void *
1697call_func_retlist(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 return NULL;
1708
1709 if (rettv.v_type != VAR_LIST)
1710 {
1711 clear_tv(&rettv);
1712 return NULL;
1713 }
1714
1715 return rettv.vval.v_list;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717#endif
1718
1719/*
1720 * Save the current function call pointer, and set it to NULL.
1721 * Used when executing autocommands and for ":source".
1722 */
1723 void *
1724save_funccal()
1725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 current_funccal = NULL;
1729 return (void *)fc;
1730}
1731
1732 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733restore_funccal(vfc)
1734 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 funccall_T *fc = (funccall_T *)vfc;
1737
1738 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#if defined(FEAT_PROFILE) || defined(PROTO)
1742/*
1743 * Prepare profiling for entering a child or something else that is not
1744 * counted for the script/function itself.
1745 * Should always be called in pair with prof_child_exit().
1746 */
1747 void
1748prof_child_enter(tm)
1749 proftime_T *tm; /* place to store waittime */
1750{
1751 funccall_T *fc = current_funccal;
1752
1753 if (fc != NULL && fc->func->uf_profiling)
1754 profile_start(&fc->prof_child);
1755 script_prof_save(tm);
1756}
1757
1758/*
1759 * Take care of time spent in a child.
1760 * Should always be called after prof_child_enter().
1761 */
1762 void
1763prof_child_exit(tm)
1764 proftime_T *tm; /* where waittime was stored */
1765{
1766 funccall_T *fc = current_funccal;
1767
1768 if (fc != NULL && fc->func->uf_profiling)
1769 {
1770 profile_end(&fc->prof_child);
1771 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1772 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1773 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1774 }
1775 script_prof_restore(tm);
1776}
1777#endif
1778
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_FOLDING
1781/*
1782 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1783 * it in "*cp". Doesn't give error messages.
1784 */
1785 int
1786eval_foldexpr(arg, cp)
1787 char_u *arg;
1788 int *cp;
1789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 int retval;
1792 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001797 if (use_sandbox)
1798 ++sandbox;
1799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (tv.v_type == VAR_NUMBER)
1807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a string, check if there is a non-digit before
1813 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (!VIM_ISDIGIT(*s) && *s != '-')
1816 *cp = *s++;
1817 retval = atol((char *)s);
1818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 --sandbox;
1824 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 return retval;
1827}
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let" list all variable values
1832 * ":let var1 var2" list variable values
1833 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 * ":let var += expr" assignment command.
1835 * ":let var -= expr" assignment command.
1836 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
1840ex_let(eap)
1841 exarg_T *eap;
1842{
1843 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 int var_count = 0;
1848 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 argend = skip_var_list(arg, &var_count, &semicolon);
1854 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001856 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1857 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001861 /*
1862 * ":let" without "=": list variables
1863 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (*arg == '[')
1865 EMSG(_(e_invarg));
1866 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_glob_vars(&first);
1873 list_buf_vars(&first);
1874 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_script_vars(&first);
1879 list_func_vars(&first);
1880 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 eap->nextcmd = check_nextcmd(arg);
1883 }
1884 else
1885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 op[0] = '=';
1887 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 {
1890 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1891 op[0] = expr[-1]; /* +=, -= or .= */
1892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (eap->skip)
1899 {
1900 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --emsg_skip;
1903 }
1904 else if (i != FAIL)
1905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911}
1912
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913/*
1914 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1915 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 * When "nextchars" is not NULL it points to a string with characters that
1917 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1918 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 * Returns OK or FAIL;
1920 */
1921 static int
1922ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1923 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int copy; /* copy values from "tv", don't move */
1926 int semicolon; /* from skip_var_list() */
1927 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929{
1930 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 listitem_T *item;
1934 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
1936 if (*arg != '[')
1937 {
1938 /*
1939 * ":let var = expr" or ":for var in list"
1940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return FAIL;
1943 return OK;
1944 }
1945
1946 /*
1947 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1948 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
1951 EMSG(_(e_listreq));
1952 return FAIL;
1953 }
1954
1955 i = list_len(l);
1956 if (semicolon == 0 && var_count < i)
1957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001958 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 return FAIL;
1960 }
1961 if (var_count - semicolon > i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966
1967 item = l->lv_first;
1968 while (*arg != ']')
1969 {
1970 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 item = item->li_next;
1973 if (arg == NULL)
1974 return FAIL;
1975
1976 arg = skipwhite(arg);
1977 if (*arg == ';')
1978 {
1979 /* Put the rest of the list (may be empty) in the var after ';'.
1980 * Create a new list for this. */
1981 l = list_alloc();
1982 if (l == NULL)
1983 return FAIL;
1984 while (item != NULL)
1985 {
1986 list_append_tv(l, &item->li_tv);
1987 item = item->li_next;
1988 }
1989
1990 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001991 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 ltv.vval.v_list = l;
1993 l->lv_refcount = 1;
1994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1996 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 clear_tv(&ltv);
1998 if (arg == NULL)
1999 return FAIL;
2000 break;
2001 }
2002 else if (*arg != ',' && *arg != ']')
2003 {
2004 EMSG2(_(e_intern2), "ex_let_vars()");
2005 return FAIL;
2006 }
2007 }
2008
2009 return OK;
2010}
2011
2012/*
2013 * Skip over assignable variable "var" or list of variables "[var, var]".
2014 * Used for ":let varvar = expr" and ":for varvar in expr".
2015 * For "[var, var]" increment "*var_count" for each variable.
2016 * for "[var, var; var]" set "semicolon".
2017 * Return NULL for an error.
2018 */
2019 static char_u *
2020skip_var_list(arg, var_count, semicolon)
2021 char_u *arg;
2022 int *var_count;
2023 int *semicolon;
2024{
2025 char_u *p, *s;
2026
2027 if (*arg == '[')
2028 {
2029 /* "[var, var]": find the matching ']'. */
2030 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002031 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
2033 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2034 s = skip_var_one(p);
2035 if (s == p)
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 ++*var_count;
2041
2042 p = skipwhite(s);
2043 if (*p == ']')
2044 break;
2045 else if (*p == ';')
2046 {
2047 if (*semicolon == 1)
2048 {
2049 EMSG(_("Double ; in list of variables"));
2050 return NULL;
2051 }
2052 *semicolon = 1;
2053 }
2054 else if (*p != ',')
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 }
2060 return p + 1;
2061 }
2062 else
2063 return skip_var_one(arg);
2064}
2065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002067 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 static char_u *
2071skip_var_one(arg)
2072 char_u *arg;
2073{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 if (*arg == '@' && arg[1] != NUL)
2075 return arg + 2;
2076 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2077 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078}
2079
Bram Moolenaara7043832005-01-21 11:56:39 +00002080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 * List variables for hashtab "ht" with prefix "prefix".
2082 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090{
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashitem_T *hi;
2092 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 int todo;
2094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2097 {
2098 if (!HASHITEM_EMPTY(hi))
2099 {
2100 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 di = HI2DI(hi);
2102 if (empty || di->di_tv.v_type != VAR_STRING
2103 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
2106 }
2107}
2108
2109/*
2110 * List global variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_glob_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
2119/*
2120 * List buffer variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_buf_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 char_u numbuf[NUMBUFLEN];
2127
Bram Moolenaar429fa852013-04-15 12:27:36 +02002128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2133 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134}
2135
2136/*
2137 * List window variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_win_vars(first)
2141 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147#ifdef FEAT_WINDOWS
2148/*
2149 * List tab page variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_tab_vars(first)
2153 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157}
2158#endif
2159
Bram Moolenaara7043832005-01-21 11:56:39 +00002160/*
2161 * List Vim variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_vim_vars(first)
2165 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168}
2169
2170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171 * List script-local variables, if there is a script.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_script_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2179 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
2183 * List function variables, if there is a function.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_func_vars(first)
2187 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188{
2189 if (current_funccal != NULL)
2190 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192}
2193
2194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * List variables in "arg".
2196 */
2197 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 exarg_T *eap;
2200 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202{
2203 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 char_u *name_start;
2207 char_u *arg_subsc;
2208 char_u *tofree;
2209 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210
2211 while (!ends_excmd(*arg) && !got_int)
2212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2217 {
2218 emsg_severe = TRUE;
2219 EMSG(_(e_trailing));
2220 break;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* get_name_len() takes care of expanding curly braces */
2226 name_start = name = arg;
2227 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2228 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* This is mainly to keep test 49 working: when expanding
2231 * curly braces fails overrule the exception error message. */
2232 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 emsg_severe = TRUE;
2235 EMSG2(_(e_invarg2), arg);
2236 break;
2237 }
2238 error = TRUE;
2239 }
2240 else
2241 {
2242 if (tofree != NULL)
2243 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002244 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 /* handle d.key, l[idx], f(expr) */
2249 arg_subsc = arg;
2250 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'g': list_glob_vars(first); break;
2259 case 'b': list_buf_vars(first); break;
2260 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'v': list_vim_vars(first); break;
2265 case 's': list_script_vars(first); break;
2266 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 default:
2268 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
2271 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 {
2273 char_u numbuf[NUMBUFLEN];
2274 char_u *tf;
2275 int c;
2276 char_u *s;
2277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002278 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 c = *arg;
2280 *arg = NUL;
2281 list_one_var_a((char_u *)"",
2282 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 tv.v_type,
2284 s == NULL ? (char_u *)"" : s,
2285 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 *arg = c;
2287 vim_free(tf);
2288 }
2289 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296
2297 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 return arg;
2301}
2302
2303/*
2304 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2305 * Returns a pointer to the char just after the var name.
2306 * Returns NULL if there is an error.
2307 */
2308 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
2316 int c1;
2317 char_u *name;
2318 char_u *p;
2319 char_u *arg_end = NULL;
2320 int len;
2321 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
2324 /*
2325 * ":let $VAR = expr": Set environment variable.
2326 */
2327 if (*arg == '$')
2328 {
2329 /* Find the end of the name. */
2330 ++arg;
2331 name = arg;
2332 len = get_env_len(&arg);
2333 if (len == 0)
2334 EMSG2(_(e_invarg2), name - 1);
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 if (op != NULL && (*op == '+' || *op == '-'))
2338 EMSG2(_(e_letwrong), op);
2339 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002342 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
2344 c1 = name[len];
2345 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 p = get_tv_string_chk(tv);
2347 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 {
2349 int mustfree = FALSE;
2350 char_u *s = vim_getenv(name, &mustfree);
2351
2352 if (s != NULL)
2353 {
2354 p = tofree = concat_str(s, p);
2355 if (mustfree)
2356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 if (STRICMP(name, "HOME") == 0)
2363 init_homedir();
2364 else if (didset_vim && STRICMP(name, "VIM") == 0)
2365 didset_vim = FALSE;
2366 else if (didset_vimruntime
2367 && STRICMP(name, "VIMRUNTIME") == 0)
2368 didset_vimruntime = FALSE;
2369 arg_end = arg;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375 }
2376
2377 /*
2378 * ":let &option = expr": Set option value.
2379 * ":let &l:option = expr": Set local option value.
2380 * ":let &g:option = expr": Set global option value.
2381 */
2382 else if (*arg == '&')
2383 {
2384 /* Find the end of the name. */
2385 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 if (p == NULL || (endchars != NULL
2387 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
2389 else
2390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 long n;
2392 int opt_type;
2393 long numval;
2394 char_u *stringval = NULL;
2395 char_u *s;
2396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 c1 = *p;
2398 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399
2400 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 s = get_tv_string_chk(tv); /* != NULL if number or string */
2402 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 opt_type = get_option_value(arg, &numval,
2405 &stringval, opt_flags);
2406 if ((opt_type == 1 && *op == '.')
2407 || (opt_type == 0 && *op != '.'))
2408 EMSG2(_(e_letwrong), op);
2409 else
2410 {
2411 if (opt_type == 1) /* number */
2412 {
2413 if (*op == '+')
2414 n = numval + n;
2415 else
2416 n = numval - n;
2417 }
2418 else if (opt_type == 0 && stringval != NULL) /* string */
2419 {
2420 s = concat_str(stringval, s);
2421 vim_free(stringval);
2422 stringval = s;
2423 }
2424 }
2425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 if (s != NULL)
2427 {
2428 set_option_value(arg, n, s, opt_flags);
2429 arg_end = p;
2430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
2434 }
2435
2436 /*
2437 * ":let @r = expr": Set register contents.
2438 */
2439 else if (*arg == '@')
2440 {
2441 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (op != NULL && (*op == '+' || *op == '-'))
2443 EMSG2(_(e_letwrong), op);
2444 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 char_u *s;
2451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 p = get_tv_string_chk(tv);
2453 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002455 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (s != NULL)
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 vim_free(s);
2460 }
2461 }
2462 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 arg_end = arg + 1;
2466 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002479 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
2493 else
2494 EMSG2(_(e_invarg2), arg);
2495
2496 return arg_end;
2497}
2498
2499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2501 */
2502 static int
2503check_changedtick(arg)
2504 char_u *arg;
2505{
2506 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2507 {
2508 EMSG2(_(e_readonlyvar), arg);
2509 return TRUE;
2510 }
2511 return FALSE;
2512}
2513
2514/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * Get an lval: variable, Dict item or List item that can be assigned a value
2516 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2517 * "name.key", "name.key[expr]" etc.
2518 * Indexing only works if "name" is an existing List or Dictionary.
2519 * "name" points to the start of the name.
2520 * If "rettv" is not NULL it points to the value to be assigned.
2521 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2522 * wrong; must end in space or cmd separator.
2523 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002524 * flags:
2525 * GLV_QUIET: do not give error messages
2526 * GLV_NO_AUTOLOAD: do not use script autoloading
2527 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002529 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 */
2532 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 typval_T *rettv;
2536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int unlet;
2538 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002540 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 char_u *expr_start, *expr_end;
2544 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 dictitem_T *v;
2546 typval_T var1;
2547 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (skip)
2559 {
2560 /* When skipping just find the end of the name. */
2561 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 }
2564
2565 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (expr_start != NULL)
2568 {
2569 /* Don't expand the name when we already know there is an error. */
2570 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2571 && *p != '[' && *p != '.')
2572 {
2573 EMSG(_(e_trailing));
2574 return NULL;
2575 }
2576
2577 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2578 if (lp->ll_exp_name == NULL)
2579 {
2580 /* Report an invalid expression in braces, unless the
2581 * expression evaluation has been cancelled due to an
2582 * aborting error, an interrupt, or an exception. */
2583 if (!aborting() && !quiet)
2584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 EMSG2(_(e_invarg2), name);
2587 return NULL;
2588 }
2589 }
2590 lp->ll_name = lp->ll_exp_name;
2591 }
2592 else
2593 lp->ll_name = name;
2594
2595 /* Without [idx] or .key we are done. */
2596 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2597 return p;
2598
2599 cc = *p;
2600 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (v == NULL && !quiet)
2603 EMSG2(_(e_undefvar), lp->ll_name);
2604 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 if (v == NULL)
2606 return NULL;
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /*
2609 * Loop until no more [idx] or .key is following.
2610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2615 && !(lp->ll_tv->v_type == VAR_DICT
2616 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E689: Can only index a List or Dictionary"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E708: [:] must come last"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 len = -1;
2630 if (*p == '.')
2631 {
2632 key = p + 1;
2633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2634 ;
2635 if (len == 0)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_emptykey));
2639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641 p = key + len;
2642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (*p == ':')
2648 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 empty1 = FALSE;
2652 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 if (get_tv_string_chk(&var1) == NULL)
2655 {
2656 /* not a number or string */
2657 clear_tv(&var1);
2658 return NULL;
2659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661
2662 /* Optionally get the second index [ :expr]. */
2663 if (*p == ':')
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (rettv != NULL && (rettv->v_type != VAR_LIST
2674 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = skipwhite(p + 1);
2683 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 else
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (!empty1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var2) == NULL)
2695 {
2696 /* not a number or string */
2697 if (!empty1)
2698 clear_tv(&var1);
2699 clear_tv(&var2);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Skip to past ']'. */
2720 ++p;
2721 }
2722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
2725 if (len == -1)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*key == NUL)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 lp->ll_list = NULL;
2738 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 /* When assigning to a scope dictionary check that a function and
2742 * variable name is valid (only variable name unless it is l: or
2743 * g: dictionary). Disallow overwriting a builtin function. */
2744 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 int prevval;
2747 int wrong;
2748
2749 if (len != -1)
2750 {
2751 prevval = key[len];
2752 key[len] = NUL;
2753 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002754 else
2755 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2757 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 || !valid_varname(key);
2760 if (len != -1)
2761 key[len] = prevval;
2762 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 return NULL;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* Can't add "v:" variable. */
2769 if (lp->ll_dict == &vimvardict)
2770 {
2771 EMSG2(_(e_illvar), name);
2772 return NULL;
2773 }
2774
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (len == -1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 if (len == -1)
2789 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 p = NULL;
2792 break;
2793 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 /* existing variable, need to check if it can be changed */
2795 else if (var_check_ro(lp->ll_di->di_flags, name))
2796 return NULL;
2797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 else
2803 {
2804 /*
2805 * Get the number and item for the only or first index of the List.
2806 */
2807 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 else
2810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var1);
2813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_list = lp->ll_tv->vval.v_list;
2816 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2817 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (lp->ll_n1 < 0)
2820 {
2821 lp->ll_n1 = 0;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 }
2824 }
2825 if (lp->ll_li == NULL)
2826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833
2834 /*
2835 * May need to find the item or absolute index for the second
2836 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 * When no index given: "lp->ll_empty2" is TRUE.
2838 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2857 if (lp->ll_n1 < 0)
2858 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2859 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002860 {
2861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return p;
2872}
2873
2874/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 */
2877 static void
2878clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 vim_free(lp->ll_exp_name);
2882 vim_free(lp->ll_newkey);
2883}
2884
2885/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 */
2890 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *ri;
2900 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901
2902 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *endp;
2907 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002913 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002914 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 {
2916 if (tv_op(&tv, rettv, op) == OK)
2917 set_var(lp->ll_name, &tv, FALSE);
2918 clear_tv(&tv);
2919 }
2920 }
2921 else
2922 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 else if (tv_check_lock(lp->ll_newkey == NULL
2927 ? lp->ll_tv->v_lock
2928 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2929 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 else if (lp->ll_range)
2931 {
2932 /*
2933 * Assign the List values to the list items.
2934 */
2935 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2939 else
2940 {
2941 clear_tv(&lp->ll_li->li_tv);
2942 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 ri = ri->li_next;
2945 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2946 break;
2947 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002950 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 ri = NULL;
2953 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_li = lp->ll_li->li_next;
2957 ++lp->ll_n1;
2958 }
2959 if (ri != NULL)
2960 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 else if (lp->ll_empty2
2962 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 : lp->ll_n1 != lp->ll_n2)
2964 EMSG(_("E711: List value has not enough items"));
2965 }
2966 else
2967 {
2968 /*
2969 * Assign to a List or Dictionary item.
2970 */
2971 if (lp->ll_newkey != NULL)
2972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
2975 EMSG2(_(e_letwrong), op);
2976 return;
2977 }
2978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002980 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (di == NULL)
2982 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002983 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2984 {
2985 vim_free(di);
2986 return;
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 else if (op != NULL && *op != '=')
2991 {
2992 tv_op(lp->ll_tv, rettv, op);
2993 return;
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the value to the variable or list item.
3000 */
3001 if (copy)
3002 copy_tv(rettv, lp->ll_tv);
3003 else
3004 {
3005 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 }
3009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010}
3011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3014 * Returns OK or FAIL.
3015 */
3016 static int
3017tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 typval_T *tv1;
3019 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 char_u *op;
3021{
3022 long n;
3023 char_u numbuf[NUMBUFLEN];
3024 char_u *s;
3025
3026 /* Can't do anything with a Funcref or a Dict on the right. */
3027 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3028 {
3029 switch (tv1->v_type)
3030 {
3031 case VAR_DICT:
3032 case VAR_FUNC:
3033 break;
3034
3035 case VAR_LIST:
3036 if (*op != '+' || tv2->v_type != VAR_LIST)
3037 break;
3038 /* List += List */
3039 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3040 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3041 return OK;
3042
3043 case VAR_NUMBER:
3044 case VAR_STRING:
3045 if (tv2->v_type == VAR_LIST)
3046 break;
3047 if (*op == '+' || *op == '-')
3048 {
3049 /* nr += nr or nr -= nr*/
3050 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051#ifdef FEAT_FLOAT
3052 if (tv2->v_type == VAR_FLOAT)
3053 {
3054 float_T f = n;
3055
3056 if (*op == '+')
3057 f += tv2->vval.v_float;
3058 else
3059 f -= tv2->vval.v_float;
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_FLOAT;
3062 tv1->vval.v_float = f;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
3066 {
3067 if (*op == '+')
3068 n += get_tv_number(tv2);
3069 else
3070 n -= get_tv_number(tv2);
3071 clear_tv(tv1);
3072 tv1->v_type = VAR_NUMBER;
3073 tv1->vval.v_number = n;
3074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 }
3076 else
3077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078 if (tv2->v_type == VAR_FLOAT)
3079 break;
3080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 /* str .= str */
3082 s = get_tv_string(tv1);
3083 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3084 clear_tv(tv1);
3085 tv1->v_type = VAR_STRING;
3086 tv1->vval.v_string = s;
3087 }
3088 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089
3090#ifdef FEAT_FLOAT
3091 case VAR_FLOAT:
3092 {
3093 float_T f;
3094
3095 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3096 && tv2->v_type != VAR_NUMBER
3097 && tv2->v_type != VAR_STRING))
3098 break;
3099 if (tv2->v_type == VAR_FLOAT)
3100 f = tv2->vval.v_float;
3101 else
3102 f = get_tv_number(tv2);
3103 if (*op == '+')
3104 tv1->vval.v_float += f;
3105 else
3106 tv1->vval.v_float -= f;
3107 }
3108 return OK;
3109#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 }
3112
3113 EMSG2(_(e_letwrong), op);
3114 return FAIL;
3115}
3116
3117/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 * Add a watcher to a list.
3119 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 list_T *l;
3123 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124{
3125 lw->lw_next = l->lv_watch;
3126 l->lv_watch = lw;
3127}
3128
3129/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003130 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 * No warning when it isn't found...
3132 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003133 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 list_T *l;
3136 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139
3140 lwp = &l->lv_watch;
3141 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3142 {
3143 if (lw == lwrem)
3144 {
3145 *lwp = lw->lw_next;
3146 break;
3147 }
3148 lwp = &lw->lw_next;
3149 }
3150}
3151
3152/*
3153 * Just before removing an item from a list: advance watchers to the next
3154 * item.
3155 */
3156 static void
3157list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 list_T *l;
3159 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162
3163 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3164 if (lw->lw_item == item)
3165 lw->lw_item = item->li_next;
3166}
3167
3168/*
3169 * Evaluate the expression used in a ":for var in expr" command.
3170 * "arg" points to "var".
3171 * Set "*errp" to TRUE for an error, FALSE otherwise;
3172 * Return a pointer that holds the info. Null when there is an error.
3173 */
3174 void *
3175eval_for_line(arg, errp, nextcmdp, skip)
3176 char_u *arg;
3177 int *errp;
3178 char_u **nextcmdp;
3179 int skip;
3180{
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 typval_T tv;
3184 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 *errp = TRUE; /* default: there is an error */
3187
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 if (fi == NULL)
3190 return NULL;
3191
3192 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3193 if (expr == NULL)
3194 return fi;
3195
3196 expr = skipwhite(expr);
3197 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3198 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003199 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 return fi;
3201 }
3202
3203 if (skip)
3204 ++emsg_skip;
3205 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3206 {
3207 *errp = FALSE;
3208 if (!skip)
3209 {
3210 l = tv.vval.v_list;
3211 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 clear_tv(&tv);
3215 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 else
3217 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003218 /* No need to increment the refcount, it's already set for the
3219 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 fi->fi_list = l;
3221 list_add_watch(l, &fi->fi_lw);
3222 fi->fi_lw.lw_item = l->lv_first;
3223 }
3224 }
3225 }
3226 if (skip)
3227 --emsg_skip;
3228
3229 return fi;
3230}
3231
3232/*
3233 * Use the first item in a ":for" list. Advance to the next.
3234 * Assign the values to the variable (list). "arg" points to the first one.
3235 * Return TRUE when a valid item was found, FALSE when at end of list or
3236 * something wrong.
3237 */
3238 int
3239next_for_item(fi_void, arg)
3240 void *fi_void;
3241 char_u *arg;
3242{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 item = fi->fi_lw.lw_item;
3248 if (item == NULL)
3249 result = FALSE;
3250 else
3251 {
3252 fi->fi_lw.lw_item = item->li_next;
3253 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3254 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3255 }
3256 return result;
3257}
3258
3259/*
3260 * Free the structure used to store info used by ":for".
3261 */
3262 void
3263free_for_info(fi_void)
3264 void *fi_void;
3265{
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003268 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 list_unref(fi->fi_list);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 vim_free(fi);
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3277
3278 void
3279set_context_for_expression(xp, arg, cmdidx)
3280 expand_T *xp;
3281 char_u *arg;
3282 cmdidx_T cmdidx;
3283{
3284 int got_eq = FALSE;
3285 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 if (cmdidx == CMD_let)
3289 {
3290 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003291 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 {
3293 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003294 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 {
3296 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 if (vim_iswhite(*p))
3299 break;
3300 }
3301 return;
3302 }
3303 }
3304 else
3305 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3306 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 while ((xp->xp_pattern = vim_strpbrk(arg,
3308 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3309 {
3310 c = *xp->xp_pattern;
3311 if (c == '&')
3312 {
3313 c = xp->xp_pattern[1];
3314 if (c == '&')
3315 {
3316 ++xp->xp_pattern;
3317 xp->xp_context = cmdidx != CMD_let || got_eq
3318 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3319 }
3320 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003323 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3324 xp->xp_pattern += 2;
3325
3326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 else if (c == '$')
3329 {
3330 /* environment variable */
3331 xp->xp_context = EXPAND_ENV_VARS;
3332 }
3333 else if (c == '=')
3334 {
3335 got_eq = TRUE;
3336 xp->xp_context = EXPAND_EXPRESSION;
3337 }
3338 else if (c == '<'
3339 && xp->xp_context == EXPAND_FUNCTIONS
3340 && vim_strchr(xp->xp_pattern, '(') == NULL)
3341 {
3342 /* Function name can start with "<SNR>" */
3343 break;
3344 }
3345 else if (cmdidx != CMD_let || got_eq)
3346 {
3347 if (c == '"') /* string */
3348 {
3349 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3350 if (c == '\\' && xp->xp_pattern[1] != NUL)
3351 ++xp->xp_pattern;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '\'') /* literal string */
3355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3358 /* skip */ ;
3359 xp->xp_context = EXPAND_NOTHING;
3360 }
3361 else if (c == '|')
3362 {
3363 if (xp->xp_pattern[1] == '|')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = EXPAND_EXPRESSION;
3367 }
3368 else
3369 xp->xp_context = EXPAND_COMMANDS;
3370 }
3371 else
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 /* Doesn't look like something valid, expand as an expression
3376 * anyway. */
3377 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 arg = xp->xp_pattern;
3379 if (*arg != NUL)
3380 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3381 /* skip */ ;
3382 }
3383 xp->xp_pattern = arg;
3384}
3385
3386#endif /* FEAT_CMDL_COMPL */
3387
3388/*
3389 * ":1,25call func(arg1, arg2)" function call.
3390 */
3391 void
3392ex_call(eap)
3393 exarg_T *eap;
3394{
3395 char_u *arg = eap->arg;
3396 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 linenr_T lnum;
3402 int doesrange;
3403 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003406 if (eap->skip)
3407 {
3408 /* trans_function_name() doesn't work well when skipping, use eval0()
3409 * instead to skip to any following command, e.g. for:
3410 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003411 ++emsg_skip;
3412 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3413 clear_tv(&rettv);
3414 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 return;
3416 }
3417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003418 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003419 if (fudi.fd_newkey != NULL)
3420 {
3421 /* Still need to give an error message for missing key. */
3422 EMSG2(_(e_dictkey), fudi.fd_newkey);
3423 vim_free(fudi.fd_newkey);
3424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 if (tofree == NULL)
3426 return;
3427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 /* Increase refcount on dictionary, it could get deleted when evaluating
3429 * the arguments. */
3430 if (fudi.fd_dict != NULL)
3431 ++fudi.fd_dict->dv_refcount;
3432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003435 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar532c7802005-01-27 14:44:31 +00003437 /* Skip white space to allow ":call func ()". Not good, but required for
3438 * backward compatibility. */
3439 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 if (*startarg != '(')
3443 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003444 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 goto end;
3446 }
3447
3448 /*
3449 * When skipping, evaluate the function once, to find the end of the
3450 * arguments.
3451 * When the function takes a range, this is discovered after the first
3452 * call, and the loop is broken.
3453 */
3454 if (eap->skip)
3455 {
3456 ++emsg_skip;
3457 lnum = eap->line2; /* do it once, also with an invalid range */
3458 }
3459 else
3460 lnum = eap->line1;
3461 for ( ; lnum <= eap->line2; ++lnum)
3462 {
3463 if (!eap->skip && eap->addr_count > 0)
3464 {
3465 curwin->w_cursor.lnum = lnum;
3466 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003467#ifdef FEAT_VIRTUALEDIT
3468 curwin->w_cursor.coladd = 0;
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 eap->line1, eap->line2, &doesrange,
3474 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 failed = TRUE;
3477 break;
3478 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
3480 /* Handle a function returning a Funcref, Dictionary or List. */
3481 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3482 {
3483 failed = TRUE;
3484 break;
3485 }
3486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (doesrange || eap->skip)
3489 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003494 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (aborting())
3496 break;
3497 }
3498 if (eap->skip)
3499 --emsg_skip;
3500
3501 if (!failed)
3502 {
3503 /* Check for trailing illegal characters and a following command. */
3504 if (!ends_excmd(*arg))
3505 {
3506 emsg_severe = TRUE;
3507 EMSG(_(e_trailing));
3508 }
3509 else
3510 eap->nextcmd = check_nextcmd(arg);
3511 }
3512
3513end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518/*
3519 * ":unlet[!] var1 ... " command.
3520 */
3521 void
3522ex_unlet(eap)
3523 exarg_T *eap;
3524{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 ex_unletlock(eap, eap->arg, 0);
3526}
3527
3528/*
3529 * ":lockvar" and ":unlockvar" commands
3530 */
3531 void
3532ex_lockvar(eap)
3533 exarg_T *eap;
3534{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 int deep = 2;
3537
3538 if (eap->forceit)
3539 deep = -1;
3540 else if (vim_isdigit(*arg))
3541 {
3542 deep = getdigits(&arg);
3543 arg = skipwhite(arg);
3544 }
3545
3546 ex_unletlock(eap, arg, deep);
3547}
3548
3549/*
3550 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3551 */
3552 static void
3553ex_unletlock(eap, argstart, deep)
3554 exarg_T *eap;
3555 char_u *argstart;
3556 int deep;
3557{
3558 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 do
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003566 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003567 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (lv.ll_name == NULL)
3569 error = TRUE; /* error but continue parsing */
3570 if (name_end == NULL || (!vim_iswhite(*name_end)
3571 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 if (name_end != NULL)
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 if (!(eap->skip || error))
3579 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581 }
3582
3583 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 {
3585 if (eap->cmdidx == CMD_unlet)
3586 {
3587 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3588 error = TRUE;
3589 }
3590 else
3591 {
3592 if (do_lock_var(&lv, name_end, deep,
3593 eap->cmdidx == CMD_lockvar) == FAIL)
3594 error = TRUE;
3595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 if (!eap->skip)
3599 clear_lval(&lv);
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 arg = skipwhite(name_end);
3602 } while (!ends_excmd(*arg));
3603
3604 eap->nextcmd = check_nextcmd(arg);
3605}
3606
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 int forceit;
3612{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 int ret = OK;
3614 int cc;
3615
3616 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 cc = *name_end;
3619 *name_end = NUL;
3620
3621 /* Normal name or expanded name. */
3622 if (check_changedtick(lp->ll_name))
3623 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3629 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 else if (lp->ll_range)
3631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633
3634 /* Delete a range of List items. */
3635 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3636 {
3637 li = lp->ll_li->li_next;
3638 listitem_remove(lp->ll_list, lp->ll_li);
3639 lp->ll_li = li;
3640 ++lp->ll_n1;
3641 }
3642 }
3643 else
3644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 /* unlet a List item. */
3647 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003650 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 }
3652
3653 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654}
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 hashtab_T *ht;
3666 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003668 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 ht = find_var_ht(name, &varname);
3671 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = hash_find(ht, varname);
3674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003676 di = HI2DI(hi);
3677 if (var_check_fixed(di->di_flags, name)
3678 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
3680 delete_var(ht, hi);
3681 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 if (forceit)
3685 return OK;
3686 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688}
3689
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690/*
3691 * Lock or unlock variable indicated by "lp".
3692 * "deep" is the levels to go (-1 for unlimited);
3693 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3694 */
3695 static int
3696do_lock_var(lp, name_end, deep, lock)
3697 lval_T *lp;
3698 char_u *name_end;
3699 int deep;
3700 int lock;
3701{
3702 int ret = OK;
3703 int cc;
3704 dictitem_T *di;
3705
3706 if (deep == 0) /* nothing to do */
3707 return OK;
3708
3709 if (lp->ll_tv == NULL)
3710 {
3711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
3717 else
3718 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003719 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 if (di == NULL)
3721 ret = FAIL;
3722 else
3723 {
3724 if (lock)
3725 di->di_flags |= DI_FLAGS_LOCK;
3726 else
3727 di->di_flags &= ~DI_FLAGS_LOCK;
3728 item_lock(&di->di_tv, deep, lock);
3729 }
3730 }
3731 *name_end = cc;
3732 }
3733 else if (lp->ll_range)
3734 {
3735 listitem_T *li = lp->ll_li;
3736
3737 /* (un)lock a range of List items. */
3738 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3739 {
3740 item_lock(&li->li_tv, deep, lock);
3741 li = li->li_next;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else if (lp->ll_list != NULL)
3746 /* (un)lock a List item. */
3747 item_lock(&lp->ll_li->li_tv, deep, lock);
3748 else
3749 /* un(lock) a Dictionary item. */
3750 item_lock(&lp->ll_di->di_tv, deep, lock);
3751
3752 return ret;
3753}
3754
3755/*
3756 * Lock or unlock an item. "deep" is nr of levels to go.
3757 */
3758 static void
3759item_lock(tv, deep, lock)
3760 typval_T *tv;
3761 int deep;
3762 int lock;
3763{
3764 static int recurse = 0;
3765 list_T *l;
3766 listitem_T *li;
3767 dict_T *d;
3768 hashitem_T *hi;
3769 int todo;
3770
3771 if (recurse >= DICT_MAXNEST)
3772 {
3773 EMSG(_("E743: variable nested too deep for (un)lock"));
3774 return;
3775 }
3776 if (deep == 0)
3777 return;
3778 ++recurse;
3779
3780 /* lock/unlock the item itself */
3781 if (lock)
3782 tv->v_lock |= VAR_LOCKED;
3783 else
3784 tv->v_lock &= ~VAR_LOCKED;
3785
3786 switch (tv->v_type)
3787 {
3788 case VAR_LIST:
3789 if ((l = tv->vval.v_list) != NULL)
3790 {
3791 if (lock)
3792 l->lv_lock |= VAR_LOCKED;
3793 else
3794 l->lv_lock &= ~VAR_LOCKED;
3795 if (deep < 0 || deep > 1)
3796 /* recursive: lock/unlock the items the List contains */
3797 for (li = l->lv_first; li != NULL; li = li->li_next)
3798 item_lock(&li->li_tv, deep - 1, lock);
3799 }
3800 break;
3801 case VAR_DICT:
3802 if ((d = tv->vval.v_dict) != NULL)
3803 {
3804 if (lock)
3805 d->dv_lock |= VAR_LOCKED;
3806 else
3807 d->dv_lock &= ~VAR_LOCKED;
3808 if (deep < 0 || deep > 1)
3809 {
3810 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003811 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3813 {
3814 if (!HASHITEM_EMPTY(hi))
3815 {
3816 --todo;
3817 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3818 }
3819 }
3820 }
3821 }
3822 }
3823 --recurse;
3824}
3825
Bram Moolenaara40058a2005-07-11 22:42:07 +00003826/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003827 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3828 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003829 */
3830 static int
3831tv_islocked(tv)
3832 typval_T *tv;
3833{
3834 return (tv->v_lock & VAR_LOCKED)
3835 || (tv->v_type == VAR_LIST
3836 && tv->vval.v_list != NULL
3837 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3838 || (tv->v_type == VAR_DICT
3839 && tv->vval.v_dict != NULL
3840 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3841}
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3844/*
3845 * Delete all "menutrans_" variables.
3846 */
3847 void
3848del_menutrans_vars()
3849{
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3861 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 }
3863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866#endif
3867
3868#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3869
3870/*
3871 * Local string buffer for the next two functions to store a variable name
3872 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3873 * get_user_var_name().
3874 */
3875
3876static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3877
3878static char_u *varnamebuf = NULL;
3879static int varnamebuflen = 0;
3880
3881/*
3882 * Function to concatenate a prefix and a variable name.
3883 */
3884 static char_u *
3885cat_prefix_varname(prefix, name)
3886 int prefix;
3887 char_u *name;
3888{
3889 int len;
3890
3891 len = (int)STRLEN(name) + 3;
3892 if (len > varnamebuflen)
3893 {
3894 vim_free(varnamebuf);
3895 len += 10; /* some additional space */
3896 varnamebuf = alloc(len);
3897 if (varnamebuf == NULL)
3898 {
3899 varnamebuflen = 0;
3900 return NULL;
3901 }
3902 varnamebuflen = len;
3903 }
3904 *varnamebuf = prefix;
3905 varnamebuf[1] = ':';
3906 STRCPY(varnamebuf + 2, name);
3907 return varnamebuf;
3908}
3909
3910/*
3911 * Function given to ExpandGeneric() to obtain the list of user defined
3912 * (global/buffer/window/built-in) variable names.
3913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 char_u *
3915get_user_var_name(xp, idx)
3916 expand_T *xp;
3917 int idx;
3918{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 static long_u gdone;
3920 static long_u bdone;
3921 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003922#ifdef FEAT_WINDOWS
3923 static long_u tdone;
3924#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static int vidx;
3926 static hashitem_T *hi;
3927 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932#ifdef FEAT_WINDOWS
3933 tdone = 0;
3934#endif
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936
3937 /* Global variables */
3938 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3947 return cat_prefix_varname('g', hi->hi_key);
3948 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
3951 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003952 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003957 else
3958 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 while (HASHITEM_EMPTY(hi))
3960 ++hi;
3961 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return (char_u *)"b:changedtick";
3967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968
3969 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003970 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003973 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 else
3976 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003977 while (HASHITEM_EMPTY(hi))
3978 ++hi;
3979 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003984 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 if (tdone < ht->ht_used)
3986 {
3987 if (tdone++ == 0)
3988 hi = ht->ht_array;
3989 else
3990 ++hi;
3991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 return cat_prefix_varname('t', hi->hi_key);
3994 }
3995#endif
3996
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 /* v: variables */
3998 if (vidx < VV_LEN)
3999 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 vim_free(varnamebuf);
4002 varnamebuf = NULL;
4003 varnamebuflen = 0;
4004 return NULL;
4005}
4006
4007#endif /* FEAT_CMDL_COMPL */
4008
4009/*
4010 * types for expressions.
4011 */
4012typedef enum
4013{
4014 TYPE_UNKNOWN = 0
4015 , TYPE_EQUAL /* == */
4016 , TYPE_NEQUAL /* != */
4017 , TYPE_GREATER /* > */
4018 , TYPE_GEQUAL /* >= */
4019 , TYPE_SMALLER /* < */
4020 , TYPE_SEQUAL /* <= */
4021 , TYPE_MATCH /* =~ */
4022 , TYPE_NOMATCH /* !~ */
4023} exptype_T;
4024
4025/*
4026 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4029 */
4030
4031/*
4032 * Handle zero level expression.
4033 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004035 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Return OK or FAIL.
4037 */
4038 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 char_u **nextcmd;
4043 int evaluate;
4044{
4045 int ret;
4046 char_u *p;
4047
4048 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (ret == FAIL || !ends_excmd(*p))
4051 {
4052 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /*
4055 * Report the invalid expression unless the expression evaluation has
4056 * been cancelled due to an aborting error, an interrupt, or an
4057 * exception.
4058 */
4059 if (!aborting())
4060 EMSG2(_(e_invexpr2), arg);
4061 ret = FAIL;
4062 }
4063 if (nextcmd != NULL)
4064 *nextcmd = check_nextcmd(p);
4065
4066 return ret;
4067}
4068
4069/*
4070 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004071 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 *
4073 * "arg" must point to the first non-white of the expression.
4074 * "arg" is advanced to the next non-white after the recognized expression.
4075 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004076 * Note: "rettv.v_lock" is not set.
4077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
4086 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 /*
4090 * Get the first variable.
4091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094
4095 if ((*arg)[0] == '?')
4096 {
4097 result = FALSE;
4098 if (evaluate)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 int error = FALSE;
4101
4102 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (error)
4106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
4109 /*
4110 * Get the second variable.
4111 */
4112 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return FAIL;
4115
4116 /*
4117 * Check for the ":".
4118 */
4119 if ((*arg)[0] != ':')
4120 {
4121 EMSG(_("E109: Missing ':' after '?'"));
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126
4127 /*
4128 * Get the third variable.
4129 */
4130 *arg = skipwhite(*arg + 1);
4131 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4132 {
4133 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return FAIL;
4136 }
4137 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 return OK;
4142}
4143
4144/*
4145 * Handle first level expression:
4146 * expr2 || expr2 || expr2 logical OR
4147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int evaluate;
4158{
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 long result;
4161 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * Get the first variable.
4166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169
4170 /*
4171 * Repeat until there is no following "||".
4172 */
4173 first = TRUE;
4174 result = FALSE;
4175 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4176 {
4177 if (evaluate && first)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 first = FALSE;
4185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 2);
4191 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4192 return FAIL;
4193
4194 /*
4195 * Compute the result.
4196 */
4197 if (evaluate && !result)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 if (error)
4203 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 if (evaluate)
4206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 rettv->v_type = VAR_NUMBER;
4208 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 return OK;
4213}
4214
4215/*
4216 * Handle second level expression:
4217 * expr3 && expr3 && expr3 logical AND
4218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 long result;
4232 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * Get the first variable.
4237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return FAIL;
4240
4241 /*
4242 * Repeat until there is no following "&&".
4243 */
4244 first = TRUE;
4245 result = TRUE;
4246 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4247 {
4248 if (evaluate && first)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (error)
4254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 first = FALSE;
4256 }
4257
4258 /*
4259 * Get the second variable.
4260 */
4261 *arg = skipwhite(*arg + 2);
4262 if (eval4(arg, &var2, evaluate && result) == FAIL)
4263 return FAIL;
4264
4265 /*
4266 * Compute the result.
4267 */
4268 if (evaluate && result)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 if (evaluate)
4277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle third level expression:
4288 * var1 == var2
4289 * var1 =~ var2
4290 * var1 != var2
4291 * var1 !~ var2
4292 * var1 > var2
4293 * var1 >= var2
4294 * var1 < var2
4295 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 * var1 is var2
4297 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 *
4299 * "arg" must point to the first non-white of the expression.
4300 * "arg" is advanced to the next non-white after the recognized expression.
4301 *
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int evaluate;
4309{
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 char_u *p;
4312 int i;
4313 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int len = 2;
4316 long n1, n2;
4317 char_u *s1, *s2;
4318 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4319 regmatch_T regmatch;
4320 int ic;
4321 char_u *save_cpo;
4322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 p = *arg;
4330 switch (p[0])
4331 {
4332 case '=': if (p[1] == '=')
4333 type = TYPE_EQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_MATCH;
4336 break;
4337 case '!': if (p[1] == '=')
4338 type = TYPE_NEQUAL;
4339 else if (p[1] == '~')
4340 type = TYPE_NOMATCH;
4341 break;
4342 case '>': if (p[1] != '=')
4343 {
4344 type = TYPE_GREATER;
4345 len = 1;
4346 }
4347 else
4348 type = TYPE_GEQUAL;
4349 break;
4350 case '<': if (p[1] != '=')
4351 {
4352 type = TYPE_SMALLER;
4353 len = 1;
4354 }
4355 else
4356 type = TYPE_SEQUAL;
4357 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 case 'i': if (p[1] == 's')
4359 {
4360 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4361 len = 5;
4362 if (!vim_isIDc(p[len]))
4363 {
4364 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4365 type_is = TRUE;
4366 }
4367 }
4368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
4374 if (type != TYPE_UNKNOWN)
4375 {
4376 /* extra question mark appended: ignore case */
4377 if (p[len] == '?')
4378 {
4379 ic = TRUE;
4380 ++len;
4381 }
4382 /* extra '#' appended: match case */
4383 else if (p[len] == '#')
4384 {
4385 ic = FALSE;
4386 ++len;
4387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 else
4390 ic = p_ic;
4391
4392 /*
4393 * Get the second variable.
4394 */
4395 *arg = skipwhite(p + len);
4396 if (eval5(arg, &var2, evaluate) == FAIL)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400 }
4401
4402 if (evaluate)
4403 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 if (type_is && rettv->v_type != var2.v_type)
4405 {
4406 /* For "is" a different type always means FALSE, for "notis"
4407 * it means TRUE. */
4408 n1 = (type == TYPE_NEQUAL);
4409 }
4410 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4411 {
4412 if (type_is)
4413 {
4414 n1 = (rettv->v_type == var2.v_type
4415 && rettv->vval.v_list == var2.vval.v_list);
4416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 else if (rettv->v_type != var2.v_type
4420 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4421 {
4422 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004423 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004425 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
4430 else
4431 {
4432 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004433 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4434 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 }
4439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004440 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4441 {
4442 if (type_is)
4443 {
4444 n1 = (rettv->v_type == var2.v_type
4445 && rettv->vval.v_dict == var2.vval.v_dict);
4446 if (type == TYPE_NEQUAL)
4447 n1 = !n1;
4448 }
4449 else if (rettv->v_type != var2.v_type
4450 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4451 {
4452 if (rettv->v_type != var2.v_type)
4453 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4454 else
4455 EMSG(_("E736: Invalid operation for Dictionary"));
4456 clear_tv(rettv);
4457 clear_tv(&var2);
4458 return FAIL;
4459 }
4460 else
4461 {
4462 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004463 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4464 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4471 {
4472 if (rettv->v_type != var2.v_type
4473 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4474 {
4475 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004476 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004478 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 clear_tv(rettv);
4480 clear_tv(&var2);
4481 return FAIL;
4482 }
4483 else
4484 {
4485 /* Compare two Funcrefs for being equal or unequal. */
4486 if (rettv->vval.v_string == NULL
4487 || var2.vval.v_string == NULL)
4488 n1 = FALSE;
4489 else
4490 n1 = STRCMP(rettv->vval.v_string,
4491 var2.vval.v_string) == 0;
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 }
4496
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004497#ifdef FEAT_FLOAT
4498 /*
4499 * If one of the two variables is a float, compare as a float.
4500 * When using "=~" or "!~", always compare as string.
4501 */
4502 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4503 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4504 {
4505 float_T f1, f2;
4506
4507 if (rettv->v_type == VAR_FLOAT)
4508 f1 = rettv->vval.v_float;
4509 else
4510 f1 = get_tv_number(rettv);
4511 if (var2.v_type == VAR_FLOAT)
4512 f2 = var2.vval.v_float;
4513 else
4514 f2 = get_tv_number(&var2);
4515 n1 = FALSE;
4516 switch (type)
4517 {
4518 case TYPE_EQUAL: n1 = (f1 == f2); break;
4519 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4520 case TYPE_GREATER: n1 = (f1 > f2); break;
4521 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4522 case TYPE_SMALLER: n1 = (f1 < f2); break;
4523 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4524 case TYPE_UNKNOWN:
4525 case TYPE_MATCH:
4526 case TYPE_NOMATCH: break; /* avoid gcc warning */
4527 }
4528 }
4529#endif
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * If one of the two variables is a number, compare as a number.
4533 * When using "=~" or "!~", always compare as string.
4534 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 n1 = get_tv_number(rettv);
4539 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 switch (type)
4541 {
4542 case TYPE_EQUAL: n1 = (n1 == n2); break;
4543 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4544 case TYPE_GREATER: n1 = (n1 > n2); break;
4545 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4546 case TYPE_SMALLER: n1 = (n1 < n2); break;
4547 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4548 case TYPE_UNKNOWN:
4549 case TYPE_MATCH:
4550 case TYPE_NOMATCH: break; /* avoid gcc warning */
4551 }
4552 }
4553 else
4554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 s1 = get_tv_string_buf(rettv, buf1);
4556 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4558 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4559 else
4560 i = 0;
4561 n1 = FALSE;
4562 switch (type)
4563 {
4564 case TYPE_EQUAL: n1 = (i == 0); break;
4565 case TYPE_NEQUAL: n1 = (i != 0); break;
4566 case TYPE_GREATER: n1 = (i > 0); break;
4567 case TYPE_GEQUAL: n1 = (i >= 0); break;
4568 case TYPE_SMALLER: n1 = (i < 0); break;
4569 case TYPE_SEQUAL: n1 = (i <= 0); break;
4570
4571 case TYPE_MATCH:
4572 case TYPE_NOMATCH:
4573 /* avoid 'l' flag in 'cpoptions' */
4574 save_cpo = p_cpo;
4575 p_cpo = (char_u *)"";
4576 regmatch.regprog = vim_regcomp(s2,
4577 RE_MAGIC + RE_STRING);
4578 regmatch.rm_ic = ic;
4579 if (regmatch.regprog != NULL)
4580 {
4581 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004582 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (type == TYPE_NOMATCH)
4584 n1 = !n1;
4585 }
4586 p_cpo = save_cpo;
4587 break;
4588
4589 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4590 }
4591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 clear_tv(rettv);
4593 clear_tv(&var2);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Handle fourth level expression:
4604 * + number addition
4605 * - number subtraction
4606 * . string concatenation
4607 *
4608 * "arg" must point to the first non-white of the expression.
4609 * "arg" is advanced to the next non-white after the recognized expression.
4610 *
4611 * Return OK or FAIL.
4612 */
4613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 int evaluate;
4618{
Bram Moolenaar33570922005-01-25 22:26:29 +00004619 typval_T var2;
4620 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 int op;
4622 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623#ifdef FEAT_FLOAT
4624 float_T f1 = 0, f2 = 0;
4625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 char_u *s1, *s2;
4627 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4628 char_u *p;
4629
4630 /*
4631 * Get the first variable.
4632 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004633 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635
4636 /*
4637 * Repeat computing, until no '+', '-' or '.' is following.
4638 */
4639 for (;;)
4640 {
4641 op = **arg;
4642 if (op != '+' && op != '-' && op != '.')
4643 break;
4644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 if ((op != '+' || rettv->v_type != VAR_LIST)
4646#ifdef FEAT_FLOAT
4647 && (op == '.' || rettv->v_type != VAR_FLOAT)
4648#endif
4649 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 {
4651 /* For "list + ...", an illegal use of the first operand as
4652 * a number cannot be determined before evaluating the 2nd
4653 * operand: if this is also a list, all is ok.
4654 * For "something . ...", "something - ..." or "non-list + ...",
4655 * we know that the first operand needs to be a string or number
4656 * without evaluating the 2nd operand. So check before to avoid
4657 * side effects after an error. */
4658 if (evaluate && get_tv_string_chk(rettv) == NULL)
4659 {
4660 clear_tv(rettv);
4661 return FAIL;
4662 }
4663 }
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
4666 * Get the second variable.
4667 */
4668 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673 }
4674
4675 if (evaluate)
4676 {
4677 /*
4678 * Compute the result.
4679 */
4680 if (op == '.')
4681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4683 s2 = get_tv_string_buf_chk(&var2, buf2);
4684 if (s2 == NULL) /* type error ? */
4685 {
4686 clear_tv(rettv);
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004690 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004695 else if (op == '+' && rettv->v_type == VAR_LIST
4696 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004697 {
4698 /* concatenate Lists */
4699 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4700 &var3) == FAIL)
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
4706 clear_tv(rettv);
4707 *rettv = var3;
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else
4710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 int error = FALSE;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 f1 = rettv->vval.v_float;
4717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 else
4720#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 n1 = get_tv_number_chk(rettv, &error);
4723 if (error)
4724 {
4725 /* This can only happen for "list + non-list". For
4726 * "non-list + ..." or "something - ...", we returned
4727 * before evaluating the 2nd operand. */
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 f1 = n1;
4734#endif
4735 }
4736#ifdef FEAT_FLOAT
4737 if (var2.v_type == VAR_FLOAT)
4738 {
4739 f2 = var2.vval.v_float;
4740 n2 = 0;
4741 }
4742 else
4743#endif
4744 {
4745 n2 = get_tv_number_chk(&var2, &error);
4746 if (error)
4747 {
4748 clear_tv(rettv);
4749 clear_tv(&var2);
4750 return FAIL;
4751 }
4752#ifdef FEAT_FLOAT
4753 if (rettv->v_type == VAR_FLOAT)
4754 f2 = n2;
4755#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758
4759#ifdef FEAT_FLOAT
4760 /* If there is a float on either side the result is a float. */
4761 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4762 {
4763 if (op == '+')
4764 f1 = f1 + f2;
4765 else
4766 f1 = f1 - f2;
4767 rettv->v_type = VAR_FLOAT;
4768 rettv->vval.v_float = f1;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#endif
4772 {
4773 if (op == '+')
4774 n1 = n1 + n2;
4775 else
4776 n1 = n1 - n2;
4777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = n1;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 }
4784 return OK;
4785}
4786
4787/*
4788 * Handle fifth level expression:
4789 * * number multiplication
4790 * / number division
4791 * % number modulo
4792 *
4793 * "arg" must point to the first non-white of the expression.
4794 * "arg" is advanced to the next non-white after the recognized expression.
4795 *
4796 * Return OK or FAIL.
4797 */
4798 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004799eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
Bram Moolenaar33570922005-01-25 22:26:29 +00004805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 int op;
4807 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 int use_float = FALSE;
4810 float_T f1 = 0, f2;
4811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * Get the first variable.
4816 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004817 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819
4820 /*
4821 * Repeat computing, until no '*', '/' or '%' is following.
4822 */
4823 for (;;)
4824 {
4825 op = **arg;
4826 if (op != '*' && op != '/' && op != '%')
4827 break;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 {
4834 f1 = rettv->vval.v_float;
4835 use_float = TRUE;
4836 n1 = 0;
4837 }
4838 else
4839#endif
4840 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 if (error)
4843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
4846 n1 = 0;
4847
4848 /*
4849 * Get the second variable.
4850 */
4851 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004852 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 return FAIL;
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857#ifdef FEAT_FLOAT
4858 if (var2.v_type == VAR_FLOAT)
4859 {
4860 if (!use_float)
4861 {
4862 f1 = n1;
4863 use_float = TRUE;
4864 }
4865 f2 = var2.vval.v_float;
4866 n2 = 0;
4867 }
4868 else
4869#endif
4870 {
4871 n2 = get_tv_number_chk(&var2, &error);
4872 clear_tv(&var2);
4873 if (error)
4874 return FAIL;
4875#ifdef FEAT_FLOAT
4876 if (use_float)
4877 f2 = n2;
4878#endif
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 if (op == '*')
4889 f1 = f1 * f2;
4890 else if (op == '/')
4891 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# ifdef VMS
4893 /* VMS crashes on divide by zero, work around it */
4894 if (f2 == 0.0)
4895 {
4896 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004897 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004899 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004901 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902 }
4903 else
4904 f1 = f1 / f2;
4905# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 /* We rely on the floating point library to handle divide
4907 * by zero to result in "inf" and not a crash. */
4908 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004913 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914 return FAIL;
4915 }
4916 rettv->v_type = VAR_FLOAT;
4917 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 if (op == '*')
4923 n1 = n1 * n2;
4924 else if (op == '/')
4925 {
4926 if (n2 == 0) /* give an error message? */
4927 {
4928 if (n1 == 0)
4929 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4930 else if (n1 < 0)
4931 n1 = -0x7fffffffL;
4932 else
4933 n1 = 0x7fffffffL;
4934 }
4935 else
4936 n1 = n1 / n2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 {
4940 if (n2 == 0) /* give an error message? */
4941 n1 = 0;
4942 else
4943 n1 = n1 % n2;
4944 }
4945 rettv->v_type = VAR_NUMBER;
4946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950
4951 return OK;
4952}
4953
4954/*
4955 * Handle sixth level expression:
4956 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004957 * "string" string constant
4958 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * &option-name option value
4960 * @r register contents
4961 * identifier variable value
4962 * function() function call
4963 * $VAR environment variable
4964 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004965 * [expr, expr] List
4966 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * Also handle:
4969 * ! in front logical NOT
4970 * - in front unary minus
4971 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * trailing [] subscript in String or List
4973 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 *
4975 * "arg" must point to the first non-white of the expression.
4976 * "arg" is advanced to the next non-white after the recognized expression.
4977 *
4978 * Return OK or FAIL.
4979 */
4980 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004981eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004985 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 long n;
4988 int len;
4989 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 char_u *start_leader, *end_leader;
4991 int ret = OK;
4992 char_u *alias;
4993
4994 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 /*
5001 * Skip '!' and '-' characters. They are handled later.
5002 */
5003 start_leader = *arg;
5004 while (**arg == '!' || **arg == '-' || **arg == '+')
5005 *arg = skipwhite(*arg + 1);
5006 end_leader = *arg;
5007
5008 switch (**arg)
5009 {
5010 /*
5011 * Number constant.
5012 */
5013 case '0':
5014 case '1':
5015 case '2':
5016 case '3':
5017 case '4':
5018 case '5':
5019 case '6':
5020 case '7':
5021 case '8':
5022 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 {
5024#ifdef FEAT_FLOAT
5025 char_u *p = skipdigits(*arg + 1);
5026 int get_float = FALSE;
5027
5028 /* We accept a float when the format matches
5029 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005030 * strict to avoid backwards compatibility problems.
5031 * Don't look for a float after the "." operator, so that
5032 * ":let vers = 1.2.3" doesn't fail. */
5033 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 get_float = TRUE;
5036 p = skipdigits(p + 2);
5037 if (*p == 'e' || *p == 'E')
5038 {
5039 ++p;
5040 if (*p == '-' || *p == '+')
5041 ++p;
5042 if (!vim_isdigit(*p))
5043 get_float = FALSE;
5044 else
5045 p = skipdigits(p + 1);
5046 }
5047 if (ASCII_ISALPHA(*p) || *p == '.')
5048 get_float = FALSE;
5049 }
5050 if (get_float)
5051 {
5052 float_T f;
5053
5054 *arg += string2float(*arg, &f);
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_FLOAT;
5058 rettv->vval.v_float = f;
5059 }
5060 }
5061 else
5062#endif
5063 {
5064 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5065 *arg += len;
5066 if (evaluate)
5067 {
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /*
5076 * String constant: "string".
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 break;
5086
5087 /*
5088 * List: [expr, expr]
5089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Dictionary: {key: val, key: val}
5095 */
5096 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5097 break;
5098
5099 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 break;
5104
5105 /*
5106 * Environment variable: $VAR.
5107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Register contents: @r.
5113 */
5114 case '@': ++*arg;
5115 if (evaluate)
5116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005118 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 if (**arg != NUL)
5121 ++*arg;
5122 break;
5123
5124 /*
5125 * nested expression: (expression).
5126 */
5127 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (**arg == ')')
5130 ++*arg;
5131 else if (ret == OK)
5132 {
5133 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ret = FAIL;
5136 }
5137 break;
5138
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142
5143 if (ret == NOTDONE)
5144 {
5145 /*
5146 * Must be a variable or function name.
5147 * Can also be a curly-braces kind of name: {expr}.
5148 */
5149 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 if (alias != NULL)
5152 s = alias;
5153
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ret = FAIL;
5156 else
5157 {
5158 if (**arg == '(') /* recursive! */
5159 {
5160 /* If "s" is the name of a variable of type VAR_FUNC
5161 * use its contents. */
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01005162 s = deref_func_name(s, &len, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163
5164 /* Invoke the function. */
5165 ret = get_func_tv(s, len, rettv, arg,
5166 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005168
5169 /* If evaluate is FALSE rettv->v_type was not set in
5170 * get_func_tv, but it's needed in handle_subscript() to parse
5171 * what follows. So set it here. */
5172 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5173 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005174 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005175 rettv->v_type = VAR_FUNC;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 /* Stop the expression evaluation when immediately
5179 * aborting on error, or when an interrupt occurred or
5180 * an exception was thrown but not caught. */
5181 if (aborting())
5182 {
5183 if (ret == OK)
5184 clear_tv(rettv);
5185 ret = FAIL;
5186 }
5187 }
5188 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005189 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005190 else
5191 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005193 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 }
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 *arg = skipwhite(*arg);
5197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5199 * expr(expr). */
5200 if (ret == OK)
5201 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 /*
5204 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5205 */
5206 if (ret == OK && evaluate && end_leader > start_leader)
5207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005209 int val = 0;
5210#ifdef FEAT_FLOAT
5211 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 if (rettv->v_type == VAR_FLOAT)
5214 f = rettv->vval.v_float;
5215 else
5216#endif
5217 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 clear_tv(rettv);
5221 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 else
5224 {
5225 while (end_leader > start_leader)
5226 {
5227 --end_leader;
5228 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 {
5230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 f = !f;
5233 else
5234#endif
5235 val = !val;
5236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 {
5239#ifdef FEAT_FLOAT
5240 if (rettv->v_type == VAR_FLOAT)
5241 f = -f;
5242 else
5243#endif
5244 val = -val;
5245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 {
5250 clear_tv(rettv);
5251 rettv->vval.v_float = f;
5252 }
5253 else
5254#endif
5255 {
5256 clear_tv(rettv);
5257 rettv->v_type = VAR_NUMBER;
5258 rettv->vval.v_number = val;
5259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
5263 return ret;
5264}
5265
5266/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005267 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5268 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5270 */
5271 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277{
5278 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 long len = -1;
5282 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005286 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 if (verbose)
5289 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 return FAIL;
5291 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292#ifdef FEAT_FLOAT
5293 else if (rettv->v_type == VAR_FLOAT)
5294 {
5295 if (verbose)
5296 EMSG(_(e_float_as_string));
5297 return FAIL;
5298 }
5299#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * dict.name
5305 */
5306 key = *arg + 1;
5307 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5308 ;
5309 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 }
5313 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 /*
5316 * something[idx]
5317 *
5318 * Get the (first) variable from inside the [].
5319 */
5320 *arg = skipwhite(*arg + 1);
5321 if (**arg == ':')
5322 empty1 = TRUE;
5323 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5324 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5326 {
5327 /* not a number or string */
5328 clear_tv(&var1);
5329 return FAIL;
5330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331
5332 /*
5333 * Get the second variable from inside the [:].
5334 */
5335 if (**arg == ':')
5336 {
5337 range = TRUE;
5338 *arg = skipwhite(*arg + 1);
5339 if (**arg == ']')
5340 empty2 = TRUE;
5341 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 if (!empty1)
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
5347 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5348 {
5349 /* not a number or string */
5350 if (!empty1)
5351 clear_tv(&var1);
5352 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 return FAIL;
5354 }
5355 }
5356
5357 /* Check for the ']'. */
5358 if (**arg != ']')
5359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 if (verbose)
5361 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 clear_tv(&var1);
5363 if (range)
5364 clear_tv(&var2);
5365 return FAIL;
5366 }
5367 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 }
5369
5370 if (evaluate)
5371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 n1 = 0;
5373 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 n1 = get_tv_number(&var1);
5376 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 if (range)
5379 {
5380 if (empty2)
5381 n2 = -1;
5382 else
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 n2 = get_tv_number(&var2);
5385 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 }
5388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
5391 case VAR_NUMBER:
5392 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 len = (long)STRLEN(s);
5395 if (range)
5396 {
5397 /* The resulting variable is a substring. If the indexes
5398 * are out of range the result is empty. */
5399 if (n1 < 0)
5400 {
5401 n1 = len + n1;
5402 if (n1 < 0)
5403 n1 = 0;
5404 }
5405 if (n2 < 0)
5406 n2 = len + n2;
5407 else if (n2 >= len)
5408 n2 = len;
5409 if (n1 >= len || n2 < 0 || n1 > n2)
5410 s = NULL;
5411 else
5412 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5413 }
5414 else
5415 {
5416 /* The resulting variable is a string of a single
5417 * character. If the index is too big or negative the
5418 * result is empty. */
5419 if (n1 >= len || n1 < 0)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, 1);
5423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 clear_tv(rettv);
5425 rettv->v_type = VAR_STRING;
5426 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 break;
5428
5429 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 if (n1 < 0)
5432 n1 = len + n1;
5433 if (!empty1 && (n1 < 0 || n1 >= len))
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 /* For a range we allow invalid values and return an empty
5436 * list. A list index out of range is an error. */
5437 if (!range)
5438 {
5439 if (verbose)
5440 EMSGN(_(e_listidx), n1);
5441 return FAIL;
5442 }
5443 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 if (range)
5446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 list_T *l;
5448 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449
5450 if (n2 < 0)
5451 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005452 else if (n2 >= len)
5453 n2 = len - 1;
5454 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 l = list_alloc();
5457 if (l == NULL)
5458 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 n1 <= n2; ++n1)
5461 {
5462 if (list_append_tv(l, &item->li_tv) == FAIL)
5463 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005464 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 return FAIL;
5466 }
5467 item = item->li_next;
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_LIST;
5471 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005472 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
5474 else
5475 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005476 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 clear_tv(rettv);
5478 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 case VAR_DICT:
5483 if (range)
5484 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (verbose)
5486 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 if (len == -1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493
5494 if (len == -1)
5495 {
5496 key = get_tv_string(&var1);
5497 if (*key == NUL)
5498 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 if (verbose)
5500 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 clear_tv(&var1);
5502 return FAIL;
5503 }
5504 }
5505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005506 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510 if (len == -1)
5511 clear_tv(&var1);
5512 if (item == NULL)
5513 return FAIL;
5514
5515 copy_tv(&item->di_tv, &var1);
5516 clear_tv(rettv);
5517 *rettv = var1;
5518 }
5519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 }
5522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return OK;
5524}
5525
5526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * Get an option value.
5528 * "arg" points to the '&' or '+' before the option name.
5529 * "arg" is advanced to character after the option name.
5530 * Return OK or FAIL.
5531 */
5532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 int evaluate;
5537{
5538 char_u *option_end;
5539 long numval;
5540 char_u *stringval;
5541 int opt_type;
5542 int c;
5543 int working = (**arg == '+'); /* has("+option") */
5544 int ret = OK;
5545 int opt_flags;
5546
5547 /*
5548 * Isolate the option name and find its value.
5549 */
5550 option_end = find_option_end(arg, &opt_flags);
5551 if (option_end == NULL)
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 EMSG2(_("E112: Option name missing: %s"), *arg);
5555 return FAIL;
5556 }
5557
5558 if (!evaluate)
5559 {
5560 *arg = option_end;
5561 return OK;
5562 }
5563
5564 c = *option_end;
5565 *option_end = NUL;
5566 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (opt_type == -3) /* invalid name */
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 EMSG2(_("E113: Unknown option: %s"), *arg);
5573 ret = FAIL;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 if (opt_type == -2) /* hidden string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == -1) /* hidden number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else if (opt_type == 1) /* number option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_NUMBER;
5590 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else /* string option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 }
5598 else if (working && (opt_type == -2 || opt_type == -1))
5599 ret = FAIL;
5600
5601 *option_end = c; /* put back for error messages */
5602 *arg = option_end;
5603
5604 return ret;
5605}
5606
5607/*
5608 * Allocate a variable for a string constant.
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int evaluate;
5616{
5617 char_u *p;
5618 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 int extra = 0;
5620
5621 /*
5622 * Find the end of the string, skipping backslashed characters.
5623 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 if (*p == '\\' && p[1] != NUL)
5627 {
5628 ++p;
5629 /* A "\<x>" form occupies at least 4 characters, and produces up
5630 * to 6 characters: reserve space for 2 extra */
5631 if (*p == '<')
5632 extra += 2;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635
5636 if (*p != '"')
5637 {
5638 EMSG2(_("E114: Missing quote: %s"), *arg);
5639 return FAIL;
5640 }
5641
5642 /* If only parsing, set *arg and return here */
5643 if (!evaluate)
5644 {
5645 *arg = p + 1;
5646 return OK;
5647 }
5648
5649 /*
5650 * Copy the string into allocated memory, handling backslashed
5651 * characters.
5652 */
5653 name = alloc((unsigned)(p - *arg + extra));
5654 if (name == NULL)
5655 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 rettv->v_type = VAR_STRING;
5657 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (*p == '\\')
5662 {
5663 switch (*++p)
5664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 case 'b': *name++ = BS; ++p; break;
5666 case 'e': *name++ = ESC; ++p; break;
5667 case 'f': *name++ = FF; ++p; break;
5668 case 'n': *name++ = NL; ++p; break;
5669 case 'r': *name++ = CAR; ++p; break;
5670 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 case 'X': /* hex: "\x1", "\x12" */
5673 case 'x':
5674 case 'u': /* Unicode: "\u0023" */
5675 case 'U':
5676 if (vim_isxdigit(p[1]))
5677 {
5678 int n, nr;
5679 int c = toupper(*p);
5680
5681 if (c == 'X')
5682 n = 2;
5683 else
5684 n = 4;
5685 nr = 0;
5686 while (--n >= 0 && vim_isxdigit(p[1]))
5687 {
5688 ++p;
5689 nr = (nr << 4) + hex2nr(*p);
5690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_MBYTE
5693 /* For "\u" store the number according to
5694 * 'encoding'. */
5695 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702
5703 /* octal: "\1", "\12", "\123" */
5704 case '0':
5705 case '1':
5706 case '2':
5707 case '3':
5708 case '4':
5709 case '5':
5710 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '7': *name = *p++ - '0';
5712 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 *name = (*name << 3) + *p++ - '0';
5715 if (*p >= '0' && *p <= '7')
5716 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720
5721 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (extra != 0)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 /* FALLTHROUGH */
5729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 break;
5732 }
5733 }
5734 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 *arg = p + 1;
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return OK;
5742}
5743
5744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 int evaluate;
5753{
5754 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 int reduce = 0;
5757
5758 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 */
5761 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 break;
5767 ++reduce;
5768 ++p;
5769 }
5770 }
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 return FAIL;
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 if (!evaluate)
5780 {
5781 *arg = p + 1;
5782 return OK;
5783 }
5784
5785 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 */
5788 str = alloc((unsigned)((p - *arg) - reduce));
5789 if (str == NULL)
5790 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 rettv->v_type = VAR_STRING;
5792 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 break;
5800 ++p;
5801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 *arg = p + 1;
5806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * Allocate a variable for a List and fill it from "*arg".
5812 * Return OK or FAIL.
5813 */
5814 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int evaluate;
5819{
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 list_T *l = NULL;
5821 typval_T tv;
5822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
5824 if (evaluate)
5825 {
5826 l = list_alloc();
5827 if (l == NULL)
5828 return FAIL;
5829 }
5830
5831 *arg = skipwhite(*arg + 1);
5832 while (**arg != ']' && **arg != NUL)
5833 {
5834 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5835 goto failret;
5836 if (evaluate)
5837 {
5838 item = listitem_alloc();
5839 if (item != NULL)
5840 {
5841 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005842 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 list_append(l, item);
5844 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005845 else
5846 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 }
5848
5849 if (**arg == ']')
5850 break;
5851 if (**arg != ',')
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 goto failret;
5855 }
5856 *arg = skipwhite(*arg + 1);
5857 }
5858
5859 if (**arg != ']')
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862failret:
5863 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005864 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 return FAIL;
5866 }
5867
5868 *arg = skipwhite(*arg + 1);
5869 if (evaluate)
5870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005871 rettv->v_type = VAR_LIST;
5872 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 ++l->lv_refcount;
5874 }
5875
5876 return OK;
5877}
5878
5879/*
5880 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005881 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005883 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_alloc()
5885{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 list_T *l;
5887
5888 l = (list_T *)alloc_clear(sizeof(list_T));
5889 if (l != NULL)
5890 {
5891 /* Prepend the list to the list of lists for garbage collection. */
5892 if (first_list != NULL)
5893 first_list->lv_used_prev = l;
5894 l->lv_used_prev = NULL;
5895 l->lv_used_next = first_list;
5896 first_list = l;
5897 }
5898 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899}
5900
5901/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005902 * Allocate an empty list for a return value.
5903 * Returns OK or FAIL.
5904 */
5905 static int
5906rettv_list_alloc(rettv)
5907 typval_T *rettv;
5908{
5909 list_T *l = list_alloc();
5910
5911 if (l == NULL)
5912 return FAIL;
5913
5914 rettv->vval.v_list = l;
5915 rettv->v_type = VAR_LIST;
5916 ++l->lv_refcount;
5917 return OK;
5918}
5919
5920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 * Unreference a list: decrement the reference count and free it when it
5922 * becomes zero.
5923 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005924 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005928 if (l != NULL && --l->lv_refcount <= 0)
5929 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
5933 * Free a list, including all items it points to.
5934 * Ignores the reference count.
5935 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005936 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937list_free(l, recurse)
5938 list_T *l;
5939 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 /* Remove the list from the list of lists for garbage collection. */
5944 if (l->lv_used_prev == NULL)
5945 first_list = l->lv_used_next;
5946 else
5947 l->lv_used_prev->lv_used_next = l->lv_used_next;
5948 if (l->lv_used_next != NULL)
5949 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5950
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953 /* Remove the item before deleting it. */
5954 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005955 if (recurse || (item->li_tv.v_type != VAR_LIST
5956 && item->li_tv.v_type != VAR_DICT))
5957 clear_tv(&item->li_tv);
5958 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 }
5960 vim_free(l);
5961}
5962
5963/*
5964 * Allocate a list item.
5965 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967listitem_alloc()
5968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970}
5971
5972/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005973 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 vim_free(item);
5981}
5982
5983/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005984 * Remove a list item from a List and free it. Also clears the value.
5985 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005986 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005987listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
5989 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990{
5991 list_remove(l, item, item);
5992 listitem_free(item);
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Get the number of items in a list.
5997 */
5998 static long
5999list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 if (l == NULL)
6003 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006004 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 * Return TRUE when two lists have exactly the same values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l1;
6013 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006019 if (l1 == NULL || l2 == NULL)
6020 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006021 if (l1 == l2)
6022 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023 if (list_len(l1) != list_len(l2))
6024 return FALSE;
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026 for (item1 = l1->lv_first, item2 = l2->lv_first;
6027 item1 != NULL && item2 != NULL;
6028 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030 return FALSE;
6031 return item1 == NULL && item2 == NULL;
6032}
6033
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006034#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6035 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036/*
6037 * Return the dictitem that an entry in a hashtable points to.
6038 */
6039 dictitem_T *
6040dict_lookup(hi)
6041 hashitem_T *hi;
6042{
6043 return HI2DI(hi);
6044}
6045#endif
6046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 * Return TRUE when two dictionaries have exactly the same key/values.
6049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *d1;
6053 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 hashitem_T *hi;
6058 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006061 if (d1 == NULL || d2 == NULL)
6062 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (d1 == d2)
6064 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 if (dict_len(d1) != dict_len(d2))
6066 return FALSE;
6067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006068 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
6073 item2 = dict_find(d2, hi->hi_key, -1);
6074 if (item2 == NULL)
6075 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 return FALSE;
6078 --todo;
6079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 }
6081 return TRUE;
6082}
6083
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084static int tv_equal_recurse_limit;
6085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 */
6091 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T *tv1;
6094 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 int ic; /* ignore case */
6096 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097{
6098 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006101 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 * recursiveness to a limit. We guess they are equal then.
6108 * A fixed limit has the problem of still taking an awful long time.
6109 * Reduce the limit every time running into it. That should work fine for
6110 * deeply linked structures that are not recursively linked and catch
6111 * recursiveness quickly. */
6112 if (!recursive)
6113 tv_equal_recurse_limit = 1000;
6114 if (recursive_cnt >= tv_equal_recurse_limit)
6115 {
6116 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119
6120 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 ++recursive_cnt;
6124 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6125 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006126 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127
6128 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, 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_FUNC:
6135 return (tv1->vval.v_string != NULL
6136 && tv2->vval.v_string != NULL
6137 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6138
6139 case VAR_NUMBER:
6140 return tv1->vval.v_number == tv2->vval.v_number;
6141
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006142#ifdef FEAT_FLOAT
6143 case VAR_FLOAT:
6144 return tv1->vval.v_float == tv2->vval.v_float;
6145#endif
6146
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147 case VAR_STRING:
6148 s1 = get_tv_string_buf(tv1, buf1);
6149 s2 = get_tv_string_buf(tv2, buf2);
6150 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152
6153 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006154 return TRUE;
6155}
6156
6157/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 * Locate item with index "n" in list "l" and return it.
6159 * A negative index is counted from the end; -1 is the last item.
6160 * Returns NULL when "n" is out of range.
6161 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 long n;
6166{
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 long idx;
6169
6170 if (l == NULL)
6171 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172
6173 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006175 n = l->lv_len + n;
6176
6177 /* Check for index out of range. */
6178 if (n < 0 || n >= l->lv_len)
6179 return NULL;
6180
6181 /* When there is a cached index may start search from there. */
6182 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_idx / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else if (n > (l->lv_idx + l->lv_len) / 2)
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
6196 else
6197 {
6198 /* closest to the cached index */
6199 item = l->lv_idx_item;
6200 idx = l->lv_idx;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
6203 else
6204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 if (n < l->lv_len / 2)
6206 {
6207 /* closest to the start of the list */
6208 item = l->lv_first;
6209 idx = 0;
6210 }
6211 else
6212 {
6213 /* closest to the end of the list */
6214 item = l->lv_last;
6215 idx = l->lv_len - 1;
6216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 while (n > idx)
6220 {
6221 /* search forward */
6222 item = item->li_next;
6223 ++idx;
6224 }
6225 while (n < idx)
6226 {
6227 /* search backward */
6228 item = item->li_prev;
6229 --idx;
6230 }
6231
6232 /* cache the used index */
6233 l->lv_idx = idx;
6234 l->lv_idx_item = item;
6235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 return item;
6237}
6238
6239/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006240 * Get list item "l[idx]" as a number.
6241 */
6242 static long
6243list_find_nr(l, idx, errorp)
6244 list_T *l;
6245 long idx;
6246 int *errorp; /* set to TRUE when something wrong */
6247{
6248 listitem_T *li;
6249
6250 li = list_find(l, idx);
6251 if (li == NULL)
6252 {
6253 if (errorp != NULL)
6254 *errorp = TRUE;
6255 return -1L;
6256 }
6257 return get_tv_number_chk(&li->li_tv, errorp);
6258}
6259
6260/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006261 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6262 */
6263 char_u *
6264list_find_str(l, idx)
6265 list_T *l;
6266 long idx;
6267{
6268 listitem_T *li;
6269
6270 li = list_find(l, idx - 1);
6271 if (li == NULL)
6272 {
6273 EMSGN(_(e_listidx), idx);
6274 return NULL;
6275 }
6276 return get_tv_string(&li->li_tv);
6277}
6278
6279/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280 * Locate "item" list "l" and return its index.
6281 * Returns -1 when "item" is not in the list.
6282 */
6283 static long
6284list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287{
6288 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290
6291 if (l == NULL)
6292 return -1;
6293 idx = 0;
6294 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6295 ++idx;
6296 if (li == NULL)
6297 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006298 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006299}
6300
6301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 * Append item "item" to the end of list "l".
6303 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l;
6307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308{
6309 if (l->lv_last == NULL)
6310 {
6311 /* empty list */
6312 l->lv_first = item;
6313 l->lv_last = item;
6314 item->li_prev = NULL;
6315 }
6316 else
6317 {
6318 l->lv_last->li_next = item;
6319 item->li_prev = l->lv_last;
6320 l->lv_last = item;
6321 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006322 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 item->li_next = NULL;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006330 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *l;
6333 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 copy_tv(tv, &li->li_tv);
6340 list_append(l, li);
6341 return OK;
6342}
6343
6344/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006345 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 * Return FAIL when out of memory.
6347 */
6348 int
6349list_append_dict(list, dict)
6350 list_T *list;
6351 dict_T *dict;
6352{
6353 listitem_T *li = listitem_alloc();
6354
6355 if (li == NULL)
6356 return FAIL;
6357 li->li_tv.v_type = VAR_DICT;
6358 li->li_tv.v_lock = 0;
6359 li->li_tv.vval.v_dict = dict;
6360 list_append(list, li);
6361 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 return OK;
6363}
6364
6365/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006367 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 * Returns FAIL when out of memory.
6369 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 list_T *l;
6373 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006374 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375{
6376 listitem_T *li = listitem_alloc();
6377
6378 if (li == NULL)
6379 return FAIL;
6380 list_append(l, li);
6381 li->li_tv.v_type = VAR_STRING;
6382 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006383 if (str == NULL)
6384 li->li_tv.vval.v_string = NULL;
6385 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006386 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 return FAIL;
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006392 * Append "n" to list "l".
6393 * Returns FAIL when out of memory.
6394 */
6395 static int
6396list_append_number(l, n)
6397 list_T *l;
6398 varnumber_T n;
6399{
6400 listitem_T *li;
6401
6402 li = listitem_alloc();
6403 if (li == NULL)
6404 return FAIL;
6405 li->li_tv.v_type = VAR_NUMBER;
6406 li->li_tv.v_lock = 0;
6407 li->li_tv.vval.v_number = n;
6408 list_append(l, li);
6409 return OK;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * If "item" is NULL append at the end.
6415 * Return FAIL when out of memory.
6416 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006417 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l;
6420 typval_T *tv;
6421 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
6425 if (ni == NULL)
6426 return FAIL;
6427 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006428 list_insert(l, ni, item);
6429 return OK;
6430}
6431
6432 void
6433list_insert(l, ni, item)
6434 list_T *l;
6435 listitem_T *ni;
6436 listitem_T *item;
6437{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 if (item == NULL)
6439 /* Append new item at end of list. */
6440 list_append(l, ni);
6441 else
6442 {
6443 /* Insert new item before existing item. */
6444 ni->li_prev = item->li_prev;
6445 ni->li_next = item;
6446 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 ++l->lv_idx;
6450 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006452 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 l->lv_idx_item = NULL;
6455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459}
6460
6461/*
6462 * Extend "l1" with "l2".
6463 * If "bef" is NULL append at the end, otherwise insert before this item.
6464 * Returns FAIL when out of memory.
6465 */
6466 static int
6467list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006473 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006475 /* We also quit the loop when we have inserted the original item count of
6476 * the list, avoid a hang when we extend a list with itself. */
6477 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6479 return FAIL;
6480 return OK;
6481}
6482
6483/*
6484 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6485 * Return FAIL when out of memory.
6486 */
6487 static int
6488list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *l1;
6490 list_T *l2;
6491 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006495 if (l1 == NULL || l2 == NULL)
6496 return FAIL;
6497
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 if (l == NULL)
6501 return FAIL;
6502 tv->v_type = VAR_LIST;
6503 tv->vval.v_list = l;
6504
6505 /* append all items from the second list */
6506 return list_extend(l, l2, NULL);
6507}
6508
6509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 * Returns NULL when out of memory.
6514 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 list_T *copy;
6522 listitem_T *item;
6523 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
6525 if (orig == NULL)
6526 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527
6528 copy = list_alloc();
6529 if (copy != NULL)
6530 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (copyID != 0)
6532 {
6533 /* Do this before adding the items, because one of the items may
6534 * refer back to this list. */
6535 orig->lv_copyID = copyID;
6536 orig->lv_copylist = copy;
6537 }
6538 for (item = orig->lv_first; item != NULL && !got_int;
6539 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 {
6541 ni = listitem_alloc();
6542 if (ni == NULL)
6543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 {
6546 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6547 {
6548 vim_free(ni);
6549 break;
6550 }
6551 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 list_append(copy, ni);
6555 }
6556 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 if (item != NULL)
6558 {
6559 list_unref(copy);
6560 copy = NULL;
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
6563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 return copy;
6565}
6566
6567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006569 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006572list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l;
6574 listitem_T *item;
6575 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 /* notify watchers */
6580 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 list_fix_watch(l, ip);
6584 if (ip == item2)
6585 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587
6588 if (item2->li_next == NULL)
6589 l->lv_last = item->li_prev;
6590 else
6591 item2->li_next->li_prev = item->li_prev;
6592 if (item->li_prev == NULL)
6593 l->lv_first = item2->li_next;
6594 else
6595 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006596 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597}
6598
6599/*
6600 * Return an allocated string with the string representation of a list.
6601 * May return NULL.
6602 */
6603 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006604list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607{
6608 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609
6610 if (tv->vval.v_list == NULL)
6611 return NULL;
6612 ga_init2(&ga, (int)sizeof(char), 80);
6613 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 {
6616 vim_free(ga.ga_data);
6617 return NULL;
6618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 ga_append(&ga, ']');
6620 ga_append(&ga, NUL);
6621 return (char_u *)ga.ga_data;
6622}
6623
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006624typedef struct join_S {
6625 char_u *s;
6626 char_u *tofree;
6627} join_T;
6628
6629 static int
6630list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6631 garray_T *gap; /* to store the result in */
6632 list_T *l;
6633 char_u *sep;
6634 int echo_style;
6635 int copyID;
6636 garray_T *join_gap; /* to keep each list item string */
6637{
6638 int i;
6639 join_T *p;
6640 int len;
6641 int sumlen = 0;
6642 int first = TRUE;
6643 char_u *tofree;
6644 char_u numbuf[NUMBUFLEN];
6645 listitem_T *item;
6646 char_u *s;
6647
6648 /* Stringify each item in the list. */
6649 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6650 {
6651 if (echo_style)
6652 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6653 else
6654 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6655 if (s == NULL)
6656 return FAIL;
6657
6658 len = (int)STRLEN(s);
6659 sumlen += len;
6660
6661 ga_grow(join_gap, 1);
6662 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6663 if (tofree != NULL || s != numbuf)
6664 {
6665 p->s = s;
6666 p->tofree = tofree;
6667 }
6668 else
6669 {
6670 p->s = vim_strnsave(s, len);
6671 p->tofree = p->s;
6672 }
6673
6674 line_breakcheck();
6675 }
6676
6677 /* Allocate result buffer with its total size, avoid re-allocation and
6678 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6679 if (join_gap->ga_len >= 2)
6680 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6681 if (ga_grow(gap, sumlen + 2) == FAIL)
6682 return FAIL;
6683
6684 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6685 {
6686 if (first)
6687 first = FALSE;
6688 else
6689 ga_concat(gap, sep);
6690 p = ((join_T *)join_gap->ga_data) + i;
6691
6692 if (p->s != NULL)
6693 ga_concat(gap, p->s);
6694 line_breakcheck();
6695 }
6696
6697 return OK;
6698}
6699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006702 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006706list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006710 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006713 garray_T join_ga;
6714 int retval;
6715 join_T *p;
6716 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6719 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6720
6721 /* Dispose each item in join_ga. */
6722 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = (join_T *)join_ga.ga_data;
6725 for (i = 0; i < join_ga.ga_len; ++i)
6726 {
6727 vim_free(p->tofree);
6728 ++p;
6729 }
6730 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732
6733 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734}
6735
6736/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 * Garbage collection for lists and dictionaries.
6738 *
6739 * We use reference counts to be able to free most items right away when they
6740 * are no longer used. But for composite items it's possible that it becomes
6741 * unused while the reference count is > 0: When there is a recursive
6742 * reference. Example:
6743 * :let l = [1, 2, 3]
6744 * :let d = {9: l}
6745 * :let l[1] = d
6746 *
6747 * Since this is quite unusual we handle this with garbage collection: every
6748 * once in a while find out which lists and dicts are not referenced from any
6749 * variable.
6750 *
6751 * Here is a good reference text about garbage collection (refers to Python
6752 * but it applies to all reference-counting mechanisms):
6753 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 * Do garbage collection for lists and dicts.
6758 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 int
6761garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 buf_T *buf;
6765 win_T *wp;
6766 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006767 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 int did_free;
6769 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006770#ifdef FEAT_WINDOWS
6771 tabpage_T *tp;
6772#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006774 /* Only do this once. */
6775 want_garbage_collect = FALSE;
6776 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006777 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006778
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006779 /* We advance by two because we add one for items referenced through
6780 * previous_funccal. */
6781 current_copyID += COPYID_INC;
6782 copyID = current_copyID;
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /*
6785 * 1. Go through all accessible variables and mark all lists and dicts
6786 * with copyID.
6787 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788
6789 /* Don't free variables in the previous_funccal list unless they are only
6790 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006791 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006792 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6796 }
6797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 /* script-local variables */
6799 for (i = 1; i <= ga_scripts.ga_len; ++i)
6800 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6801
6802 /* buffer-local variables */
6803 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006804 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805
6806 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006807 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006808 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006809#ifdef FEAT_AUTOCMD
6810 if (aucmd_win != NULL)
6811 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814#ifdef FEAT_WINDOWS
6815 /* tabpage-local variables */
6816 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006817 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006818#endif
6819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 /* global variables */
6821 set_ref_in_ht(&globvarht, copyID);
6822
6823 /* function-local variables */
6824 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6825 {
6826 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6827 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6828 }
6829
Bram Moolenaard812df62008-11-09 12:46:09 +00006830 /* v: vars */
6831 set_ref_in_ht(&vimvarht, copyID);
6832
Bram Moolenaar1dced572012-04-05 16:54:08 +02006833#ifdef FEAT_LUA
6834 set_ref_in_lua(copyID);
6835#endif
6836
Bram Moolenaardb913952012-06-29 12:54:53 +02006837#ifdef FEAT_PYTHON
6838 set_ref_in_python(copyID);
6839#endif
6840
6841#ifdef FEAT_PYTHON3
6842 set_ref_in_python3(copyID);
6843#endif
6844
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006845 /*
6846 * 2. Free lists and dictionaries that are not referenced.
6847 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 did_free = free_unref_items(copyID);
6849
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006850 /*
6851 * 3. Check if any funccal can be freed now.
6852 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 for (pfc = &previous_funccal; *pfc != NULL; )
6854 {
6855 if (can_free_funccal(*pfc, copyID))
6856 {
6857 fc = *pfc;
6858 *pfc = fc->caller;
6859 free_funccal(fc, TRUE);
6860 did_free = TRUE;
6861 did_free_funccal = TRUE;
6862 }
6863 else
6864 pfc = &(*pfc)->caller;
6865 }
6866 if (did_free_funccal)
6867 /* When a funccal was freed some more items might be garbage
6868 * collected, so run again. */
6869 (void)garbage_collect();
6870
6871 return did_free;
6872}
6873
6874/*
6875 * Free lists and dictionaries that are no longer referenced.
6876 */
6877 static int
6878free_unref_items(copyID)
6879 int copyID;
6880{
6881 dict_T *dd;
6882 list_T *ll;
6883 int did_free = FALSE;
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 */
6888 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the Dictionary and ordinary items it contains, but don't
6892 * recurse into Lists and Dictionaries, they will be in the list
6893 * of dicts or list of lists. */
6894 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
6897 /* restart, next dict may also have been freed */
6898 dd = first_dict;
6899 }
6900 else
6901 dd = dd->dv_used_next;
6902
6903 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006904 * Go through the list of lists and free items without the copyID.
6905 * But don't free a list that has a watcher (used in a for loop), these
6906 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 */
6908 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6910 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the List and ordinary items it contains, but don't recurse
6913 * into Lists and Dictionaries, they will be in the list of dicts
6914 * or list of lists. */
6915 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
6917
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006918 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 ll = first_list;
6920 }
6921 else
6922 ll = ll->lv_used_next;
6923
6924 return did_free;
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_ht(ht, copyID)
6932 hashtab_T *ht;
6933 int copyID;
6934{
6935 int todo;
6936 hashitem_T *hi;
6937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006938 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 for (hi = ht->ht_array; todo > 0; ++hi)
6940 if (!HASHITEM_EMPTY(hi))
6941 {
6942 --todo;
6943 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6944 }
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through list "l" with "copyID".
6949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006950 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951set_ref_in_list(l, copyID)
6952 list_T *l;
6953 int copyID;
6954{
6955 listitem_T *li;
6956
6957 for (li = l->lv_first; li != NULL; li = li->li_next)
6958 set_ref_in_item(&li->li_tv, copyID);
6959}
6960
6961/*
6962 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6963 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006964 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965set_ref_in_item(tv, copyID)
6966 typval_T *tv;
6967 int copyID;
6968{
6969 dict_T *dd;
6970 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971
6972 switch (tv->v_type)
6973 {
6974 case VAR_DICT:
6975 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006976 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /* Didn't see this dict yet. */
6979 dd->dv_copyID = copyID;
6980 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 case VAR_LIST:
6985 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this list yet. */
6989 ll->lv_copyID = copyID;
6990 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995}
6996
6997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998 * Allocate an empty header for a dictionary.
6999 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007000 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001dict_alloc()
7002{
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007007 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007008 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 if (first_dict != NULL)
7010 first_dict->dv_used_prev = d;
7011 d->dv_used_next = first_dict;
7012 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007013 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007016 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007017 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007018 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007020 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007021 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007025 * Allocate an empty dict for a return value.
7026 * Returns OK or FAIL.
7027 */
7028 static int
7029rettv_dict_alloc(rettv)
7030 typval_T *rettv;
7031{
7032 dict_T *d = dict_alloc();
7033
7034 if (d == NULL)
7035 return FAIL;
7036
7037 rettv->vval.v_dict = d;
7038 rettv->v_type = VAR_DICT;
7039 ++d->dv_refcount;
7040 return OK;
7041}
7042
7043
7044/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 * Unreference a Dictionary: decrement the reference count and free it when it
7046 * becomes zero.
7047 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007048 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007052 if (d != NULL && --d->dv_refcount <= 0)
7053 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054}
7055
7056/*
7057 * Free a Dictionary, including all items it contains.
7058 * Ignores the reference count.
7059 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007060 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007061dict_free(d, recurse)
7062 dict_T *d;
7063 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007067 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 /* Remove the dict from the list of dicts for garbage collection. */
7070 if (d->dv_used_prev == NULL)
7071 first_dict = d->dv_used_next;
7072 else
7073 d->dv_used_prev->dv_used_next = d->dv_used_next;
7074 if (d->dv_used_next != NULL)
7075 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7076
7077 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007078 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 if (!HASHITEM_EMPTY(hi))
7083 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007084 /* Remove the item before deleting it, just in case there is
7085 * something recursive causing trouble. */
7086 di = HI2DI(hi);
7087 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007088 if (recurse || (di->di_tv.v_type != VAR_LIST
7089 && di->di_tv.v_type != VAR_DICT))
7090 clear_tv(&di->di_tv);
7091 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 --todo;
7093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 vim_free(d);
7097}
7098
7099/*
7100 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 * The "key" is copied to the new item.
7102 * Note that the value of the item "di_tv" still needs to be initialized!
7103 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007105 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106dictitem_alloc(key)
7107 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007115 di->di_flags = 0;
7116 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118}
7119
7120/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 * Make a copy of a Dictionary item.
7122 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007124dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7130 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 if (di != NULL)
7132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 copy_tv(&org->di_tv, &di->di_tv);
7136 }
7137 return di;
7138}
7139
7140/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 * Remove item "item" from Dictionary "dict" and free it.
7142 */
7143 static void
7144dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *dict;
7146 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 if (HASHITEM_EMPTY(hi))
7152 EMSG2(_(e_intern2), "dictitem_remove()");
7153 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 dictitem_free(item);
7156}
7157
7158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159 * Free a dict item. Also clears the value.
7160 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007161 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 clear_tv(&item->di_tv);
7166 vim_free(item);
7167}
7168
7169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7171 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 * Returns NULL when out of memory.
7174 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *copy;
7182 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185
7186 if (orig == NULL)
7187 return NULL;
7188
7189 copy = dict_alloc();
7190 if (copy != NULL)
7191 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 if (copyID != 0)
7193 {
7194 orig->dv_copyID = copyID;
7195 orig->dv_copydict = copy;
7196 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 --todo;
7203
7204 di = dictitem_alloc(hi->hi_key);
7205 if (di == NULL)
7206 break;
7207 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 {
7209 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7210 copyID) == FAIL)
7211 {
7212 vim_free(di);
7213 break;
7214 }
7215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 else
7217 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7218 if (dict_add(copy, di) == FAIL)
7219 {
7220 dictitem_free(di);
7221 break;
7222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 if (todo > 0)
7228 {
7229 dict_unref(copy);
7230 copy = NULL;
7231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 }
7233
7234 return copy;
7235}
7236
7237/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007239 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007241 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 dict_T *d;
7244 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247}
7248
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007250 * Add a number or string entry to dictionary "d".
7251 * When "str" is NULL use number "nr", otherwise use "str".
7252 * Returns FAIL when out of memory and when key already exists.
7253 */
7254 int
7255dict_add_nr_str(d, key, nr, str)
7256 dict_T *d;
7257 char *key;
7258 long nr;
7259 char_u *str;
7260{
7261 dictitem_T *item;
7262
7263 item = dictitem_alloc((char_u *)key);
7264 if (item == NULL)
7265 return FAIL;
7266 item->di_tv.v_lock = 0;
7267 if (str == NULL)
7268 {
7269 item->di_tv.v_type = VAR_NUMBER;
7270 item->di_tv.vval.v_number = nr;
7271 }
7272 else
7273 {
7274 item->di_tv.v_type = VAR_STRING;
7275 item->di_tv.vval.v_string = vim_strsave(str);
7276 }
7277 if (dict_add(d, item) == FAIL)
7278 {
7279 dictitem_free(item);
7280 return FAIL;
7281 }
7282 return OK;
7283}
7284
7285/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007286 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007287 * Returns FAIL when out of memory and when key already exists.
7288 */
7289 int
7290dict_add_list(d, key, list)
7291 dict_T *d;
7292 char *key;
7293 list_T *list;
7294{
7295 dictitem_T *item;
7296
7297 item = dictitem_alloc((char_u *)key);
7298 if (item == NULL)
7299 return FAIL;
7300 item->di_tv.v_lock = 0;
7301 item->di_tv.v_type = VAR_LIST;
7302 item->di_tv.vval.v_list = list;
7303 if (dict_add(d, item) == FAIL)
7304 {
7305 dictitem_free(item);
7306 return FAIL;
7307 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007308 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007309 return OK;
7310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Get the number of items in a Dictionary.
7314 */
7315 static long
7316dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 if (d == NULL)
7320 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322}
7323
7324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 * Find item "key[len]" in Dictionary "d".
7326 * If "len" is negative use strlen(key).
7327 * Returns NULL when not found.
7328 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007329 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 char_u *key;
7333 int len;
7334{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335#define AKEYLEN 200
7336 char_u buf[AKEYLEN];
7337 char_u *akey;
7338 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 if (len < 0)
7342 akey = key;
7343 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 tofree = akey = vim_strnsave(key, len);
7346 if (akey == NULL)
7347 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007348 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 else
7350 {
7351 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007352 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 akey = buf;
7354 }
7355
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 vim_free(tofree);
7358 if (HASHITEM_EMPTY(hi))
7359 return NULL;
7360 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361}
7362
7363/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007364 * Get a string item from a dictionary.
7365 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366 * Returns NULL if the entry doesn't exist or out of memory.
7367 */
7368 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007369get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007370 dict_T *d;
7371 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007372 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007373{
7374 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007375 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376
7377 di = dict_find(d, key, -1);
7378 if (di == NULL)
7379 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007380 s = get_tv_string(&di->di_tv);
7381 if (save && s != NULL)
7382 s = vim_strsave(s);
7383 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007384}
7385
7386/*
7387 * Get a number item from a dictionary.
7388 * Returns 0 if the entry doesn't exist or out of memory.
7389 */
7390 long
7391get_dict_number(d, key)
7392 dict_T *d;
7393 char_u *key;
7394{
7395 dictitem_T *di;
7396
7397 di = dict_find(d, key, -1);
7398 if (di == NULL)
7399 return 0;
7400 return get_tv_number(&di->di_tv);
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Return an allocated string with the string representation of a Dictionary.
7405 * May return NULL.
7406 */
7407 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007408dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411{
7412 garray_T ga;
7413 int first = TRUE;
7414 char_u *tofree;
7415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007419 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 return NULL;
7423 ga_init2(&ga, (int)sizeof(char), 80);
7424 ga_append(&ga, '{');
7425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007426 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 --todo;
7432
7433 if (first)
7434 first = FALSE;
7435 else
7436 ga_concat(&ga, (char_u *)", ");
7437
7438 tofree = string_quote(hi->hi_key, FALSE);
7439 if (tofree != NULL)
7440 {
7441 ga_concat(&ga, tofree);
7442 vim_free(tofree);
7443 }
7444 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007445 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 if (s != NULL)
7447 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007449 if (s == NULL)
7450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 if (todo > 0)
7454 {
7455 vim_free(ga.ga_data);
7456 return NULL;
7457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458
7459 ga_append(&ga, '}');
7460 ga_append(&ga, NUL);
7461 return (char_u *)ga.ga_data;
7462}
7463
7464/*
7465 * Allocate a variable for a Dictionary and fill it from "*arg".
7466 * Return OK or FAIL. Returns NOTDONE for {expr}.
7467 */
7468 static int
7469get_dict_tv(arg, rettv, evaluate)
7470 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 int evaluate;
7473{
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 dict_T *d = NULL;
7475 typval_T tvkey;
7476 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007477 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 /*
7483 * First check if it's not a curly-braces thing: {expr}.
7484 * Must do this without evaluating, otherwise a function may be called
7485 * twice. Unfortunately this means we need to call eval1() twice for the
7486 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 if (*start != '}')
7490 {
7491 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7492 return FAIL;
7493 if (*start == '}')
7494 return NOTDONE;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496
7497 if (evaluate)
7498 {
7499 d = dict_alloc();
7500 if (d == NULL)
7501 return FAIL;
7502 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 tvkey.v_type = VAR_UNKNOWN;
7504 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
7506 *arg = skipwhite(*arg + 1);
7507 while (**arg != '}' && **arg != NUL)
7508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 goto failret;
7511 if (**arg != ':')
7512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007513 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007517 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007519 key = get_tv_string_buf_chk(&tvkey, buf);
7520 if (key == NULL || *key == NUL)
7521 {
7522 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7523 if (key != NULL)
7524 EMSG(_(e_emptykey));
7525 clear_tv(&tvkey);
7526 goto failret;
7527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529
7530 *arg = skipwhite(*arg + 1);
7531 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7532 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007533 if (evaluate)
7534 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 if (evaluate)
7538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 if (item != NULL)
7541 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007542 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 clear_tv(&tv);
7545 goto failret;
7546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 item = dictitem_alloc(key);
7548 clear_tv(&tvkey);
7549 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 if (dict_add(d, item) == FAIL)
7554 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 }
7556 }
7557
7558 if (**arg == '}')
7559 break;
7560 if (**arg != ',')
7561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007562 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 goto failret;
7564 }
7565 *arg = skipwhite(*arg + 1);
7566 }
7567
7568 if (**arg != '}')
7569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007570 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571failret:
7572 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007573 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 return FAIL;
7575 }
7576
7577 *arg = skipwhite(*arg + 1);
7578 if (evaluate)
7579 {
7580 rettv->v_type = VAR_DICT;
7581 rettv->vval.v_dict = d;
7582 ++d->dv_refcount;
7583 }
7584
7585 return OK;
7586}
7587
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 * Return a string with the string representation of a variable.
7590 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007591 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007594 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 */
7596 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007600 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007603 static int recurse = 0;
7604 char_u *r = NULL;
7605
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 *tofree = NULL;
7610 return NULL;
7611 }
7612 ++recurse;
7613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 switch (tv->v_type)
7615 {
7616 case VAR_FUNC:
7617 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 r = tv->vval.v_string;
7619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 if (tv->vval.v_list == NULL)
7623 {
7624 *tofree = NULL;
7625 r = NULL;
7626 }
7627 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7628 {
7629 *tofree = NULL;
7630 r = (char_u *)"[...]";
7631 }
7632 else
7633 {
7634 tv->vval.v_list->lv_copyID = copyID;
7635 *tofree = list2string(tv, copyID);
7636 r = *tofree;
7637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007639
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007641 if (tv->vval.v_dict == NULL)
7642 {
7643 *tofree = NULL;
7644 r = NULL;
7645 }
7646 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7647 {
7648 *tofree = NULL;
7649 r = (char_u *)"{...}";
7650 }
7651 else
7652 {
7653 tv->vval.v_dict->dv_copyID = copyID;
7654 *tofree = dict2string(tv, copyID);
7655 r = *tofree;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 case VAR_STRING:
7660 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661 *tofree = NULL;
7662 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007665#ifdef FEAT_FLOAT
7666 case VAR_FLOAT:
7667 *tofree = NULL;
7668 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7669 r = numbuf;
7670 break;
7671#endif
7672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677
7678 --recurse;
7679 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680}
7681
7682/*
7683 * Return a string with the string representation of a variable.
7684 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7685 * "numbuf" is used for a number.
7686 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007687 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 */
7689 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007690tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 char_u **tofree;
7693 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695{
7696 switch (tv->v_type)
7697 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 case VAR_FUNC:
7699 *tofree = string_quote(tv->vval.v_string, TRUE);
7700 return *tofree;
7701 case VAR_STRING:
7702 *tofree = string_quote(tv->vval.v_string, FALSE);
7703 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#ifdef FEAT_FLOAT
7705 case VAR_FLOAT:
7706 *tofree = NULL;
7707 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7708 return numbuf;
7709#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007714 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007717 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718}
7719
7720/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 * Return string "str" in ' quotes, doubling ' characters.
7722 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 */
7725 static char_u *
7726string_quote(str, function)
7727 char_u *str;
7728 int function;
7729{
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 char_u *p, *r, *s;
7732
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 len = (function ? 13 : 3);
7734 if (str != NULL)
7735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007736 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 for (p = str; *p != NUL; mb_ptr_adv(p))
7738 if (*p == '\'')
7739 ++len;
7740 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 s = r = alloc(len);
7742 if (r != NULL)
7743 {
7744 if (function)
7745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 r += 10;
7748 }
7749 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 if (str != NULL)
7752 for (p = str; *p != NUL; )
7753 {
7754 if (*p == '\'')
7755 *r++ = '\'';
7756 MB_COPY_CHAR(p, r);
7757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 if (function)
7760 *r++ = ')';
7761 *r++ = NUL;
7762 }
7763 return s;
7764}
7765
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007766#ifdef FEAT_FLOAT
7767/*
7768 * Convert the string "text" to a floating point number.
7769 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7770 * this always uses a decimal point.
7771 * Returns the length of the text that was consumed.
7772 */
7773 static int
7774string2float(text, value)
7775 char_u *text;
7776 float_T *value; /* result stored here */
7777{
7778 char *s = (char *)text;
7779 float_T f;
7780
7781 f = strtod(s, &s);
7782 *value = f;
7783 return (int)((char_u *)s - text);
7784}
7785#endif
7786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * Get the value of an environment variable.
7789 * "arg" is pointing to the '$'. It is advanced to after the name.
7790 * If the environment variable was not set, silently assume it is empty.
7791 * Always return OK.
7792 */
7793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 int evaluate;
7798{
7799 char_u *string = NULL;
7800 int len;
7801 int cc;
7802 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 ++*arg;
7806 name = *arg;
7807 len = get_env_len(arg);
7808 if (evaluate)
7809 {
7810 if (len != 0)
7811 {
7812 cc = name[len];
7813 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007814 /* first try vim_getenv(), fast for normal environment vars */
7815 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007817 {
7818 if (!mustfree)
7819 string = vim_strsave(string);
7820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
7822 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823 if (mustfree)
7824 vim_free(string);
7825
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 /* next try expanding things like $VIM and ${HOME} */
7827 string = expand_env_save(name - 1);
7828 if (string != NULL && *string == '$')
7829 {
7830 vim_free(string);
7831 string = NULL;
7832 }
7833 }
7834 name[len] = cc;
7835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 rettv->v_type = VAR_STRING;
7837 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839
7840 return OK;
7841}
7842
7843/*
7844 * Array with names and number of arguments of all internal functions
7845 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7846 */
7847static struct fst
7848{
7849 char *f_name; /* function name */
7850 char f_min_argc; /* minimal number of arguments */
7851 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007853 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854} functions[] =
7855{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007858 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007860 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007861 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"append", 2, 2, f_append},
7863 {"argc", 0, 0, f_argc},
7864 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007865 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007869 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"bufexists", 1, 1, f_bufexists},
7874 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7875 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7876 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7877 {"buflisted", 1, 1, f_buflisted},
7878 {"bufloaded", 1, 1, f_bufloaded},
7879 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007880 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"bufwinnr", 1, 1, f_bufwinnr},
7882 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007883 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007884 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"ceil", 1, 1, f_ceil},
7888#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007889 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007890 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007892 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007894#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007895 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007896 {"complete_add", 1, 1, f_complete_add},
7897 {"complete_check", 0, 0, f_complete_check},
7898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007907 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007908 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"delete", 1, 1, f_delete},
7910 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007911 {"diff_filler", 1, 1, f_diff_filler},
7912 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007913 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"eventhandler", 0, 0, f_eventhandler},
7917 {"executable", 1, 1, f_executable},
7918 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919#ifdef FEAT_FLOAT
7920 {"exp", 1, 1, f_exp},
7921#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007922 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007923 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007924 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7926 {"filereadable", 1, 1, f_filereadable},
7927 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007929 {"finddir", 1, 3, f_finddir},
7930 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#ifdef FEAT_FLOAT
7932 {"float2nr", 1, 1, f_float2nr},
7933 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007934 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007936 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"fnamemodify", 2, 2, f_fnamemodify},
7938 {"foldclosed", 1, 1, f_foldclosed},
7939 {"foldclosedend", 1, 1, f_foldclosedend},
7940 {"foldlevel", 1, 1, f_foldlevel},
7941 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007942 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007945 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007946 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007947 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007948 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getchar", 0, 1, f_getchar},
7950 {"getcharmod", 0, 0, f_getcharmod},
7951 {"getcmdline", 0, 0, f_getcmdline},
7952 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007953 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007955 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007956 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"getfsize", 1, 1, f_getfsize},
7958 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007959 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007961 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007962 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007963 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007964 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007965 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007966 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007968 {"gettabvar", 2, 3, f_gettabvar},
7969 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"getwinposx", 0, 0, f_getwinposx},
7971 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007972 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007973 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007974 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007977 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007978 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7980 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7981 {"histadd", 2, 2, f_histadd},
7982 {"histdel", 1, 2, f_histdel},
7983 {"histget", 1, 2, f_histget},
7984 {"histnr", 1, 1, f_histnr},
7985 {"hlID", 1, 1, f_hlID},
7986 {"hlexists", 1, 1, f_hlexists},
7987 {"hostname", 0, 0, f_hostname},
7988 {"iconv", 3, 3, f_iconv},
7989 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007991 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007993 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"inputrestore", 0, 0, f_inputrestore},
7995 {"inputsave", 0, 0, f_inputsave},
7996 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007998 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008000 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008003 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"libcall", 3, 3, f_libcall},
8007 {"libcallnr", 3, 3, f_libcallnr},
8008 {"line", 1, 1, f_line},
8009 {"line2byte", 1, 1, f_line2byte},
8010 {"lispindent", 1, 1, f_lispindent},
8011 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008013 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014 {"log10", 1, 1, f_log10},
8015#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008016#ifdef FEAT_LUA
8017 {"luaeval", 1, 2, f_luaeval},
8018#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008019 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008020 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008021 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008022 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008023 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008024 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008025 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008026 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008027 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008028 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008029 {"max", 1, 1, f_max},
8030 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008031#ifdef vim_mkdir
8032 {"mkdir", 1, 3, f_mkdir},
8033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008035#ifdef FEAT_MZSCHEME
8036 {"mzeval", 1, 1, f_mzeval},
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008039 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008040 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008041 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"pow", 2, 2, f_pow},
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008046 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008048#ifdef FEAT_PYTHON3
8049 {"py3eval", 1, 1, f_py3eval},
8050#endif
8051#ifdef FEAT_PYTHON
8052 {"pyeval", 1, 1, f_pyeval},
8053#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008055 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008056 {"reltime", 0, 2, f_reltime},
8057 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"remote_expr", 2, 3, f_remote_expr},
8059 {"remote_foreground", 1, 1, f_remote_foreground},
8060 {"remote_peek", 1, 2, f_remote_peek},
8061 {"remote_read", 1, 1, f_remote_read},
8062 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008065 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008067 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
8069 {"round", 1, 1, f_round},
8070#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008071 {"screenattr", 2, 2, f_screenattr},
8072 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008073 {"screencol", 0, 0, f_screencol},
8074 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008075 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008076 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008077 {"searchpair", 3, 7, f_searchpair},
8078 {"searchpairpos", 3, 7, f_searchpairpos},
8079 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"server2client", 2, 2, f_server2client},
8081 {"serverlist", 0, 0, f_serverlist},
8082 {"setbufvar", 3, 3, f_setbufvar},
8083 {"setcmdpos", 1, 1, f_setcmdpos},
8084 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008085 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008086 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008087 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008088 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008090 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008091 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008093#ifdef FEAT_CRYPT
8094 {"sha256", 1, 1, f_sha256},
8095#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008096 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008097 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008103 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008104 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008105 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008106 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008107 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"sqrt", 1, 1, f_sqrt},
8110 {"str2float", 1, 1, f_str2float},
8111#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008112 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008113 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008114 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#ifdef HAVE_STRFTIME
8116 {"strftime", 1, 2, f_strftime},
8117#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"strlen", 1, 1, f_strlen},
8121 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008122 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"submatch", 1, 1, f_submatch},
8126 {"substitute", 4, 4, f_substitute},
8127 {"synID", 3, 3, f_synID},
8128 {"synIDattr", 2, 3, f_synIDattr},
8129 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008130 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008131 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008132 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008133 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008134 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008135 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008136 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008137 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138#ifdef FEAT_FLOAT
8139 {"tan", 1, 1, f_tan},
8140 {"tanh", 1, 1, f_tanh},
8141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008143 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"tolower", 1, 1, f_tolower},
8145 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008146 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008147#ifdef FEAT_FLOAT
8148 {"trunc", 1, 1, f_trunc},
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008151 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008152 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008153 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"virtcol", 1, 1, f_virtcol},
8155 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008156 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"winbufnr", 1, 1, f_winbufnr},
8158 {"wincol", 0, 0, f_wincol},
8159 {"winheight", 1, 1, f_winheight},
8160 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008163 {"winrestview", 1, 1, f_winrestview},
8164 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008166 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008167 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168};
8169
8170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8171
8172/*
8173 * Function given to ExpandGeneric() to obtain the list of internal
8174 * or user defined function names.
8175 */
8176 char_u *
8177get_function_name(xp, idx)
8178 expand_T *xp;
8179 int idx;
8180{
8181 static int intidx = -1;
8182 char_u *name;
8183
8184 if (idx == 0)
8185 intidx = -1;
8186 if (intidx < 0)
8187 {
8188 name = get_user_func_name(xp, idx);
8189 if (name != NULL)
8190 return name;
8191 }
8192 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8193 {
8194 STRCPY(IObuff, functions[intidx].f_name);
8195 STRCAT(IObuff, "(");
8196 if (functions[intidx].f_max_argc == 0)
8197 STRCAT(IObuff, ")");
8198 return IObuff;
8199 }
8200
8201 return NULL;
8202}
8203
8204/*
8205 * Function given to ExpandGeneric() to obtain the list of internal or
8206 * user defined variable or function names.
8207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 char_u *
8209get_expr_name(xp, idx)
8210 expand_T *xp;
8211 int idx;
8212{
8213 static int intidx = -1;
8214 char_u *name;
8215
8216 if (idx == 0)
8217 intidx = -1;
8218 if (intidx < 0)
8219 {
8220 name = get_function_name(xp, idx);
8221 if (name != NULL)
8222 return name;
8223 }
8224 return get_user_var_name(xp, ++intidx);
8225}
8226
8227#endif /* FEAT_CMDL_COMPL */
8228
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008229#if defined(EBCDIC) || defined(PROTO)
8230/*
8231 * Compare struct fst by function name.
8232 */
8233 static int
8234compare_func_name(s1, s2)
8235 const void *s1;
8236 const void *s2;
8237{
8238 struct fst *p1 = (struct fst *)s1;
8239 struct fst *p2 = (struct fst *)s2;
8240
8241 return STRCMP(p1->f_name, p2->f_name);
8242}
8243
8244/*
8245 * Sort the function table by function name.
8246 * The sorting of the table above is ASCII dependant.
8247 * On machines using EBCDIC we have to sort it.
8248 */
8249 static void
8250sortFunctions()
8251{
8252 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8253
8254 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8255}
8256#endif
8257
8258
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259/*
8260 * Find internal function in table above.
8261 * Return index, or -1 if not found
8262 */
8263 static int
8264find_internal_func(name)
8265 char_u *name; /* name of the function */
8266{
8267 int first = 0;
8268 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269 int cmp;
8270 int x;
8271
8272 /*
8273 * Find the function name in the table. Binary search.
8274 */
8275 while (first <= last)
8276 {
8277 x = first + ((unsigned)(last - first) >> 1);
8278 cmp = STRCMP(name, functions[x].f_name);
8279 if (cmp < 0)
8280 last = x - 1;
8281 else if (cmp > 0)
8282 first = x + 1;
8283 else
8284 return x;
8285 }
8286 return -1;
8287}
8288
8289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8291 * name it contains, otherwise return "name".
8292 */
8293 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008294deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008295 char_u *name;
8296 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008297 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008298{
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300 int cc;
8301
8302 cc = name[*lenp];
8303 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008304 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008307 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008308 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008309 {
8310 *lenp = 0;
8311 return (char_u *)""; /* just in case */
8312 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008313 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008314 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008315 }
8316
8317 return name;
8318}
8319
8320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 * Allocate a variable for the result of a function.
8322 * Return OK or FAIL.
8323 */
8324 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008325get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8326 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 char_u *name; /* name of the function */
8328 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 char_u **arg; /* argument, pointing to the '(' */
8331 linenr_T firstline; /* first line of range */
8332 linenr_T lastline; /* last line of range */
8333 int *doesrange; /* return: function handled range */
8334 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 char_u *argp;
8338 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008339 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 int argcount = 0; /* number of arguments found */
8341
8342 /*
8343 * Get the arguments.
8344 */
8345 argp = *arg;
8346 while (argcount < MAX_FUNC_ARGS)
8347 {
8348 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8349 if (*argp == ')' || *argp == ',' || *argp == NUL)
8350 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8352 {
8353 ret = FAIL;
8354 break;
8355 }
8356 ++argcount;
8357 if (*argp != ',')
8358 break;
8359 }
8360 if (*argp == ')')
8361 ++argp;
8362 else
8363 ret = FAIL;
8364
8365 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008366 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008367 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008369 {
8370 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008371 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008373 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375
8376 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
8379 *arg = skipwhite(argp);
8380 return ret;
8381}
8382
8383
8384/*
8385 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008386 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008387 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 */
8389 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008390call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008392 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008394 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008396 typval_T *argvars; /* vars for arguments, must have "argcount"
8397 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 linenr_T firstline; /* first line of range */
8399 linenr_T lastline; /* last line of range */
8400 int *doesrange; /* return: function handled range */
8401 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405#define ERROR_UNKNOWN 0
8406#define ERROR_TOOMANY 1
8407#define ERROR_TOOFEW 2
8408#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008409#define ERROR_DICT 4
8410#define ERROR_NONE 5
8411#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 int error = ERROR_NONE;
8413 int i;
8414 int llen;
8415 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#define FLEN_FIXED 40
8417 char_u fname_buf[FLEN_FIXED + 1];
8418 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008419 char_u *name;
8420
8421 /* Make a copy of the name, if it comes from a funcref variable it could
8422 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008423 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008424 if (name == NULL)
8425 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426
8427 /*
8428 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8429 * Change <SNR>123_name() to K_SNR 123_name().
8430 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 llen = eval_fname_script(name);
8433 if (llen > 0)
8434 {
8435 fname_buf[0] = K_SPECIAL;
8436 fname_buf[1] = KS_EXTRA;
8437 fname_buf[2] = (int)KE_SNR;
8438 i = 3;
8439 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8440 {
8441 if (current_SID <= 0)
8442 error = ERROR_SCRIPT;
8443 else
8444 {
8445 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8446 i = (int)STRLEN(fname_buf);
8447 }
8448 }
8449 if (i + STRLEN(name + llen) < FLEN_FIXED)
8450 {
8451 STRCPY(fname_buf + i, name + llen);
8452 fname = fname_buf;
8453 }
8454 else
8455 {
8456 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8457 if (fname == NULL)
8458 error = ERROR_OTHER;
8459 else
8460 {
8461 mch_memmove(fname, fname_buf, (size_t)i);
8462 STRCPY(fname + i, name + llen);
8463 }
8464 }
8465 }
8466 else
8467 fname = name;
8468
8469 *doesrange = FALSE;
8470
8471
8472 /* execute the function if no errors detected and executing */
8473 if (evaluate && error == ERROR_NONE)
8474 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008475 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8476 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 error = ERROR_UNKNOWN;
8478
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008479 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 /*
8482 * User defined function.
8483 */
8484 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008487 /* Trigger FuncUndefined event, may load the function. */
8488 if (fp == NULL
8489 && apply_autocmds(EVENT_FUNCUNDEFINED,
8490 fname, fname, TRUE, NULL)
8491 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008493 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 fp = find_func(fname);
8495 }
8496#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008497 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008498 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008499 {
8500 /* loaded a package, search for the function again */
8501 fp = find_func(fname);
8502 }
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (fp != NULL)
8505 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008506 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008508 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008510 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008512 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008513 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 else
8515 {
8516 /*
8517 * Call the user function.
8518 * Save and restore search patterns, script variables and
8519 * redo buffer.
8520 */
8521 save_search_patterns();
8522 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008523 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008524 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008525 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008526 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8527 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8528 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008529 /* Function was unreferenced while being used, free it
8530 * now. */
8531 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 restoreRedobuff();
8533 restore_search_patterns();
8534 error = ERROR_NONE;
8535 }
8536 }
8537 }
8538 else
8539 {
8540 /*
8541 * Find the function name in the table, call its implementation.
8542 */
8543 i = find_internal_func(fname);
8544 if (i >= 0)
8545 {
8546 if (argcount < functions[i].f_min_argc)
8547 error = ERROR_TOOFEW;
8548 else if (argcount > functions[i].f_max_argc)
8549 error = ERROR_TOOMANY;
8550 else
8551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008552 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 error = ERROR_NONE;
8555 }
8556 }
8557 }
8558 /*
8559 * The function call (or "FuncUndefined" autocommand sequence) might
8560 * have been aborted by an error, an interrupt, or an explicitly thrown
8561 * exception that has not been caught so far. This situation can be
8562 * tested for by calling aborting(). For an error in an internal
8563 * function or for the "E132" error in call_user_func(), however, the
8564 * throw point at which the "force_abort" flag (temporarily reset by
8565 * emsg()) is normally updated has not been reached yet. We need to
8566 * update that flag first to make aborting() reliable.
8567 */
8568 update_force_abort();
8569 }
8570 if (error == ERROR_NONE)
8571 ret = OK;
8572
8573 /*
8574 * Report an error unless the argument evaluation or function call has been
8575 * cancelled due to an aborting error, an interrupt, or an exception.
8576 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 if (!aborting())
8578 {
8579 switch (error)
8580 {
8581 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008582 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008583 break;
8584 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008585 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008586 break;
8587 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008588 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008589 name);
8590 break;
8591 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008592 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008593 name);
8594 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008596 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 name);
8598 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008599 }
8600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (fname != name && fname != fname_buf)
8603 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008604 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605
8606 return ret;
8607}
8608
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008609/*
8610 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008611 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008612 */
8613 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008614emsg_funcname(ermsg, name)
8615 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008616 char_u *name;
8617{
8618 char_u *p;
8619
8620 if (*name == K_SPECIAL)
8621 p = concat_str((char_u *)"<SNR>", name + 3);
8622 else
8623 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008624 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008625 if (p != name)
8626 vim_free(p);
8627}
8628
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008629/*
8630 * Return TRUE for a non-zero Number and a non-empty String.
8631 */
8632 static int
8633non_zero_arg(argvars)
8634 typval_T *argvars;
8635{
8636 return ((argvars[0].v_type == VAR_NUMBER
8637 && argvars[0].vval.v_number != 0)
8638 || (argvars[0].v_type == VAR_STRING
8639 && argvars[0].vval.v_string != NULL
8640 && *argvars[0].vval.v_string != NUL));
8641}
8642
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643/*********************************************
8644 * Implementation of the built-in functions
8645 */
8646
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008647#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008648static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8649
8650/*
8651 * Get the float value of "argvars[0]" into "f".
8652 * Returns FAIL when the argument is not a Number or Float.
8653 */
8654 static int
8655get_float_arg(argvars, f)
8656 typval_T *argvars;
8657 float_T *f;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 *f = argvars[0].vval.v_float;
8662 return OK;
8663 }
8664 if (argvars[0].v_type == VAR_NUMBER)
8665 {
8666 *f = (float_T)argvars[0].vval.v_number;
8667 return OK;
8668 }
8669 EMSG(_("E808: Number or Float required"));
8670 return FAIL;
8671}
8672
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008673/*
8674 * "abs(expr)" function
8675 */
8676 static void
8677f_abs(argvars, rettv)
8678 typval_T *argvars;
8679 typval_T *rettv;
8680{
8681 if (argvars[0].v_type == VAR_FLOAT)
8682 {
8683 rettv->v_type = VAR_FLOAT;
8684 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8685 }
8686 else
8687 {
8688 varnumber_T n;
8689 int error = FALSE;
8690
8691 n = get_tv_number_chk(&argvars[0], &error);
8692 if (error)
8693 rettv->vval.v_number = -1;
8694 else if (n > 0)
8695 rettv->vval.v_number = n;
8696 else
8697 rettv->vval.v_number = -n;
8698 }
8699}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008700
8701/*
8702 * "acos()" function
8703 */
8704 static void
8705f_acos(argvars, rettv)
8706 typval_T *argvars;
8707 typval_T *rettv;
8708{
8709 float_T f;
8710
8711 rettv->v_type = VAR_FLOAT;
8712 if (get_float_arg(argvars, &f) == OK)
8713 rettv->vval.v_float = acos(f);
8714 else
8715 rettv->vval.v_float = 0.0;
8716}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008717#endif
8718
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008720 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 */
8722 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008723f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *argvars;
8725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008730 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008732 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008733 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008734 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008736 }
8737 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 EMSG(_(e_listreq));
8739}
8740
8741/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008742 * "and(expr, expr)" function
8743 */
8744 static void
8745f_and(argvars, rettv)
8746 typval_T *argvars;
8747 typval_T *rettv;
8748{
8749 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8750 & get_tv_number_chk(&argvars[1], NULL);
8751}
8752
8753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 * "append(lnum, string/list)" function
8755 */
8756 static void
8757f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *argvars;
8759 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760{
8761 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008762 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 list_T *l = NULL;
8764 listitem_T *li = NULL;
8765 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 long added = 0;
8767
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008768 /* When coming here from Insert mode, sync undo, so that this can be
8769 * undone separately from what was previously inserted. */
8770 if (u_sync_once == 2)
8771 {
8772 u_sync_once = 1; /* notify that u_sync() was called */
8773 u_sync(TRUE);
8774 }
8775
Bram Moolenaar0d660222005-01-07 21:51:51 +00008776 lnum = get_tv_lnum(argvars);
8777 if (lnum >= 0
8778 && lnum <= curbuf->b_ml.ml_line_count
8779 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008780 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008782 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008783 l = argvars[1].vval.v_list;
8784 if (l == NULL)
8785 return;
8786 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008787 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008788 for (;;)
8789 {
8790 if (l == NULL)
8791 tv = &argvars[1]; /* append a string */
8792 else if (li == NULL)
8793 break; /* end of list */
8794 else
8795 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796 line = get_tv_string_chk(tv);
8797 if (line == NULL) /* type error */
8798 {
8799 rettv->vval.v_number = 1; /* Failed */
8800 break;
8801 }
8802 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008803 ++added;
8804 if (l == NULL)
8805 break;
8806 li = li->li_next;
8807 }
8808
8809 appended_lines_mark(lnum, added);
8810 if (curwin->w_cursor.lnum > lnum)
8811 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 else
8814 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "argc()" function
8819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828/*
8829 * "argidx()" function
8830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008833 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008834 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839/*
8840 * "argv(nr)" function
8841 */
8842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008843f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 typval_T *argvars;
8845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 int idx;
8848
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008849 if (argvars[0].v_type != VAR_UNKNOWN)
8850 {
8851 idx = get_tv_number_chk(&argvars[0], NULL);
8852 if (idx >= 0 && idx < ARGCOUNT)
8853 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8854 else
8855 rettv->vval.v_string = NULL;
8856 rettv->v_type = VAR_STRING;
8857 }
8858 else if (rettv_list_alloc(rettv) == OK)
8859 for (idx = 0; idx < ARGCOUNT; ++idx)
8860 list_append_string(rettv->vval.v_list,
8861 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862}
8863
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008865/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008866 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008867 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008868 static void
8869f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008870 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008871 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008873 float_T f;
8874
8875 rettv->v_type = VAR_FLOAT;
8876 if (get_float_arg(argvars, &f) == OK)
8877 rettv->vval.v_float = asin(f);
8878 else
8879 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880}
8881
8882/*
8883 * "atan()" function
8884 */
8885 static void
8886f_atan(argvars, rettv)
8887 typval_T *argvars;
8888 typval_T *rettv;
8889{
8890 float_T f;
8891
8892 rettv->v_type = VAR_FLOAT;
8893 if (get_float_arg(argvars, &f) == OK)
8894 rettv->vval.v_float = atan(f);
8895 else
8896 rettv->vval.v_float = 0.0;
8897}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008898
8899/*
8900 * "atan2()" function
8901 */
8902 static void
8903f_atan2(argvars, rettv)
8904 typval_T *argvars;
8905 typval_T *rettv;
8906{
8907 float_T fx, fy;
8908
8909 rettv->v_type = VAR_FLOAT;
8910 if (get_float_arg(argvars, &fx) == OK
8911 && get_float_arg(&argvars[1], &fy) == OK)
8912 rettv->vval.v_float = atan2(fx, fy);
8913 else
8914 rettv->vval.v_float = 0.0;
8915}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008916#endif
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918/*
8919 * "browse(save, title, initdir, default)" function
8920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008924 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926#ifdef FEAT_BROWSE
8927 int save;
8928 char_u *title;
8929 char_u *initdir;
8930 char_u *defname;
8931 char_u buf[NUMBUFLEN];
8932 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935 save = get_tv_number_chk(&argvars[0], &error);
8936 title = get_tv_string_chk(&argvars[1]);
8937 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8938 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 if (error || title == NULL || initdir == NULL || defname == NULL)
8941 rettv->vval.v_string = NULL;
8942 else
8943 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008944 do_browse(save ? BROWSE_SAVE : 0,
8945 title, defname, NULL, initdir, NULL, curbuf);
8946#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950}
8951
8952/*
8953 * "browsedir(title, initdir)" function
8954 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008957 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008958 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008959{
8960#ifdef FEAT_BROWSE
8961 char_u *title;
8962 char_u *initdir;
8963 char_u buf[NUMBUFLEN];
8964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 title = get_tv_string_chk(&argvars[0]);
8966 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008967
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 if (title == NULL || initdir == NULL)
8969 rettv->vval.v_string = NULL;
8970 else
8971 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008972 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977}
8978
Bram Moolenaar33570922005-01-25 22:26:29 +00008979static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008980
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981/*
8982 * Find a buffer by number or exact name.
8983 */
8984 static buf_T *
8985find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 if (avar->v_type == VAR_NUMBER)
8991 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008992 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008995 if (buf == NULL)
8996 {
8997 /* No full path name match, try a match with a URL or a "nofile"
8998 * buffer, these don't use the full path. */
8999 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9000 if (buf->b_fname != NULL
9001 && (path_with_url(buf->b_fname)
9002#ifdef FEAT_QUICKFIX
9003 || bt_nofile(buf)
9004#endif
9005 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009007 break;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 }
9010 return buf;
9011}
9012
9013/*
9014 * "bufexists(expr)" function
9015 */
9016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009017f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009018 typval_T *argvars;
9019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022}
9023
9024/*
9025 * "buflisted(expr)" function
9026 */
9027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009028f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009029 typval_T *argvars;
9030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 buf_T *buf;
9033
9034 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009035 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036}
9037
9038/*
9039 * "bufloaded(expr)" function
9040 */
9041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009042f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009043 typval_T *argvars;
9044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 buf_T *buf;
9047
9048 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009049 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050}
9051
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009052static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054/*
9055 * Get buffer by number or pattern.
9056 */
9057 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009058get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009059 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 int save_magic;
9064 char_u *save_cpo;
9065 buf_T *buf;
9066
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 if (tv->v_type == VAR_NUMBER)
9068 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009069 if (tv->v_type != VAR_STRING)
9070 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (name == NULL || *name == NUL)
9072 return curbuf;
9073 if (name[0] == '$' && name[1] == NUL)
9074 return lastbuf;
9075
9076 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9077 save_magic = p_magic;
9078 p_magic = TRUE;
9079 save_cpo = p_cpo;
9080 p_cpo = (char_u *)"";
9081
9082 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009083 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
9085 p_magic = save_magic;
9086 p_cpo = save_cpo;
9087
9088 /* If not found, try expanding the name, like done for bufexists(). */
9089 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
9092 return buf;
9093}
9094
9095/*
9096 * "bufname(expr)" function
9097 */
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103 buf_T *buf;
9104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 --emsg_off;
9114}
9115
9116/*
9117 * "bufnr(expr)" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009121 typval_T *argvars;
9122 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009125 int error = FALSE;
9126 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009130 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009131 --emsg_off;
9132
9133 /* If the buffer isn't found and the second argument is not zero create a
9134 * new buffer. */
9135 if (buf == NULL
9136 && argvars[1].v_type != VAR_UNKNOWN
9137 && get_tv_number_chk(&argvars[1], &error) != 0
9138 && !error
9139 && (name = get_tv_string_chk(&argvars[0])) != NULL
9140 && !error)
9141 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9142
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147}
9148
9149/*
9150 * "bufwinnr(nr)" function
9151 */
9152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 typval_T *argvars;
9155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifdef FEAT_WINDOWS
9158 win_T *wp;
9159 int winnr = 0;
9160#endif
9161 buf_T *buf;
9162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009163 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009165 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#ifdef FEAT_WINDOWS
9167 for (wp = firstwin; wp; wp = wp->w_next)
9168 {
9169 ++winnr;
9170 if (wp->w_buffer == buf)
9171 break;
9172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176#endif
9177 --emsg_off;
9178}
9179
9180/*
9181 * "byte2line(byte)" function
9182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009185 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187{
9188#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190#else
9191 long boff = 0;
9192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 (linenr_T)0, &boff);
9199#endif
9200}
9201
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009202 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009203byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009204 typval_T *argvars;
9205 typval_T *rettv;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009206 int comp;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009207{
9208#ifdef FEAT_MBYTE
9209 char_u *t;
9210#endif
9211 char_u *str;
9212 long idx;
9213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 str = get_tv_string_chk(&argvars[0]);
9215 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009218 return;
9219
9220#ifdef FEAT_MBYTE
9221 t = str;
9222 for ( ; idx > 0; idx--)
9223 {
9224 if (*t == NUL) /* EOL reached */
9225 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009226 if (enc_utf8 && comp)
9227 t += utf_ptr2len(t);
9228 else
9229 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009230 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009231 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009232#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009233 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009234 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009235#endif
9236}
9237
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009238/*
9239 * "byteidx()" function
9240 */
9241 static void
9242f_byteidx(argvars, rettv)
9243 typval_T *argvars;
9244 typval_T *rettv;
9245{
9246 byteidx(argvars, rettv, FALSE);
9247}
9248
9249/*
9250 * "byteidxcomp()" function
9251 */
9252 static void
9253f_byteidxcomp(argvars, rettv)
9254 typval_T *argvars;
9255 typval_T *rettv;
9256{
9257 byteidx(argvars, rettv, TRUE);
9258}
9259
Bram Moolenaardb913952012-06-29 12:54:53 +02009260 int
9261func_call(name, args, selfdict, rettv)
9262 char_u *name;
9263 typval_T *args;
9264 dict_T *selfdict;
9265 typval_T *rettv;
9266{
9267 listitem_T *item;
9268 typval_T argv[MAX_FUNC_ARGS + 1];
9269 int argc = 0;
9270 int dummy;
9271 int r = 0;
9272
9273 for (item = args->vval.v_list->lv_first; item != NULL;
9274 item = item->li_next)
9275 {
9276 if (argc == MAX_FUNC_ARGS)
9277 {
9278 EMSG(_("E699: Too many arguments"));
9279 break;
9280 }
9281 /* Make a copy of each argument. This is needed to be able to set
9282 * v_lock to VAR_FIXED in the copy without changing the original list.
9283 */
9284 copy_tv(&item->li_tv, &argv[argc++]);
9285 }
9286
9287 if (item == NULL)
9288 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9289 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9290 &dummy, TRUE, selfdict);
9291
9292 /* Free the arguments. */
9293 while (argc > 0)
9294 clear_tv(&argv[--argc]);
9295
9296 return r;
9297}
9298
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009299/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009300 * "call(func, arglist)" function
9301 */
9302 static void
9303f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 typval_T *argvars;
9305 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009306{
9307 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009308 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009309
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009310 if (argvars[1].v_type != VAR_LIST)
9311 {
9312 EMSG(_(e_listreq));
9313 return;
9314 }
9315 if (argvars[1].vval.v_list == NULL)
9316 return;
9317
9318 if (argvars[0].v_type == VAR_FUNC)
9319 func = argvars[0].vval.v_string;
9320 else
9321 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 if (*func == NUL)
9323 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009324
Bram Moolenaare9a41262005-01-15 22:18:47 +00009325 if (argvars[2].v_type != VAR_UNKNOWN)
9326 {
9327 if (argvars[2].v_type != VAR_DICT)
9328 {
9329 EMSG(_(e_dictreq));
9330 return;
9331 }
9332 selfdict = argvars[2].vval.v_dict;
9333 }
9334
Bram Moolenaardb913952012-06-29 12:54:53 +02009335 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009336}
9337
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#ifdef FEAT_FLOAT
9339/*
9340 * "ceil({float})" function
9341 */
9342 static void
9343f_ceil(argvars, rettv)
9344 typval_T *argvars;
9345 typval_T *rettv;
9346{
9347 float_T f;
9348
9349 rettv->v_type = VAR_FLOAT;
9350 if (get_float_arg(argvars, &f) == OK)
9351 rettv->vval.v_float = ceil(f);
9352 else
9353 rettv->vval.v_float = 0.0;
9354}
9355#endif
9356
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009357/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009358 * "changenr()" function
9359 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009360 static void
9361f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009362 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009363 typval_T *rettv;
9364{
9365 rettv->vval.v_number = curbuf->b_u_seq_cur;
9366}
9367
9368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 * "char2nr(string)" function
9370 */
9371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009373 typval_T *argvars;
9374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375{
9376#ifdef FEAT_MBYTE
9377 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009378 {
9379 int utf8 = 0;
9380
9381 if (argvars[1].v_type != VAR_UNKNOWN)
9382 utf8 = get_tv_number_chk(&argvars[1], NULL);
9383
9384 if (utf8)
9385 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9386 else
9387 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 else
9390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392}
9393
9394/*
9395 * "cindent(lnum)" function
9396 */
9397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
9402#ifdef FEAT_CINDENT
9403 pos_T pos;
9404 linenr_T lnum;
9405
9406 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9409 {
9410 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 curwin->w_cursor = pos;
9413 }
9414 else
9415#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009416 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417}
9418
9419/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009420 * "clearmatches()" function
9421 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009422 static void
9423f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009424 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009425 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009426{
9427#ifdef FEAT_SEARCH_EXTRA
9428 clear_matches(curwin);
9429#endif
9430}
9431
9432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 * "col(string)" function
9434 */
9435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009437 typval_T *argvars;
9438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 colnr_T col = 0;
9441 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009442 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009444 fp = var2fpos(&argvars[0], FALSE, &fnum);
9445 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 if (fp->col == MAXCOL)
9448 {
9449 /* '> can be MAXCOL, get the length of the line then */
9450 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009451 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
9453 col = MAXCOL;
9454 }
9455 else
9456 {
9457 col = fp->col + 1;
9458#ifdef FEAT_VIRTUALEDIT
9459 /* col(".") when the cursor is on the NUL at the end of the line
9460 * because of "coladd" can be seen as an extra column. */
9461 if (virtual_active() && fp == &curwin->w_cursor)
9462 {
9463 char_u *p = ml_get_cursor();
9464
9465 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9466 curwin->w_virtcol - curwin->w_cursor.coladd))
9467 {
9468# ifdef FEAT_MBYTE
9469 int l;
9470
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009471 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 col += l;
9473# else
9474 if (*p != NUL && p[1] == NUL)
9475 ++col;
9476# endif
9477 }
9478 }
9479#endif
9480 }
9481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483}
9484
Bram Moolenaar572cb562005-08-05 21:35:02 +00009485#if defined(FEAT_INS_EXPAND)
9486/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009487 * "complete()" function
9488 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009489 static void
9490f_complete(argvars, rettv)
9491 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009492 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009493{
9494 int startcol;
9495
9496 if ((State & INSERT) == 0)
9497 {
9498 EMSG(_("E785: complete() can only be used in Insert mode"));
9499 return;
9500 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009501
9502 /* Check for undo allowed here, because if something was already inserted
9503 * the line was already saved for undo and this check isn't done. */
9504 if (!undo_allowed())
9505 return;
9506
Bram Moolenaarade00832006-03-10 21:46:58 +00009507 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9508 {
9509 EMSG(_(e_invarg));
9510 return;
9511 }
9512
9513 startcol = get_tv_number_chk(&argvars[0], NULL);
9514 if (startcol <= 0)
9515 return;
9516
9517 set_completion(startcol - 1, argvars[1].vval.v_list);
9518}
9519
9520/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009521 * "complete_add()" function
9522 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009523 static void
9524f_complete_add(argvars, rettv)
9525 typval_T *argvars;
9526 typval_T *rettv;
9527{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009528 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009529}
9530
9531/*
9532 * "complete_check()" function
9533 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009534 static void
9535f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009536 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009537 typval_T *rettv;
9538{
9539 int saved = RedrawingDisabled;
9540
9541 RedrawingDisabled = 0;
9542 ins_compl_check_keys(0);
9543 rettv->vval.v_number = compl_interrupted;
9544 RedrawingDisabled = saved;
9545}
9546#endif
9547
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548/*
9549 * "confirm(message, buttons[, default [, type]])" function
9550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009553 typval_T *argvars UNUSED;
9554 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9557 char_u *message;
9558 char_u *buttons = NULL;
9559 char_u buf[NUMBUFLEN];
9560 char_u buf2[NUMBUFLEN];
9561 int def = 1;
9562 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 char_u *typestr;
9564 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 message = get_tv_string_chk(&argvars[0]);
9567 if (message == NULL)
9568 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009571 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9572 if (buttons == NULL)
9573 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009577 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9580 if (typestr == NULL)
9581 error = TRUE;
9582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 switch (TOUPPER_ASC(*typestr))
9585 {
9586 case 'E': type = VIM_ERROR; break;
9587 case 'Q': type = VIM_QUESTION; break;
9588 case 'I': type = VIM_INFO; break;
9589 case 'W': type = VIM_WARNING; break;
9590 case 'G': type = VIM_GENERIC; break;
9591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 }
9593 }
9594 }
9595 }
9596
9597 if (buttons == NULL || *buttons == NUL)
9598 buttons = (char_u *)_("&Ok");
9599
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009600 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009601 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009602 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#endif
9604}
9605
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009606/*
9607 * "copy()" function
9608 */
9609 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009611 typval_T *argvars;
9612 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009614 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009617#ifdef FEAT_FLOAT
9618/*
9619 * "cos()" function
9620 */
9621 static void
9622f_cos(argvars, rettv)
9623 typval_T *argvars;
9624 typval_T *rettv;
9625{
9626 float_T f;
9627
9628 rettv->v_type = VAR_FLOAT;
9629 if (get_float_arg(argvars, &f) == OK)
9630 rettv->vval.v_float = cos(f);
9631 else
9632 rettv->vval.v_float = 0.0;
9633}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009634
9635/*
9636 * "cosh()" function
9637 */
9638 static void
9639f_cosh(argvars, rettv)
9640 typval_T *argvars;
9641 typval_T *rettv;
9642{
9643 float_T f;
9644
9645 rettv->v_type = VAR_FLOAT;
9646 if (get_float_arg(argvars, &f) == OK)
9647 rettv->vval.v_float = cosh(f);
9648 else
9649 rettv->vval.v_float = 0.0;
9650}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009651#endif
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009654 * "count()" function
9655 */
9656 static void
9657f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009658 typval_T *argvars;
9659 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009661 long n = 0;
9662 int ic = FALSE;
9663
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 listitem_T *li;
9667 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009669
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 if ((l = argvars[0].vval.v_list) != NULL)
9671 {
9672 li = l->lv_first;
9673 if (argvars[2].v_type != VAR_UNKNOWN)
9674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009675 int error = FALSE;
9676
9677 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 if (argvars[3].v_type != VAR_UNKNOWN)
9679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 idx = get_tv_number_chk(&argvars[3], &error);
9681 if (!error)
9682 {
9683 li = list_find(l, idx);
9684 if (li == NULL)
9685 EMSGN(_(e_listidx), idx);
9686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009687 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 if (error)
9689 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009690 }
9691
9692 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009693 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009694 ++n;
9695 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009697 else if (argvars[0].v_type == VAR_DICT)
9698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009699 int todo;
9700 dict_T *d;
9701 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009702
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009703 if ((d = argvars[0].vval.v_dict) != NULL)
9704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009705 int error = FALSE;
9706
Bram Moolenaare9a41262005-01-15 22:18:47 +00009707 if (argvars[2].v_type != VAR_UNKNOWN)
9708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009710 if (argvars[3].v_type != VAR_UNKNOWN)
9711 EMSG(_(e_invarg));
9712 }
9713
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009714 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009715 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009716 {
9717 if (!HASHITEM_EMPTY(hi))
9718 {
9719 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009720 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009721 ++n;
9722 }
9723 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009724 }
9725 }
9726 else
9727 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728 rettv->vval.v_number = n;
9729}
9730
9731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9733 *
9734 * Checks the existence of a cscope connection.
9735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009738 typval_T *argvars UNUSED;
9739 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741#ifdef FEAT_CSCOPE
9742 int num = 0;
9743 char_u *dbpath = NULL;
9744 char_u *prepend = NULL;
9745 char_u buf[NUMBUFLEN];
9746
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009747 if (argvars[0].v_type != VAR_UNKNOWN
9748 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 num = (int)get_tv_number(&argvars[0]);
9751 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009752 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 }
9755
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#endif
9758}
9759
9760/*
9761 * "cursor(lnum, col)" function
9762 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009763 * Moves the cursor to the specified line and column.
9764 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009772#ifdef FEAT_VIRTUALEDIT
9773 long coladd = 0;
9774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009777 if (argvars[1].v_type == VAR_UNKNOWN)
9778 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009779 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009780
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009781 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009782 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009783 line = pos.lnum;
9784 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009785#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009786 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009787#endif
9788 }
9789 else
9790 {
9791 line = get_tv_lnum(argvars);
9792 col = get_tv_number_chk(&argvars[1], NULL);
9793#ifdef FEAT_VIRTUALEDIT
9794 if (argvars[2].v_type != VAR_UNKNOWN)
9795 coladd = get_tv_number_chk(&argvars[2], NULL);
9796#endif
9797 }
9798 if (line < 0 || col < 0
9799#ifdef FEAT_VIRTUALEDIT
9800 || coladd < 0
9801#endif
9802 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (line > 0)
9805 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (col > 0)
9807 curwin->w_cursor.col = col - 1;
9808#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009809 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#endif
9811
9812 /* Make sure the cursor is in a valid position. */
9813 check_cursor();
9814#ifdef FEAT_MBYTE
9815 /* Correct cursor for multi-byte character. */
9816 if (has_mbyte)
9817 mb_adjust_cursor();
9818#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009819
9820 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009821 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822}
9823
9824/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009825 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 */
9827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009829 typval_T *argvars;
9830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009832 int noref = 0;
9833
9834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009835 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009836 if (noref < 0 || noref > 1)
9837 EMSG(_(e_invarg));
9838 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009839 {
9840 current_copyID += COPYID_INC;
9841 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843}
9844
9845/*
9846 * "delete()" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009850 typval_T *argvars;
9851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009856 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857}
9858
9859/*
9860 * "did_filetype()" function
9861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009864 typval_T *argvars UNUSED;
9865 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869#endif
9870}
9871
9872/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009873 * "diff_filler()" function
9874 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009877 typval_T *argvars UNUSED;
9878 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009879{
9880#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882#endif
9883}
9884
9885/*
9886 * "diff_hlID()" function
9887 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009890 typval_T *argvars UNUSED;
9891 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009892{
9893#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009895 static linenr_T prev_lnum = 0;
9896 static int changedtick = 0;
9897 static int fnum = 0;
9898 static int change_start = 0;
9899 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009900 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009901 int filler_lines;
9902 int col;
9903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 if (lnum < 0) /* ignore type error in {lnum} arg */
9905 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009906 if (lnum != prev_lnum
9907 || changedtick != curbuf->b_changedtick
9908 || fnum != curbuf->b_fnum)
9909 {
9910 /* New line, buffer, change: need to get the values. */
9911 filler_lines = diff_check(curwin, lnum);
9912 if (filler_lines < 0)
9913 {
9914 if (filler_lines == -1)
9915 {
9916 change_start = MAXCOL;
9917 change_end = -1;
9918 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9919 hlID = HLF_ADD; /* added line */
9920 else
9921 hlID = HLF_CHD; /* changed line */
9922 }
9923 else
9924 hlID = HLF_ADD; /* added line */
9925 }
9926 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009927 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009928 prev_lnum = lnum;
9929 changedtick = curbuf->b_changedtick;
9930 fnum = curbuf->b_fnum;
9931 }
9932
9933 if (hlID == HLF_CHD || hlID == HLF_TXD)
9934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009936 if (col >= change_start && col <= change_end)
9937 hlID = HLF_TXD; /* changed text */
9938 else
9939 hlID = HLF_CHD; /* changed line */
9940 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009941 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009942#endif
9943}
9944
9945/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009946 * "empty({expr})" function
9947 */
9948 static void
9949f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009950 typval_T *argvars;
9951 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009952{
9953 int n;
9954
9955 switch (argvars[0].v_type)
9956 {
9957 case VAR_STRING:
9958 case VAR_FUNC:
9959 n = argvars[0].vval.v_string == NULL
9960 || *argvars[0].vval.v_string == NUL;
9961 break;
9962 case VAR_NUMBER:
9963 n = argvars[0].vval.v_number == 0;
9964 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009965#ifdef FEAT_FLOAT
9966 case VAR_FLOAT:
9967 n = argvars[0].vval.v_float == 0.0;
9968 break;
9969#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009970 case VAR_LIST:
9971 n = argvars[0].vval.v_list == NULL
9972 || argvars[0].vval.v_list->lv_first == NULL;
9973 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 case VAR_DICT:
9975 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009978 default:
9979 EMSG2(_(e_intern2), "f_empty()");
9980 n = 0;
9981 }
9982
9983 rettv->vval.v_number = n;
9984}
9985
9986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 * "escape({string}, {chars})" function
9988 */
9989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009990f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009991 typval_T *argvars;
9992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 char_u buf[NUMBUFLEN];
9995
Bram Moolenaar758711c2005-02-02 23:11:38 +00009996 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9997 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010002 * "eval()" function
10003 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010004 static void
10005f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010006 typval_T *argvars;
10007 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010008{
10009 char_u *s;
10010
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 s = get_tv_string_chk(&argvars[0]);
10012 if (s != NULL)
10013 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10016 {
10017 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010018 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010020 else if (*s != NUL)
10021 EMSG(_(e_trailing));
10022}
10023
10024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 * "eventhandler()" function
10026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010028f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010029 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "executable()" function
10037 */
10038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010039f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010040 typval_T *argvars;
10041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010043 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044}
10045
10046/*
10047 * "exists()" function
10048 */
10049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010051 typval_T *argvars;
10052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 char_u *p;
10055 char_u *name;
10056 int n = FALSE;
10057 int len = 0;
10058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 if (*p == '$') /* environment variable */
10061 {
10062 /* first try "normal" environment variables (fast) */
10063 if (mch_getenv(p + 1) != NULL)
10064 n = TRUE;
10065 else
10066 {
10067 /* try expanding things like $VIM and ${HOME} */
10068 p = expand_env_save(p);
10069 if (p != NULL && *p != '$')
10070 n = TRUE;
10071 vim_free(p);
10072 }
10073 }
10074 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010076 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010077 if (*skipwhite(p) != NUL)
10078 n = FALSE; /* trailing garbage */
10079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 else if (*p == '*') /* internal or user defined function */
10081 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010082 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 }
10084 else if (*p == ':')
10085 {
10086 n = cmd_exists(p + 1);
10087 }
10088 else if (*p == '#')
10089 {
10090#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010091 if (p[1] == '#')
10092 n = autocmd_supported(p + 2);
10093 else
10094 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095#endif
10096 }
10097 else /* internal variable */
10098 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010099 char_u *tofree;
10100 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010102 /* get_name_len() takes care of expanding curly braces */
10103 name = p;
10104 len = get_name_len(&p, &tofree, TRUE, FALSE);
10105 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010107 if (tofree != NULL)
10108 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010109 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010110 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010112 /* handle d.key, l[idx], f(expr) */
10113 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10114 if (n)
10115 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 }
10117 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010118 if (*p != NUL)
10119 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010121 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 }
10123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125}
10126
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010127#ifdef FEAT_FLOAT
10128/*
10129 * "exp()" function
10130 */
10131 static void
10132f_exp(argvars, rettv)
10133 typval_T *argvars;
10134 typval_T *rettv;
10135{
10136 float_T f;
10137
10138 rettv->v_type = VAR_FLOAT;
10139 if (get_float_arg(argvars, &f) == OK)
10140 rettv->vval.v_float = exp(f);
10141 else
10142 rettv->vval.v_float = 0.0;
10143}
10144#endif
10145
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146/*
10147 * "expand()" function
10148 */
10149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010151 typval_T *argvars;
10152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 char_u *s;
10155 int len;
10156 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010157 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010160 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010163 if (argvars[1].v_type != VAR_UNKNOWN
10164 && argvars[2].v_type != VAR_UNKNOWN
10165 && get_tv_number_chk(&argvars[2], &error)
10166 && !error)
10167 {
10168 rettv->v_type = VAR_LIST;
10169 rettv->vval.v_list = NULL;
10170 }
10171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010172 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 if (*s == '%' || *s == '#' || *s == '<')
10174 {
10175 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010176 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010178 if (rettv->v_type == VAR_LIST)
10179 {
10180 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10181 list_append_string(rettv->vval.v_list, result, -1);
10182 else
10183 vim_free(result);
10184 }
10185 else
10186 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 }
10188 else
10189 {
10190 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010191 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 if (argvars[1].v_type != VAR_UNKNOWN
10193 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010194 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 if (!error)
10196 {
10197 ExpandInit(&xpc);
10198 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010199 if (p_wic)
10200 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010201 if (rettv->v_type == VAR_STRING)
10202 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10203 options, WILD_ALL);
10204 else if (rettv_list_alloc(rettv) != FAIL)
10205 {
10206 int i;
10207
10208 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10209 for (i = 0; i < xpc.xp_numfiles; i++)
10210 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10211 ExpandCleanup(&xpc);
10212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 }
10214 else
10215 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 }
10217}
10218
10219/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010220 * Go over all entries in "d2" and add them to "d1".
10221 * When "action" is "error" then a duplicate key is an error.
10222 * When "action" is "force" then a duplicate key is overwritten.
10223 * Otherwise duplicate keys are ignored ("action" is "keep").
10224 */
10225 void
10226dict_extend(d1, d2, action)
10227 dict_T *d1;
10228 dict_T *d2;
10229 char_u *action;
10230{
10231 dictitem_T *di1;
10232 hashitem_T *hi2;
10233 int todo;
10234
10235 todo = (int)d2->dv_hashtab.ht_used;
10236 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10237 {
10238 if (!HASHITEM_EMPTY(hi2))
10239 {
10240 --todo;
10241 di1 = dict_find(d1, hi2->hi_key, -1);
10242 if (d1->dv_scope != 0)
10243 {
10244 /* Disallow replacing a builtin function in l: and g:.
10245 * Check the key to be valid when adding to any
10246 * scope. */
10247 if (d1->dv_scope == VAR_DEF_SCOPE
10248 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10249 && var_check_func_name(hi2->hi_key,
10250 di1 == NULL))
10251 break;
10252 if (!valid_varname(hi2->hi_key))
10253 break;
10254 }
10255 if (di1 == NULL)
10256 {
10257 di1 = dictitem_copy(HI2DI(hi2));
10258 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10259 dictitem_free(di1);
10260 }
10261 else if (*action == 'e')
10262 {
10263 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10264 break;
10265 }
10266 else if (*action == 'f' && HI2DI(hi2) != di1)
10267 {
10268 clear_tv(&di1->di_tv);
10269 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10270 }
10271 }
10272 }
10273}
10274
10275/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010278 */
10279 static void
10280f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010283{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010284 char *arg_errmsg = N_("extend() argument");
10285
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 list_T *l1, *l2;
10289 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010292
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 l1 = argvars[0].vval.v_list;
10294 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010295 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010296 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010297 {
10298 if (argvars[2].v_type != VAR_UNKNOWN)
10299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 before = get_tv_number_chk(&argvars[2], &error);
10301 if (error)
10302 return; /* type error; errmsg already given */
10303
Bram Moolenaar758711c2005-02-02 23:11:38 +000010304 if (before == l1->lv_len)
10305 item = NULL;
10306 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010308 item = list_find(l1, before);
10309 if (item == NULL)
10310 {
10311 EMSGN(_(e_listidx), before);
10312 return;
10313 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 }
10315 }
10316 else
10317 item = NULL;
10318 list_extend(l1, l2, item);
10319
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 copy_tv(&argvars[0], rettv);
10321 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010323 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10324 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010325 dict_T *d1, *d2;
10326 char_u *action;
10327 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010328
10329 d1 = argvars[0].vval.v_dict;
10330 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010331 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010332 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 {
10334 /* Check the third argument. */
10335 if (argvars[2].v_type != VAR_UNKNOWN)
10336 {
10337 static char *(av[]) = {"keep", "force", "error"};
10338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 action = get_tv_string_chk(&argvars[2]);
10340 if (action == NULL)
10341 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 for (i = 0; i < 3; ++i)
10343 if (STRCMP(action, av[i]) == 0)
10344 break;
10345 if (i == 3)
10346 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010347 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010348 return;
10349 }
10350 }
10351 else
10352 action = (char_u *)"force";
10353
Bram Moolenaara9922d62013-05-30 13:01:18 +020010354 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355
Bram Moolenaare9a41262005-01-15 22:18:47 +000010356 copy_tv(&argvars[0], rettv);
10357 }
10358 }
10359 else
10360 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010361}
10362
10363/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010364 * "feedkeys()" function
10365 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010366 static void
10367f_feedkeys(argvars, rettv)
10368 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010369 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370{
10371 int remap = TRUE;
10372 char_u *keys, *flags;
10373 char_u nbuf[NUMBUFLEN];
10374 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010375 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010376
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010377 /* This is not allowed in the sandbox. If the commands would still be
10378 * executed in the sandbox it would be OK, but it probably happens later,
10379 * when "sandbox" is no longer set. */
10380 if (check_secure())
10381 return;
10382
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010383 keys = get_tv_string(&argvars[0]);
10384 if (*keys != NUL)
10385 {
10386 if (argvars[1].v_type != VAR_UNKNOWN)
10387 {
10388 flags = get_tv_string_buf(&argvars[1], nbuf);
10389 for ( ; *flags != NUL; ++flags)
10390 {
10391 switch (*flags)
10392 {
10393 case 'n': remap = FALSE; break;
10394 case 'm': remap = TRUE; break;
10395 case 't': typed = TRUE; break;
10396 }
10397 }
10398 }
10399
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010400 /* Need to escape K_SPECIAL and CSI before putting the string in the
10401 * typeahead buffer. */
10402 keys_esc = vim_strsave_escape_csi(keys);
10403 if (keys_esc != NULL)
10404 {
10405 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010406 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010407 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010408 if (vgetc_busy)
10409 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010410 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010411 }
10412}
10413
10414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 * "filereadable()" function
10416 */
10417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 typval_T *argvars;
10420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010422 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 char_u *p;
10424 int n;
10425
Bram Moolenaarc236c162008-07-13 17:41:49 +000010426#ifndef O_NONBLOCK
10427# define O_NONBLOCK 0
10428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010430 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10431 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 {
10433 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010434 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 }
10436 else
10437 n = FALSE;
10438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010439 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440}
10441
10442/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010443 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 * rights to write into.
10445 */
10446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010447f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010448 typval_T *argvars;
10449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010451 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452}
10453
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010454static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010455
10456 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010457findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010458 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010460 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010461{
10462#ifdef FEAT_SEARCHPATH
10463 char_u *fname;
10464 char_u *fresult = NULL;
10465 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10466 char_u *p;
10467 char_u pathbuf[NUMBUFLEN];
10468 int count = 1;
10469 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010470 int error = FALSE;
10471#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010472
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010473 rettv->vval.v_string = NULL;
10474 rettv->v_type = VAR_STRING;
10475
10476#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010480 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10482 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010483 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 else
10485 {
10486 if (*p != NUL)
10487 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010490 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010491 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010492 }
10493
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010494 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10495 error = TRUE;
10496
10497 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010499 do
10500 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010501 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010502 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010503 fresult = find_file_in_path_option(first ? fname : NULL,
10504 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010505 0, first, path,
10506 find_what,
10507 curbuf->b_ffname,
10508 find_what == FINDFILE_DIR
10509 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010510 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010511
10512 if (fresult != NULL && rettv->v_type == VAR_LIST)
10513 list_append_string(rettv->vval.v_list, fresult, -1);
10514
10515 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010517
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010518 if (rettv->v_type == VAR_STRING)
10519 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010521}
10522
Bram Moolenaar33570922005-01-25 22:26:29 +000010523static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10524static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010525
10526/*
10527 * Implementation of map() and filter().
10528 */
10529 static void
10530filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010531 typval_T *argvars;
10532 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010533 int map;
10534{
10535 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010536 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 listitem_T *li, *nli;
10538 list_T *l = NULL;
10539 dictitem_T *di;
10540 hashtab_T *ht;
10541 hashitem_T *hi;
10542 dict_T *d = NULL;
10543 typval_T save_val;
10544 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010545 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010546 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010547 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10548 char *arg_errmsg = (map ? N_("map() argument")
10549 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010550 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010551 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010552
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010554 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010555 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010556 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 return;
10558 }
10559 else if (argvars[0].v_type == VAR_DICT)
10560 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010561 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010562 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 return;
10564 }
10565 else
10566 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010567 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568 return;
10569 }
10570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 expr = get_tv_string_buf_chk(&argvars[1], buf);
10572 /* On type errors, the preceding call has already displayed an error
10573 * message. Avoid a misleading error message for an empty string that
10574 * was not passed as argument. */
10575 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 prepare_vimvar(VV_VAL, &save_val);
10578 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010579
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010580 /* We reset "did_emsg" to be able to detect whether an error
10581 * occurred during evaluation of the expression. */
10582 save_did_emsg = did_emsg;
10583 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010584
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010585 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010588 vimvars[VV_KEY].vv_type = VAR_STRING;
10589
10590 ht = &d->dv_hashtab;
10591 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010592 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 if (!HASHITEM_EMPTY(hi))
10596 {
10597 --todo;
10598 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010599 if (tv_check_lock(di->di_tv.v_lock,
10600 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 break;
10602 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010603 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010604 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010605 break;
10606 if (!map && rem)
10607 dictitem_remove(d, di);
10608 clear_tv(&vimvars[VV_KEY].vv_tv);
10609 }
10610 }
10611 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 }
10613 else
10614 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010615 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 for (li = l->lv_first; li != NULL; li = nli)
10618 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010619 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010620 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010622 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010623 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010624 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010625 break;
10626 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010628 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010631
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010632 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010634
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010635 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637
10638 copy_tv(&argvars[0], rettv);
10639}
10640
10641 static int
10642filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010643 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010644 char_u *expr;
10645 int map;
10646 int *remp;
10647{
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010649 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010650 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 s = expr;
10654 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010655 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010656 if (*s != NUL) /* check for trailing chars after expr */
10657 {
10658 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010659 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010660 }
10661 if (map)
10662 {
10663 /* map(): replace the list item value */
10664 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010665 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 *tv = rettv;
10667 }
10668 else
10669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010670 int error = FALSE;
10671
Bram Moolenaare9a41262005-01-15 22:18:47 +000010672 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010673 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010674 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 /* On type error, nothing has been removed; return FAIL to stop the
10676 * loop. The error message was given by get_tv_number_chk(). */
10677 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010678 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010679 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010680 retval = OK;
10681theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010683 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684}
10685
10686/*
10687 * "filter()" function
10688 */
10689 static void
10690f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693{
10694 filter_map(argvars, rettv, FALSE);
10695}
10696
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010698 * "finddir({fname}[, {path}[, {count}]])" function
10699 */
10700 static void
10701f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010702 typval_T *argvars;
10703 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010705 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706}
10707
10708/*
10709 * "findfile({fname}[, {path}[, {count}]])" function
10710 */
10711 static void
10712f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010713 typval_T *argvars;
10714 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010716 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717}
10718
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010719#ifdef FEAT_FLOAT
10720/*
10721 * "float2nr({float})" function
10722 */
10723 static void
10724f_float2nr(argvars, rettv)
10725 typval_T *argvars;
10726 typval_T *rettv;
10727{
10728 float_T f;
10729
10730 if (get_float_arg(argvars, &f) == OK)
10731 {
10732 if (f < -0x7fffffff)
10733 rettv->vval.v_number = -0x7fffffff;
10734 else if (f > 0x7fffffff)
10735 rettv->vval.v_number = 0x7fffffff;
10736 else
10737 rettv->vval.v_number = (varnumber_T)f;
10738 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010739}
10740
10741/*
10742 * "floor({float})" function
10743 */
10744 static void
10745f_floor(argvars, rettv)
10746 typval_T *argvars;
10747 typval_T *rettv;
10748{
10749 float_T f;
10750
10751 rettv->v_type = VAR_FLOAT;
10752 if (get_float_arg(argvars, &f) == OK)
10753 rettv->vval.v_float = floor(f);
10754 else
10755 rettv->vval.v_float = 0.0;
10756}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010757
10758/*
10759 * "fmod()" function
10760 */
10761 static void
10762f_fmod(argvars, rettv)
10763 typval_T *argvars;
10764 typval_T *rettv;
10765{
10766 float_T fx, fy;
10767
10768 rettv->v_type = VAR_FLOAT;
10769 if (get_float_arg(argvars, &fx) == OK
10770 && get_float_arg(&argvars[1], &fy) == OK)
10771 rettv->vval.v_float = fmod(fx, fy);
10772 else
10773 rettv->vval.v_float = 0.0;
10774}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010775#endif
10776
Bram Moolenaar0d660222005-01-07 21:51:51 +000010777/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010778 * "fnameescape({string})" function
10779 */
10780 static void
10781f_fnameescape(argvars, rettv)
10782 typval_T *argvars;
10783 typval_T *rettv;
10784{
10785 rettv->vval.v_string = vim_strsave_fnameescape(
10786 get_tv_string(&argvars[0]), FALSE);
10787 rettv->v_type = VAR_STRING;
10788}
10789
10790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 * "fnamemodify({fname}, {mods})" function
10792 */
10793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010795 typval_T *argvars;
10796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797{
10798 char_u *fname;
10799 char_u *mods;
10800 int usedlen = 0;
10801 int len;
10802 char_u *fbuf = NULL;
10803 char_u buf[NUMBUFLEN];
10804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 fname = get_tv_string_chk(&argvars[0]);
10806 mods = get_tv_string_buf_chk(&argvars[1], buf);
10807 if (fname == NULL || mods == NULL)
10808 fname = NULL;
10809 else
10810 {
10811 len = (int)STRLEN(fname);
10812 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 vim_free(fbuf);
10821}
10822
Bram Moolenaar33570922005-01-25 22:26:29 +000010823static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824
10825/*
10826 * "foldclosed()" function
10827 */
10828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010830 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010832 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834#ifdef FEAT_FOLDING
10835 linenr_T lnum;
10836 linenr_T first, last;
10837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10840 {
10841 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10842 {
10843 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 return;
10848 }
10849 }
10850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852}
10853
10854/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855 * "foldclosed()" function
10856 */
10857 static void
10858f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010859 typval_T *argvars;
10860 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010861{
10862 foldclosed_both(argvars, rettv, FALSE);
10863}
10864
10865/*
10866 * "foldclosedend()" function
10867 */
10868 static void
10869f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010870 typval_T *argvars;
10871 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010872{
10873 foldclosed_both(argvars, rettv, TRUE);
10874}
10875
10876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 * "foldlevel()" function
10878 */
10879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010881 typval_T *argvars UNUSED;
10882 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883{
10884#ifdef FEAT_FOLDING
10885 linenr_T lnum;
10886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
10893/*
10894 * "foldtext()" function
10895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010898 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#ifdef FEAT_FOLDING
10902 linenr_T lnum;
10903 char_u *s;
10904 char_u *r;
10905 int len;
10906 char *txt;
10907#endif
10908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010909 rettv->v_type = VAR_STRING;
10910 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010912 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10913 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10914 <= curbuf->b_ml.ml_line_count
10915 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 {
10917 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10919 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
10921 if (!linewhite(lnum))
10922 break;
10923 ++lnum;
10924 }
10925
10926 /* Find interesting text in this line. */
10927 s = skipwhite(ml_get(lnum));
10928 /* skip C comment-start */
10929 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010932 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010933 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010934 {
10935 s = skipwhite(ml_get(lnum + 1));
10936 if (*s == '*')
10937 s = skipwhite(s + 1);
10938 }
10939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 txt = _("+-%s%3ld lines: ");
10941 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010942 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 + 20 /* for %3ld */
10944 + STRLEN(s))); /* concatenated */
10945 if (r != NULL)
10946 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10948 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10949 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 len = (int)STRLEN(r);
10951 STRCAT(r, s);
10952 /* remove 'foldmarker' and 'commentstring' */
10953 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 }
10956 }
10957#endif
10958}
10959
10960/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010961 * "foldtextresult(lnum)" function
10962 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010966 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010967{
10968#ifdef FEAT_FOLDING
10969 linenr_T lnum;
10970 char_u *text;
10971 char_u buf[51];
10972 foldinfo_T foldinfo;
10973 int fold_count;
10974#endif
10975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->v_type = VAR_STRING;
10977 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010978#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 /* treat illegal types and illegal string values for {lnum} the same */
10981 if (lnum < 0)
10982 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010983 fold_count = foldedCount(curwin, lnum, &foldinfo);
10984 if (fold_count > 0)
10985 {
10986 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10987 &foldinfo, buf);
10988 if (text == buf)
10989 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010991 }
10992#endif
10993}
10994
10995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 * "foreground()" function
10997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011000 typval_T *argvars UNUSED;
11001 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003#ifdef FEAT_GUI
11004 if (gui.in_use)
11005 gui_mch_set_foreground();
11006#else
11007# ifdef WIN32
11008 win32_set_foreground();
11009# endif
11010#endif
11011}
11012
11013/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014 * "function()" function
11015 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011018 typval_T *argvars;
11019 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011020{
11021 char_u *s;
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011024 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011025 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011026 /* Don't check an autoload name for existence here. */
11027 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011028 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011029 else
11030 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011031 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011032 {
11033 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011034 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011035
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011036 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11037 * also be called from another script. Using trans_function_name()
11038 * would also work, but some plugins depend on the name being
11039 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011040 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011041 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011042 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011043 if (rettv->vval.v_string != NULL)
11044 {
11045 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011046 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011047 }
11048 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011049 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011050 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052 }
11053}
11054
11055/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011056 * "garbagecollect()" function
11057 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011058 static void
11059f_garbagecollect(argvars, rettv)
11060 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011061 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011062{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011063 /* This is postponed until we are back at the toplevel, because we may be
11064 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11065 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011066
11067 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11068 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011069}
11070
11071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011072 * "get()" function
11073 */
11074 static void
11075f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011076 typval_T *argvars;
11077 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011078{
Bram Moolenaar33570922005-01-25 22:26:29 +000011079 listitem_T *li;
11080 list_T *l;
11081 dictitem_T *di;
11082 dict_T *d;
11083 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011084
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011086 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011087 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011089 int error = FALSE;
11090
11091 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11092 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011095 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011096 else if (argvars[0].v_type == VAR_DICT)
11097 {
11098 if ((d = argvars[0].vval.v_dict) != NULL)
11099 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011100 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 if (di != NULL)
11102 tv = &di->di_tv;
11103 }
11104 }
11105 else
11106 EMSG2(_(e_listdictarg), "get()");
11107
11108 if (tv == NULL)
11109 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 copy_tv(&argvars[2], rettv);
11112 }
11113 else
11114 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011115}
11116
Bram Moolenaar342337a2005-07-21 21:11:17 +000011117static 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 +000011118
11119/*
11120 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011121 * Return a range (from start to end) of lines in rettv from the specified
11122 * buffer.
11123 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011124 */
11125 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011126get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011127 buf_T *buf;
11128 linenr_T start;
11129 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011130 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011131 typval_T *rettv;
11132{
11133 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011134
Bram Moolenaar959a1432013-12-14 12:17:38 +010011135 rettv->v_type = VAR_STRING;
11136 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011137 if (retlist && rettv_list_alloc(rettv) == FAIL)
11138 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011139
11140 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11141 return;
11142
11143 if (!retlist)
11144 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011145 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11146 p = ml_get_buf(buf, start, FALSE);
11147 else
11148 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149 rettv->vval.v_string = vim_strsave(p);
11150 }
11151 else
11152 {
11153 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011154 return;
11155
11156 if (start < 1)
11157 start = 1;
11158 if (end > buf->b_ml.ml_line_count)
11159 end = buf->b_ml.ml_line_count;
11160 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011161 if (list_append_string(rettv->vval.v_list,
11162 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011163 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011164 }
11165}
11166
11167/*
11168 * "getbufline()" function
11169 */
11170 static void
11171f_getbufline(argvars, rettv)
11172 typval_T *argvars;
11173 typval_T *rettv;
11174{
11175 linenr_T lnum;
11176 linenr_T end;
11177 buf_T *buf;
11178
11179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11180 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011181 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011182 --emsg_off;
11183
Bram Moolenaar661b1822005-07-28 22:36:45 +000011184 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011185 if (argvars[2].v_type == VAR_UNKNOWN)
11186 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011187 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011188 end = get_tv_lnum_buf(&argvars[2], buf);
11189
Bram Moolenaar342337a2005-07-21 21:11:17 +000011190 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011191}
11192
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193/*
11194 * "getbufvar()" function
11195 */
11196 static void
11197f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011198 typval_T *argvars;
11199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011200{
11201 buf_T *buf;
11202 buf_T *save_curbuf;
11203 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011204 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011205 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011207 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11208 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011209 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011210 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011212 rettv->v_type = VAR_STRING;
11213 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214
11215 if (buf != NULL && varname != NULL)
11216 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011217 /* set curbuf to be our buf, temporarily */
11218 save_curbuf = curbuf;
11219 curbuf = buf;
11220
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011222 {
11223 if (get_option_tv(&varname, rettv, TRUE) == OK)
11224 done = TRUE;
11225 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011226 else if (STRCMP(varname, "changedtick") == 0)
11227 {
11228 rettv->v_type = VAR_NUMBER;
11229 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011230 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011231 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 else
11233 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011234 /* Look up the variable. */
11235 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11236 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11237 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011239 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011241 done = TRUE;
11242 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011243 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011244
11245 /* restore previous notion of curbuf */
11246 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011247 }
11248
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011249 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11250 /* use the default value */
11251 copy_tv(&argvars[2], rettv);
11252
Bram Moolenaar0d660222005-01-07 21:51:51 +000011253 --emsg_off;
11254}
11255
11256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 * "getchar()" function
11258 */
11259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011260f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 typval_T *argvars;
11262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
11264 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011267 /* Position the cursor. Needed after a message that ends in a space. */
11268 windgoto(msg_row, msg_col);
11269
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 ++no_mapping;
11271 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011272 for (;;)
11273 {
11274 if (argvars[0].v_type == VAR_UNKNOWN)
11275 /* getchar(): blocking wait. */
11276 n = safe_vgetc();
11277 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11278 /* getchar(1): only check if char avail */
11279 n = vpeekc();
11280 else if (error || vpeekc() == NUL)
11281 /* illegal argument or getchar(0) and no char avail: return zero */
11282 n = 0;
11283 else
11284 /* getchar(0) and char avail: return char */
11285 n = safe_vgetc();
11286 if (n == K_IGNORE)
11287 continue;
11288 break;
11289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 --no_mapping;
11291 --allow_keys;
11292
Bram Moolenaar219b8702006-11-01 14:32:36 +000011293 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11294 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11295 vimvars[VV_MOUSE_COL].vv_nr = 0;
11296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 if (IS_SPECIAL(n) || mod_mask != 0)
11299 {
11300 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11301 int i = 0;
11302
11303 /* Turn a special key into three bytes, plus modifier. */
11304 if (mod_mask != 0)
11305 {
11306 temp[i++] = K_SPECIAL;
11307 temp[i++] = KS_MODIFIER;
11308 temp[i++] = mod_mask;
11309 }
11310 if (IS_SPECIAL(n))
11311 {
11312 temp[i++] = K_SPECIAL;
11313 temp[i++] = K_SECOND(n);
11314 temp[i++] = K_THIRD(n);
11315 }
11316#ifdef FEAT_MBYTE
11317 else if (has_mbyte)
11318 i += (*mb_char2bytes)(n, temp + i);
11319#endif
11320 else
11321 temp[i++] = n;
11322 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->v_type = VAR_STRING;
11324 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011325
11326#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011327 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011328 {
11329 int row = mouse_row;
11330 int col = mouse_col;
11331 win_T *win;
11332 linenr_T lnum;
11333# ifdef FEAT_WINDOWS
11334 win_T *wp;
11335# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011336 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011337
11338 if (row >= 0 && col >= 0)
11339 {
11340 /* Find the window at the mouse coordinates and compute the
11341 * text position. */
11342 win = mouse_find_win(&row, &col);
11343 (void)mouse_comp_pos(win, &row, &col, &lnum);
11344# ifdef FEAT_WINDOWS
11345 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011346 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011347# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011348 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011349 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11350 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11351 }
11352 }
11353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 }
11355}
11356
11357/*
11358 * "getcharmod()" function
11359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011362 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366}
11367
11368/*
11369 * "getcmdline()" function
11370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
11377 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378}
11379
11380/*
11381 * "getcmdpos()" function
11382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384f_getcmdpos(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 = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011392 * "getcmdtype()" function
11393 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011394 static void
11395f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011396 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011397 typval_T *rettv;
11398{
11399 rettv->v_type = VAR_STRING;
11400 rettv->vval.v_string = alloc(2);
11401 if (rettv->vval.v_string != NULL)
11402 {
11403 rettv->vval.v_string[0] = get_cmdline_type();
11404 rettv->vval.v_string[1] = NUL;
11405 }
11406}
11407
11408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 * "getcwd()" function
11410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011413 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011416 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011419 rettv->vval.v_string = NULL;
11420 cwd = alloc(MAXPATHL);
11421 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011423 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11424 {
11425 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011427 if (rettv->vval.v_string != NULL)
11428 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011430 }
11431 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433}
11434
11435/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011436 * "getfontname()" function
11437 */
11438 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011440 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011441 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443 rettv->v_type = VAR_STRING;
11444 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011445#ifdef FEAT_GUI
11446 if (gui.in_use)
11447 {
11448 GuiFont font;
11449 char_u *name = NULL;
11450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011452 {
11453 /* Get the "Normal" font. Either the name saved by
11454 * hl_set_font_name() or from the font ID. */
11455 font = gui.norm_font;
11456 name = hl_get_font_name();
11457 }
11458 else
11459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011461 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11462 return;
11463 font = gui_mch_get_font(name, FALSE);
11464 if (font == NOFONT)
11465 return; /* Invalid font name, return empty string. */
11466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011468 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011469 gui_mch_free_font(font);
11470 }
11471#endif
11472}
11473
11474/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011475 * "getfperm({fname})" function
11476 */
11477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011479 typval_T *argvars;
11480 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481{
11482 char_u *fname;
11483 struct stat st;
11484 char_u *perm = NULL;
11485 char_u flags[] = "rwx";
11486 int i;
11487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011490 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491 if (mch_stat((char *)fname, &st) >= 0)
11492 {
11493 perm = vim_strsave((char_u *)"---------");
11494 if (perm != NULL)
11495 {
11496 for (i = 0; i < 9; i++)
11497 {
11498 if (st.st_mode & (1 << (8 - i)))
11499 perm[i] = flags[i % 3];
11500 }
11501 }
11502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011503 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011504}
11505
11506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 * "getfsize({fname})" function
11508 */
11509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *argvars;
11512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
11514 char_u *fname;
11515 struct stat st;
11516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011517 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520
11521 if (mch_stat((char *)fname, &st) >= 0)
11522 {
11523 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011524 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011528
11529 /* non-perfect check for overflow */
11530 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11531 rettv->vval.v_number = -2;
11532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533 }
11534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536}
11537
11538/*
11539 * "getftime({fname})" function
11540 */
11541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 typval_T *argvars;
11544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011545{
11546 char_u *fname;
11547 struct stat st;
11548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550
11551 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011552 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555}
11556
11557/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011558 * "getftype({fname})" function
11559 */
11560 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011564{
11565 char_u *fname;
11566 struct stat st;
11567 char_u *type = NULL;
11568 char *t;
11569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011570 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011573 if (mch_lstat((char *)fname, &st) >= 0)
11574 {
11575#ifdef S_ISREG
11576 if (S_ISREG(st.st_mode))
11577 t = "file";
11578 else if (S_ISDIR(st.st_mode))
11579 t = "dir";
11580# ifdef S_ISLNK
11581 else if (S_ISLNK(st.st_mode))
11582 t = "link";
11583# endif
11584# ifdef S_ISBLK
11585 else if (S_ISBLK(st.st_mode))
11586 t = "bdev";
11587# endif
11588# ifdef S_ISCHR
11589 else if (S_ISCHR(st.st_mode))
11590 t = "cdev";
11591# endif
11592# ifdef S_ISFIFO
11593 else if (S_ISFIFO(st.st_mode))
11594 t = "fifo";
11595# endif
11596# ifdef S_ISSOCK
11597 else if (S_ISSOCK(st.st_mode))
11598 t = "fifo";
11599# endif
11600 else
11601 t = "other";
11602#else
11603# ifdef S_IFMT
11604 switch (st.st_mode & S_IFMT)
11605 {
11606 case S_IFREG: t = "file"; break;
11607 case S_IFDIR: t = "dir"; break;
11608# ifdef S_IFLNK
11609 case S_IFLNK: t = "link"; break;
11610# endif
11611# ifdef S_IFBLK
11612 case S_IFBLK: t = "bdev"; break;
11613# endif
11614# ifdef S_IFCHR
11615 case S_IFCHR: t = "cdev"; break;
11616# endif
11617# ifdef S_IFIFO
11618 case S_IFIFO: t = "fifo"; break;
11619# endif
11620# ifdef S_IFSOCK
11621 case S_IFSOCK: t = "socket"; break;
11622# endif
11623 default: t = "other";
11624 }
11625# else
11626 if (mch_isdir(fname))
11627 t = "dir";
11628 else
11629 t = "file";
11630# endif
11631#endif
11632 type = vim_strsave((char_u *)t);
11633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011635}
11636
11637/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011638 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011639 */
11640 static void
11641f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 typval_T *argvars;
11643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011644{
11645 linenr_T lnum;
11646 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011647 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011648
11649 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011650 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011651 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011652 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011653 retlist = FALSE;
11654 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011655 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011656 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011658 retlist = TRUE;
11659 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011660
Bram Moolenaar342337a2005-07-21 21:11:17 +000011661 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662}
11663
11664/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011665 * "getmatches()" function
11666 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011667 static void
11668f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011669 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011670 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011671{
11672#ifdef FEAT_SEARCH_EXTRA
11673 dict_T *dict;
11674 matchitem_T *cur = curwin->w_match_head;
11675
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011676 if (rettv_list_alloc(rettv) == OK)
11677 {
11678 while (cur != NULL)
11679 {
11680 dict = dict_alloc();
11681 if (dict == NULL)
11682 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011683 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11684 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11685 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11686 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11687 list_append_dict(rettv->vval.v_list, dict);
11688 cur = cur->next;
11689 }
11690 }
11691#endif
11692}
11693
11694/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011695 * "getpid()" function
11696 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011697 static void
11698f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011699 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011700 typval_T *rettv;
11701{
11702 rettv->vval.v_number = mch_get_pid();
11703}
11704
11705/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011706 * "getpos(string)" function
11707 */
11708 static void
11709f_getpos(argvars, rettv)
11710 typval_T *argvars;
11711 typval_T *rettv;
11712{
11713 pos_T *fp;
11714 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011715 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011716
11717 if (rettv_list_alloc(rettv) == OK)
11718 {
11719 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011720 fp = var2fpos(&argvars[0], TRUE, &fnum);
11721 if (fnum != -1)
11722 list_append_number(l, (varnumber_T)fnum);
11723 else
11724 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011725 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11726 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011727 list_append_number(l, (fp != NULL)
11728 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011729 : (varnumber_T)0);
11730 list_append_number(l,
11731#ifdef FEAT_VIRTUALEDIT
11732 (fp != NULL) ? (varnumber_T)fp->coladd :
11733#endif
11734 (varnumber_T)0);
11735 }
11736 else
11737 rettv->vval.v_number = FALSE;
11738}
11739
11740/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011741 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011742 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011743 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011744f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011745 typval_T *argvars UNUSED;
11746 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011747{
11748#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011749 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011750#endif
11751
Bram Moolenaar2641f772005-03-25 21:58:17 +000011752#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011753 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011754 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011755 wp = NULL;
11756 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11757 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011758 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011759 if (wp == NULL)
11760 return;
11761 }
11762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011763 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011764 }
11765#endif
11766}
11767
11768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769 * "getreg()" function
11770 */
11771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011772f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011773 typval_T *argvars;
11774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775{
11776 char_u *strregname;
11777 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011778 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011781 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 strregname = get_tv_string_chk(&argvars[0]);
11784 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011789 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790 regname = (strregname == NULL ? '"' : *strregname);
11791 if (regname == 0)
11792 regname = '"';
11793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 rettv->vval.v_string = error ? NULL :
11796 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797}
11798
11799/*
11800 * "getregtype()" function
11801 */
11802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011803f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011804 typval_T *argvars;
11805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806{
11807 char_u *strregname;
11808 int regname;
11809 char_u buf[NUMBUFLEN + 2];
11810 long reglen = 0;
11811
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 {
11814 strregname = get_tv_string_chk(&argvars[0]);
11815 if (strregname == NULL) /* type error; errmsg already given */
11816 {
11817 rettv->v_type = VAR_STRING;
11818 rettv->vval.v_string = NULL;
11819 return;
11820 }
11821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822 else
11823 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011824 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825
11826 regname = (strregname == NULL ? '"' : *strregname);
11827 if (regname == 0)
11828 regname = '"';
11829
11830 buf[0] = NUL;
11831 buf[1] = NUL;
11832 switch (get_reg_type(regname, &reglen))
11833 {
11834 case MLINE: buf[0] = 'V'; break;
11835 case MCHAR: buf[0] = 'v'; break;
11836#ifdef FEAT_VISUAL
11837 case MBLOCK:
11838 buf[0] = Ctrl_V;
11839 sprintf((char *)buf + 1, "%ld", reglen + 1);
11840 break;
11841#endif
11842 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011843 rettv->v_type = VAR_STRING;
11844 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845}
11846
11847/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011848 * "gettabvar()" function
11849 */
11850 static void
11851f_gettabvar(argvars, rettv)
11852 typval_T *argvars;
11853 typval_T *rettv;
11854{
11855 tabpage_T *tp;
11856 dictitem_T *v;
11857 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011858 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011859
11860 rettv->v_type = VAR_STRING;
11861 rettv->vval.v_string = NULL;
11862
11863 varname = get_tv_string_chk(&argvars[1]);
11864 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11865 if (tp != NULL && varname != NULL)
11866 {
11867 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011868 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011869 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011870 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011871 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011872 done = TRUE;
11873 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011874 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011875
11876 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011877 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011878}
11879
11880/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881 * "gettabwinvar()" function
11882 */
11883 static void
11884f_gettabwinvar(argvars, rettv)
11885 typval_T *argvars;
11886 typval_T *rettv;
11887{
11888 getwinvar(argvars, rettv, 1);
11889}
11890
11891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 * "getwinposx()" function
11893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900#ifdef FEAT_GUI
11901 if (gui.in_use)
11902 {
11903 int x, y;
11904
11905 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011906 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 }
11908#endif
11909}
11910
11911/*
11912 * "getwinposy()" function
11913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011916 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011919 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920#ifdef FEAT_GUI
11921 if (gui.in_use)
11922 {
11923 int x, y;
11924
11925 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 }
11928#endif
11929}
11930
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011931/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011932 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011933 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011934 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011935find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011936 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011937 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011938{
11939#ifdef FEAT_WINDOWS
11940 win_T *wp;
11941#endif
11942 int nr;
11943
11944 nr = get_tv_number_chk(vp, NULL);
11945
11946#ifdef FEAT_WINDOWS
11947 if (nr < 0)
11948 return NULL;
11949 if (nr == 0)
11950 return curwin;
11951
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011952 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11953 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011954 if (--nr <= 0)
11955 break;
11956 return wp;
11957#else
11958 if (nr == 0 || nr == 1)
11959 return curwin;
11960 return NULL;
11961#endif
11962}
11963
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964/*
11965 * "getwinvar()" function
11966 */
11967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *argvars;
11970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011972 getwinvar(argvars, rettv, 0);
11973}
11974
11975/*
11976 * getwinvar() and gettabwinvar()
11977 */
11978 static void
11979getwinvar(argvars, rettv, off)
11980 typval_T *argvars;
11981 typval_T *rettv;
11982 int off; /* 1 for gettabwinvar() */
11983{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 win_T *win, *oldcurwin;
11985 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011986 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011987 tabpage_T *tp = NULL;
11988 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011989 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011991#ifdef FEAT_WINDOWS
11992 if (off == 1)
11993 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11994 else
11995 tp = curtab;
11996#endif
11997 win = find_win_by_nr(&argvars[off], tp);
11998 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011999 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012001 rettv->v_type = VAR_STRING;
12002 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003
12004 if (win != NULL && varname != NULL)
12005 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012006 /* Set curwin to be our win, temporarily. Also set the tabpage,
12007 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012008 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012009
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012011 {
12012 if (get_option_tv(&varname, rettv, 1) == OK)
12013 done = TRUE;
12014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 else
12016 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012017 /* Look up the variable. */
12018 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12019 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012022 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012023 done = TRUE;
12024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012026
12027 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012028 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029 }
12030
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012031 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12032 /* use the default return value */
12033 copy_tv(&argvars[off + 2], rettv);
12034
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 --emsg_off;
12036}
12037
12038/*
12039 * "glob()" function
12040 */
12041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012043 typval_T *argvars;
12044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012046 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012048 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012050 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012051 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012053 if (argvars[1].v_type != VAR_UNKNOWN)
12054 {
12055 if (get_tv_number_chk(&argvars[1], &error))
12056 options |= WILD_KEEP_ALL;
12057 if (argvars[2].v_type != VAR_UNKNOWN
12058 && get_tv_number_chk(&argvars[2], &error))
12059 {
12060 rettv->v_type = VAR_LIST;
12061 rettv->vval.v_list = NULL;
12062 }
12063 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012064 if (!error)
12065 {
12066 ExpandInit(&xpc);
12067 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012068 if (p_wic)
12069 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012070 if (rettv->v_type == VAR_STRING)
12071 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012072 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012073 else if (rettv_list_alloc(rettv) != FAIL)
12074 {
12075 int i;
12076
12077 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12078 NULL, options, WILD_ALL_KEEP);
12079 for (i = 0; i < xpc.xp_numfiles; i++)
12080 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12081
12082 ExpandCleanup(&xpc);
12083 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012084 }
12085 else
12086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087}
12088
12089/*
12090 * "globpath()" function
12091 */
12092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012093f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012094 typval_T *argvars;
12095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012097 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012099 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012100 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012102 /* When the optional second argument is non-zero, don't remove matches
12103 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12104 if (argvars[2].v_type != VAR_UNKNOWN
12105 && get_tv_number_chk(&argvars[2], &error))
12106 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012108 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012109 rettv->vval.v_string = NULL;
12110 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012111 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12112 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012113}
12114
12115/*
12116 * "has()" function
12117 */
12118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012120 typval_T *argvars;
12121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122{
12123 int i;
12124 char_u *name;
12125 int n = FALSE;
12126 static char *(has_list[]) =
12127 {
12128#ifdef AMIGA
12129 "amiga",
12130# ifdef FEAT_ARP
12131 "arp",
12132# endif
12133#endif
12134#ifdef __BEOS__
12135 "beos",
12136#endif
12137#ifdef MSDOS
12138# ifdef DJGPP
12139 "dos32",
12140# else
12141 "dos16",
12142# endif
12143#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012144#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 "mac",
12146#endif
12147#if defined(MACOS_X_UNIX)
12148 "macunix",
12149#endif
12150#ifdef OS2
12151 "os2",
12152#endif
12153#ifdef __QNX__
12154 "qnx",
12155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156#ifdef UNIX
12157 "unix",
12158#endif
12159#ifdef VMS
12160 "vms",
12161#endif
12162#ifdef WIN16
12163 "win16",
12164#endif
12165#ifdef WIN32
12166 "win32",
12167#endif
12168#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12169 "win32unix",
12170#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012171#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 "win64",
12173#endif
12174#ifdef EBCDIC
12175 "ebcdic",
12176#endif
12177#ifndef CASE_INSENSITIVE_FILENAME
12178 "fname_case",
12179#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012180#ifdef HAVE_ACL
12181 "acl",
12182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183#ifdef FEAT_ARABIC
12184 "arabic",
12185#endif
12186#ifdef FEAT_AUTOCMD
12187 "autocmd",
12188#endif
12189#ifdef FEAT_BEVAL
12190 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012191# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12192 "balloon_multiline",
12193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#endif
12195#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12196 "builtin_terms",
12197# ifdef ALL_BUILTIN_TCAPS
12198 "all_builtin_terms",
12199# endif
12200#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012201#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12202 || defined(FEAT_GUI_W32) \
12203 || defined(FEAT_GUI_MOTIF))
12204 "browsefilter",
12205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206#ifdef FEAT_BYTEOFF
12207 "byte_offset",
12208#endif
12209#ifdef FEAT_CINDENT
12210 "cindent",
12211#endif
12212#ifdef FEAT_CLIENTSERVER
12213 "clientserver",
12214#endif
12215#ifdef FEAT_CLIPBOARD
12216 "clipboard",
12217#endif
12218#ifdef FEAT_CMDL_COMPL
12219 "cmdline_compl",
12220#endif
12221#ifdef FEAT_CMDHIST
12222 "cmdline_hist",
12223#endif
12224#ifdef FEAT_COMMENTS
12225 "comments",
12226#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012227#ifdef FEAT_CONCEAL
12228 "conceal",
12229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230#ifdef FEAT_CRYPT
12231 "cryptv",
12232#endif
12233#ifdef FEAT_CSCOPE
12234 "cscope",
12235#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012236#ifdef FEAT_CURSORBIND
12237 "cursorbind",
12238#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012239#ifdef CURSOR_SHAPE
12240 "cursorshape",
12241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242#ifdef DEBUG
12243 "debug",
12244#endif
12245#ifdef FEAT_CON_DIALOG
12246 "dialog_con",
12247#endif
12248#ifdef FEAT_GUI_DIALOG
12249 "dialog_gui",
12250#endif
12251#ifdef FEAT_DIFF
12252 "diff",
12253#endif
12254#ifdef FEAT_DIGRAPHS
12255 "digraphs",
12256#endif
12257#ifdef FEAT_DND
12258 "dnd",
12259#endif
12260#ifdef FEAT_EMACS_TAGS
12261 "emacs_tags",
12262#endif
12263 "eval", /* always present, of course! */
12264#ifdef FEAT_EX_EXTRA
12265 "ex_extra",
12266#endif
12267#ifdef FEAT_SEARCH_EXTRA
12268 "extra_search",
12269#endif
12270#ifdef FEAT_FKMAP
12271 "farsi",
12272#endif
12273#ifdef FEAT_SEARCHPATH
12274 "file_in_path",
12275#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012276#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012277 "filterpipe",
12278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279#ifdef FEAT_FIND_ID
12280 "find_in_path",
12281#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012282#ifdef FEAT_FLOAT
12283 "float",
12284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285#ifdef FEAT_FOLDING
12286 "folding",
12287#endif
12288#ifdef FEAT_FOOTER
12289 "footer",
12290#endif
12291#if !defined(USE_SYSTEM) && defined(UNIX)
12292 "fork",
12293#endif
12294#ifdef FEAT_GETTEXT
12295 "gettext",
12296#endif
12297#ifdef FEAT_GUI
12298 "gui",
12299#endif
12300#ifdef FEAT_GUI_ATHENA
12301# ifdef FEAT_GUI_NEXTAW
12302 "gui_neXtaw",
12303# else
12304 "gui_athena",
12305# endif
12306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307#ifdef FEAT_GUI_GTK
12308 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012311#ifdef FEAT_GUI_GNOME
12312 "gui_gnome",
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#ifdef FEAT_GUI_MAC
12315 "gui_mac",
12316#endif
12317#ifdef FEAT_GUI_MOTIF
12318 "gui_motif",
12319#endif
12320#ifdef FEAT_GUI_PHOTON
12321 "gui_photon",
12322#endif
12323#ifdef FEAT_GUI_W16
12324 "gui_win16",
12325#endif
12326#ifdef FEAT_GUI_W32
12327 "gui_win32",
12328#endif
12329#ifdef FEAT_HANGULIN
12330 "hangul_input",
12331#endif
12332#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12333 "iconv",
12334#endif
12335#ifdef FEAT_INS_EXPAND
12336 "insert_expand",
12337#endif
12338#ifdef FEAT_JUMPLIST
12339 "jumplist",
12340#endif
12341#ifdef FEAT_KEYMAP
12342 "keymap",
12343#endif
12344#ifdef FEAT_LANGMAP
12345 "langmap",
12346#endif
12347#ifdef FEAT_LIBCALL
12348 "libcall",
12349#endif
12350#ifdef FEAT_LINEBREAK
12351 "linebreak",
12352#endif
12353#ifdef FEAT_LISP
12354 "lispindent",
12355#endif
12356#ifdef FEAT_LISTCMDS
12357 "listcmds",
12358#endif
12359#ifdef FEAT_LOCALMAP
12360 "localmap",
12361#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012362#ifdef FEAT_LUA
12363# ifndef DYNAMIC_LUA
12364 "lua",
12365# endif
12366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367#ifdef FEAT_MENU
12368 "menu",
12369#endif
12370#ifdef FEAT_SESSION
12371 "mksession",
12372#endif
12373#ifdef FEAT_MODIFY_FNAME
12374 "modify_fname",
12375#endif
12376#ifdef FEAT_MOUSE
12377 "mouse",
12378#endif
12379#ifdef FEAT_MOUSESHAPE
12380 "mouseshape",
12381#endif
12382#if defined(UNIX) || defined(VMS)
12383# ifdef FEAT_MOUSE_DEC
12384 "mouse_dec",
12385# endif
12386# ifdef FEAT_MOUSE_GPM
12387 "mouse_gpm",
12388# endif
12389# ifdef FEAT_MOUSE_JSB
12390 "mouse_jsbterm",
12391# endif
12392# ifdef FEAT_MOUSE_NET
12393 "mouse_netterm",
12394# endif
12395# ifdef FEAT_MOUSE_PTERM
12396 "mouse_pterm",
12397# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012398# ifdef FEAT_MOUSE_SGR
12399 "mouse_sgr",
12400# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012401# ifdef FEAT_SYSMOUSE
12402 "mouse_sysmouse",
12403# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012404# ifdef FEAT_MOUSE_URXVT
12405 "mouse_urxvt",
12406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407# ifdef FEAT_MOUSE_XTERM
12408 "mouse_xterm",
12409# endif
12410#endif
12411#ifdef FEAT_MBYTE
12412 "multi_byte",
12413#endif
12414#ifdef FEAT_MBYTE_IME
12415 "multi_byte_ime",
12416#endif
12417#ifdef FEAT_MULTI_LANG
12418 "multi_lang",
12419#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012420#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012421#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012422 "mzscheme",
12423#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425#ifdef FEAT_OLE
12426 "ole",
12427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428#ifdef FEAT_PATH_EXTRA
12429 "path_extra",
12430#endif
12431#ifdef FEAT_PERL
12432#ifndef DYNAMIC_PERL
12433 "perl",
12434#endif
12435#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012436#ifdef FEAT_PERSISTENT_UNDO
12437 "persistent_undo",
12438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#ifdef FEAT_PYTHON
12440#ifndef DYNAMIC_PYTHON
12441 "python",
12442#endif
12443#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012444#ifdef FEAT_PYTHON3
12445#ifndef DYNAMIC_PYTHON3
12446 "python3",
12447#endif
12448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449#ifdef FEAT_POSTSCRIPT
12450 "postscript",
12451#endif
12452#ifdef FEAT_PRINTER
12453 "printer",
12454#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012455#ifdef FEAT_PROFILE
12456 "profile",
12457#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012458#ifdef FEAT_RELTIME
12459 "reltime",
12460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461#ifdef FEAT_QUICKFIX
12462 "quickfix",
12463#endif
12464#ifdef FEAT_RIGHTLEFT
12465 "rightleft",
12466#endif
12467#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12468 "ruby",
12469#endif
12470#ifdef FEAT_SCROLLBIND
12471 "scrollbind",
12472#endif
12473#ifdef FEAT_CMDL_INFO
12474 "showcmd",
12475 "cmdline_info",
12476#endif
12477#ifdef FEAT_SIGNS
12478 "signs",
12479#endif
12480#ifdef FEAT_SMARTINDENT
12481 "smartindent",
12482#endif
12483#ifdef FEAT_SNIFF
12484 "sniff",
12485#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012486#ifdef STARTUPTIME
12487 "startuptime",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_STL_OPT
12490 "statusline",
12491#endif
12492#ifdef FEAT_SUN_WORKSHOP
12493 "sun_workshop",
12494#endif
12495#ifdef FEAT_NETBEANS_INTG
12496 "netbeans_intg",
12497#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012498#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012499 "spell",
12500#endif
12501#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502 "syntax",
12503#endif
12504#if defined(USE_SYSTEM) || !defined(UNIX)
12505 "system",
12506#endif
12507#ifdef FEAT_TAG_BINS
12508 "tag_binary",
12509#endif
12510#ifdef FEAT_TAG_OLDSTATIC
12511 "tag_old_static",
12512#endif
12513#ifdef FEAT_TAG_ANYWHITE
12514 "tag_any_white",
12515#endif
12516#ifdef FEAT_TCL
12517# ifndef DYNAMIC_TCL
12518 "tcl",
12519# endif
12520#endif
12521#ifdef TERMINFO
12522 "terminfo",
12523#endif
12524#ifdef FEAT_TERMRESPONSE
12525 "termresponse",
12526#endif
12527#ifdef FEAT_TEXTOBJ
12528 "textobjects",
12529#endif
12530#ifdef HAVE_TGETENT
12531 "tgetent",
12532#endif
12533#ifdef FEAT_TITLE
12534 "title",
12535#endif
12536#ifdef FEAT_TOOLBAR
12537 "toolbar",
12538#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012539#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12540 "unnamedplus",
12541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542#ifdef FEAT_USR_CMDS
12543 "user-commands", /* was accidentally included in 5.4 */
12544 "user_commands",
12545#endif
12546#ifdef FEAT_VIMINFO
12547 "viminfo",
12548#endif
12549#ifdef FEAT_VERTSPLIT
12550 "vertsplit",
12551#endif
12552#ifdef FEAT_VIRTUALEDIT
12553 "virtualedit",
12554#endif
12555#ifdef FEAT_VISUAL
12556 "visual",
12557#endif
12558#ifdef FEAT_VISUALEXTRA
12559 "visualextra",
12560#endif
12561#ifdef FEAT_VREPLACE
12562 "vreplace",
12563#endif
12564#ifdef FEAT_WILDIGN
12565 "wildignore",
12566#endif
12567#ifdef FEAT_WILDMENU
12568 "wildmenu",
12569#endif
12570#ifdef FEAT_WINDOWS
12571 "windows",
12572#endif
12573#ifdef FEAT_WAK
12574 "winaltkeys",
12575#endif
12576#ifdef FEAT_WRITEBACKUP
12577 "writebackup",
12578#endif
12579#ifdef FEAT_XIM
12580 "xim",
12581#endif
12582#ifdef FEAT_XFONTSET
12583 "xfontset",
12584#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012585#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012586 "xpm",
12587 "xpm_w32", /* for backward compatibility */
12588#else
12589# if defined(HAVE_XPM)
12590 "xpm",
12591# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593#ifdef USE_XSMP
12594 "xsmp",
12595#endif
12596#ifdef USE_XSMP_INTERACT
12597 "xsmp_interact",
12598#endif
12599#ifdef FEAT_XCLIPBOARD
12600 "xterm_clipboard",
12601#endif
12602#ifdef FEAT_XTERM_SAVE
12603 "xterm_save",
12604#endif
12605#if defined(UNIX) && defined(FEAT_X11)
12606 "X11",
12607#endif
12608 NULL
12609 };
12610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012611 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 for (i = 0; has_list[i] != NULL; ++i)
12613 if (STRICMP(name, has_list[i]) == 0)
12614 {
12615 n = TRUE;
12616 break;
12617 }
12618
12619 if (n == FALSE)
12620 {
12621 if (STRNICMP(name, "patch", 5) == 0)
12622 n = has_patch(atoi((char *)name + 5));
12623 else if (STRICMP(name, "vim_starting") == 0)
12624 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012625#ifdef FEAT_MBYTE
12626 else if (STRICMP(name, "multi_byte_encoding") == 0)
12627 n = has_mbyte;
12628#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012629#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12630 else if (STRICMP(name, "balloon_multiline") == 0)
12631 n = multiline_balloon_available();
12632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633#ifdef DYNAMIC_TCL
12634 else if (STRICMP(name, "tcl") == 0)
12635 n = tcl_enabled(FALSE);
12636#endif
12637#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12638 else if (STRICMP(name, "iconv") == 0)
12639 n = iconv_enabled(FALSE);
12640#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012641#ifdef DYNAMIC_LUA
12642 else if (STRICMP(name, "lua") == 0)
12643 n = lua_enabled(FALSE);
12644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012645#ifdef DYNAMIC_MZSCHEME
12646 else if (STRICMP(name, "mzscheme") == 0)
12647 n = mzscheme_enabled(FALSE);
12648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649#ifdef DYNAMIC_RUBY
12650 else if (STRICMP(name, "ruby") == 0)
12651 n = ruby_enabled(FALSE);
12652#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012653#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654#ifdef DYNAMIC_PYTHON
12655 else if (STRICMP(name, "python") == 0)
12656 n = python_enabled(FALSE);
12657#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012658#endif
12659#ifdef FEAT_PYTHON3
12660#ifdef DYNAMIC_PYTHON3
12661 else if (STRICMP(name, "python3") == 0)
12662 n = python3_enabled(FALSE);
12663#endif
12664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665#ifdef DYNAMIC_PERL
12666 else if (STRICMP(name, "perl") == 0)
12667 n = perl_enabled(FALSE);
12668#endif
12669#ifdef FEAT_GUI
12670 else if (STRICMP(name, "gui_running") == 0)
12671 n = (gui.in_use || gui.starting);
12672# ifdef FEAT_GUI_W32
12673 else if (STRICMP(name, "gui_win32s") == 0)
12674 n = gui_is_win32s();
12675# endif
12676# ifdef FEAT_BROWSE
12677 else if (STRICMP(name, "browse") == 0)
12678 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12679# endif
12680#endif
12681#ifdef FEAT_SYN_HL
12682 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012683 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684#endif
12685#if defined(WIN3264)
12686 else if (STRICMP(name, "win95") == 0)
12687 n = mch_windows95();
12688#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012689#ifdef FEAT_NETBEANS_INTG
12690 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012691 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 }
12694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696}
12697
12698/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 * "has_key()" function
12700 */
12701 static void
12702f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012703 typval_T *argvars;
12704 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012705{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012706 if (argvars[0].v_type != VAR_DICT)
12707 {
12708 EMSG(_(e_dictreq));
12709 return;
12710 }
12711 if (argvars[0].vval.v_dict == NULL)
12712 return;
12713
12714 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012715 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012716}
12717
12718/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012719 * "haslocaldir()" function
12720 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012721 static void
12722f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012723 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012724 typval_T *rettv;
12725{
12726 rettv->vval.v_number = (curwin->w_localdir != NULL);
12727}
12728
12729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 * "hasmapto()" function
12731 */
12732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012734 typval_T *argvars;
12735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736{
12737 char_u *name;
12738 char_u *mode;
12739 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012740 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 mode = (char_u *)"nvo";
12745 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012748 if (argvars[2].v_type != VAR_UNKNOWN)
12749 abbr = get_tv_number(&argvars[2]);
12750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751
Bram Moolenaar2c932302006-03-18 21:42:09 +000012752 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756}
12757
12758/*
12759 * "histadd()" function
12760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766#ifdef FEAT_CMDHIST
12767 int histype;
12768 char_u *str;
12769 char_u buf[NUMBUFLEN];
12770#endif
12771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 if (check_restricted() || check_secure())
12774 return;
12775#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12777 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778 if (histype >= 0)
12779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 if (*str != NUL)
12782 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012783 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 return;
12787 }
12788 }
12789#endif
12790}
12791
12792/*
12793 * "histdel()" function
12794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012797 typval_T *argvars UNUSED;
12798 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799{
12800#ifdef FEAT_CMDHIST
12801 int n;
12802 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012803 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012805 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12806 if (str == NULL)
12807 n = 0;
12808 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012810 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012811 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012813 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 else
12816 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012817 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 get_tv_string_buf(&argvars[1], buf));
12819 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820#endif
12821}
12822
12823/*
12824 * "histget()" function
12825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830{
12831#ifdef FEAT_CMDHIST
12832 int type;
12833 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012834 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012836 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12837 if (str == NULL)
12838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012840 {
12841 type = get_histtype(str);
12842 if (argvars[1].v_type == VAR_UNKNOWN)
12843 idx = get_history_idx(type);
12844 else
12845 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12846 /* -1 on type error */
12847 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853}
12854
12855/*
12856 * "histnr()" function
12857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012860 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
12863 int i;
12864
12865#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 char_u *history = get_tv_string_chk(&argvars[0]);
12867
12868 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 if (i >= HIST_CMD && i < HIST_COUNT)
12870 i = get_history_idx(i);
12871 else
12872#endif
12873 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875}
12876
12877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 * "highlightID(name)" function
12879 */
12880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012882 typval_T *argvars;
12883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886}
12887
12888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012889 * "highlight_exists()" function
12890 */
12891 static void
12892f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012893 typval_T *argvars;
12894 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012895{
12896 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12897}
12898
12899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 * "hostname()" function
12901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906{
12907 char_u hostname[256];
12908
12909 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 rettv->v_type = VAR_STRING;
12911 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912}
12913
12914/*
12915 * iconv() function
12916 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921{
12922#ifdef FEAT_MBYTE
12923 char_u buf1[NUMBUFLEN];
12924 char_u buf2[NUMBUFLEN];
12925 char_u *from, *to, *str;
12926 vimconv_T vimconv;
12927#endif
12928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->v_type = VAR_STRING;
12930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931
12932#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 str = get_tv_string(&argvars[0]);
12934 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12935 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 vimconv.vc_type = CONV_NONE;
12937 convert_setup(&vimconv, from, to);
12938
12939 /* If the encodings are equal, no conversion needed. */
12940 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
12945 convert_setup(&vimconv, NULL, NULL);
12946 vim_free(from);
12947 vim_free(to);
12948#endif
12949}
12950
12951/*
12952 * "indent()" function
12953 */
12954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 typval_T *argvars;
12957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958{
12959 linenr_T lnum;
12960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012961 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012963 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966}
12967
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012968/*
12969 * "index()" function
12970 */
12971 static void
12972f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012973 typval_T *argvars;
12974 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012975{
Bram Moolenaar33570922005-01-25 22:26:29 +000012976 list_T *l;
12977 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012978 long idx = 0;
12979 int ic = FALSE;
12980
12981 rettv->vval.v_number = -1;
12982 if (argvars[0].v_type != VAR_LIST)
12983 {
12984 EMSG(_(e_listreq));
12985 return;
12986 }
12987 l = argvars[0].vval.v_list;
12988 if (l != NULL)
12989 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012990 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012992 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012993 int error = FALSE;
12994
Bram Moolenaar758711c2005-02-02 23:11:38 +000012995 /* Start at specified item. Use the cached index that list_find()
12996 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012997 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012998 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012999 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013000 ic = get_tv_number_chk(&argvars[3], &error);
13001 if (error)
13002 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013003 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013004
Bram Moolenaar758711c2005-02-02 23:11:38 +000013005 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013006 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013007 {
13008 rettv->vval.v_number = idx;
13009 break;
13010 }
13011 }
13012}
13013
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014static int inputsecret_flag = 0;
13015
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013016static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13017
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013019 * This function is used by f_input() and f_inputdialog() functions. The third
13020 * argument to f_input() specifies the type of completion to use at the
13021 * prompt. The third argument to f_inputdialog() specifies the value to return
13022 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 */
13024 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013025get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013026 typval_T *argvars;
13027 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013028 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013030 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 char_u *p = NULL;
13032 int c;
13033 char_u buf[NUMBUFLEN];
13034 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013035 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013036 int xp_type = EXPAND_NOTHING;
13037 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013040 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
13042#ifdef NO_CONSOLE_INPUT
13043 /* While starting up, there is no place to enter text. */
13044 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046#endif
13047
13048 cmd_silent = FALSE; /* Want to see the prompt. */
13049 if (prompt != NULL)
13050 {
13051 /* Only the part of the message after the last NL is considered as
13052 * prompt for the command line */
13053 p = vim_strrchr(prompt, '\n');
13054 if (p == NULL)
13055 p = prompt;
13056 else
13057 {
13058 ++p;
13059 c = *p;
13060 *p = NUL;
13061 msg_start();
13062 msg_clr_eos();
13063 msg_puts_attr(prompt, echo_attr);
13064 msg_didout = FALSE;
13065 msg_starthere();
13066 *p = c;
13067 }
13068 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070 if (argvars[1].v_type != VAR_UNKNOWN)
13071 {
13072 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13073 if (defstr != NULL)
13074 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013076 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013077 {
13078 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013079 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013080 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013081
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013082 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013083 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013084
Bram Moolenaar4463f292005-09-25 22:20:24 +000013085 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13086 if (xp_name == NULL)
13087 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013089 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013090
Bram Moolenaar4463f292005-09-25 22:20:24 +000013091 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13092 &xp_arg) == FAIL)
13093 return;
13094 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013095 }
13096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013098 {
13099# ifdef FEAT_EX_EXTRA
13100 int save_ex_normal_busy = ex_normal_busy;
13101 ex_normal_busy = 0;
13102# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013104 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13105 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013106# ifdef FEAT_EX_EXTRA
13107 ex_normal_busy = save_ex_normal_busy;
13108# endif
13109 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013110 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013111 && argvars[1].v_type != VAR_UNKNOWN
13112 && argvars[2].v_type != VAR_UNKNOWN)
13113 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13114 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013115
13116 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 /* since the user typed this, no need to wait for return */
13119 need_wait_return = FALSE;
13120 msg_didout = FALSE;
13121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 cmd_silent = cmd_silent_save;
13123}
13124
13125/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013126 * "input()" function
13127 * Also handles inputsecret() when inputsecret is set.
13128 */
13129 static void
13130f_input(argvars, rettv)
13131 typval_T *argvars;
13132 typval_T *rettv;
13133{
13134 get_user_input(argvars, rettv, FALSE);
13135}
13136
13137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 * "inputdialog()" function
13139 */
13140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013142 typval_T *argvars;
13143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144{
13145#if defined(FEAT_GUI_TEXTDIALOG)
13146 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13147 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13148 {
13149 char_u *message;
13150 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013151 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 message = get_tv_string_chk(&argvars[0]);
13154 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013155 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013156 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 else
13158 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 if (message != NULL && defstr != NULL
13160 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013161 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 else
13164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013165 if (message != NULL && defstr != NULL
13166 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013167 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 rettv->vval.v_string = vim_strsave(
13169 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 }
13175 else
13176#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013177 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178}
13179
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013180/*
13181 * "inputlist()" function
13182 */
13183 static void
13184f_inputlist(argvars, rettv)
13185 typval_T *argvars;
13186 typval_T *rettv;
13187{
13188 listitem_T *li;
13189 int selected;
13190 int mouse_used;
13191
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013192#ifdef NO_CONSOLE_INPUT
13193 /* While starting up, there is no place to enter text. */
13194 if (no_console_input())
13195 return;
13196#endif
13197 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13198 {
13199 EMSG2(_(e_listarg), "inputlist()");
13200 return;
13201 }
13202
13203 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013204 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013205 lines_left = Rows; /* avoid more prompt */
13206 msg_scroll = TRUE;
13207 msg_clr_eos();
13208
13209 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13210 {
13211 msg_puts(get_tv_string(&li->li_tv));
13212 msg_putchar('\n');
13213 }
13214
13215 /* Ask for choice. */
13216 selected = prompt_for_number(&mouse_used);
13217 if (mouse_used)
13218 selected -= lines_left;
13219
13220 rettv->vval.v_number = selected;
13221}
13222
13223
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13225
13226/*
13227 * "inputrestore()" function
13228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013231 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013232 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233{
13234 if (ga_userinput.ga_len > 0)
13235 {
13236 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13238 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013239 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 }
13241 else if (p_verbose > 1)
13242 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013243 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 }
13246}
13247
13248/*
13249 * "inputsave()" function
13250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013256 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257 if (ga_grow(&ga_userinput, 1) == OK)
13258 {
13259 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13260 + ga_userinput.ga_len);
13261 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013262 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 }
13264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266}
13267
13268/*
13269 * "inputsecret()" function
13270 */
13271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275{
13276 ++cmdline_star;
13277 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 --cmdline_star;
13280 --inputsecret_flag;
13281}
13282
13283/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013284 * "insert()" function
13285 */
13286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013288 typval_T *argvars;
13289 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013290{
13291 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013292 listitem_T *item;
13293 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013294 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013295
13296 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013297 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013298 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013299 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013300 {
13301 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 before = get_tv_number_chk(&argvars[2], &error);
13303 if (error)
13304 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013305
Bram Moolenaar758711c2005-02-02 23:11:38 +000013306 if (before == l->lv_len)
13307 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 else
13309 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013310 item = list_find(l, before);
13311 if (item == NULL)
13312 {
13313 EMSGN(_(e_listidx), before);
13314 l = NULL;
13315 }
13316 }
13317 if (l != NULL)
13318 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013319 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013320 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013321 }
13322 }
13323}
13324
13325/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013326 * "invert(expr)" function
13327 */
13328 static void
13329f_invert(argvars, rettv)
13330 typval_T *argvars;
13331 typval_T *rettv;
13332{
13333 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13334}
13335
13336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337 * "isdirectory()" function
13338 */
13339 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013340f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013341 typval_T *argvars;
13342 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345}
13346
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013347/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013348 * "islocked()" function
13349 */
13350 static void
13351f_islocked(argvars, rettv)
13352 typval_T *argvars;
13353 typval_T *rettv;
13354{
13355 lval_T lv;
13356 char_u *end;
13357 dictitem_T *di;
13358
13359 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013360 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13361 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013362 if (end != NULL && lv.ll_name != NULL)
13363 {
13364 if (*end != NUL)
13365 EMSG(_(e_trailing));
13366 else
13367 {
13368 if (lv.ll_tv == NULL)
13369 {
13370 if (check_changedtick(lv.ll_name))
13371 rettv->vval.v_number = 1; /* always locked */
13372 else
13373 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013374 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013375 if (di != NULL)
13376 {
13377 /* Consider a variable locked when:
13378 * 1. the variable itself is locked
13379 * 2. the value of the variable is locked.
13380 * 3. the List or Dict value is locked.
13381 */
13382 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13383 || tv_islocked(&di->di_tv));
13384 }
13385 }
13386 }
13387 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013388 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013389 else if (lv.ll_newkey != NULL)
13390 EMSG2(_(e_dictkey), lv.ll_newkey);
13391 else if (lv.ll_list != NULL)
13392 /* List item. */
13393 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13394 else
13395 /* Dictionary item. */
13396 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13397 }
13398 }
13399
13400 clear_lval(&lv);
13401}
13402
Bram Moolenaar33570922005-01-25 22:26:29 +000013403static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013404
13405/*
13406 * Turn a dict into a list:
13407 * "what" == 0: list of keys
13408 * "what" == 1: list of values
13409 * "what" == 2: list of items
13410 */
13411 static void
13412dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013413 typval_T *argvars;
13414 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013415 int what;
13416{
Bram Moolenaar33570922005-01-25 22:26:29 +000013417 list_T *l2;
13418 dictitem_T *di;
13419 hashitem_T *hi;
13420 listitem_T *li;
13421 listitem_T *li2;
13422 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013423 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013424
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425 if (argvars[0].v_type != VAR_DICT)
13426 {
13427 EMSG(_(e_dictreq));
13428 return;
13429 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013430 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013431 return;
13432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013433 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013434 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013435
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013436 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013438 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013439 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013441 --todo;
13442 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013443
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013444 li = listitem_alloc();
13445 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013446 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013447 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013448
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013449 if (what == 0)
13450 {
13451 /* keys() */
13452 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013453 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013454 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13455 }
13456 else if (what == 1)
13457 {
13458 /* values() */
13459 copy_tv(&di->di_tv, &li->li_tv);
13460 }
13461 else
13462 {
13463 /* items() */
13464 l2 = list_alloc();
13465 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013466 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013467 li->li_tv.vval.v_list = l2;
13468 if (l2 == NULL)
13469 break;
13470 ++l2->lv_refcount;
13471
13472 li2 = listitem_alloc();
13473 if (li2 == NULL)
13474 break;
13475 list_append(l2, li2);
13476 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013477 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013478 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13479
13480 li2 = listitem_alloc();
13481 if (li2 == NULL)
13482 break;
13483 list_append(l2, li2);
13484 copy_tv(&di->di_tv, &li2->li_tv);
13485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013486 }
13487 }
13488}
13489
13490/*
13491 * "items(dict)" function
13492 */
13493 static void
13494f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 typval_T *argvars;
13496 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013497{
13498 dict_list(argvars, rettv, 2);
13499}
13500
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013502 * "join()" function
13503 */
13504 static void
13505f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013506 typval_T *argvars;
13507 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013508{
13509 garray_T ga;
13510 char_u *sep;
13511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 if (argvars[0].v_type != VAR_LIST)
13513 {
13514 EMSG(_(e_listreq));
13515 return;
13516 }
13517 if (argvars[0].vval.v_list == NULL)
13518 return;
13519 if (argvars[1].v_type == VAR_UNKNOWN)
13520 sep = (char_u *)" ";
13521 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013522 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013523
13524 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013525
13526 if (sep != NULL)
13527 {
13528 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013529 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 ga_append(&ga, NUL);
13531 rettv->vval.v_string = (char_u *)ga.ga_data;
13532 }
13533 else
13534 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013535}
13536
13537/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013538 * "keys()" function
13539 */
13540 static void
13541f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013542 typval_T *argvars;
13543 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013544{
13545 dict_list(argvars, rettv, 0);
13546}
13547
13548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 * "last_buffer_nr()" function.
13550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013552f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013553 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555{
13556 int n = 0;
13557 buf_T *buf;
13558
13559 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13560 if (n < buf->b_fnum)
13561 n = buf->b_fnum;
13562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013564}
13565
13566/*
13567 * "len()" function
13568 */
13569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013573{
13574 switch (argvars[0].v_type)
13575 {
13576 case VAR_STRING:
13577 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578 rettv->vval.v_number = (varnumber_T)STRLEN(
13579 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013580 break;
13581 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013584 case VAR_DICT:
13585 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13586 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013587 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013588 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 break;
13590 }
13591}
13592
Bram Moolenaar33570922005-01-25 22:26:29 +000013593static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013594
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013599 int type;
13600{
13601#ifdef FEAT_LIBCALL
13602 char_u *string_in;
13603 char_u **string_result;
13604 int nr_result;
13605#endif
13606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013608 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013610
13611 if (check_restricted() || check_secure())
13612 return;
13613
13614#ifdef FEAT_LIBCALL
13615 /* The first two args must be strings, otherwise its meaningless */
13616 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13617 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013618 string_in = NULL;
13619 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013620 string_in = argvars[2].vval.v_string;
13621 if (type == VAR_NUMBER)
13622 string_result = NULL;
13623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013625 if (mch_libcall(argvars[0].vval.v_string,
13626 argvars[1].vval.v_string,
13627 string_in,
13628 argvars[2].vval.v_number,
13629 string_result,
13630 &nr_result) == OK
13631 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013632 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013633 }
13634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635}
13636
13637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638 * "libcall()" function
13639 */
13640 static void
13641f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013642 typval_T *argvars;
13643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644{
13645 libcall_common(argvars, rettv, VAR_STRING);
13646}
13647
13648/*
13649 * "libcallnr()" function
13650 */
13651 static void
13652f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013653 typval_T *argvars;
13654 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655{
13656 libcall_common(argvars, rettv, VAR_NUMBER);
13657}
13658
13659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 * "line(string)" function
13661 */
13662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *argvars;
13665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
13667 linenr_T lnum = 0;
13668 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013669 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013671 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 if (fp != NULL)
13673 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675}
13676
13677/*
13678 * "line2byte(lnum)" function
13679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013681f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013682 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684{
13685#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687#else
13688 linenr_T lnum;
13689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013690 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13695 if (rettv->vval.v_number >= 0)
13696 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#endif
13698}
13699
13700/*
13701 * "lispindent(lnum)" function
13702 */
13703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013704f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707{
13708#ifdef FEAT_LISP
13709 pos_T pos;
13710 linenr_T lnum;
13711
13712 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013713 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13715 {
13716 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013717 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 curwin->w_cursor = pos;
13719 }
13720 else
13721#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723}
13724
13725/*
13726 * "localtime()" function
13727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734}
13735
Bram Moolenaar33570922005-01-25 22:26:29 +000013736static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737
13738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *argvars;
13741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 int exact;
13743{
13744 char_u *keys;
13745 char_u *which;
13746 char_u buf[NUMBUFLEN];
13747 char_u *keys_buf = NULL;
13748 char_u *rhs;
13749 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013750 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013751 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013752 mapblock_T *mp;
13753 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754
13755 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013756 rettv->v_type = VAR_STRING;
13757 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013759 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 if (*keys == NUL)
13761 return;
13762
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013763 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013766 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013767 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013768 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013769 if (argvars[3].v_type != VAR_UNKNOWN)
13770 get_dict = get_tv_number(&argvars[3]);
13771 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 else
13774 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013775 if (which == NULL)
13776 return;
13777
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778 mode = get_map_mode(&which, 0);
13779
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013780 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013781 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013783
13784 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013786 /* Return a string. */
13787 if (rhs != NULL)
13788 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789
Bram Moolenaarbd743252010-10-20 21:23:33 +020013790 }
13791 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13792 {
13793 /* Return a dictionary. */
13794 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13795 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13796 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797
Bram Moolenaarbd743252010-10-20 21:23:33 +020013798 dict_add_nr_str(dict, "lhs", 0L, lhs);
13799 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13800 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13801 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13802 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13803 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13804 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013805 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013806 dict_add_nr_str(dict, "mode", 0L, mapmode);
13807
13808 vim_free(lhs);
13809 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 }
13811}
13812
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013813#ifdef FEAT_FLOAT
13814/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013815 * "log()" function
13816 */
13817 static void
13818f_log(argvars, rettv)
13819 typval_T *argvars;
13820 typval_T *rettv;
13821{
13822 float_T f;
13823
13824 rettv->v_type = VAR_FLOAT;
13825 if (get_float_arg(argvars, &f) == OK)
13826 rettv->vval.v_float = log(f);
13827 else
13828 rettv->vval.v_float = 0.0;
13829}
13830
13831/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013832 * "log10()" function
13833 */
13834 static void
13835f_log10(argvars, rettv)
13836 typval_T *argvars;
13837 typval_T *rettv;
13838{
13839 float_T f;
13840
13841 rettv->v_type = VAR_FLOAT;
13842 if (get_float_arg(argvars, &f) == OK)
13843 rettv->vval.v_float = log10(f);
13844 else
13845 rettv->vval.v_float = 0.0;
13846}
13847#endif
13848
Bram Moolenaar1dced572012-04-05 16:54:08 +020013849#ifdef FEAT_LUA
13850/*
13851 * "luaeval()" function
13852 */
13853 static void
13854f_luaeval(argvars, rettv)
13855 typval_T *argvars;
13856 typval_T *rettv;
13857{
13858 char_u *str;
13859 char_u buf[NUMBUFLEN];
13860
13861 str = get_tv_string_buf(&argvars[0], buf);
13862 do_luaeval(str, argvars + 1, rettv);
13863}
13864#endif
13865
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013867 * "map()" function
13868 */
13869 static void
13870f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013871 typval_T *argvars;
13872 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013873{
13874 filter_map(argvars, rettv, TRUE);
13875}
13876
13877/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 */
13880 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013881f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013882 typval_T *argvars;
13883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013885 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886}
13887
13888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013889 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 */
13891 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013892f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013893 typval_T *argvars;
13894 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013896 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897}
13898
Bram Moolenaar33570922005-01-25 22:26:29 +000013899static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900
13901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013902find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 typval_T *argvars;
13904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 int type;
13906{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013907 char_u *str = NULL;
13908 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 char_u *pat;
13910 regmatch_T regmatch;
13911 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013912 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 char_u *save_cpo;
13914 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013915 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013916 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013917 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013918 list_T *l = NULL;
13919 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013921 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922
13923 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13924 save_cpo = p_cpo;
13925 p_cpo = (char_u *)"";
13926
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013927 rettv->vval.v_number = -1;
13928 if (type == 3)
13929 {
13930 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013931 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013932 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013933 }
13934 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013936 rettv->v_type = VAR_STRING;
13937 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013940 if (argvars[0].v_type == VAR_LIST)
13941 {
13942 if ((l = argvars[0].vval.v_list) == NULL)
13943 goto theend;
13944 li = l->lv_first;
13945 }
13946 else
13947 expr = str = get_tv_string(&argvars[0]);
13948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13950 if (pat == NULL)
13951 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013953 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013955 int error = FALSE;
13956
13957 start = get_tv_number_chk(&argvars[2], &error);
13958 if (error)
13959 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013960 if (l != NULL)
13961 {
13962 li = list_find(l, start);
13963 if (li == NULL)
13964 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013965 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966 }
13967 else
13968 {
13969 if (start < 0)
13970 start = 0;
13971 if (start > (long)STRLEN(str))
13972 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013973 /* When "count" argument is there ignore matches before "start",
13974 * otherwise skip part of the string. Differs when pattern is "^"
13975 * or "\<". */
13976 if (argvars[3].v_type != VAR_UNKNOWN)
13977 startcol = start;
13978 else
13979 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013980 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013981
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013982 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013983 nth = get_tv_number_chk(&argvars[3], &error);
13984 if (error)
13985 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 }
13987
13988 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13989 if (regmatch.regprog != NULL)
13990 {
13991 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013992
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013993 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013994 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013995 if (l != NULL)
13996 {
13997 if (li == NULL)
13998 {
13999 match = FALSE;
14000 break;
14001 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014002 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014003 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014004 if (str == NULL)
14005 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014006 }
14007
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014008 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014009
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014010 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014011 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014012 if (l == NULL && !match)
14013 break;
14014
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014015 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014016 if (l != NULL)
14017 {
14018 li = li->li_next;
14019 ++idx;
14020 }
14021 else
14022 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014023#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014024 startcol = (colnr_T)(regmatch.startp[0]
14025 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014026#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014027 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014028#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014029 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014030 }
14031
14032 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014034 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014035 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014036 int i;
14037
14038 /* return list with matched string and submatches */
14039 for (i = 0; i < NSUBEXP; ++i)
14040 {
14041 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014042 {
14043 if (list_append_string(rettv->vval.v_list,
14044 (char_u *)"", 0) == FAIL)
14045 break;
14046 }
14047 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014048 regmatch.startp[i],
14049 (int)(regmatch.endp[i] - regmatch.startp[i]))
14050 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014051 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014052 }
14053 }
14054 else if (type == 2)
14055 {
14056 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014057 if (l != NULL)
14058 copy_tv(&li->li_tv, rettv);
14059 else
14060 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014062 }
14063 else if (l != NULL)
14064 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 else
14066 {
14067 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 (varnumber_T)(regmatch.startp[0] - str);
14070 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014073 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074 }
14075 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014076 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 }
14078
14079theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014080 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 p_cpo = save_cpo;
14082}
14083
14084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014085 * "match()" function
14086 */
14087 static void
14088f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014089 typval_T *argvars;
14090 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014091{
14092 find_some_match(argvars, rettv, 1);
14093}
14094
14095/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014096 * "matchadd()" function
14097 */
14098 static void
14099f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014100 typval_T *argvars UNUSED;
14101 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014102{
14103#ifdef FEAT_SEARCH_EXTRA
14104 char_u buf[NUMBUFLEN];
14105 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14106 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14107 int prio = 10; /* default priority */
14108 int id = -1;
14109 int error = FALSE;
14110
14111 rettv->vval.v_number = -1;
14112
14113 if (grp == NULL || pat == NULL)
14114 return;
14115 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014116 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014117 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014118 if (argvars[3].v_type != VAR_UNKNOWN)
14119 id = get_tv_number_chk(&argvars[3], &error);
14120 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014121 if (error == TRUE)
14122 return;
14123 if (id >= 1 && id <= 3)
14124 {
14125 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14126 return;
14127 }
14128
14129 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14130#endif
14131}
14132
14133/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014134 * "matcharg()" function
14135 */
14136 static void
14137f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014138 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014139 typval_T *rettv;
14140{
14141 if (rettv_list_alloc(rettv) == OK)
14142 {
14143#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014144 int id = get_tv_number(&argvars[0]);
14145 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014146
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014147 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014148 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014149 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14150 {
14151 list_append_string(rettv->vval.v_list,
14152 syn_id2name(m->hlg_id), -1);
14153 list_append_string(rettv->vval.v_list, m->pattern, -1);
14154 }
14155 else
14156 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014157 list_append_string(rettv->vval.v_list, NULL, -1);
14158 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014159 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014160 }
14161#endif
14162 }
14163}
14164
14165/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014166 * "matchdelete()" function
14167 */
14168 static void
14169f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014170 typval_T *argvars UNUSED;
14171 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014172{
14173#ifdef FEAT_SEARCH_EXTRA
14174 rettv->vval.v_number = match_delete(curwin,
14175 (int)get_tv_number(&argvars[0]), TRUE);
14176#endif
14177}
14178
14179/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014180 * "matchend()" function
14181 */
14182 static void
14183f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 typval_T *argvars;
14185 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014186{
14187 find_some_match(argvars, rettv, 0);
14188}
14189
14190/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014191 * "matchlist()" function
14192 */
14193 static void
14194f_matchlist(argvars, rettv)
14195 typval_T *argvars;
14196 typval_T *rettv;
14197{
14198 find_some_match(argvars, rettv, 3);
14199}
14200
14201/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014202 * "matchstr()" function
14203 */
14204 static void
14205f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014206 typval_T *argvars;
14207 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014208{
14209 find_some_match(argvars, rettv, 2);
14210}
14211
Bram Moolenaar33570922005-01-25 22:26:29 +000014212static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014213
14214 static void
14215max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014216 typval_T *argvars;
14217 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014218 int domax;
14219{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014220 long n = 0;
14221 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014223
14224 if (argvars[0].v_type == VAR_LIST)
14225 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014226 list_T *l;
14227 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014228
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014229 l = argvars[0].vval.v_list;
14230 if (l != NULL)
14231 {
14232 li = l->lv_first;
14233 if (li != NULL)
14234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014236 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014237 {
14238 li = li->li_next;
14239 if (li == NULL)
14240 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014242 if (domax ? i > n : i < n)
14243 n = i;
14244 }
14245 }
14246 }
14247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014248 else if (argvars[0].v_type == VAR_DICT)
14249 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014251 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014252 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014253 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014254
14255 d = argvars[0].vval.v_dict;
14256 if (d != NULL)
14257 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014258 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014259 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014260 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014261 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014262 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014263 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014264 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014265 if (first)
14266 {
14267 n = i;
14268 first = FALSE;
14269 }
14270 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014271 n = i;
14272 }
14273 }
14274 }
14275 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014276 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014277 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014279}
14280
14281/*
14282 * "max()" function
14283 */
14284 static void
14285f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014286 typval_T *argvars;
14287 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014288{
14289 max_min(argvars, rettv, TRUE);
14290}
14291
14292/*
14293 * "min()" function
14294 */
14295 static void
14296f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014297 typval_T *argvars;
14298 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014299{
14300 max_min(argvars, rettv, FALSE);
14301}
14302
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014303static int mkdir_recurse __ARGS((char_u *dir, int prot));
14304
14305/*
14306 * Create the directory in which "dir" is located, and higher levels when
14307 * needed.
14308 */
14309 static int
14310mkdir_recurse(dir, prot)
14311 char_u *dir;
14312 int prot;
14313{
14314 char_u *p;
14315 char_u *updir;
14316 int r = FAIL;
14317
14318 /* Get end of directory name in "dir".
14319 * We're done when it's "/" or "c:/". */
14320 p = gettail_sep(dir);
14321 if (p <= get_past_head(dir))
14322 return OK;
14323
14324 /* If the directory exists we're done. Otherwise: create it.*/
14325 updir = vim_strnsave(dir, (int)(p - dir));
14326 if (updir == NULL)
14327 return FAIL;
14328 if (mch_isdir(updir))
14329 r = OK;
14330 else if (mkdir_recurse(updir, prot) == OK)
14331 r = vim_mkdir_emsg(updir, prot);
14332 vim_free(updir);
14333 return r;
14334}
14335
14336#ifdef vim_mkdir
14337/*
14338 * "mkdir()" function
14339 */
14340 static void
14341f_mkdir(argvars, rettv)
14342 typval_T *argvars;
14343 typval_T *rettv;
14344{
14345 char_u *dir;
14346 char_u buf[NUMBUFLEN];
14347 int prot = 0755;
14348
14349 rettv->vval.v_number = FAIL;
14350 if (check_restricted() || check_secure())
14351 return;
14352
14353 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014354 if (*dir == NUL)
14355 rettv->vval.v_number = FAIL;
14356 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014357 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014358 if (*gettail(dir) == NUL)
14359 /* remove trailing slashes */
14360 *gettail_sep(dir) = NUL;
14361
14362 if (argvars[1].v_type != VAR_UNKNOWN)
14363 {
14364 if (argvars[2].v_type != VAR_UNKNOWN)
14365 prot = get_tv_number_chk(&argvars[2], NULL);
14366 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14367 mkdir_recurse(dir, prot);
14368 }
14369 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014370 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014371}
14372#endif
14373
Bram Moolenaar0d660222005-01-07 21:51:51 +000014374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 * "mode()" function
14376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014379 typval_T *argvars;
14380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014382 char_u buf[3];
14383
14384 buf[1] = NUL;
14385 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386
14387#ifdef FEAT_VISUAL
14388 if (VIsual_active)
14389 {
14390 if (VIsual_select)
14391 buf[0] = VIsual_mode + 's' - 'v';
14392 else
14393 buf[0] = VIsual_mode;
14394 }
14395 else
14396#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014397 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14398 || State == CONFIRM)
14399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014401 if (State == ASKMORE)
14402 buf[1] = 'm';
14403 else if (State == CONFIRM)
14404 buf[1] = '?';
14405 }
14406 else if (State == EXTERNCMD)
14407 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 else if (State & INSERT)
14409 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014410#ifdef FEAT_VREPLACE
14411 if (State & VREPLACE_FLAG)
14412 {
14413 buf[0] = 'R';
14414 buf[1] = 'v';
14415 }
14416 else
14417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 if (State & REPLACE_FLAG)
14419 buf[0] = 'R';
14420 else
14421 buf[0] = 'i';
14422 }
14423 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014426 if (exmode_active)
14427 buf[1] = 'v';
14428 }
14429 else if (exmode_active)
14430 {
14431 buf[0] = 'c';
14432 buf[1] = 'e';
14433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014437 if (finish_op)
14438 buf[1] = 'o';
14439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014441 /* Clear out the minor mode when the argument is not a non-zero number or
14442 * non-empty string. */
14443 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014444 buf[1] = NUL;
14445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014446 rettv->vval.v_string = vim_strsave(buf);
14447 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448}
14449
Bram Moolenaar429fa852013-04-15 12:27:36 +020014450#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014451/*
14452 * "mzeval()" function
14453 */
14454 static void
14455f_mzeval(argvars, rettv)
14456 typval_T *argvars;
14457 typval_T *rettv;
14458{
14459 char_u *str;
14460 char_u buf[NUMBUFLEN];
14461
14462 str = get_tv_string_buf(&argvars[0], buf);
14463 do_mzeval(str, rettv);
14464}
Bram Moolenaar75676462013-01-30 14:55:42 +010014465
14466 void
14467mzscheme_call_vim(name, args, rettv)
14468 char_u *name;
14469 typval_T *args;
14470 typval_T *rettv;
14471{
14472 typval_T argvars[3];
14473
14474 argvars[0].v_type = VAR_STRING;
14475 argvars[0].vval.v_string = name;
14476 copy_tv(args, &argvars[1]);
14477 argvars[2].v_type = VAR_UNKNOWN;
14478 f_call(argvars, rettv);
14479 clear_tv(&argvars[1]);
14480}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014481#endif
14482
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484 * "nextnonblank()" function
14485 */
14486 static void
14487f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014488 typval_T *argvars;
14489 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490{
14491 linenr_T lnum;
14492
14493 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014496 {
14497 lnum = 0;
14498 break;
14499 }
14500 if (*skipwhite(ml_get(lnum)) != NUL)
14501 break;
14502 }
14503 rettv->vval.v_number = lnum;
14504}
14505
14506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 * "nr2char()" function
14508 */
14509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014510f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014511 typval_T *argvars;
14512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513{
14514 char_u buf[NUMBUFLEN];
14515
14516#ifdef FEAT_MBYTE
14517 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014518 {
14519 int utf8 = 0;
14520
14521 if (argvars[1].v_type != VAR_UNKNOWN)
14522 utf8 = get_tv_number_chk(&argvars[1], NULL);
14523 if (utf8)
14524 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14525 else
14526 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 else
14529#endif
14530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 buf[1] = NUL;
14533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->v_type = VAR_STRING;
14535 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014536}
14537
14538/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014539 * "or(expr, expr)" function
14540 */
14541 static void
14542f_or(argvars, rettv)
14543 typval_T *argvars;
14544 typval_T *rettv;
14545{
14546 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14547 | get_tv_number_chk(&argvars[1], NULL);
14548}
14549
14550/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014551 * "pathshorten()" function
14552 */
14553 static void
14554f_pathshorten(argvars, rettv)
14555 typval_T *argvars;
14556 typval_T *rettv;
14557{
14558 char_u *p;
14559
14560 rettv->v_type = VAR_STRING;
14561 p = get_tv_string_chk(&argvars[0]);
14562 if (p == NULL)
14563 rettv->vval.v_string = NULL;
14564 else
14565 {
14566 p = vim_strsave(p);
14567 rettv->vval.v_string = p;
14568 if (p != NULL)
14569 shorten_dir(p);
14570 }
14571}
14572
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014573#ifdef FEAT_FLOAT
14574/*
14575 * "pow()" function
14576 */
14577 static void
14578f_pow(argvars, rettv)
14579 typval_T *argvars;
14580 typval_T *rettv;
14581{
14582 float_T fx, fy;
14583
14584 rettv->v_type = VAR_FLOAT;
14585 if (get_float_arg(argvars, &fx) == OK
14586 && get_float_arg(&argvars[1], &fy) == OK)
14587 rettv->vval.v_float = pow(fx, fy);
14588 else
14589 rettv->vval.v_float = 0.0;
14590}
14591#endif
14592
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014593/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594 * "prevnonblank()" function
14595 */
14596 static void
14597f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 typval_T *argvars;
14599 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600{
14601 linenr_T lnum;
14602
14603 lnum = get_tv_lnum(argvars);
14604 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14605 lnum = 0;
14606 else
14607 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14608 --lnum;
14609 rettv->vval.v_number = lnum;
14610}
14611
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014612#ifdef HAVE_STDARG_H
14613/* This dummy va_list is here because:
14614 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14615 * - locally in the function results in a "used before set" warning
14616 * - using va_start() to initialize it gives "function with fixed args" error */
14617static va_list ap;
14618#endif
14619
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014621 * "printf()" function
14622 */
14623 static void
14624f_printf(argvars, rettv)
14625 typval_T *argvars;
14626 typval_T *rettv;
14627{
14628 rettv->v_type = VAR_STRING;
14629 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014630#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014631 {
14632 char_u buf[NUMBUFLEN];
14633 int len;
14634 char_u *s;
14635 int saved_did_emsg = did_emsg;
14636 char *fmt;
14637
14638 /* Get the required length, allocate the buffer and do it for real. */
14639 did_emsg = FALSE;
14640 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014641 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014642 if (!did_emsg)
14643 {
14644 s = alloc(len + 1);
14645 if (s != NULL)
14646 {
14647 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014648 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014649 }
14650 }
14651 did_emsg |= saved_did_emsg;
14652 }
14653#endif
14654}
14655
14656/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014657 * "pumvisible()" function
14658 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014659 static void
14660f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014661 typval_T *argvars UNUSED;
14662 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014663{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014664#ifdef FEAT_INS_EXPAND
14665 if (pum_visible())
14666 rettv->vval.v_number = 1;
14667#endif
14668}
14669
Bram Moolenaardb913952012-06-29 12:54:53 +020014670#ifdef FEAT_PYTHON3
14671/*
14672 * "py3eval()" function
14673 */
14674 static void
14675f_py3eval(argvars, rettv)
14676 typval_T *argvars;
14677 typval_T *rettv;
14678{
14679 char_u *str;
14680 char_u buf[NUMBUFLEN];
14681
14682 str = get_tv_string_buf(&argvars[0], buf);
14683 do_py3eval(str, rettv);
14684}
14685#endif
14686
14687#ifdef FEAT_PYTHON
14688/*
14689 * "pyeval()" function
14690 */
14691 static void
14692f_pyeval(argvars, rettv)
14693 typval_T *argvars;
14694 typval_T *rettv;
14695{
14696 char_u *str;
14697 char_u buf[NUMBUFLEN];
14698
14699 str = get_tv_string_buf(&argvars[0], buf);
14700 do_pyeval(str, rettv);
14701}
14702#endif
14703
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014704/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014705 * "range()" function
14706 */
14707 static void
14708f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 typval_T *argvars;
14710 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014711{
14712 long start;
14713 long end;
14714 long stride = 1;
14715 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014716 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014718 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014719 if (argvars[1].v_type == VAR_UNKNOWN)
14720 {
14721 end = start - 1;
14722 start = 0;
14723 }
14724 else
14725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014726 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014727 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014729 }
14730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014731 if (error)
14732 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014733 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014734 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014735 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014736 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014737 else
14738 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014739 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014740 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014741 if (list_append_number(rettv->vval.v_list,
14742 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014743 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744 }
14745}
14746
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014747/*
14748 * "readfile()" function
14749 */
14750 static void
14751f_readfile(argvars, rettv)
14752 typval_T *argvars;
14753 typval_T *rettv;
14754{
14755 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014756 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757 char_u *fname;
14758 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014759 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14760 int io_size = sizeof(buf);
14761 int readlen; /* size of last fread() */
14762 char_u *prev = NULL; /* previously read bytes, if any */
14763 long prevlen = 0; /* length of data in prev */
14764 long prevsize = 0; /* size of prev buffer */
14765 long maxline = MAXLNUM;
14766 long cnt = 0;
14767 char_u *p; /* position in buf */
14768 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014770 if (argvars[1].v_type != VAR_UNKNOWN)
14771 {
14772 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14773 binary = TRUE;
14774 if (argvars[2].v_type != VAR_UNKNOWN)
14775 maxline = get_tv_number(&argvars[2]);
14776 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014778 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014779 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014780
14781 /* Always open the file in binary mode, library functions have a mind of
14782 * their own about CR-LF conversion. */
14783 fname = get_tv_string(&argvars[0]);
14784 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14785 {
14786 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14787 return;
14788 }
14789
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014790 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014792 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014793
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014794 /* This for loop processes what was read, but is also entered at end
14795 * of file so that either:
14796 * - an incomplete line gets written
14797 * - a "binary" file gets an empty line at the end if it ends in a
14798 * newline. */
14799 for (p = buf, start = buf;
14800 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14801 ++p)
14802 {
14803 if (*p == '\n' || readlen <= 0)
14804 {
14805 listitem_T *li;
14806 char_u *s = NULL;
14807 long_u len = p - start;
14808
14809 /* Finished a line. Remove CRs before NL. */
14810 if (readlen > 0 && !binary)
14811 {
14812 while (len > 0 && start[len - 1] == '\r')
14813 --len;
14814 /* removal may cross back to the "prev" string */
14815 if (len == 0)
14816 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14817 --prevlen;
14818 }
14819 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014820 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 else
14822 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 /* Change "prev" buffer to be the right size. This way
14824 * the bytes are only copied once, and very long lines are
14825 * allocated only once. */
14826 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014827 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014828 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014829 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 prev = NULL; /* the list will own the string */
14831 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014832 }
14833 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014834 if (s == NULL)
14835 {
14836 do_outofmem_msg((long_u) prevlen + len + 1);
14837 failed = TRUE;
14838 break;
14839 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014841 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014842 {
14843 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014844 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014845 break;
14846 }
14847 li->li_tv.v_type = VAR_STRING;
14848 li->li_tv.v_lock = 0;
14849 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014850 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014852 start = p + 1; /* step over newline */
14853 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014854 break;
14855 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014856 else if (*p == NUL)
14857 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014858#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014859 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14860 * when finding the BF and check the previous two bytes. */
14861 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014862 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014863 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14864 * + 1, these may be in the "prev" string. */
14865 char_u back1 = p >= buf + 1 ? p[-1]
14866 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14867 char_u back2 = p >= buf + 2 ? p[-2]
14868 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14869 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014870
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014871 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014872 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014873 char_u *dest = p - 2;
14874
14875 /* Usually a BOM is at the beginning of a file, and so at
14876 * the beginning of a line; then we can just step over it.
14877 */
14878 if (start == dest)
14879 start = p + 1;
14880 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014881 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014882 /* have to shuffle buf to close gap */
14883 int adjust_prevlen = 0;
14884
14885 if (dest < buf)
14886 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014887 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014888 dest = buf;
14889 }
14890 if (readlen > p - buf + 1)
14891 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14892 readlen -= 3 - adjust_prevlen;
14893 prevlen -= adjust_prevlen;
14894 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014895 }
14896 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014897 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014898#endif
14899 } /* for */
14900
14901 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14902 break;
14903 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014904 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014905 /* There's part of a line in buf, store it in "prev". */
14906 if (p - start + prevlen >= prevsize)
14907 {
14908 /* need bigger "prev" buffer */
14909 char_u *newprev;
14910
14911 /* A common use case is ordinary text files and "prev" gets a
14912 * fragment of a line, so the first allocation is made
14913 * small, to avoid repeatedly 'allocing' large and
14914 * 'reallocing' small. */
14915 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014916 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014917 else
14918 {
14919 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014920 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014921 prevsize = grow50pc > growmin ? grow50pc : growmin;
14922 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014923 newprev = prev == NULL ? alloc(prevsize)
14924 : vim_realloc(prev, prevsize);
14925 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014926 {
14927 do_outofmem_msg((long_u)prevsize);
14928 failed = TRUE;
14929 break;
14930 }
14931 prev = newprev;
14932 }
14933 /* Add the line part to end of "prev". */
14934 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014935 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014936 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014937 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014938
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014939 /*
14940 * For a negative line count use only the lines at the end of the file,
14941 * free the rest.
14942 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014943 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014944 while (cnt > -maxline)
14945 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014946 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014947 --cnt;
14948 }
14949
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014950 if (failed)
14951 {
14952 list_free(rettv->vval.v_list, TRUE);
14953 /* readfile doc says an empty list is returned on error */
14954 rettv->vval.v_list = list_alloc();
14955 }
14956
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014957 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014958 fclose(fd);
14959}
14960
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014961#if defined(FEAT_RELTIME)
14962static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14963
14964/*
14965 * Convert a List to proftime_T.
14966 * Return FAIL when there is something wrong.
14967 */
14968 static int
14969list2proftime(arg, tm)
14970 typval_T *arg;
14971 proftime_T *tm;
14972{
14973 long n1, n2;
14974 int error = FALSE;
14975
14976 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14977 || arg->vval.v_list->lv_len != 2)
14978 return FAIL;
14979 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14980 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14981# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014982 tm->HighPart = n1;
14983 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014984# else
14985 tm->tv_sec = n1;
14986 tm->tv_usec = n2;
14987# endif
14988 return error ? FAIL : OK;
14989}
14990#endif /* FEAT_RELTIME */
14991
14992/*
14993 * "reltime()" function
14994 */
14995 static void
14996f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014997 typval_T *argvars UNUSED;
14998 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014999{
15000#ifdef FEAT_RELTIME
15001 proftime_T res;
15002 proftime_T start;
15003
15004 if (argvars[0].v_type == VAR_UNKNOWN)
15005 {
15006 /* No arguments: get current time. */
15007 profile_start(&res);
15008 }
15009 else if (argvars[1].v_type == VAR_UNKNOWN)
15010 {
15011 if (list2proftime(&argvars[0], &res) == FAIL)
15012 return;
15013 profile_end(&res);
15014 }
15015 else
15016 {
15017 /* Two arguments: compute the difference. */
15018 if (list2proftime(&argvars[0], &start) == FAIL
15019 || list2proftime(&argvars[1], &res) == FAIL)
15020 return;
15021 profile_sub(&res, &start);
15022 }
15023
15024 if (rettv_list_alloc(rettv) == OK)
15025 {
15026 long n1, n2;
15027
15028# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015029 n1 = res.HighPart;
15030 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015031# else
15032 n1 = res.tv_sec;
15033 n2 = res.tv_usec;
15034# endif
15035 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15036 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15037 }
15038#endif
15039}
15040
15041/*
15042 * "reltimestr()" function
15043 */
15044 static void
15045f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015046 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015047 typval_T *rettv;
15048{
15049#ifdef FEAT_RELTIME
15050 proftime_T tm;
15051#endif
15052
15053 rettv->v_type = VAR_STRING;
15054 rettv->vval.v_string = NULL;
15055#ifdef FEAT_RELTIME
15056 if (list2proftime(&argvars[0], &tm) == OK)
15057 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15058#endif
15059}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015060
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15062static void make_connection __ARGS((void));
15063static int check_connection __ARGS((void));
15064
15065 static void
15066make_connection()
15067{
15068 if (X_DISPLAY == NULL
15069# ifdef FEAT_GUI
15070 && !gui.in_use
15071# endif
15072 )
15073 {
15074 x_force_connect = TRUE;
15075 setup_term_clip();
15076 x_force_connect = FALSE;
15077 }
15078}
15079
15080 static int
15081check_connection()
15082{
15083 make_connection();
15084 if (X_DISPLAY == NULL)
15085 {
15086 EMSG(_("E240: No connection to Vim server"));
15087 return FAIL;
15088 }
15089 return OK;
15090}
15091#endif
15092
15093#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015094static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095
15096 static void
15097remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015098 typval_T *argvars;
15099 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015100 int expr;
15101{
15102 char_u *server_name;
15103 char_u *keys;
15104 char_u *r = NULL;
15105 char_u buf[NUMBUFLEN];
15106# ifdef WIN32
15107 HWND w;
15108# else
15109 Window w;
15110# endif
15111
15112 if (check_restricted() || check_secure())
15113 return;
15114
15115# ifdef FEAT_X11
15116 if (check_connection() == FAIL)
15117 return;
15118# endif
15119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 server_name = get_tv_string_chk(&argvars[0]);
15121 if (server_name == NULL)
15122 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 keys = get_tv_string_buf(&argvars[1], buf);
15124# ifdef WIN32
15125 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15126# else
15127 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15128 < 0)
15129# endif
15130 {
15131 if (r != NULL)
15132 EMSG(r); /* sending worked but evaluation failed */
15133 else
15134 EMSG2(_("E241: Unable to send to %s"), server_name);
15135 return;
15136 }
15137
15138 rettv->vval.v_string = r;
15139
15140 if (argvars[2].v_type != VAR_UNKNOWN)
15141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015143 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015146 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 v.di_tv.v_type = VAR_STRING;
15148 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015149 idvar = get_tv_string_chk(&argvars[2]);
15150 if (idvar != NULL)
15151 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015152 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 }
15154}
15155#endif
15156
15157/*
15158 * "remote_expr()" function
15159 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 static void
15161f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015163 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164{
15165 rettv->v_type = VAR_STRING;
15166 rettv->vval.v_string = NULL;
15167#ifdef FEAT_CLIENTSERVER
15168 remote_common(argvars, rettv, TRUE);
15169#endif
15170}
15171
15172/*
15173 * "remote_foreground()" function
15174 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 static void
15176f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015177 typval_T *argvars UNUSED;
15178 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180#ifdef FEAT_CLIENTSERVER
15181# ifdef WIN32
15182 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015183 {
15184 char_u *server_name = get_tv_string_chk(&argvars[0]);
15185
15186 if (server_name != NULL)
15187 serverForeground(server_name);
15188 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189# else
15190 /* Send a foreground() expression to the server. */
15191 argvars[1].v_type = VAR_STRING;
15192 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15193 argvars[2].v_type = VAR_UNKNOWN;
15194 remote_common(argvars, rettv, TRUE);
15195 vim_free(argvars[1].vval.v_string);
15196# endif
15197#endif
15198}
15199
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 static void
15201f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015202 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015203 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204{
15205#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015206 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 char_u *s = NULL;
15208# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015209 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212
15213 if (check_restricted() || check_secure())
15214 {
15215 rettv->vval.v_number = -1;
15216 return;
15217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 serverid = get_tv_string_chk(&argvars[0]);
15219 if (serverid == NULL)
15220 {
15221 rettv->vval.v_number = -1;
15222 return; /* type error; errmsg already given */
15223 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015225 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015226 if (n == 0)
15227 rettv->vval.v_number = -1;
15228 else
15229 {
15230 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15231 rettv->vval.v_number = (s != NULL);
15232 }
15233# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015234 if (check_connection() == FAIL)
15235 return;
15236
15237 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015238 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015239# endif
15240
15241 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015243 char_u *retvar;
15244
Bram Moolenaar33570922005-01-25 22:26:29 +000015245 v.di_tv.v_type = VAR_STRING;
15246 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 retvar = get_tv_string_chk(&argvars[1]);
15248 if (retvar != NULL)
15249 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015250 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015251 }
15252#else
15253 rettv->vval.v_number = -1;
15254#endif
15255}
15256
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257 static void
15258f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015259 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015261{
15262 char_u *r = NULL;
15263
15264#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015265 char_u *serverid = get_tv_string_chk(&argvars[0]);
15266
15267 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268 {
15269# ifdef WIN32
15270 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015271 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015273 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015274 if (n != 0)
15275 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15276 if (r == NULL)
15277# else
15278 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015279 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280# endif
15281 EMSG(_("E277: Unable to read a server reply"));
15282 }
15283#endif
15284 rettv->v_type = VAR_STRING;
15285 rettv->vval.v_string = r;
15286}
15287
15288/*
15289 * "remote_send()" function
15290 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015291 static void
15292f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015293 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015294 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015295{
15296 rettv->v_type = VAR_STRING;
15297 rettv->vval.v_string = NULL;
15298#ifdef FEAT_CLIENTSERVER
15299 remote_common(argvars, rettv, FALSE);
15300#endif
15301}
15302
15303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015304 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015305 */
15306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015307f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015308 typval_T *argvars;
15309 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015310{
Bram Moolenaar33570922005-01-25 22:26:29 +000015311 list_T *l;
15312 listitem_T *item, *item2;
15313 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015314 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015315 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015316 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015317 dict_T *d;
15318 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015319 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015320
Bram Moolenaar8c711452005-01-14 21:53:12 +000015321 if (argvars[0].v_type == VAR_DICT)
15322 {
15323 if (argvars[2].v_type != VAR_UNKNOWN)
15324 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015325 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015326 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 key = get_tv_string_chk(&argvars[1]);
15329 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015331 di = dict_find(d, key, -1);
15332 if (di == NULL)
15333 EMSG2(_(e_dictkey), key);
15334 else
15335 {
15336 *rettv = di->di_tv;
15337 init_tv(&di->di_tv);
15338 dictitem_remove(d, di);
15339 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015340 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015341 }
15342 }
15343 else if (argvars[0].v_type != VAR_LIST)
15344 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015345 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015346 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 int error = FALSE;
15349
15350 idx = get_tv_number_chk(&argvars[1], &error);
15351 if (error)
15352 ; /* type error: do nothing, errmsg already given */
15353 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015354 EMSGN(_(e_listidx), idx);
15355 else
15356 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015357 if (argvars[2].v_type == VAR_UNKNOWN)
15358 {
15359 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015360 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015361 *rettv = item->li_tv;
15362 vim_free(item);
15363 }
15364 else
15365 {
15366 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015367 end = get_tv_number_chk(&argvars[2], &error);
15368 if (error)
15369 ; /* type error: do nothing */
15370 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015371 EMSGN(_(e_listidx), end);
15372 else
15373 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015374 int cnt = 0;
15375
15376 for (li = item; li != NULL; li = li->li_next)
15377 {
15378 ++cnt;
15379 if (li == item2)
15380 break;
15381 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015382 if (li == NULL) /* didn't find "item2" after "item" */
15383 EMSG(_(e_invrange));
15384 else
15385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015386 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015387 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015388 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015389 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015390 l->lv_first = item;
15391 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015392 item->li_prev = NULL;
15393 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015394 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015395 }
15396 }
15397 }
15398 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399 }
15400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401}
15402
15403/*
15404 * "rename({from}, {to})" function
15405 */
15406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015407f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015408 typval_T *argvars;
15409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410{
15411 char_u buf[NUMBUFLEN];
15412
15413 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15417 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418}
15419
15420/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015421 * "repeat()" function
15422 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015423 static void
15424f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015425 typval_T *argvars;
15426 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015427{
15428 char_u *p;
15429 int n;
15430 int slen;
15431 int len;
15432 char_u *r;
15433 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015434
15435 n = get_tv_number(&argvars[1]);
15436 if (argvars[0].v_type == VAR_LIST)
15437 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015438 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015439 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015440 if (list_extend(rettv->vval.v_list,
15441 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015442 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015443 }
15444 else
15445 {
15446 p = get_tv_string(&argvars[0]);
15447 rettv->v_type = VAR_STRING;
15448 rettv->vval.v_string = NULL;
15449
15450 slen = (int)STRLEN(p);
15451 len = slen * n;
15452 if (len <= 0)
15453 return;
15454
15455 r = alloc(len + 1);
15456 if (r != NULL)
15457 {
15458 for (i = 0; i < n; i++)
15459 mch_memmove(r + i * slen, p, (size_t)slen);
15460 r[len] = NUL;
15461 }
15462
15463 rettv->vval.v_string = r;
15464 }
15465}
15466
15467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 * "resolve()" function
15469 */
15470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015471f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015472 typval_T *argvars;
15473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474{
15475 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015476#ifdef HAVE_READLINK
15477 char_u *buf = NULL;
15478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015480 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481#ifdef FEAT_SHORTCUT
15482 {
15483 char_u *v = NULL;
15484
15485 v = mch_resolve_shortcut(p);
15486 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015489 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 }
15491#else
15492# ifdef HAVE_READLINK
15493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 char_u *cpy;
15495 int len;
15496 char_u *remain = NULL;
15497 char_u *q;
15498 int is_relative_to_current = FALSE;
15499 int has_trailing_pathsep = FALSE;
15500 int limit = 100;
15501
15502 p = vim_strsave(p);
15503
15504 if (p[0] == '.' && (vim_ispathsep(p[1])
15505 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15506 is_relative_to_current = TRUE;
15507
15508 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015509 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015512 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514
15515 q = getnextcomp(p);
15516 if (*q != NUL)
15517 {
15518 /* Separate the first path component in "p", and keep the
15519 * remainder (beginning with the path separator). */
15520 remain = vim_strsave(q - 1);
15521 q[-1] = NUL;
15522 }
15523
Bram Moolenaard9462e32011-04-11 21:35:11 +020015524 buf = alloc(MAXPATHL + 1);
15525 if (buf == NULL)
15526 goto fail;
15527
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 for (;;)
15529 {
15530 for (;;)
15531 {
15532 len = readlink((char *)p, (char *)buf, MAXPATHL);
15533 if (len <= 0)
15534 break;
15535 buf[len] = NUL;
15536
15537 if (limit-- == 0)
15538 {
15539 vim_free(p);
15540 vim_free(remain);
15541 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 goto fail;
15544 }
15545
15546 /* Ensure that the result will have a trailing path separator
15547 * if the argument has one. */
15548 if (remain == NULL && has_trailing_pathsep)
15549 add_pathsep(buf);
15550
15551 /* Separate the first path component in the link value and
15552 * concatenate the remainders. */
15553 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15554 if (*q != NUL)
15555 {
15556 if (remain == NULL)
15557 remain = vim_strsave(q - 1);
15558 else
15559 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015560 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 if (cpy != NULL)
15562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 vim_free(remain);
15564 remain = cpy;
15565 }
15566 }
15567 q[-1] = NUL;
15568 }
15569
15570 q = gettail(p);
15571 if (q > p && *q == NUL)
15572 {
15573 /* Ignore trailing path separator. */
15574 q[-1] = NUL;
15575 q = gettail(p);
15576 }
15577 if (q > p && !mch_isFullName(buf))
15578 {
15579 /* symlink is relative to directory of argument */
15580 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15581 if (cpy != NULL)
15582 {
15583 STRCPY(cpy, p);
15584 STRCPY(gettail(cpy), buf);
15585 vim_free(p);
15586 p = cpy;
15587 }
15588 }
15589 else
15590 {
15591 vim_free(p);
15592 p = vim_strsave(buf);
15593 }
15594 }
15595
15596 if (remain == NULL)
15597 break;
15598
15599 /* Append the first path component of "remain" to "p". */
15600 q = getnextcomp(remain + 1);
15601 len = q - remain - (*q != NUL);
15602 cpy = vim_strnsave(p, STRLEN(p) + len);
15603 if (cpy != NULL)
15604 {
15605 STRNCAT(cpy, remain, len);
15606 vim_free(p);
15607 p = cpy;
15608 }
15609 /* Shorten "remain". */
15610 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015611 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 else
15613 {
15614 vim_free(remain);
15615 remain = NULL;
15616 }
15617 }
15618
15619 /* If the result is a relative path name, make it explicitly relative to
15620 * the current directory if and only if the argument had this form. */
15621 if (!vim_ispathsep(*p))
15622 {
15623 if (is_relative_to_current
15624 && *p != NUL
15625 && !(p[0] == '.'
15626 && (p[1] == NUL
15627 || vim_ispathsep(p[1])
15628 || (p[1] == '.'
15629 && (p[2] == NUL
15630 || vim_ispathsep(p[2]))))))
15631 {
15632 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015633 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 if (cpy != NULL)
15635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 vim_free(p);
15637 p = cpy;
15638 }
15639 }
15640 else if (!is_relative_to_current)
15641 {
15642 /* Strip leading "./". */
15643 q = p;
15644 while (q[0] == '.' && vim_ispathsep(q[1]))
15645 q += 2;
15646 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015647 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 }
15649 }
15650
15651 /* Ensure that the result will have no trailing path separator
15652 * if the argument had none. But keep "/" or "//". */
15653 if (!has_trailing_pathsep)
15654 {
15655 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015656 if (after_pathsep(p, q))
15657 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 }
15659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 }
15662# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664# endif
15665#endif
15666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015667 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668
15669#ifdef HAVE_READLINK
15670fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015671 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015673 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674}
15675
15676/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 */
15679 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015681 typval_T *argvars;
15682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683{
Bram Moolenaar33570922005-01-25 22:26:29 +000015684 list_T *l;
15685 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686
Bram Moolenaar0d660222005-01-07 21:51:51 +000015687 if (argvars[0].v_type != VAR_LIST)
15688 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015689 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015690 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015691 {
15692 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015693 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015694 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 while (li != NULL)
15696 {
15697 ni = li->li_prev;
15698 list_append(l, li);
15699 li = ni;
15700 }
15701 rettv->vval.v_list = l;
15702 rettv->v_type = VAR_LIST;
15703 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015704 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706}
15707
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015708#define SP_NOMOVE 0x01 /* don't move cursor */
15709#define SP_REPEAT 0x02 /* repeat to find outer pair */
15710#define SP_RETCOUNT 0x04 /* return matchcount */
15711#define SP_SETPCMARK 0x08 /* set previous context mark */
15712#define SP_START 0x10 /* accept match at start position */
15713#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15714#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015715
Bram Moolenaar33570922005-01-25 22:26:29 +000015716static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015717
15718/*
15719 * Get flags for a search function.
15720 * Possibly sets "p_ws".
15721 * Returns BACKWARD, FORWARD or zero (for an error).
15722 */
15723 static int
15724get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726 int *flagsp;
15727{
15728 int dir = FORWARD;
15729 char_u *flags;
15730 char_u nbuf[NUMBUFLEN];
15731 int mask;
15732
15733 if (varp->v_type != VAR_UNKNOWN)
15734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 flags = get_tv_string_buf_chk(varp, nbuf);
15736 if (flags == NULL)
15737 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738 while (*flags != NUL)
15739 {
15740 switch (*flags)
15741 {
15742 case 'b': dir = BACKWARD; break;
15743 case 'w': p_ws = TRUE; break;
15744 case 'W': p_ws = FALSE; break;
15745 default: mask = 0;
15746 if (flagsp != NULL)
15747 switch (*flags)
15748 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015749 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015750 case 'e': mask = SP_END; break;
15751 case 'm': mask = SP_RETCOUNT; break;
15752 case 'n': mask = SP_NOMOVE; break;
15753 case 'p': mask = SP_SUBPAT; break;
15754 case 'r': mask = SP_REPEAT; break;
15755 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015756 }
15757 if (mask == 0)
15758 {
15759 EMSG2(_(e_invarg2), flags);
15760 dir = 0;
15761 }
15762 else
15763 *flagsp |= mask;
15764 }
15765 if (dir == 0)
15766 break;
15767 ++flags;
15768 }
15769 }
15770 return dir;
15771}
15772
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015774 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015776 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015777search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015778 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015779 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015780 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015782 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 char_u *pat;
15784 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015785 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 int save_p_ws = p_ws;
15787 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015788 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015789 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015790 proftime_T tm;
15791#ifdef FEAT_RELTIME
15792 long time_limit = 0;
15793#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015794 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015795 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015797 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015798 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015799 if (dir == 0)
15800 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015801 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015802 if (flags & SP_START)
15803 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015804 if (flags & SP_END)
15805 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015806
Bram Moolenaar76929292008-01-06 19:07:36 +000015807 /* Optional arguments: line number to stop searching and timeout. */
15808 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015809 {
15810 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15811 if (lnum_stop < 0)
15812 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015813#ifdef FEAT_RELTIME
15814 if (argvars[3].v_type != VAR_UNKNOWN)
15815 {
15816 time_limit = get_tv_number_chk(&argvars[3], NULL);
15817 if (time_limit < 0)
15818 goto theend;
15819 }
15820#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015821 }
15822
Bram Moolenaar76929292008-01-06 19:07:36 +000015823#ifdef FEAT_RELTIME
15824 /* Set the time limit, if there is one. */
15825 profile_setlimit(time_limit, &tm);
15826#endif
15827
Bram Moolenaar231334e2005-07-25 20:46:57 +000015828 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015829 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015830 * Check to make sure only those flags are set.
15831 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15832 * flags cannot be set. Check for that condition also.
15833 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015834 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015835 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015836 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015837 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015838 goto theend;
15839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015841 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015842 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015843 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015844 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015846 if (flags & SP_SUBPAT)
15847 retval = subpatnum;
15848 else
15849 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015850 if (flags & SP_SETPCMARK)
15851 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015853 if (match_pos != NULL)
15854 {
15855 /* Store the match cursor position */
15856 match_pos->lnum = pos.lnum;
15857 match_pos->col = pos.col + 1;
15858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 /* "/$" will put the cursor after the end of the line, may need to
15860 * correct that here */
15861 check_cursor();
15862 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015863
15864 /* If 'n' flag is used: restore cursor position. */
15865 if (flags & SP_NOMOVE)
15866 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015867 else
15868 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015869theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015871
15872 return retval;
15873}
15874
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015875#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015876
15877/*
15878 * round() is not in C90, use ceil() or floor() instead.
15879 */
15880 float_T
15881vim_round(f)
15882 float_T f;
15883{
15884 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15885}
15886
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015887/*
15888 * "round({float})" function
15889 */
15890 static void
15891f_round(argvars, rettv)
15892 typval_T *argvars;
15893 typval_T *rettv;
15894{
15895 float_T f;
15896
15897 rettv->v_type = VAR_FLOAT;
15898 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015899 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015900 else
15901 rettv->vval.v_float = 0.0;
15902}
15903#endif
15904
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015905/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015906 * "screenattr()" function
15907 */
15908 static void
15909f_screenattr(argvars, rettv)
15910 typval_T *argvars UNUSED;
15911 typval_T *rettv;
15912{
15913 int row;
15914 int col;
15915 int c;
15916
15917 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15918 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15919 if (row < 0 || row >= screen_Rows
15920 || col < 0 || col >= screen_Columns)
15921 c = -1;
15922 else
15923 c = ScreenAttrs[LineOffset[row] + col];
15924 rettv->vval.v_number = c;
15925}
15926
15927/*
15928 * "screenchar()" function
15929 */
15930 static void
15931f_screenchar(argvars, rettv)
15932 typval_T *argvars UNUSED;
15933 typval_T *rettv;
15934{
15935 int row;
15936 int col;
15937 int off;
15938 int c;
15939
15940 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15941 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15942 if (row < 0 || row >= screen_Rows
15943 || col < 0 || col >= screen_Columns)
15944 c = -1;
15945 else
15946 {
15947 off = LineOffset[row] + col;
15948#ifdef FEAT_MBYTE
15949 if (enc_utf8 && ScreenLinesUC[off] != 0)
15950 c = ScreenLinesUC[off];
15951 else
15952#endif
15953 c = ScreenLines[off];
15954 }
15955 rettv->vval.v_number = c;
15956}
15957
15958/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015959 * "screencol()" function
15960 *
15961 * First column is 1 to be consistent with virtcol().
15962 */
15963 static void
15964f_screencol(argvars, rettv)
15965 typval_T *argvars UNUSED;
15966 typval_T *rettv;
15967{
15968 rettv->vval.v_number = screen_screencol() + 1;
15969}
15970
15971/*
15972 * "screenrow()" function
15973 */
15974 static void
15975f_screenrow(argvars, rettv)
15976 typval_T *argvars UNUSED;
15977 typval_T *rettv;
15978{
15979 rettv->vval.v_number = screen_screenrow() + 1;
15980}
15981
15982/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015983 * "search()" function
15984 */
15985 static void
15986f_search(argvars, rettv)
15987 typval_T *argvars;
15988 typval_T *rettv;
15989{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015990 int flags = 0;
15991
15992 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993}
15994
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015996 * "searchdecl()" function
15997 */
15998 static void
15999f_searchdecl(argvars, rettv)
16000 typval_T *argvars;
16001 typval_T *rettv;
16002{
16003 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016004 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016005 int error = FALSE;
16006 char_u *name;
16007
16008 rettv->vval.v_number = 1; /* default: FAIL */
16009
16010 name = get_tv_string_chk(&argvars[0]);
16011 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016012 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016013 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016014 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16015 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16016 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016017 if (!error && name != NULL)
16018 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016019 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016020}
16021
16022/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016023 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016025 static int
16026searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016027 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016028 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029{
16030 char_u *spat, *mpat, *epat;
16031 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 int dir;
16034 int flags = 0;
16035 char_u nbuf1[NUMBUFLEN];
16036 char_u nbuf2[NUMBUFLEN];
16037 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016038 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016039 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016040 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 spat = get_tv_string_chk(&argvars[0]);
16044 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16045 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16046 if (spat == NULL || mpat == NULL || epat == NULL)
16047 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 /* Handle the optional fourth argument: flags */
16050 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016051 if (dir == 0)
16052 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016053
16054 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016055 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16056 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016057 if ((flags & (SP_END | SP_SUBPAT)) != 0
16058 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016059 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016060 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016061 goto theend;
16062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016063
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016064 /* Using 'r' implies 'W', otherwise it doesn't work. */
16065 if (flags & SP_REPEAT)
16066 p_ws = FALSE;
16067
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016068 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016069 if (argvars[3].v_type == VAR_UNKNOWN
16070 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 skip = (char_u *)"";
16072 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016074 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016075 if (argvars[5].v_type != VAR_UNKNOWN)
16076 {
16077 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16078 if (lnum_stop < 0)
16079 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016080#ifdef FEAT_RELTIME
16081 if (argvars[6].v_type != VAR_UNKNOWN)
16082 {
16083 time_limit = get_tv_number_chk(&argvars[6], NULL);
16084 if (time_limit < 0)
16085 goto theend;
16086 }
16087#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016088 }
16089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016090 if (skip == NULL)
16091 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016093 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016094 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016095
16096theend:
16097 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016098
16099 return retval;
16100}
16101
16102/*
16103 * "searchpair()" function
16104 */
16105 static void
16106f_searchpair(argvars, rettv)
16107 typval_T *argvars;
16108 typval_T *rettv;
16109{
16110 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16111}
16112
16113/*
16114 * "searchpairpos()" function
16115 */
16116 static void
16117f_searchpairpos(argvars, rettv)
16118 typval_T *argvars;
16119 typval_T *rettv;
16120{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016121 pos_T match_pos;
16122 int lnum = 0;
16123 int col = 0;
16124
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016125 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016126 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127
16128 if (searchpair_cmn(argvars, &match_pos) > 0)
16129 {
16130 lnum = match_pos.lnum;
16131 col = match_pos.col;
16132 }
16133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016134 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16135 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016136}
16137
16138/*
16139 * Search for a start/middle/end thing.
16140 * Used by searchpair(), see its documentation for the details.
16141 * Returns 0 or -1 for no match,
16142 */
16143 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016144do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16145 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016146 char_u *spat; /* start pattern */
16147 char_u *mpat; /* middle pattern */
16148 char_u *epat; /* end pattern */
16149 int dir; /* BACKWARD or FORWARD */
16150 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016151 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016152 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016153 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016154 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016155{
16156 char_u *save_cpo;
16157 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16158 long retval = 0;
16159 pos_T pos;
16160 pos_T firstpos;
16161 pos_T foundpos;
16162 pos_T save_cursor;
16163 pos_T save_pos;
16164 int n;
16165 int r;
16166 int nest = 1;
16167 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016168 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016169 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016170
16171 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16172 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016173 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016174
Bram Moolenaar76929292008-01-06 19:07:36 +000016175#ifdef FEAT_RELTIME
16176 /* Set the time limit, if there is one. */
16177 profile_setlimit(time_limit, &tm);
16178#endif
16179
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016180 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16181 * start/middle/end (pat3, for the top pair). */
16182 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16183 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16184 if (pat2 == NULL || pat3 == NULL)
16185 goto theend;
16186 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16187 if (*mpat == NUL)
16188 STRCPY(pat3, pat2);
16189 else
16190 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16191 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016192 if (flags & SP_START)
16193 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016194
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 save_cursor = curwin->w_cursor;
16196 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016197 clearpos(&firstpos);
16198 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 pat = pat3;
16200 for (;;)
16201 {
16202 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016203 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16205 /* didn't find it or found the first match again: FAIL */
16206 break;
16207
16208 if (firstpos.lnum == 0)
16209 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016210 if (equalpos(pos, foundpos))
16211 {
16212 /* Found the same position again. Can happen with a pattern that
16213 * has "\zs" at the end and searching backwards. Advance one
16214 * character and try again. */
16215 if (dir == BACKWARD)
16216 decl(&pos);
16217 else
16218 incl(&pos);
16219 }
16220 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016222 /* clear the start flag to avoid getting stuck here */
16223 options &= ~SEARCH_START;
16224
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 /* If the skip pattern matches, ignore this match. */
16226 if (*skip != NUL)
16227 {
16228 save_pos = curwin->w_cursor;
16229 curwin->w_cursor = pos;
16230 r = eval_to_bool(skip, &err, NULL, FALSE);
16231 curwin->w_cursor = save_pos;
16232 if (err)
16233 {
16234 /* Evaluating {skip} caused an error, break here. */
16235 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016236 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237 break;
16238 }
16239 if (r)
16240 continue;
16241 }
16242
16243 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16244 {
16245 /* Found end when searching backwards or start when searching
16246 * forward: nested pair. */
16247 ++nest;
16248 pat = pat2; /* nested, don't search for middle */
16249 }
16250 else
16251 {
16252 /* Found end when searching forward or start when searching
16253 * backward: end of (nested) pair; or found middle in outer pair. */
16254 if (--nest == 1)
16255 pat = pat3; /* outer level, search for middle */
16256 }
16257
16258 if (nest == 0)
16259 {
16260 /* Found the match: return matchcount or line number. */
16261 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016262 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016264 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016265 if (flags & SP_SETPCMARK)
16266 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 curwin->w_cursor = pos;
16268 if (!(flags & SP_REPEAT))
16269 break;
16270 nest = 1; /* search for next unmatched */
16271 }
16272 }
16273
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016274 if (match_pos != NULL)
16275 {
16276 /* Store the match cursor position */
16277 match_pos->lnum = curwin->w_cursor.lnum;
16278 match_pos->col = curwin->w_cursor.col + 1;
16279 }
16280
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016282 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283 curwin->w_cursor = save_cursor;
16284
16285theend:
16286 vim_free(pat2);
16287 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016288 if (p_cpo == empty_option)
16289 p_cpo = save_cpo;
16290 else
16291 /* Darn, evaluating the {skip} expression changed the value. */
16292 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016293
16294 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295}
16296
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016297/*
16298 * "searchpos()" function
16299 */
16300 static void
16301f_searchpos(argvars, rettv)
16302 typval_T *argvars;
16303 typval_T *rettv;
16304{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016305 pos_T match_pos;
16306 int lnum = 0;
16307 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016308 int n;
16309 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016311 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016312 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016313
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016314 n = search_cmn(argvars, &match_pos, &flags);
16315 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016316 {
16317 lnum = match_pos.lnum;
16318 col = match_pos.col;
16319 }
16320
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016321 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16322 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016323 if (flags & SP_SUBPAT)
16324 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016325}
16326
16327
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 static void
16329f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016330 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016331 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333#ifdef FEAT_CLIENTSERVER
16334 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 char_u *server = get_tv_string_chk(&argvars[0]);
16336 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 if (server == NULL || reply == NULL)
16340 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341 if (check_restricted() || check_secure())
16342 return;
16343# ifdef FEAT_X11
16344 if (check_connection() == FAIL)
16345 return;
16346# endif
16347
16348 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350 EMSG(_("E258: Unable to send to client"));
16351 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353 rettv->vval.v_number = 0;
16354#else
16355 rettv->vval.v_number = -1;
16356#endif
16357}
16358
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359 static void
16360f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016361 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363{
16364 char_u *r = NULL;
16365
16366#ifdef FEAT_CLIENTSERVER
16367# ifdef WIN32
16368 r = serverGetVimNames();
16369# else
16370 make_connection();
16371 if (X_DISPLAY != NULL)
16372 r = serverGetVimNames(X_DISPLAY);
16373# endif
16374#endif
16375 rettv->v_type = VAR_STRING;
16376 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377}
16378
16379/*
16380 * "setbufvar()" function
16381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016383f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016384 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016385 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386{
16387 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 char_u nbuf[NUMBUFLEN];
16392
16393 if (check_restricted() || check_secure())
16394 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16396 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016397 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016398 varp = &argvars[2];
16399
16400 if (buf != NULL && varname != NULL && varp != NULL)
16401 {
16402 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404
16405 if (*varname == '&')
16406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 long numval;
16408 char_u *strval;
16409 int error = FALSE;
16410
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 numval = get_tv_number_chk(varp, &error);
16413 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016414 if (!error && strval != NULL)
16415 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 }
16417 else
16418 {
16419 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16420 if (bufvarname != NULL)
16421 {
16422 STRCPY(bufvarname, "b:");
16423 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016424 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 vim_free(bufvarname);
16426 }
16427 }
16428
16429 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432}
16433
16434/*
16435 * "setcmdpos()" function
16436 */
16437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016439 typval_T *argvars;
16440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016441{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016442 int pos = (int)get_tv_number(&argvars[0]) - 1;
16443
16444 if (pos >= 0)
16445 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446}
16447
16448/*
16449 * "setline()" function
16450 */
16451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016452f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016453 typval_T *argvars;
16454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016455{
16456 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016457 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016458 list_T *l = NULL;
16459 listitem_T *li = NULL;
16460 long added = 0;
16461 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016463 lnum = get_tv_lnum(&argvars[0]);
16464 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016465 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016466 l = argvars[1].vval.v_list;
16467 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016469 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016470 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016471
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016472 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016473 for (;;)
16474 {
16475 if (l != NULL)
16476 {
16477 /* list argument, get next string */
16478 if (li == NULL)
16479 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016480 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016481 li = li->li_next;
16482 }
16483
16484 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016486 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016487
16488 /* When coming here from Insert mode, sync undo, so that this can be
16489 * undone separately from what was previously inserted. */
16490 if (u_sync_once == 2)
16491 {
16492 u_sync_once = 1; /* notify that u_sync() was called */
16493 u_sync(TRUE);
16494 }
16495
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016496 if (lnum <= curbuf->b_ml.ml_line_count)
16497 {
16498 /* existing line, replace it */
16499 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16500 {
16501 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016502 if (lnum == curwin->w_cursor.lnum)
16503 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016504 rettv->vval.v_number = 0; /* OK */
16505 }
16506 }
16507 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16508 {
16509 /* lnum is one past the last line, append the line */
16510 ++added;
16511 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16512 rettv->vval.v_number = 0; /* OK */
16513 }
16514
16515 if (l == NULL) /* only one string argument */
16516 break;
16517 ++lnum;
16518 }
16519
16520 if (added > 0)
16521 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016522}
16523
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016524static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16525
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016527 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016528 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016529 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016530set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016531 win_T *wp UNUSED;
16532 typval_T *list_arg UNUSED;
16533 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016534 typval_T *rettv;
16535{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016536#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016537 char_u *act;
16538 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016539#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016540
Bram Moolenaar2641f772005-03-25 21:58:17 +000016541 rettv->vval.v_number = -1;
16542
16543#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016544 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016545 EMSG(_(e_listreq));
16546 else
16547 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016548 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016549
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016550 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016551 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016552 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016553 if (act == NULL)
16554 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016555 if (*act == 'a' || *act == 'r')
16556 action = *act;
16557 }
16558
Bram Moolenaar81484f42012-12-05 15:16:47 +010016559 if (l != NULL && set_errorlist(wp, l, action,
16560 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016561 rettv->vval.v_number = 0;
16562 }
16563#endif
16564}
16565
16566/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016567 * "setloclist()" function
16568 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016569 static void
16570f_setloclist(argvars, rettv)
16571 typval_T *argvars;
16572 typval_T *rettv;
16573{
16574 win_T *win;
16575
16576 rettv->vval.v_number = -1;
16577
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016578 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016579 if (win != NULL)
16580 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16581}
16582
16583/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016584 * "setmatches()" function
16585 */
16586 static void
16587f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016588 typval_T *argvars UNUSED;
16589 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016590{
16591#ifdef FEAT_SEARCH_EXTRA
16592 list_T *l;
16593 listitem_T *li;
16594 dict_T *d;
16595
16596 rettv->vval.v_number = -1;
16597 if (argvars[0].v_type != VAR_LIST)
16598 {
16599 EMSG(_(e_listreq));
16600 return;
16601 }
16602 if ((l = argvars[0].vval.v_list) != NULL)
16603 {
16604
16605 /* To some extent make sure that we are dealing with a list from
16606 * "getmatches()". */
16607 li = l->lv_first;
16608 while (li != NULL)
16609 {
16610 if (li->li_tv.v_type != VAR_DICT
16611 || (d = li->li_tv.vval.v_dict) == NULL)
16612 {
16613 EMSG(_(e_invarg));
16614 return;
16615 }
16616 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16617 && dict_find(d, (char_u *)"pattern", -1) != NULL
16618 && dict_find(d, (char_u *)"priority", -1) != NULL
16619 && dict_find(d, (char_u *)"id", -1) != NULL))
16620 {
16621 EMSG(_(e_invarg));
16622 return;
16623 }
16624 li = li->li_next;
16625 }
16626
16627 clear_matches(curwin);
16628 li = l->lv_first;
16629 while (li != NULL)
16630 {
16631 d = li->li_tv.vval.v_dict;
16632 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16633 get_dict_string(d, (char_u *)"pattern", FALSE),
16634 (int)get_dict_number(d, (char_u *)"priority"),
16635 (int)get_dict_number(d, (char_u *)"id"));
16636 li = li->li_next;
16637 }
16638 rettv->vval.v_number = 0;
16639 }
16640#endif
16641}
16642
16643/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016644 * "setpos()" function
16645 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016646 static void
16647f_setpos(argvars, rettv)
16648 typval_T *argvars;
16649 typval_T *rettv;
16650{
16651 pos_T pos;
16652 int fnum;
16653 char_u *name;
16654
Bram Moolenaar08250432008-02-13 11:42:46 +000016655 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016656 name = get_tv_string_chk(argvars);
16657 if (name != NULL)
16658 {
16659 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16660 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016661 if (--pos.col < 0)
16662 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016663 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016664 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016665 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016666 if (fnum == curbuf->b_fnum)
16667 {
16668 curwin->w_cursor = pos;
16669 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016670 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016671 }
16672 else
16673 EMSG(_(e_invarg));
16674 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016675 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16676 {
16677 /* set mark */
16678 if (setmark_pos(name[1], &pos, fnum) == OK)
16679 rettv->vval.v_number = 0;
16680 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016681 else
16682 EMSG(_(e_invarg));
16683 }
16684 }
16685}
16686
16687/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016688 * "setqflist()" function
16689 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016690 static void
16691f_setqflist(argvars, rettv)
16692 typval_T *argvars;
16693 typval_T *rettv;
16694{
16695 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16696}
16697
16698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 * "setreg()" function
16700 */
16701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016702f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016703 typval_T *argvars;
16704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705{
16706 int regname;
16707 char_u *strregname;
16708 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 int append;
16711 char_u yank_type;
16712 long block_len;
16713
16714 block_len = -1;
16715 yank_type = MAUTO;
16716 append = FALSE;
16717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016718 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 if (strregname == NULL)
16722 return; /* type error; errmsg already given */
16723 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 if (regname == 0 || regname == '@')
16725 regname = '"';
16726 else if (regname == '=')
16727 return;
16728
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016729 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 stropt = get_tv_string_chk(&argvars[2]);
16732 if (stropt == NULL)
16733 return; /* type error */
16734 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735 switch (*stropt)
16736 {
16737 case 'a': case 'A': /* append */
16738 append = TRUE;
16739 break;
16740 case 'v': case 'c': /* character-wise selection */
16741 yank_type = MCHAR;
16742 break;
16743 case 'V': case 'l': /* line-wise selection */
16744 yank_type = MLINE;
16745 break;
16746#ifdef FEAT_VISUAL
16747 case 'b': case Ctrl_V: /* block-wise selection */
16748 yank_type = MBLOCK;
16749 if (VIM_ISDIGIT(stropt[1]))
16750 {
16751 ++stropt;
16752 block_len = getdigits(&stropt) - 1;
16753 --stropt;
16754 }
16755 break;
16756#endif
16757 }
16758 }
16759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016760 strval = get_tv_string_chk(&argvars[1]);
16761 if (strval != NULL)
16762 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016764 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765}
16766
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016767/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016768 * "settabvar()" function
16769 */
16770 static void
16771f_settabvar(argvars, rettv)
16772 typval_T *argvars;
16773 typval_T *rettv;
16774{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016775#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016776 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016777 tabpage_T *tp;
16778#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016779 char_u *varname, *tabvarname;
16780 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016781
16782 rettv->vval.v_number = 0;
16783
16784 if (check_restricted() || check_secure())
16785 return;
16786
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016787#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016788 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016789#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016790 varname = get_tv_string_chk(&argvars[1]);
16791 varp = &argvars[2];
16792
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016793 if (varname != NULL && varp != NULL
16794#ifdef FEAT_WINDOWS
16795 && tp != NULL
16796#endif
16797 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016798 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016799#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016800 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016801 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016802#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016803
16804 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16805 if (tabvarname != NULL)
16806 {
16807 STRCPY(tabvarname, "t:");
16808 STRCPY(tabvarname + 2, varname);
16809 set_var(tabvarname, varp, TRUE);
16810 vim_free(tabvarname);
16811 }
16812
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016813#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016814 /* Restore current tabpage */
16815 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016816 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016817#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016818 }
16819}
16820
16821/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016822 * "settabwinvar()" function
16823 */
16824 static void
16825f_settabwinvar(argvars, rettv)
16826 typval_T *argvars;
16827 typval_T *rettv;
16828{
16829 setwinvar(argvars, rettv, 1);
16830}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831
16832/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016833 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016836f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016837 typval_T *argvars;
16838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016840 setwinvar(argvars, rettv, 0);
16841}
16842
16843/*
16844 * "setwinvar()" and "settabwinvar()" functions
16845 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016846
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016847 static void
16848setwinvar(argvars, rettv, off)
16849 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016850 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016851 int off;
16852{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 win_T *win;
16854#ifdef FEAT_WINDOWS
16855 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016856 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857#endif
16858 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016859 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016861 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862
16863 if (check_restricted() || check_secure())
16864 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016865
16866#ifdef FEAT_WINDOWS
16867 if (off == 1)
16868 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16869 else
16870 tp = curtab;
16871#endif
16872 win = find_win_by_nr(&argvars[off], tp);
16873 varname = get_tv_string_chk(&argvars[off + 1]);
16874 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875
16876 if (win != NULL && varname != NULL && varp != NULL)
16877 {
16878#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016879 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881#endif
16882
16883 if (*varname == '&')
16884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016885 long numval;
16886 char_u *strval;
16887 int error = FALSE;
16888
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016890 numval = get_tv_number_chk(varp, &error);
16891 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016892 if (!error && strval != NULL)
16893 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 }
16895 else
16896 {
16897 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16898 if (winvarname != NULL)
16899 {
16900 STRCPY(winvarname, "w:");
16901 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016902 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 vim_free(winvarname);
16904 }
16905 }
16906
16907#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016908 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909#endif
16910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911}
16912
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016913#ifdef FEAT_CRYPT
16914/*
16915 * "sha256({string})" function
16916 */
16917 static void
16918f_sha256(argvars, rettv)
16919 typval_T *argvars;
16920 typval_T *rettv;
16921{
16922 char_u *p;
16923
16924 p = get_tv_string(&argvars[0]);
16925 rettv->vval.v_string = vim_strsave(
16926 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16927 rettv->v_type = VAR_STRING;
16928}
16929#endif /* FEAT_CRYPT */
16930
Bram Moolenaar071d4272004-06-13 20:20:40 +000016931/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016932 * "shellescape({string})" function
16933 */
16934 static void
16935f_shellescape(argvars, rettv)
16936 typval_T *argvars;
16937 typval_T *rettv;
16938{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016939 rettv->vval.v_string = vim_strsave_shellescape(
16940 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016941 rettv->v_type = VAR_STRING;
16942}
16943
16944/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016945 * shiftwidth() function
16946 */
16947 static void
16948f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016949 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016950 typval_T *rettv;
16951{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016952 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016953}
16954
16955/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 */
16958 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 typval_T *argvars;
16961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 p = get_tv_string(&argvars[0]);
16966 rettv->vval.v_string = vim_strsave(p);
16967 simplify_filename(rettv->vval.v_string); /* simplify in place */
16968 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969}
16970
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016971#ifdef FEAT_FLOAT
16972/*
16973 * "sin()" function
16974 */
16975 static void
16976f_sin(argvars, rettv)
16977 typval_T *argvars;
16978 typval_T *rettv;
16979{
16980 float_T f;
16981
16982 rettv->v_type = VAR_FLOAT;
16983 if (get_float_arg(argvars, &f) == OK)
16984 rettv->vval.v_float = sin(f);
16985 else
16986 rettv->vval.v_float = 0.0;
16987}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016988
16989/*
16990 * "sinh()" function
16991 */
16992 static void
16993f_sinh(argvars, rettv)
16994 typval_T *argvars;
16995 typval_T *rettv;
16996{
16997 float_T f;
16998
16999 rettv->v_type = VAR_FLOAT;
17000 if (get_float_arg(argvars, &f) == OK)
17001 rettv->vval.v_float = sinh(f);
17002 else
17003 rettv->vval.v_float = 0.0;
17004}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017005#endif
17006
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007static int
17008#ifdef __BORLANDC__
17009 _RTLENTRYF
17010#endif
17011 item_compare __ARGS((const void *s1, const void *s2));
17012static int
17013#ifdef __BORLANDC__
17014 _RTLENTRYF
17015#endif
17016 item_compare2 __ARGS((const void *s1, const void *s2));
17017
17018static int item_compare_ic;
17019static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017020static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022#define ITEM_COMPARE_FAIL 999
17023
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027 static int
17028#ifdef __BORLANDC__
17029_RTLENTRYF
17030#endif
17031item_compare(s1, s2)
17032 const void *s1;
17033 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035 char_u *p1, *p2;
17036 char_u *tofree1, *tofree2;
17037 int res;
17038 char_u numbuf1[NUMBUFLEN];
17039 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017041 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17042 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017043 if (p1 == NULL)
17044 p1 = (char_u *)"";
17045 if (p2 == NULL)
17046 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047 if (item_compare_ic)
17048 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050 res = STRCMP(p1, p2);
17051 vim_free(tofree1);
17052 vim_free(tofree2);
17053 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054}
17055
17056 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057#ifdef __BORLANDC__
17058_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060item_compare2(s1, s2)
17061 const void *s1;
17062 const void *s2;
17063{
17064 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017066 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017067 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 /* shortcut after failure in previous call; compare all items equal */
17070 if (item_compare_func_err)
17071 return 0;
17072
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017073 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17074 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017075 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17076 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017077
17078 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017079 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017080 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17081 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 clear_tv(&argv[0]);
17083 clear_tv(&argv[1]);
17084
17085 if (res == FAIL)
17086 res = ITEM_COMPARE_FAIL;
17087 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17089 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017090 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 clear_tv(&rettv);
17092 return res;
17093}
17094
17095/*
17096 * "sort({list})" function
17097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017100 typval_T *argvars;
17101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102{
Bram Moolenaar33570922005-01-25 22:26:29 +000017103 list_T *l;
17104 listitem_T *li;
17105 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 long len;
17107 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 if (argvars[0].v_type != VAR_LIST)
17110 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111 else
17112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017114 if (l == NULL || tv_check_lock(l->lv_lock,
17115 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 return;
17117 rettv->vval.v_list = l;
17118 rettv->v_type = VAR_LIST;
17119 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121 len = list_len(l);
17122 if (len <= 1)
17123 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 item_compare_ic = FALSE;
17126 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017127 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 if (argvars[1].v_type != VAR_UNKNOWN)
17129 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017130 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133 else
17134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 int error = FALSE;
17136
17137 i = get_tv_number_chk(&argvars[1], &error);
17138 if (error)
17139 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140 if (i == 1)
17141 item_compare_ic = TRUE;
17142 else
17143 item_compare_func = get_tv_string(&argvars[1]);
17144 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017145
17146 if (argvars[2].v_type != VAR_UNKNOWN)
17147 {
17148 /* optional third argument: {dict} */
17149 if (argvars[2].v_type != VAR_DICT)
17150 {
17151 EMSG(_(e_dictreq));
17152 return;
17153 }
17154 item_compare_selfdict = argvars[2].vval.v_dict;
17155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157
Bram Moolenaar0d660222005-01-07 21:51:51 +000017158 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 if (ptrs == NULL)
17161 return;
17162 i = 0;
17163 for (li = l->lv_first; li != NULL; li = li->li_next)
17164 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017167 /* test the compare function */
17168 if (item_compare_func != NULL
17169 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17170 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017171 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 {
17174 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017175 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176 item_compare_func == NULL ? item_compare : item_compare2);
17177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017178 if (!item_compare_func_err)
17179 {
17180 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017181 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 l->lv_len = 0;
17183 for (i = 0; i < len; ++i)
17184 list_append(l, ptrs[i]);
17185 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017186 }
17187
17188 vim_free(ptrs);
17189 }
17190}
17191
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017192/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017193 * "soundfold({word})" function
17194 */
17195 static void
17196f_soundfold(argvars, rettv)
17197 typval_T *argvars;
17198 typval_T *rettv;
17199{
17200 char_u *s;
17201
17202 rettv->v_type = VAR_STRING;
17203 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017204#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017205 rettv->vval.v_string = eval_soundfold(s);
17206#else
17207 rettv->vval.v_string = vim_strsave(s);
17208#endif
17209}
17210
17211/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017212 * "spellbadword()" function
17213 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017214 static void
17215f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017216 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017217 typval_T *rettv;
17218{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017219 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017220 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017221 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017222
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017223 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017224 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017225
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017226#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017227 if (argvars[0].v_type == VAR_UNKNOWN)
17228 {
17229 /* Find the start and length of the badly spelled word. */
17230 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17231 if (len != 0)
17232 word = ml_get_cursor();
17233 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017234 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017235 {
17236 char_u *str = get_tv_string_chk(&argvars[0]);
17237 int capcol = -1;
17238
17239 if (str != NULL)
17240 {
17241 /* Check the argument for spelling. */
17242 while (*str != NUL)
17243 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017244 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017245 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017246 {
17247 word = str;
17248 break;
17249 }
17250 str += len;
17251 }
17252 }
17253 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017254#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017255
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017256 list_append_string(rettv->vval.v_list, word, len);
17257 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017258 attr == HLF_SPB ? "bad" :
17259 attr == HLF_SPR ? "rare" :
17260 attr == HLF_SPL ? "local" :
17261 attr == HLF_SPC ? "caps" :
17262 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017263}
17264
17265/*
17266 * "spellsuggest()" function
17267 */
17268 static void
17269f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017270 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017271 typval_T *rettv;
17272{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017273#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017274 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017275 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017276 int maxcount;
17277 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017278 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017279 listitem_T *li;
17280 int need_capital = FALSE;
17281#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017282
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017283 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017284 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017285
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017286#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017287 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017288 {
17289 str = get_tv_string(&argvars[0]);
17290 if (argvars[1].v_type != VAR_UNKNOWN)
17291 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017292 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017293 if (maxcount <= 0)
17294 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017295 if (argvars[2].v_type != VAR_UNKNOWN)
17296 {
17297 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17298 if (typeerr)
17299 return;
17300 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017301 }
17302 else
17303 maxcount = 25;
17304
Bram Moolenaar4770d092006-01-12 23:22:24 +000017305 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017306
17307 for (i = 0; i < ga.ga_len; ++i)
17308 {
17309 str = ((char_u **)ga.ga_data)[i];
17310
17311 li = listitem_alloc();
17312 if (li == NULL)
17313 vim_free(str);
17314 else
17315 {
17316 li->li_tv.v_type = VAR_STRING;
17317 li->li_tv.v_lock = 0;
17318 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017319 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017320 }
17321 }
17322 ga_clear(&ga);
17323 }
17324#endif
17325}
17326
Bram Moolenaar0d660222005-01-07 21:51:51 +000017327 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017328f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017329 typval_T *argvars;
17330 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017331{
17332 char_u *str;
17333 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017334 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017335 regmatch_T regmatch;
17336 char_u patbuf[NUMBUFLEN];
17337 char_u *save_cpo;
17338 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017340 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017341 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017342
17343 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17344 save_cpo = p_cpo;
17345 p_cpo = (char_u *)"";
17346
17347 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017348 if (argvars[1].v_type != VAR_UNKNOWN)
17349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017350 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17351 if (pat == NULL)
17352 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017353 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017354 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017355 }
17356 if (pat == NULL || *pat == NUL)
17357 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017358
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017359 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017361 if (typeerr)
17362 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17365 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017367 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017368 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017369 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017370 if (*str == NUL)
17371 match = FALSE; /* empty item at the end */
17372 else
17373 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017374 if (match)
17375 end = regmatch.startp[0];
17376 else
17377 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017378 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17379 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017380 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017381 if (list_append_string(rettv->vval.v_list, str,
17382 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017384 }
17385 if (!match)
17386 break;
17387 /* Advance to just after the match. */
17388 if (regmatch.endp[0] > str)
17389 col = 0;
17390 else
17391 {
17392 /* Don't get stuck at the same match. */
17393#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017394 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017395#else
17396 col = 1;
17397#endif
17398 }
17399 str = regmatch.endp[0];
17400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401
Bram Moolenaar473de612013-06-08 18:19:48 +020017402 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406}
17407
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017408#ifdef FEAT_FLOAT
17409/*
17410 * "sqrt()" function
17411 */
17412 static void
17413f_sqrt(argvars, rettv)
17414 typval_T *argvars;
17415 typval_T *rettv;
17416{
17417 float_T f;
17418
17419 rettv->v_type = VAR_FLOAT;
17420 if (get_float_arg(argvars, &f) == OK)
17421 rettv->vval.v_float = sqrt(f);
17422 else
17423 rettv->vval.v_float = 0.0;
17424}
17425
17426/*
17427 * "str2float()" function
17428 */
17429 static void
17430f_str2float(argvars, rettv)
17431 typval_T *argvars;
17432 typval_T *rettv;
17433{
17434 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17435
17436 if (*p == '+')
17437 p = skipwhite(p + 1);
17438 (void)string2float(p, &rettv->vval.v_float);
17439 rettv->v_type = VAR_FLOAT;
17440}
17441#endif
17442
Bram Moolenaar2c932302006-03-18 21:42:09 +000017443/*
17444 * "str2nr()" function
17445 */
17446 static void
17447f_str2nr(argvars, rettv)
17448 typval_T *argvars;
17449 typval_T *rettv;
17450{
17451 int base = 10;
17452 char_u *p;
17453 long n;
17454
17455 if (argvars[1].v_type != VAR_UNKNOWN)
17456 {
17457 base = get_tv_number(&argvars[1]);
17458 if (base != 8 && base != 10 && base != 16)
17459 {
17460 EMSG(_(e_invarg));
17461 return;
17462 }
17463 }
17464
17465 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017466 if (*p == '+')
17467 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017468 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17469 rettv->vval.v_number = n;
17470}
17471
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472#ifdef HAVE_STRFTIME
17473/*
17474 * "strftime({format}[, {time}])" function
17475 */
17476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017477f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 typval_T *argvars;
17479 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480{
17481 char_u result_buf[256];
17482 struct tm *curtime;
17483 time_t seconds;
17484 char_u *p;
17485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017486 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017489 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 seconds = time(NULL);
17491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 curtime = localtime(&seconds);
17494 /* MSVC returns NULL for an invalid value of seconds. */
17495 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 else
17498 {
17499# ifdef FEAT_MBYTE
17500 vimconv_T conv;
17501 char_u *enc;
17502
17503 conv.vc_type = CONV_NONE;
17504 enc = enc_locale();
17505 convert_setup(&conv, p_enc, enc);
17506 if (conv.vc_type != CONV_NONE)
17507 p = string_convert(&conv, p, NULL);
17508# endif
17509 if (p != NULL)
17510 (void)strftime((char *)result_buf, sizeof(result_buf),
17511 (char *)p, curtime);
17512 else
17513 result_buf[0] = NUL;
17514
17515# ifdef FEAT_MBYTE
17516 if (conv.vc_type != CONV_NONE)
17517 vim_free(p);
17518 convert_setup(&conv, enc, p_enc);
17519 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 else
17522# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524
17525# ifdef FEAT_MBYTE
17526 /* Release conversion descriptors */
17527 convert_setup(&conv, NULL, NULL);
17528 vim_free(enc);
17529# endif
17530 }
17531}
17532#endif
17533
17534/*
17535 * "stridx()" function
17536 */
17537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017538f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017539 typval_T *argvars;
17540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541{
17542 char_u buf[NUMBUFLEN];
17543 char_u *needle;
17544 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017545 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017547 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017549 needle = get_tv_string_chk(&argvars[1]);
17550 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017551 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017552 if (needle == NULL || haystack == NULL)
17553 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 if (argvars[2].v_type != VAR_UNKNOWN)
17556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017557 int error = FALSE;
17558
17559 start_idx = get_tv_number_chk(&argvars[2], &error);
17560 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017562 if (start_idx >= 0)
17563 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 }
17565
17566 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17567 if (pos != NULL)
17568 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569}
17570
17571/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017572 * "string()" function
17573 */
17574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017575f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017576 typval_T *argvars;
17577 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017578{
17579 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017580 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017583 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017584 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017585 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587}
17588
17589/*
17590 * "strlen()" function
17591 */
17592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017593f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017594 typval_T *argvars;
17595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017597 rettv->vval.v_number = (varnumber_T)(STRLEN(
17598 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599}
17600
17601/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017602 * "strchars()" function
17603 */
17604 static void
17605f_strchars(argvars, rettv)
17606 typval_T *argvars;
17607 typval_T *rettv;
17608{
17609 char_u *s = get_tv_string(&argvars[0]);
17610#ifdef FEAT_MBYTE
17611 varnumber_T len = 0;
17612
17613 while (*s != NUL)
17614 {
17615 mb_cptr2char_adv(&s);
17616 ++len;
17617 }
17618 rettv->vval.v_number = len;
17619#else
17620 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17621#endif
17622}
17623
17624/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017625 * "strdisplaywidth()" function
17626 */
17627 static void
17628f_strdisplaywidth(argvars, rettv)
17629 typval_T *argvars;
17630 typval_T *rettv;
17631{
17632 char_u *s = get_tv_string(&argvars[0]);
17633 int col = 0;
17634
17635 if (argvars[1].v_type != VAR_UNKNOWN)
17636 col = get_tv_number(&argvars[1]);
17637
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017638 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017639}
17640
17641/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017642 * "strwidth()" function
17643 */
17644 static void
17645f_strwidth(argvars, rettv)
17646 typval_T *argvars;
17647 typval_T *rettv;
17648{
17649 char_u *s = get_tv_string(&argvars[0]);
17650
17651 rettv->vval.v_number = (varnumber_T)(
17652#ifdef FEAT_MBYTE
17653 mb_string2cells(s, -1)
17654#else
17655 STRLEN(s)
17656#endif
17657 );
17658}
17659
17660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 * "strpart()" function
17662 */
17663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017664f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 typval_T *argvars;
17666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667{
17668 char_u *p;
17669 int n;
17670 int len;
17671 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017672 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017674 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 slen = (int)STRLEN(p);
17676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017677 n = get_tv_number_chk(&argvars[1], &error);
17678 if (error)
17679 len = 0;
17680 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017681 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 else
17683 len = slen - n; /* default len: all bytes that are available. */
17684
17685 /*
17686 * Only return the overlap between the specified part and the actual
17687 * string.
17688 */
17689 if (n < 0)
17690 {
17691 len += n;
17692 n = 0;
17693 }
17694 else if (n > slen)
17695 n = slen;
17696 if (len < 0)
17697 len = 0;
17698 else if (n + len > slen)
17699 len = slen - n;
17700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017701 rettv->v_type = VAR_STRING;
17702 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703}
17704
17705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706 * "strridx()" function
17707 */
17708 static void
17709f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017710 typval_T *argvars;
17711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712{
17713 char_u buf[NUMBUFLEN];
17714 char_u *needle;
17715 char_u *haystack;
17716 char_u *rest;
17717 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017718 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017720 needle = get_tv_string_chk(&argvars[1]);
17721 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017722
17723 rettv->vval.v_number = -1;
17724 if (needle == NULL || haystack == NULL)
17725 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017726
17727 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017728 if (argvars[2].v_type != VAR_UNKNOWN)
17729 {
17730 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017731 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017732 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017734 }
17735 else
17736 end_idx = haystack_len;
17737
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017739 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017740 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017741 lastmatch = haystack + end_idx;
17742 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017743 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017745 for (rest = haystack; *rest != '\0'; ++rest)
17746 {
17747 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017748 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017749 break;
17750 lastmatch = rest;
17751 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017752 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017753
17754 if (lastmatch == NULL)
17755 rettv->vval.v_number = -1;
17756 else
17757 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17758}
17759
17760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 * "strtrans()" function
17762 */
17763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017764f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 typval_T *argvars;
17766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017768 rettv->v_type = VAR_STRING;
17769 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770}
17771
17772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017773 * "submatch()" function
17774 */
17775 static void
17776f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 typval_T *argvars;
17778 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017779{
17780 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017781 rettv->vval.v_string =
17782 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017783}
17784
17785/*
17786 * "substitute()" function
17787 */
17788 static void
17789f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 typval_T *argvars;
17791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792{
17793 char_u patbuf[NUMBUFLEN];
17794 char_u subbuf[NUMBUFLEN];
17795 char_u flagsbuf[NUMBUFLEN];
17796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017797 char_u *str = get_tv_string_chk(&argvars[0]);
17798 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17799 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17800 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17801
Bram Moolenaar0d660222005-01-07 21:51:51 +000017802 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017803 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17804 rettv->vval.v_string = NULL;
17805 else
17806 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017807}
17808
17809/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017810 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816{
17817 int id = 0;
17818#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017819 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 long col;
17821 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017822 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017824 lnum = get_tv_lnum(argvars); /* -1 on type error */
17825 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17826 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017828 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017829 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017830 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831#endif
17832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017833 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834}
17835
17836/*
17837 * "synIDattr(id, what [, mode])" function
17838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017840f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017841 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017843{
17844 char_u *p = NULL;
17845#ifdef FEAT_SYN_HL
17846 int id;
17847 char_u *what;
17848 char_u *mode;
17849 char_u modebuf[NUMBUFLEN];
17850 int modec;
17851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852 id = get_tv_number(&argvars[0]);
17853 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017854 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017856 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017858 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 modec = 0; /* replace invalid with current */
17860 }
17861 else
17862 {
17863#ifdef FEAT_GUI
17864 if (gui.in_use)
17865 modec = 'g';
17866 else
17867#endif
17868 if (t_colors > 1)
17869 modec = 'c';
17870 else
17871 modec = 't';
17872 }
17873
17874
17875 switch (TOLOWER_ASC(what[0]))
17876 {
17877 case 'b':
17878 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17879 p = highlight_color(id, what, modec);
17880 else /* bold */
17881 p = highlight_has_attr(id, HL_BOLD, modec);
17882 break;
17883
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017884 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 p = highlight_color(id, what, modec);
17886 break;
17887
17888 case 'i':
17889 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17890 p = highlight_has_attr(id, HL_INVERSE, modec);
17891 else /* italic */
17892 p = highlight_has_attr(id, HL_ITALIC, modec);
17893 break;
17894
17895 case 'n': /* name */
17896 p = get_highlight_name(NULL, id - 1);
17897 break;
17898
17899 case 'r': /* reverse */
17900 p = highlight_has_attr(id, HL_INVERSE, modec);
17901 break;
17902
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017903 case 's':
17904 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17905 p = highlight_color(id, what, modec);
17906 else /* standout */
17907 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 break;
17909
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017910 case 'u':
17911 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17912 /* underline */
17913 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17914 else
17915 /* undercurl */
17916 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 break;
17918 }
17919
17920 if (p != NULL)
17921 p = vim_strsave(p);
17922#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017923 rettv->v_type = VAR_STRING;
17924 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925}
17926
17927/*
17928 * "synIDtrans(id)" function
17929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934{
17935 int id;
17936
17937#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017938 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939
17940 if (id > 0)
17941 id = syn_get_final_id(id);
17942 else
17943#endif
17944 id = 0;
17945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017946 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947}
17948
17949/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017950 * "synconcealed(lnum, col)" function
17951 */
17952 static void
17953f_synconcealed(argvars, rettv)
17954 typval_T *argvars UNUSED;
17955 typval_T *rettv;
17956{
17957#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17958 long lnum;
17959 long col;
17960 int syntax_flags = 0;
17961 int cchar;
17962 int matchid = 0;
17963 char_u str[NUMBUFLEN];
17964#endif
17965
17966 rettv->v_type = VAR_LIST;
17967 rettv->vval.v_list = NULL;
17968
17969#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17970 lnum = get_tv_lnum(argvars); /* -1 on type error */
17971 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17972
17973 vim_memset(str, NUL, sizeof(str));
17974
17975 if (rettv_list_alloc(rettv) != FAIL)
17976 {
17977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17978 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17979 && curwin->w_p_cole > 0)
17980 {
17981 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17982 syntax_flags = get_syntax_info(&matchid);
17983
17984 /* get the conceal character */
17985 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17986 {
17987 cchar = syn_get_sub_char();
17988 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17989 cchar = lcs_conceal;
17990 if (cchar != NUL)
17991 {
17992# ifdef FEAT_MBYTE
17993 if (has_mbyte)
17994 (*mb_char2bytes)(cchar, str);
17995 else
17996# endif
17997 str[0] = cchar;
17998 }
17999 }
18000 }
18001
18002 list_append_number(rettv->vval.v_list,
18003 (syntax_flags & HL_CONCEAL) != 0);
18004 /* -1 to auto-determine strlen */
18005 list_append_string(rettv->vval.v_list, str, -1);
18006 list_append_number(rettv->vval.v_list, matchid);
18007 }
18008#endif
18009}
18010
18011/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018012 * "synstack(lnum, col)" function
18013 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018014 static void
18015f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018016 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018017 typval_T *rettv;
18018{
18019#ifdef FEAT_SYN_HL
18020 long lnum;
18021 long col;
18022 int i;
18023 int id;
18024#endif
18025
18026 rettv->v_type = VAR_LIST;
18027 rettv->vval.v_list = NULL;
18028
18029#ifdef FEAT_SYN_HL
18030 lnum = get_tv_lnum(argvars); /* -1 on type error */
18031 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18032
18033 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018034 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018035 && rettv_list_alloc(rettv) != FAIL)
18036 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018037 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018038 for (i = 0; ; ++i)
18039 {
18040 id = syn_get_stack_item(i);
18041 if (id < 0)
18042 break;
18043 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18044 break;
18045 }
18046 }
18047#endif
18048}
18049
18050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051 * "system()" function
18052 */
18053 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018055 typval_T *argvars;
18056 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018058 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018060 char_u *infile = NULL;
18061 char_u buf[NUMBUFLEN];
18062 int err = FALSE;
18063 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018065 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018066 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018067
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018068 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018069 {
18070 /*
18071 * Write the string to a temp file, to be used for input of the shell
18072 * command.
18073 */
18074 if ((infile = vim_tempname('i')) == NULL)
18075 {
18076 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018077 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018078 }
18079
18080 fd = mch_fopen((char *)infile, WRITEBIN);
18081 if (fd == NULL)
18082 {
18083 EMSG2(_(e_notopen), infile);
18084 goto done;
18085 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018086 p = get_tv_string_buf_chk(&argvars[1], buf);
18087 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018088 {
18089 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018090 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018091 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018092 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18093 err = TRUE;
18094 if (fclose(fd) != 0)
18095 err = TRUE;
18096 if (err)
18097 {
18098 EMSG(_("E677: Error writing temp file"));
18099 goto done;
18100 }
18101 }
18102
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018103 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18104 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018105
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106#ifdef USE_CR
18107 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018108 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 {
18110 char_u *s;
18111
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018112 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 {
18114 if (*s == CAR)
18115 *s = NL;
18116 }
18117 }
18118#else
18119# ifdef USE_CRNL
18120 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018121 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 {
18123 char_u *s, *d;
18124
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018125 d = res;
18126 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 {
18128 if (s[0] == CAR && s[1] == NL)
18129 ++s;
18130 *d++ = *s;
18131 }
18132 *d = NUL;
18133 }
18134# endif
18135#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018136
18137done:
18138 if (infile != NULL)
18139 {
18140 mch_remove(infile);
18141 vim_free(infile);
18142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018143 rettv->v_type = VAR_STRING;
18144 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145}
18146
18147/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018148 * "tabpagebuflist()" function
18149 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018150 static void
18151f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018152 typval_T *argvars UNUSED;
18153 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018154{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018155#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018156 tabpage_T *tp;
18157 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018158
18159 if (argvars[0].v_type == VAR_UNKNOWN)
18160 wp = firstwin;
18161 else
18162 {
18163 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18164 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018165 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018166 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018167 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018168 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018169 for (; wp != NULL; wp = wp->w_next)
18170 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018171 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018172 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018173 }
18174#endif
18175}
18176
18177
18178/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018179 * "tabpagenr()" function
18180 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018181 static void
18182f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018183 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018184 typval_T *rettv;
18185{
18186 int nr = 1;
18187#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018188 char_u *arg;
18189
18190 if (argvars[0].v_type != VAR_UNKNOWN)
18191 {
18192 arg = get_tv_string_chk(&argvars[0]);
18193 nr = 0;
18194 if (arg != NULL)
18195 {
18196 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018197 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018198 else
18199 EMSG2(_(e_invexpr2), arg);
18200 }
18201 }
18202 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018203 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018204#endif
18205 rettv->vval.v_number = nr;
18206}
18207
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018208
18209#ifdef FEAT_WINDOWS
18210static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18211
18212/*
18213 * Common code for tabpagewinnr() and winnr().
18214 */
18215 static int
18216get_winnr(tp, argvar)
18217 tabpage_T *tp;
18218 typval_T *argvar;
18219{
18220 win_T *twin;
18221 int nr = 1;
18222 win_T *wp;
18223 char_u *arg;
18224
18225 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18226 if (argvar->v_type != VAR_UNKNOWN)
18227 {
18228 arg = get_tv_string_chk(argvar);
18229 if (arg == NULL)
18230 nr = 0; /* type error; errmsg already given */
18231 else if (STRCMP(arg, "$") == 0)
18232 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18233 else if (STRCMP(arg, "#") == 0)
18234 {
18235 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18236 if (twin == NULL)
18237 nr = 0;
18238 }
18239 else
18240 {
18241 EMSG2(_(e_invexpr2), arg);
18242 nr = 0;
18243 }
18244 }
18245
18246 if (nr > 0)
18247 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18248 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018249 {
18250 if (wp == NULL)
18251 {
18252 /* didn't find it in this tabpage */
18253 nr = 0;
18254 break;
18255 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018256 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018257 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018258 return nr;
18259}
18260#endif
18261
18262/*
18263 * "tabpagewinnr()" function
18264 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018265 static void
18266f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018267 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018268 typval_T *rettv;
18269{
18270 int nr = 1;
18271#ifdef FEAT_WINDOWS
18272 tabpage_T *tp;
18273
18274 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18275 if (tp == NULL)
18276 nr = 0;
18277 else
18278 nr = get_winnr(tp, &argvars[1]);
18279#endif
18280 rettv->vval.v_number = nr;
18281}
18282
18283
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018284/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018285 * "tagfiles()" function
18286 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018287 static void
18288f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018289 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018290 typval_T *rettv;
18291{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018292 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018293 tagname_T tn;
18294 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018295
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018296 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018297 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018298 fname = alloc(MAXPATHL);
18299 if (fname == NULL)
18300 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018301
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018302 for (first = TRUE; ; first = FALSE)
18303 if (get_tagfname(&tn, first, fname) == FAIL
18304 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018305 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018306 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018307 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018308}
18309
18310/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018311 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018312 */
18313 static void
18314f_taglist(argvars, rettv)
18315 typval_T *argvars;
18316 typval_T *rettv;
18317{
18318 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018319
18320 tag_pattern = get_tv_string(&argvars[0]);
18321
18322 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018323 if (*tag_pattern == NUL)
18324 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018326 if (rettv_list_alloc(rettv) == OK)
18327 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018328}
18329
18330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 * "tempname()" function
18332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018334f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337{
18338 static int x = 'A';
18339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018340 rettv->v_type = VAR_STRING;
18341 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342
18343 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18344 * names. Skip 'I' and 'O', they are used for shell redirection. */
18345 do
18346 {
18347 if (x == 'Z')
18348 x = '0';
18349 else if (x == '9')
18350 x = 'A';
18351 else
18352 {
18353#ifdef EBCDIC
18354 if (x == 'I')
18355 x = 'J';
18356 else if (x == 'R')
18357 x = 'S';
18358 else
18359#endif
18360 ++x;
18361 }
18362 } while (x == 'I' || x == 'O');
18363}
18364
18365/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018366 * "test(list)" function: Just checking the walls...
18367 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018368 static void
18369f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018370 typval_T *argvars UNUSED;
18371 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018372{
18373 /* Used for unit testing. Change the code below to your liking. */
18374#if 0
18375 listitem_T *li;
18376 list_T *l;
18377 char_u *bad, *good;
18378
18379 if (argvars[0].v_type != VAR_LIST)
18380 return;
18381 l = argvars[0].vval.v_list;
18382 if (l == NULL)
18383 return;
18384 li = l->lv_first;
18385 if (li == NULL)
18386 return;
18387 bad = get_tv_string(&li->li_tv);
18388 li = li->li_next;
18389 if (li == NULL)
18390 return;
18391 good = get_tv_string(&li->li_tv);
18392 rettv->vval.v_number = test_edit_score(bad, good);
18393#endif
18394}
18395
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018396#ifdef FEAT_FLOAT
18397/*
18398 * "tan()" function
18399 */
18400 static void
18401f_tan(argvars, rettv)
18402 typval_T *argvars;
18403 typval_T *rettv;
18404{
18405 float_T f;
18406
18407 rettv->v_type = VAR_FLOAT;
18408 if (get_float_arg(argvars, &f) == OK)
18409 rettv->vval.v_float = tan(f);
18410 else
18411 rettv->vval.v_float = 0.0;
18412}
18413
18414/*
18415 * "tanh()" function
18416 */
18417 static void
18418f_tanh(argvars, rettv)
18419 typval_T *argvars;
18420 typval_T *rettv;
18421{
18422 float_T f;
18423
18424 rettv->v_type = VAR_FLOAT;
18425 if (get_float_arg(argvars, &f) == OK)
18426 rettv->vval.v_float = tanh(f);
18427 else
18428 rettv->vval.v_float = 0.0;
18429}
18430#endif
18431
Bram Moolenaard52d9742005-08-21 22:20:28 +000018432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018433 * "tolower(string)" function
18434 */
18435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018436f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018437 typval_T *argvars;
18438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439{
18440 char_u *p;
18441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018442 p = vim_strsave(get_tv_string(&argvars[0]));
18443 rettv->v_type = VAR_STRING;
18444 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445
18446 if (p != NULL)
18447 while (*p != NUL)
18448 {
18449#ifdef FEAT_MBYTE
18450 int l;
18451
18452 if (enc_utf8)
18453 {
18454 int c, lc;
18455
18456 c = utf_ptr2char(p);
18457 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018458 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 /* TODO: reallocate string when byte count changes. */
18460 if (utf_char2len(lc) == l)
18461 utf_char2bytes(lc, p);
18462 p += l;
18463 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018464 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 p += l; /* skip multi-byte character */
18466 else
18467#endif
18468 {
18469 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18470 ++p;
18471 }
18472 }
18473}
18474
18475/*
18476 * "toupper(string)" function
18477 */
18478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 typval_T *argvars;
18481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018483 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018484 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485}
18486
18487/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018488 * "tr(string, fromstr, tostr)" function
18489 */
18490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018491f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *argvars;
18493 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018494{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018495 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018496 char_u *fromstr;
18497 char_u *tostr;
18498 char_u *p;
18499#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018500 int inlen;
18501 int fromlen;
18502 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018503 int idx;
18504 char_u *cpstr;
18505 int cplen;
18506 int first = TRUE;
18507#endif
18508 char_u buf[NUMBUFLEN];
18509 char_u buf2[NUMBUFLEN];
18510 garray_T ga;
18511
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018512 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018513 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18514 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018515
18516 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018517 rettv->v_type = VAR_STRING;
18518 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 if (fromstr == NULL || tostr == NULL)
18520 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018521 ga_init2(&ga, (int)sizeof(char), 80);
18522
18523#ifdef FEAT_MBYTE
18524 if (!has_mbyte)
18525#endif
18526 /* not multi-byte: fromstr and tostr must be the same length */
18527 if (STRLEN(fromstr) != STRLEN(tostr))
18528 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018529#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018530error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018531#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018532 EMSG2(_(e_invarg2), fromstr);
18533 ga_clear(&ga);
18534 return;
18535 }
18536
18537 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018538 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018539 {
18540#ifdef FEAT_MBYTE
18541 if (has_mbyte)
18542 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018543 inlen = (*mb_ptr2len)(in_str);
18544 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018545 cplen = inlen;
18546 idx = 0;
18547 for (p = fromstr; *p != NUL; p += fromlen)
18548 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018549 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018550 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018551 {
18552 for (p = tostr; *p != NUL; p += tolen)
18553 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018554 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018555 if (idx-- == 0)
18556 {
18557 cplen = tolen;
18558 cpstr = p;
18559 break;
18560 }
18561 }
18562 if (*p == NUL) /* tostr is shorter than fromstr */
18563 goto error;
18564 break;
18565 }
18566 ++idx;
18567 }
18568
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018569 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018570 {
18571 /* Check that fromstr and tostr have the same number of
18572 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018573 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018574 first = FALSE;
18575 for (p = tostr; *p != NUL; p += tolen)
18576 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018577 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018578 --idx;
18579 }
18580 if (idx != 0)
18581 goto error;
18582 }
18583
18584 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018585 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018586 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018587
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018588 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018589 }
18590 else
18591#endif
18592 {
18593 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018594 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018595 if (p != NULL)
18596 ga_append(&ga, tostr[p - fromstr]);
18597 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018598 ga_append(&ga, *in_str);
18599 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018600 }
18601 }
18602
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018603 /* add a terminating NUL */
18604 ga_grow(&ga, 1);
18605 ga_append(&ga, NUL);
18606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018607 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018608}
18609
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018610#ifdef FEAT_FLOAT
18611/*
18612 * "trunc({float})" function
18613 */
18614 static void
18615f_trunc(argvars, rettv)
18616 typval_T *argvars;
18617 typval_T *rettv;
18618{
18619 float_T f;
18620
18621 rettv->v_type = VAR_FLOAT;
18622 if (get_float_arg(argvars, &f) == OK)
18623 /* trunc() is not in C90, use floor() or ceil() instead. */
18624 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18625 else
18626 rettv->vval.v_float = 0.0;
18627}
18628#endif
18629
Bram Moolenaar8299df92004-07-10 09:47:34 +000018630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 * "type(expr)" function
18632 */
18633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018634f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018635 typval_T *argvars;
18636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018638 int n;
18639
18640 switch (argvars[0].v_type)
18641 {
18642 case VAR_NUMBER: n = 0; break;
18643 case VAR_STRING: n = 1; break;
18644 case VAR_FUNC: n = 2; break;
18645 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018646 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018647#ifdef FEAT_FLOAT
18648 case VAR_FLOAT: n = 5; break;
18649#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018650 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18651 }
18652 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653}
18654
18655/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018656 * "undofile(name)" function
18657 */
18658 static void
18659f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018660 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018661 typval_T *rettv;
18662{
18663 rettv->v_type = VAR_STRING;
18664#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018665 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018666 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018667
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018668 if (*fname == NUL)
18669 {
18670 /* If there is no file name there will be no undo file. */
18671 rettv->vval.v_string = NULL;
18672 }
18673 else
18674 {
18675 char_u *ffname = FullName_save(fname, FALSE);
18676
18677 if (ffname != NULL)
18678 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18679 vim_free(ffname);
18680 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018681 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018682#else
18683 rettv->vval.v_string = NULL;
18684#endif
18685}
18686
18687/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018688 * "undotree()" function
18689 */
18690 static void
18691f_undotree(argvars, rettv)
18692 typval_T *argvars UNUSED;
18693 typval_T *rettv;
18694{
18695 if (rettv_dict_alloc(rettv) == OK)
18696 {
18697 dict_T *dict = rettv->vval.v_dict;
18698 list_T *list;
18699
Bram Moolenaar730cde92010-06-27 05:18:54 +020018700 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018701 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018702 dict_add_nr_str(dict, "save_last",
18703 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018704 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18705 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018706 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018707
18708 list = list_alloc();
18709 if (list != NULL)
18710 {
18711 u_eval_tree(curbuf->b_u_oldhead, list);
18712 dict_add_list(dict, "entries", list);
18713 }
18714 }
18715}
18716
18717/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018718 * "values(dict)" function
18719 */
18720 static void
18721f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018722 typval_T *argvars;
18723 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018724{
18725 dict_list(argvars, rettv, 1);
18726}
18727
18728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 * "virtcol(string)" function
18730 */
18731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018733 typval_T *argvars;
18734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735{
18736 colnr_T vcol = 0;
18737 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018738 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018740 fp = var2fpos(&argvars[0], FALSE, &fnum);
18741 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18742 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 {
18744 getvvcol(curwin, fp, NULL, NULL, &vcol);
18745 ++vcol;
18746 }
18747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749}
18750
18751/*
18752 * "visualmode()" function
18753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018755f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018756 typval_T *argvars UNUSED;
18757 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758{
18759#ifdef FEAT_VISUAL
18760 char_u str[2];
18761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 str[0] = curbuf->b_visual_mode_eval;
18764 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018765 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766
18767 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018768 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770#endif
18771}
18772
18773/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018774 * "wildmenumode()" function
18775 */
18776 static void
18777f_wildmenumode(argvars, rettv)
18778 typval_T *argvars UNUSED;
18779 typval_T *rettv UNUSED;
18780{
18781#ifdef FEAT_WILDMENU
18782 if (wild_menu_showing)
18783 rettv->vval.v_number = 1;
18784#endif
18785}
18786
18787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 * "winbufnr(nr)" function
18789 */
18790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018791f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018792 typval_T *argvars;
18793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794{
18795 win_T *wp;
18796
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018797 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018801 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802}
18803
18804/*
18805 * "wincol()" function
18806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811{
18812 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814}
18815
18816/*
18817 * "winheight(nr)" function
18818 */
18819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018820f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018821 typval_T *argvars;
18822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823{
18824 win_T *wp;
18825
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018826 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018830 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018831}
18832
18833/*
18834 * "winline()" function
18835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018837f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018838 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018839 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840{
18841 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
18845/*
18846 * "winnr()" function
18847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018849f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018850 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018851 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852{
18853 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018854
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018856 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018858 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859}
18860
18861/*
18862 * "winrestcmd()" function
18863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018865f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018866 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868{
18869#ifdef FEAT_WINDOWS
18870 win_T *wp;
18871 int winnr = 1;
18872 garray_T ga;
18873 char_u buf[50];
18874
18875 ga_init2(&ga, (int)sizeof(char), 70);
18876 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18877 {
18878 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18879 ga_concat(&ga, buf);
18880# ifdef FEAT_VERTSPLIT
18881 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18882 ga_concat(&ga, buf);
18883# endif
18884 ++winnr;
18885 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018886 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018888 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018890 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018892 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893}
18894
18895/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018896 * "winrestview()" function
18897 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018898 static void
18899f_winrestview(argvars, rettv)
18900 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018901 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018902{
18903 dict_T *dict;
18904
18905 if (argvars[0].v_type != VAR_DICT
18906 || (dict = argvars[0].vval.v_dict) == NULL)
18907 EMSG(_(e_invarg));
18908 else
18909 {
18910 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18911 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18912#ifdef FEAT_VIRTUALEDIT
18913 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18914#endif
18915 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018916 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018917
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018918 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018919#ifdef FEAT_DIFF
18920 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18921#endif
18922 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18923 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18924
18925 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018926 win_new_height(curwin, curwin->w_height);
18927# ifdef FEAT_VERTSPLIT
18928 win_new_width(curwin, W_WIDTH(curwin));
18929# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018930 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018931
18932 if (curwin->w_topline == 0)
18933 curwin->w_topline = 1;
18934 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18935 curwin->w_topline = curbuf->b_ml.ml_line_count;
18936#ifdef FEAT_DIFF
18937 check_topfill(curwin, TRUE);
18938#endif
18939 }
18940}
18941
18942/*
18943 * "winsaveview()" function
18944 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018945 static void
18946f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018947 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018948 typval_T *rettv;
18949{
18950 dict_T *dict;
18951
Bram Moolenaara800b422010-06-27 01:15:55 +020018952 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018953 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018954 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018955
18956 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18957 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18958#ifdef FEAT_VIRTUALEDIT
18959 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18960#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018961 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018962 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18963
18964 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18965#ifdef FEAT_DIFF
18966 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18967#endif
18968 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18969 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18970}
18971
18972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 * "winwidth(nr)" function
18974 */
18975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018976f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018977 typval_T *argvars;
18978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979{
18980 win_T *wp;
18981
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018982 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 else
18986#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990#endif
18991}
18992
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018994 * "writefile()" function
18995 */
18996 static void
18997f_writefile(argvars, rettv)
18998 typval_T *argvars;
18999 typval_T *rettv;
19000{
19001 int binary = FALSE;
19002 char_u *fname;
19003 FILE *fd;
19004 listitem_T *li;
19005 char_u *s;
19006 int ret = 0;
19007 int c;
19008
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019009 if (check_restricted() || check_secure())
19010 return;
19011
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019012 if (argvars[0].v_type != VAR_LIST)
19013 {
19014 EMSG2(_(e_listarg), "writefile()");
19015 return;
19016 }
19017 if (argvars[0].vval.v_list == NULL)
19018 return;
19019
19020 if (argvars[2].v_type != VAR_UNKNOWN
19021 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19022 binary = TRUE;
19023
19024 /* Always open the file in binary mode, library functions have a mind of
19025 * their own about CR-LF conversion. */
19026 fname = get_tv_string(&argvars[1]);
19027 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19028 {
19029 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19030 ret = -1;
19031 }
19032 else
19033 {
19034 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19035 li = li->li_next)
19036 {
19037 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19038 {
19039 if (*s == '\n')
19040 c = putc(NUL, fd);
19041 else
19042 c = putc(*s, fd);
19043 if (c == EOF)
19044 {
19045 ret = -1;
19046 break;
19047 }
19048 }
19049 if (!binary || li->li_next != NULL)
19050 if (putc('\n', fd) == EOF)
19051 {
19052 ret = -1;
19053 break;
19054 }
19055 if (ret < 0)
19056 {
19057 EMSG(_(e_write));
19058 break;
19059 }
19060 }
19061 fclose(fd);
19062 }
19063
19064 rettv->vval.v_number = ret;
19065}
19066
19067/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019068 * "xor(expr, expr)" function
19069 */
19070 static void
19071f_xor(argvars, rettv)
19072 typval_T *argvars;
19073 typval_T *rettv;
19074{
19075 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19076 ^ get_tv_number_chk(&argvars[1], NULL);
19077}
19078
19079
19080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019082 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 */
19084 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019085var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019087 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019088 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019090 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019092 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093
Bram Moolenaara5525202006-03-02 22:52:09 +000019094 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019095 if (varp->v_type == VAR_LIST)
19096 {
19097 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019098 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019099 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019100 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019101
19102 l = varp->vval.v_list;
19103 if (l == NULL)
19104 return NULL;
19105
19106 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019107 pos.lnum = list_find_nr(l, 0L, &error);
19108 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019109 return NULL; /* invalid line number */
19110
19111 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019112 pos.col = list_find_nr(l, 1L, &error);
19113 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019114 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019115 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019116
19117 /* We accept "$" for the column number: last column. */
19118 li = list_find(l, 1L);
19119 if (li != NULL && li->li_tv.v_type == VAR_STRING
19120 && li->li_tv.vval.v_string != NULL
19121 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19122 pos.col = len + 1;
19123
Bram Moolenaara5525202006-03-02 22:52:09 +000019124 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019125 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019126 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019127 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019128
Bram Moolenaara5525202006-03-02 22:52:09 +000019129#ifdef FEAT_VIRTUALEDIT
19130 /* Get the virtual offset. Defaults to zero. */
19131 pos.coladd = list_find_nr(l, 2L, &error);
19132 if (error)
19133 pos.coladd = 0;
19134#endif
19135
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019136 return &pos;
19137 }
19138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019139 name = get_tv_string_chk(varp);
19140 if (name == NULL)
19141 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019142 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019144#ifdef FEAT_VISUAL
19145 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19146 {
19147 if (VIsual_active)
19148 return &VIsual;
19149 return &curwin->w_cursor;
19150 }
19151#endif
19152 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019154 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19156 return NULL;
19157 return pp;
19158 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019159
19160#ifdef FEAT_VIRTUALEDIT
19161 pos.coladd = 0;
19162#endif
19163
Bram Moolenaar477933c2007-07-17 14:32:23 +000019164 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019165 {
19166 pos.col = 0;
19167 if (name[1] == '0') /* "w0": first visible line */
19168 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019169 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019170 pos.lnum = curwin->w_topline;
19171 return &pos;
19172 }
19173 else if (name[1] == '$') /* "w$": last visible line */
19174 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019175 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019176 pos.lnum = curwin->w_botline - 1;
19177 return &pos;
19178 }
19179 }
19180 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019182 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 {
19184 pos.lnum = curbuf->b_ml.ml_line_count;
19185 pos.col = 0;
19186 }
19187 else
19188 {
19189 pos.lnum = curwin->w_cursor.lnum;
19190 pos.col = (colnr_T)STRLEN(ml_get_curline());
19191 }
19192 return &pos;
19193 }
19194 return NULL;
19195}
19196
19197/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019198 * Convert list in "arg" into a position and optional file number.
19199 * When "fnump" is NULL there is no file number, only 3 items.
19200 * Note that the column is passed on as-is, the caller may want to decrement
19201 * it to use 1 for the first column.
19202 * Return FAIL when conversion is not possible, doesn't check the position for
19203 * validity.
19204 */
19205 static int
19206list2fpos(arg, posp, fnump)
19207 typval_T *arg;
19208 pos_T *posp;
19209 int *fnump;
19210{
19211 list_T *l = arg->vval.v_list;
19212 long i = 0;
19213 long n;
19214
Bram Moolenaarbde35262006-07-23 20:12:24 +000019215 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19216 * when "fnump" isn't NULL and "coladd" is optional. */
19217 if (arg->v_type != VAR_LIST
19218 || l == NULL
19219 || l->lv_len < (fnump == NULL ? 2 : 3)
19220 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019221 return FAIL;
19222
19223 if (fnump != NULL)
19224 {
19225 n = list_find_nr(l, i++, NULL); /* fnum */
19226 if (n < 0)
19227 return FAIL;
19228 if (n == 0)
19229 n = curbuf->b_fnum; /* current buffer */
19230 *fnump = n;
19231 }
19232
19233 n = list_find_nr(l, i++, NULL); /* lnum */
19234 if (n < 0)
19235 return FAIL;
19236 posp->lnum = n;
19237
19238 n = list_find_nr(l, i++, NULL); /* col */
19239 if (n < 0)
19240 return FAIL;
19241 posp->col = n;
19242
19243#ifdef FEAT_VIRTUALEDIT
19244 n = list_find_nr(l, i, NULL);
19245 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019246 posp->coladd = 0;
19247 else
19248 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019249#endif
19250
19251 return OK;
19252}
19253
19254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 * Get the length of an environment variable name.
19256 * Advance "arg" to the first character after the name.
19257 * Return 0 for error.
19258 */
19259 static int
19260get_env_len(arg)
19261 char_u **arg;
19262{
19263 char_u *p;
19264 int len;
19265
19266 for (p = *arg; vim_isIDc(*p); ++p)
19267 ;
19268 if (p == *arg) /* no name found */
19269 return 0;
19270
19271 len = (int)(p - *arg);
19272 *arg = p;
19273 return len;
19274}
19275
19276/*
19277 * Get the length of the name of a function or internal variable.
19278 * "arg" is advanced to the first non-white character after the name.
19279 * Return 0 if something is wrong.
19280 */
19281 static int
19282get_id_len(arg)
19283 char_u **arg;
19284{
19285 char_u *p;
19286 int len;
19287
19288 /* Find the end of the name. */
19289 for (p = *arg; eval_isnamec(*p); ++p)
19290 ;
19291 if (p == *arg) /* no name found */
19292 return 0;
19293
19294 len = (int)(p - *arg);
19295 *arg = skipwhite(p);
19296
19297 return len;
19298}
19299
19300/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019301 * Get the length of the name of a variable or function.
19302 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019304 * Return -1 if curly braces expansion failed.
19305 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 * If the name contains 'magic' {}'s, expand them and return the
19307 * expanded name in an allocated string via 'alias' - caller must free.
19308 */
19309 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019310get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 char_u **arg;
19312 char_u **alias;
19313 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019314 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315{
19316 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 char_u *p;
19318 char_u *expr_start;
19319 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320
19321 *alias = NULL; /* default to no alias */
19322
19323 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19324 && (*arg)[2] == (int)KE_SNR)
19325 {
19326 /* hard coded <SNR>, already translated */
19327 *arg += 3;
19328 return get_id_len(arg) + 3;
19329 }
19330 len = eval_fname_script(*arg);
19331 if (len > 0)
19332 {
19333 /* literal "<SID>", "s:" or "<SNR>" */
19334 *arg += len;
19335 }
19336
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019340 p = find_name_end(*arg, &expr_start, &expr_end,
19341 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 if (expr_start != NULL)
19343 {
19344 char_u *temp_string;
19345
19346 if (!evaluate)
19347 {
19348 len += (int)(p - *arg);
19349 *arg = skipwhite(p);
19350 return len;
19351 }
19352
19353 /*
19354 * Include any <SID> etc in the expanded string:
19355 * Thus the -len here.
19356 */
19357 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19358 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019359 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 *alias = temp_string;
19361 *arg = skipwhite(p);
19362 return (int)STRLEN(temp_string);
19363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364
19365 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019366 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 EMSG2(_(e_invexpr2), *arg);
19368
19369 return len;
19370}
19371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019372/*
19373 * Find the end of a variable or function name, taking care of magic braces.
19374 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19375 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019376 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 * Return a pointer to just after the name. Equal to "arg" if there is no
19378 * valid name.
19379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019381find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 char_u *arg;
19383 char_u **expr_start;
19384 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019385 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387 int mb_nest = 0;
19388 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 char_u *p;
19390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019391 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019393 *expr_start = NULL;
19394 *expr_end = NULL;
19395 }
19396
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019397 /* Quick check for valid starting character. */
19398 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19399 return arg;
19400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019401 for (p = arg; *p != NUL
19402 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019403 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019404 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019405 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019406 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019407 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019408 if (*p == '\'')
19409 {
19410 /* skip over 'string' to avoid counting [ and ] inside it. */
19411 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19412 ;
19413 if (*p == NUL)
19414 break;
19415 }
19416 else if (*p == '"')
19417 {
19418 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19419 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19420 if (*p == '\\' && p[1] != NUL)
19421 ++p;
19422 if (*p == NUL)
19423 break;
19424 }
19425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019426 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 if (*p == '[')
19429 ++br_nest;
19430 else if (*p == ']')
19431 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019434 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019436 if (*p == '{')
19437 {
19438 mb_nest++;
19439 if (expr_start != NULL && *expr_start == NULL)
19440 *expr_start = p;
19441 }
19442 else if (*p == '}')
19443 {
19444 mb_nest--;
19445 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19446 *expr_end = p;
19447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 }
19450
19451 return p;
19452}
19453
19454/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019455 * Expands out the 'magic' {}'s in a variable/function name.
19456 * Note that this can call itself recursively, to deal with
19457 * constructs like foo{bar}{baz}{bam}
19458 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19459 * "in_start" ^
19460 * "expr_start" ^
19461 * "expr_end" ^
19462 * "in_end" ^
19463 *
19464 * Returns a new allocated string, which the caller must free.
19465 * Returns NULL for failure.
19466 */
19467 static char_u *
19468make_expanded_name(in_start, expr_start, expr_end, in_end)
19469 char_u *in_start;
19470 char_u *expr_start;
19471 char_u *expr_end;
19472 char_u *in_end;
19473{
19474 char_u c1;
19475 char_u *retval = NULL;
19476 char_u *temp_result;
19477 char_u *nextcmd = NULL;
19478
19479 if (expr_end == NULL || in_end == NULL)
19480 return NULL;
19481 *expr_start = NUL;
19482 *expr_end = NUL;
19483 c1 = *in_end;
19484 *in_end = NUL;
19485
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019486 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019487 if (temp_result != NULL && nextcmd == NULL)
19488 {
19489 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19490 + (in_end - expr_end) + 1));
19491 if (retval != NULL)
19492 {
19493 STRCPY(retval, in_start);
19494 STRCAT(retval, temp_result);
19495 STRCAT(retval, expr_end + 1);
19496 }
19497 }
19498 vim_free(temp_result);
19499
19500 *in_end = c1; /* put char back for error messages */
19501 *expr_start = '{';
19502 *expr_end = '}';
19503
19504 if (retval != NULL)
19505 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019506 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019507 if (expr_start != NULL)
19508 {
19509 /* Further expansion! */
19510 temp_result = make_expanded_name(retval, expr_start,
19511 expr_end, temp_result);
19512 vim_free(retval);
19513 retval = temp_result;
19514 }
19515 }
19516
19517 return retval;
19518}
19519
19520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019522 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 */
19524 static int
19525eval_isnamec(c)
19526 int c;
19527{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019528 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19529}
19530
19531/*
19532 * Return TRUE if character "c" can be used as the first character in a
19533 * variable or function name (excluding '{' and '}').
19534 */
19535 static int
19536eval_isnamec1(c)
19537 int c;
19538{
19539 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540}
19541
19542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 * Set number v: variable to "val".
19544 */
19545 void
19546set_vim_var_nr(idx, val)
19547 int idx;
19548 long val;
19549{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019550 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551}
19552
19553/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019554 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 */
19556 long
19557get_vim_var_nr(idx)
19558 int idx;
19559{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019560 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561}
19562
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019563/*
19564 * Get string v: variable value. Uses a static buffer, can only be used once.
19565 */
19566 char_u *
19567get_vim_var_str(idx)
19568 int idx;
19569{
19570 return get_tv_string(&vimvars[idx].vv_tv);
19571}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019572
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019574 * Get List v: variable value. Caller must take care of reference count when
19575 * needed.
19576 */
19577 list_T *
19578get_vim_var_list(idx)
19579 int idx;
19580{
19581 return vimvars[idx].vv_list;
19582}
19583
19584/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019585 * Set v:char to character "c".
19586 */
19587 void
19588set_vim_var_char(c)
19589 int c;
19590{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019591 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019592
19593#ifdef FEAT_MBYTE
19594 if (has_mbyte)
19595 buf[(*mb_char2bytes)(c, buf)] = NUL;
19596 else
19597#endif
19598 {
19599 buf[0] = c;
19600 buf[1] = NUL;
19601 }
19602 set_vim_var_string(VV_CHAR, buf, -1);
19603}
19604
19605/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019606 * Set v:count to "count" and v:count1 to "count1".
19607 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 */
19609 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019610set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 long count;
19612 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019613 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019615 if (set_prevcount)
19616 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019617 vimvars[VV_COUNT].vv_nr = count;
19618 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619}
19620
19621/*
19622 * Set string v: variable to a copy of "val".
19623 */
19624 void
19625set_vim_var_string(idx, val, len)
19626 int idx;
19627 char_u *val;
19628 int len; /* length of "val" to use or -1 (whole string) */
19629{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019630 /* Need to do this (at least) once, since we can't initialize a union.
19631 * Will always be invoked when "v:progname" is set. */
19632 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19633
Bram Moolenaare9a41262005-01-15 22:18:47 +000019634 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019636 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019638 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019640 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641}
19642
19643/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019644 * Set List v: variable to "val".
19645 */
19646 void
19647set_vim_var_list(idx, val)
19648 int idx;
19649 list_T *val;
19650{
19651 list_unref(vimvars[idx].vv_list);
19652 vimvars[idx].vv_list = val;
19653 if (val != NULL)
19654 ++val->lv_refcount;
19655}
19656
19657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 * Set v:register if needed.
19659 */
19660 void
19661set_reg_var(c)
19662 int c;
19663{
19664 char_u regname;
19665
19666 if (c == 0 || c == ' ')
19667 regname = '"';
19668 else
19669 regname = c;
19670 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019671 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 set_vim_var_string(VV_REG, &regname, 1);
19673}
19674
19675/*
19676 * Get or set v:exception. If "oldval" == NULL, return the current value.
19677 * Otherwise, restore the value to "oldval" and return NULL.
19678 * Must always be called in pairs to save and restore v:exception! Does not
19679 * take care of memory allocations.
19680 */
19681 char_u *
19682v_exception(oldval)
19683 char_u *oldval;
19684{
19685 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019686 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687
Bram Moolenaare9a41262005-01-15 22:18:47 +000019688 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689 return NULL;
19690}
19691
19692/*
19693 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19694 * Otherwise, restore the value to "oldval" and return NULL.
19695 * Must always be called in pairs to save and restore v:throwpoint! Does not
19696 * take care of memory allocations.
19697 */
19698 char_u *
19699v_throwpoint(oldval)
19700 char_u *oldval;
19701{
19702 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019703 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019704
Bram Moolenaare9a41262005-01-15 22:18:47 +000019705 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 return NULL;
19707}
19708
19709#if defined(FEAT_AUTOCMD) || defined(PROTO)
19710/*
19711 * Set v:cmdarg.
19712 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19713 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19714 * Must always be called in pairs!
19715 */
19716 char_u *
19717set_cmdarg(eap, oldarg)
19718 exarg_T *eap;
19719 char_u *oldarg;
19720{
19721 char_u *oldval;
19722 char_u *newval;
19723 unsigned len;
19724
Bram Moolenaare9a41262005-01-15 22:18:47 +000019725 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019726 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019728 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019729 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019730 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 }
19732
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019733 if (eap->force_bin == FORCE_BIN)
19734 len = 6;
19735 else if (eap->force_bin == FORCE_NOBIN)
19736 len = 8;
19737 else
19738 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019739
19740 if (eap->read_edit)
19741 len += 7;
19742
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019743 if (eap->force_ff != 0)
19744 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19745# ifdef FEAT_MBYTE
19746 if (eap->force_enc != 0)
19747 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019748 if (eap->bad_char != 0)
19749 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019750# endif
19751
19752 newval = alloc(len + 1);
19753 if (newval == NULL)
19754 return NULL;
19755
19756 if (eap->force_bin == FORCE_BIN)
19757 sprintf((char *)newval, " ++bin");
19758 else if (eap->force_bin == FORCE_NOBIN)
19759 sprintf((char *)newval, " ++nobin");
19760 else
19761 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019762
19763 if (eap->read_edit)
19764 STRCAT(newval, " ++edit");
19765
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019766 if (eap->force_ff != 0)
19767 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19768 eap->cmd + eap->force_ff);
19769# ifdef FEAT_MBYTE
19770 if (eap->force_enc != 0)
19771 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19772 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019773 if (eap->bad_char == BAD_KEEP)
19774 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19775 else if (eap->bad_char == BAD_DROP)
19776 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19777 else if (eap->bad_char != 0)
19778 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019779# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019780 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019781 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782}
19783#endif
19784
19785/*
19786 * Get the value of internal variable "name".
19787 * Return OK or FAIL.
19788 */
19789 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019790get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 char_u *name;
19792 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019794 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019795 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796{
19797 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019798 typval_T *tv = NULL;
19799 typval_T atv;
19800 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802
19803 /* truncate the name, so that we can use strcmp() */
19804 cc = name[len];
19805 name[len] = NUL;
19806
19807 /*
19808 * Check for "b:changedtick".
19809 */
19810 if (STRCMP(name, "b:changedtick") == 0)
19811 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019812 atv.v_type = VAR_NUMBER;
19813 atv.vval.v_number = curbuf->b_changedtick;
19814 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 }
19816
19817 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 * Check for user-defined variables.
19819 */
19820 else
19821 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019822 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019824 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 }
19826
Bram Moolenaare9a41262005-01-15 22:18:47 +000019827 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019829 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 ret = FAIL;
19832 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019834 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835
19836 name[len] = cc;
19837
19838 return ret;
19839}
19840
19841/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019842 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19843 * Also handle function call with Funcref variable: func(expr)
19844 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19845 */
19846 static int
19847handle_subscript(arg, rettv, evaluate, verbose)
19848 char_u **arg;
19849 typval_T *rettv;
19850 int evaluate; /* do more than finding the end */
19851 int verbose; /* give error messages */
19852{
19853 int ret = OK;
19854 dict_T *selfdict = NULL;
19855 char_u *s;
19856 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019857 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019858
19859 while (ret == OK
19860 && (**arg == '['
19861 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019862 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019863 && !vim_iswhite(*(*arg - 1)))
19864 {
19865 if (**arg == '(')
19866 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019867 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019868 if (evaluate)
19869 {
19870 functv = *rettv;
19871 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019872
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019873 /* Invoke the function. Recursive! */
19874 s = functv.vval.v_string;
19875 }
19876 else
19877 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019878 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019879 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19880 &len, evaluate, selfdict);
19881
19882 /* Clear the funcref afterwards, so that deleting it while
19883 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019884 if (evaluate)
19885 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019886
19887 /* Stop the expression evaluation when immediately aborting on
19888 * error, or when an interrupt occurred or an exception was thrown
19889 * but not caught. */
19890 if (aborting())
19891 {
19892 if (ret == OK)
19893 clear_tv(rettv);
19894 ret = FAIL;
19895 }
19896 dict_unref(selfdict);
19897 selfdict = NULL;
19898 }
19899 else /* **arg == '[' || **arg == '.' */
19900 {
19901 dict_unref(selfdict);
19902 if (rettv->v_type == VAR_DICT)
19903 {
19904 selfdict = rettv->vval.v_dict;
19905 if (selfdict != NULL)
19906 ++selfdict->dv_refcount;
19907 }
19908 else
19909 selfdict = NULL;
19910 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19911 {
19912 clear_tv(rettv);
19913 ret = FAIL;
19914 }
19915 }
19916 }
19917 dict_unref(selfdict);
19918 return ret;
19919}
19920
19921/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019922 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019923 * value).
19924 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927{
Bram Moolenaar33570922005-01-25 22:26:29 +000019928 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019929}
19930
19931/*
19932 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 * The string "s" must have been allocated, it is consumed.
19934 * Return NULL for out of memory, the variable otherwise.
19935 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019936 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 char_u *s;
19939{
Bram Moolenaar33570922005-01-25 22:26:29 +000019940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019942 rettv = alloc_tv();
19943 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019945 rettv->v_type = VAR_STRING;
19946 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 }
19948 else
19949 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951}
19952
19953/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019956 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019957free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019958 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959{
19960 if (varp != NULL)
19961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019962 switch (varp->v_type)
19963 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019964 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019965 func_unref(varp->vval.v_string);
19966 /*FALLTHROUGH*/
19967 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019968 vim_free(varp->vval.v_string);
19969 break;
19970 case VAR_LIST:
19971 list_unref(varp->vval.v_list);
19972 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019973 case VAR_DICT:
19974 dict_unref(varp->vval.v_dict);
19975 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019976 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019977#ifdef FEAT_FLOAT
19978 case VAR_FLOAT:
19979#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019980 case VAR_UNKNOWN:
19981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019982 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019983 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 break;
19985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 vim_free(varp);
19987 }
19988}
19989
19990/*
19991 * Free the memory for a variable value and set the value to NULL or 0.
19992 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019993 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019995 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996{
19997 if (varp != NULL)
19998 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019999 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020002 func_unref(varp->vval.v_string);
20003 /*FALLTHROUGH*/
20004 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020005 vim_free(varp->vval.v_string);
20006 varp->vval.v_string = NULL;
20007 break;
20008 case VAR_LIST:
20009 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020010 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020011 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020012 case VAR_DICT:
20013 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020014 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020015 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020016 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020017 varp->vval.v_number = 0;
20018 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020019#ifdef FEAT_FLOAT
20020 case VAR_FLOAT:
20021 varp->vval.v_float = 0.0;
20022 break;
20023#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 case VAR_UNKNOWN:
20025 break;
20026 default:
20027 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020029 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 }
20031}
20032
20033/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020034 * Set the value of a variable to NULL without freeing items.
20035 */
20036 static void
20037init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039{
20040 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042}
20043
20044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045 * Get the number value of a variable.
20046 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 * For incompatible types, return 0.
20048 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20049 * caller of incompatible types: it sets *denote to TRUE if "denote"
20050 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 */
20052 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020053get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020056 int error = FALSE;
20057
20058 return get_tv_number_chk(varp, &error); /* return 0L on error */
20059}
20060
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020061 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020062get_tv_number_chk(varp, denote)
20063 typval_T *varp;
20064 int *denote;
20065{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020066 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020068 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020070 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020071 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020072#ifdef FEAT_FLOAT
20073 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020074 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020075 break;
20076#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020077 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020078 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020079 break;
20080 case VAR_STRING:
20081 if (varp->vval.v_string != NULL)
20082 vim_str2nr(varp->vval.v_string, NULL, NULL,
20083 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020084 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020085 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020086 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020087 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020088 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020089 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020090 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020092 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020093 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020095 if (denote == NULL) /* useful for values that must be unsigned */
20096 n = -1;
20097 else
20098 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020099 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100}
20101
20102/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020103 * Get the lnum from the first argument.
20104 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020105 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 */
20107 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110{
Bram Moolenaar33570922005-01-25 22:26:29 +000020111 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 linenr_T lnum;
20113
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020114 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 if (lnum == 0) /* no valid number, try using line() */
20116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 rettv.v_type = VAR_NUMBER;
20118 f_line(argvars, &rettv);
20119 lnum = rettv.vval.v_number;
20120 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 }
20122 return lnum;
20123}
20124
20125/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020126 * Get the lnum from the first argument.
20127 * Also accepts "$", then "buf" is used.
20128 * Returns 0 on error.
20129 */
20130 static linenr_T
20131get_tv_lnum_buf(argvars, buf)
20132 typval_T *argvars;
20133 buf_T *buf;
20134{
20135 if (argvars[0].v_type == VAR_STRING
20136 && argvars[0].vval.v_string != NULL
20137 && argvars[0].vval.v_string[0] == '$'
20138 && buf != NULL)
20139 return buf->b_ml.ml_line_count;
20140 return get_tv_number_chk(&argvars[0], NULL);
20141}
20142
20143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144 * Get the string value of a variable.
20145 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020146 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20147 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 * If the String variable has never been set, return an empty string.
20149 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020150 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20151 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 */
20153 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020154get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020155 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156{
20157 static char_u mybuf[NUMBUFLEN];
20158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020159 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160}
20161
20162 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020165 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020167 char_u *res = get_tv_string_buf_chk(varp, buf);
20168
20169 return res != NULL ? res : (char_u *)"";
20170}
20171
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020172 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020173get_tv_string_chk(varp)
20174 typval_T *varp;
20175{
20176 static char_u mybuf[NUMBUFLEN];
20177
20178 return get_tv_string_buf_chk(varp, mybuf);
20179}
20180
20181 static char_u *
20182get_tv_string_buf_chk(varp, buf)
20183 typval_T *varp;
20184 char_u *buf;
20185{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020186 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020188 case VAR_NUMBER:
20189 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20190 return buf;
20191 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020192 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020193 break;
20194 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020195 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020196 break;
20197 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020198 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020199 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020200#ifdef FEAT_FLOAT
20201 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020202 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020203 break;
20204#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020205 case VAR_STRING:
20206 if (varp->vval.v_string != NULL)
20207 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020208 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020209 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020210 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020213 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214}
20215
20216/*
20217 * Find variable "name" in the list of variables.
20218 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020219 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020220 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020224find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020227 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231
Bram Moolenaara7043832005-01-21 11:56:39 +000020232 ht = find_var_ht(name, &varname);
20233 if (htp != NULL)
20234 *htp = ht;
20235 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020237 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238}
20239
20240/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020241 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020242 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020245find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020246 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020247 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020248 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020249 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020250{
Bram Moolenaar33570922005-01-25 22:26:29 +000020251 hashitem_T *hi;
20252
20253 if (*varname == NUL)
20254 {
20255 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020256 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020257 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020258 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020259 case 'g': return &globvars_var;
20260 case 'v': return &vimvars_var;
20261 case 'b': return &curbuf->b_bufvar;
20262 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020263#ifdef FEAT_WINDOWS
20264 case 't': return &curtab->tp_winvar;
20265#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020266 case 'l': return current_funccal == NULL
20267 ? NULL : &current_funccal->l_vars_var;
20268 case 'a': return current_funccal == NULL
20269 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 }
20271 return NULL;
20272 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020273
20274 hi = hash_find(ht, varname);
20275 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020276 {
20277 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020278 * worked find the variable again. Don't auto-load a script if it was
20279 * loaded already, otherwise it would be loaded every time when
20280 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020281 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020282 {
20283 /* Note: script_autoload() may make "hi" invalid. It must either
20284 * be obtained again or not used. */
20285 if (!script_autoload(varname, FALSE) || aborting())
20286 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020287 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020288 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020289 if (HASHITEM_EMPTY(hi))
20290 return NULL;
20291 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020293}
20294
20295/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020297 * Set "varname" to the start of name without ':'.
20298 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020299 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020300find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 char_u *name;
20302 char_u **varname;
20303{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020304 hashitem_T *hi;
20305
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306 if (name[1] != ':')
20307 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020308 /* The name must not start with a colon or #. */
20309 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 return NULL;
20311 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020312
20313 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020314 hi = hash_find(&compat_hashtab, name);
20315 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020316 return &compat_hashtab;
20317
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 return &globvarht; /* global variable */
20320 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 }
20322 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020323 if (*name == 'g') /* global variable */
20324 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020325 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20326 */
20327 if (vim_strchr(name + 2, ':') != NULL
20328 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020329 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020331 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020333 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020334#ifdef FEAT_WINDOWS
20335 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020336 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020337#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020338 if (*name == 'v') /* v: variable */
20339 return &vimvarht;
20340 if (*name == 'a' && current_funccal != NULL) /* function argument */
20341 return &current_funccal->l_avars.dv_hashtab;
20342 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20343 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 if (*name == 's' /* script variable */
20345 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20346 return &SCRIPT_VARS(current_SID);
20347 return NULL;
20348}
20349
20350/*
20351 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020352 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 * Returns NULL when it doesn't exist.
20354 */
20355 char_u *
20356get_var_value(name)
20357 char_u *name;
20358{
Bram Moolenaar33570922005-01-25 22:26:29 +000020359 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020361 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 if (v == NULL)
20363 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365}
20366
20367/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 * sourcing this script and when executing functions defined in the script.
20370 */
20371 void
20372new_script_vars(id)
20373 scid_T id;
20374{
Bram Moolenaara7043832005-01-21 11:56:39 +000020375 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 hashtab_T *ht;
20377 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020378
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20380 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020381 /* Re-allocating ga_data means that an ht_array pointing to
20382 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020384 for (i = 1; i <= ga_scripts.ga_len; ++i)
20385 {
20386 ht = &SCRIPT_VARS(i);
20387 if (ht->ht_mask == HT_INIT_SIZE - 1)
20388 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020389 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020391 }
20392
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 while (ga_scripts.ga_len < id)
20394 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020395 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020396 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020397 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 }
20400 }
20401}
20402
20403/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20405 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 */
20407 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020408init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 dict_T *dict;
20410 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020411 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412{
Bram Moolenaar33570922005-01-25 22:26:29 +000020413 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020414 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020415 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020416 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020417 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020418 dict_var->di_tv.vval.v_dict = dict;
20419 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020420 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020421 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20422 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
20425/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020426 * Unreference a dictionary initialized by init_var_dict().
20427 */
20428 void
20429unref_var_dict(dict)
20430 dict_T *dict;
20431{
20432 /* Now the dict needs to be freed if no one else is using it, go back to
20433 * normal reference counting. */
20434 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20435 dict_unref(dict);
20436}
20437
20438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020440 * Frees all allocated variables and the value they contain.
20441 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 */
20443 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020444vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020445 hashtab_T *ht;
20446{
20447 vars_clear_ext(ht, TRUE);
20448}
20449
20450/*
20451 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20452 */
20453 static void
20454vars_clear_ext(ht, free_val)
20455 hashtab_T *ht;
20456 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457{
Bram Moolenaara7043832005-01-21 11:56:39 +000020458 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020459 hashitem_T *hi;
20460 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020463 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020464 for (hi = ht->ht_array; todo > 0; ++hi)
20465 {
20466 if (!HASHITEM_EMPTY(hi))
20467 {
20468 --todo;
20469
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020471 * ht_array might change then. hash_clear() takes care of it
20472 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 v = HI2DI(hi);
20474 if (free_val)
20475 clear_tv(&v->di_tv);
20476 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20477 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020478 }
20479 }
20480 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020481 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482}
20483
Bram Moolenaara7043832005-01-21 11:56:39 +000020484/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020485 * Delete a variable from hashtab "ht" at item "hi".
20486 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020489delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020490 hashtab_T *ht;
20491 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492{
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020494
20495 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020496 clear_tv(&di->di_tv);
20497 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498}
20499
20500/*
20501 * List the value of one internal variable.
20502 */
20503 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020504list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020505 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020507 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020509 char_u *tofree;
20510 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020511 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020512
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020513 current_copyID += COPYID_INC;
20514 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020515 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020516 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020517 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518}
20519
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020521list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 char_u *prefix;
20523 char_u *name;
20524 int type;
20525 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020526 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527{
Bram Moolenaar31859182007-08-14 20:41:13 +000020528 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20529 msg_start();
20530 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 if (name != NULL) /* "a:" vars don't have a name stored */
20532 msg_puts(name);
20533 msg_putchar(' ');
20534 msg_advance(22);
20535 if (type == VAR_NUMBER)
20536 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020537 else if (type == VAR_FUNC)
20538 msg_putchar('*');
20539 else if (type == VAR_LIST)
20540 {
20541 msg_putchar('[');
20542 if (*string == '[')
20543 ++string;
20544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020545 else if (type == VAR_DICT)
20546 {
20547 msg_putchar('{');
20548 if (*string == '{')
20549 ++string;
20550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 else
20552 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020555
20556 if (type == VAR_FUNC)
20557 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020558 if (*first)
20559 {
20560 msg_clr_eos();
20561 *first = FALSE;
20562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563}
20564
20565/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020566 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 * If the variable already exists, the value is updated.
20568 * Otherwise the variable is created.
20569 */
20570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020571set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020573 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020574 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575{
Bram Moolenaar33570922005-01-25 22:26:29 +000020576 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020580 ht = find_var_ht(name, &varname);
20581 if (ht == NULL || *varname == NUL)
20582 {
20583 EMSG2(_(e_illvar), name);
20584 return;
20585 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020586 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020587
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020588 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20589 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020590
Bram Moolenaar33570922005-01-25 22:26:29 +000020591 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020593 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020594 if (var_check_ro(v->di_flags, name)
20595 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020596 return;
20597 if (v->di_tv.v_type != tv->v_type
20598 && !((v->di_tv.v_type == VAR_STRING
20599 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020601 || tv->v_type == VAR_NUMBER))
20602#ifdef FEAT_FLOAT
20603 && !((v->di_tv.v_type == VAR_NUMBER
20604 || v->di_tv.v_type == VAR_FLOAT)
20605 && (tv->v_type == VAR_NUMBER
20606 || tv->v_type == VAR_FLOAT))
20607#endif
20608 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020610 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020611 return;
20612 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020613
20614 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020615 * Handle setting internal v: variables separately: we don't change
20616 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 */
20618 if (ht == &vimvarht)
20619 {
20620 if (v->di_tv.v_type == VAR_STRING)
20621 {
20622 vim_free(v->di_tv.vval.v_string);
20623 if (copy || tv->v_type != VAR_STRING)
20624 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20625 else
20626 {
20627 /* Take over the string to avoid an extra alloc/free. */
20628 v->di_tv.vval.v_string = tv->vval.v_string;
20629 tv->vval.v_string = NULL;
20630 }
20631 }
20632 else if (v->di_tv.v_type != VAR_NUMBER)
20633 EMSG2(_(e_intern2), "set_var()");
20634 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020635 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020636 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020637 if (STRCMP(varname, "searchforward") == 0)
20638 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020639#ifdef FEAT_SEARCH_EXTRA
20640 else if (STRCMP(varname, "hlsearch") == 0)
20641 {
20642 no_hlsearch = !v->di_tv.vval.v_number;
20643 redraw_all_later(SOME_VALID);
20644 }
20645#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020646 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020647 return;
20648 }
20649
20650 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020651 }
20652 else /* add a new variable */
20653 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020654 /* Can't add "v:" variable. */
20655 if (ht == &vimvarht)
20656 {
20657 EMSG2(_(e_illvar), name);
20658 return;
20659 }
20660
Bram Moolenaar92124a32005-06-17 22:03:40 +000020661 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020662 if (!valid_varname(varname))
20663 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020664
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020665 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20666 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020667 if (v == NULL)
20668 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020670 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020672 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673 return;
20674 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020675 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020677
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020678 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020680 else
20681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020682 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020683 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020684 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686}
20687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020688/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020689 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020690 * Also give an error message.
20691 */
20692 static int
20693var_check_ro(flags, name)
20694 int flags;
20695 char_u *name;
20696{
20697 if (flags & DI_FLAGS_RO)
20698 {
20699 EMSG2(_(e_readonlyvar), name);
20700 return TRUE;
20701 }
20702 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20703 {
20704 EMSG2(_(e_readonlysbx), name);
20705 return TRUE;
20706 }
20707 return FALSE;
20708}
20709
20710/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020711 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20712 * Also give an error message.
20713 */
20714 static int
20715var_check_fixed(flags, name)
20716 int flags;
20717 char_u *name;
20718{
20719 if (flags & DI_FLAGS_FIX)
20720 {
20721 EMSG2(_("E795: Cannot delete variable %s"), name);
20722 return TRUE;
20723 }
20724 return FALSE;
20725}
20726
20727/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020728 * Check if a funcref is assigned to a valid variable name.
20729 * Return TRUE and give an error if not.
20730 */
20731 static int
20732var_check_func_name(name, new_var)
20733 char_u *name; /* points to start of variable name */
20734 int new_var; /* TRUE when creating the variable */
20735{
20736 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20737 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20738 ? name[2] : name[0]))
20739 {
20740 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20741 name);
20742 return TRUE;
20743 }
20744 /* Don't allow hiding a function. When "v" is not NULL we might be
20745 * assigning another function to the same var, the type is checked
20746 * below. */
20747 if (new_var && function_exists(name))
20748 {
20749 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20750 name);
20751 return TRUE;
20752 }
20753 return FALSE;
20754}
20755
20756/*
20757 * Check if a variable name is valid.
20758 * Return FALSE and give an error if not.
20759 */
20760 static int
20761valid_varname(varname)
20762 char_u *varname;
20763{
20764 char_u *p;
20765
20766 for (p = varname; *p != NUL; ++p)
20767 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20768 && *p != AUTOLOAD_CHAR)
20769 {
20770 EMSG2(_(e_illvar), varname);
20771 return FALSE;
20772 }
20773 return TRUE;
20774}
20775
20776/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020777 * Return TRUE if typeval "tv" is set to be locked (immutable).
20778 * Also give an error message, using "name".
20779 */
20780 static int
20781tv_check_lock(lock, name)
20782 int lock;
20783 char_u *name;
20784{
20785 if (lock & VAR_LOCKED)
20786 {
20787 EMSG2(_("E741: Value is locked: %s"),
20788 name == NULL ? (char_u *)_("Unknown") : name);
20789 return TRUE;
20790 }
20791 if (lock & VAR_FIXED)
20792 {
20793 EMSG2(_("E742: Cannot change value of %s"),
20794 name == NULL ? (char_u *)_("Unknown") : name);
20795 return TRUE;
20796 }
20797 return FALSE;
20798}
20799
20800/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020801 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020802 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020803 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020804 * It is OK for "from" and "to" to point to the same item. This is used to
20805 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020806 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020807 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020808copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020809 typval_T *from;
20810 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020812 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020813 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020814 switch (from->v_type)
20815 {
20816 case VAR_NUMBER:
20817 to->vval.v_number = from->vval.v_number;
20818 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020819#ifdef FEAT_FLOAT
20820 case VAR_FLOAT:
20821 to->vval.v_float = from->vval.v_float;
20822 break;
20823#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 case VAR_STRING:
20825 case VAR_FUNC:
20826 if (from->vval.v_string == NULL)
20827 to->vval.v_string = NULL;
20828 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020829 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020830 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020831 if (from->v_type == VAR_FUNC)
20832 func_ref(to->vval.v_string);
20833 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020834 break;
20835 case VAR_LIST:
20836 if (from->vval.v_list == NULL)
20837 to->vval.v_list = NULL;
20838 else
20839 {
20840 to->vval.v_list = from->vval.v_list;
20841 ++to->vval.v_list->lv_refcount;
20842 }
20843 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020844 case VAR_DICT:
20845 if (from->vval.v_dict == NULL)
20846 to->vval.v_dict = NULL;
20847 else
20848 {
20849 to->vval.v_dict = from->vval.v_dict;
20850 ++to->vval.v_dict->dv_refcount;
20851 }
20852 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020853 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020854 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020855 break;
20856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857}
20858
20859/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020860 * Make a copy of an item.
20861 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020862 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20863 * reference to an already copied list/dict can be used.
20864 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020865 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020866 static int
20867item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 typval_T *from;
20869 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020870 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020871 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020872{
20873 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020874 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020875
Bram Moolenaar33570922005-01-25 22:26:29 +000020876 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020877 {
20878 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020879 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020880 }
20881 ++recurse;
20882
20883 switch (from->v_type)
20884 {
20885 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020886#ifdef FEAT_FLOAT
20887 case VAR_FLOAT:
20888#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020889 case VAR_STRING:
20890 case VAR_FUNC:
20891 copy_tv(from, to);
20892 break;
20893 case VAR_LIST:
20894 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020895 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020896 if (from->vval.v_list == NULL)
20897 to->vval.v_list = NULL;
20898 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20899 {
20900 /* use the copy made earlier */
20901 to->vval.v_list = from->vval.v_list->lv_copylist;
20902 ++to->vval.v_list->lv_refcount;
20903 }
20904 else
20905 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20906 if (to->vval.v_list == NULL)
20907 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020908 break;
20909 case VAR_DICT:
20910 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020911 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020912 if (from->vval.v_dict == NULL)
20913 to->vval.v_dict = NULL;
20914 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20915 {
20916 /* use the copy made earlier */
20917 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20918 ++to->vval.v_dict->dv_refcount;
20919 }
20920 else
20921 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20922 if (to->vval.v_dict == NULL)
20923 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020924 break;
20925 default:
20926 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020927 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020928 }
20929 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020930 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020931}
20932
20933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 * ":echo expr1 ..." print each argument separated with a space, add a
20935 * newline at the end.
20936 * ":echon expr1 ..." print each argument plain.
20937 */
20938 void
20939ex_echo(eap)
20940 exarg_T *eap;
20941{
20942 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020943 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020944 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 char_u *p;
20946 int needclr = TRUE;
20947 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020948 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949
20950 if (eap->skip)
20951 ++emsg_skip;
20952 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020954 /* If eval1() causes an error message the text from the command may
20955 * still need to be cleared. E.g., "echo 22,44". */
20956 need_clr_eos = needclr;
20957
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020959 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 {
20961 /*
20962 * Report the invalid expression unless the expression evaluation
20963 * has been cancelled due to an aborting error, an interrupt, or an
20964 * exception.
20965 */
20966 if (!aborting())
20967 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020968 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 break;
20970 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020971 need_clr_eos = FALSE;
20972
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 if (!eap->skip)
20974 {
20975 if (atstart)
20976 {
20977 atstart = FALSE;
20978 /* Call msg_start() after eval1(), evaluating the expression
20979 * may cause a message to appear. */
20980 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020981 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020982 /* Mark the saved text as finishing the line, so that what
20983 * follows is displayed on a new line when scrolling back
20984 * at the more prompt. */
20985 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 }
20989 else if (eap->cmdidx == CMD_echo)
20990 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020991 current_copyID += COPYID_INC;
20992 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020993 if (p != NULL)
20994 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020996 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020998 if (*p != TAB && needclr)
20999 {
21000 /* remove any text still there from the command */
21001 msg_clr_eos();
21002 needclr = FALSE;
21003 }
21004 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 }
21006 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021007 {
21008#ifdef FEAT_MBYTE
21009 if (has_mbyte)
21010 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021011 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021012
21013 (void)msg_outtrans_len_attr(p, i, echo_attr);
21014 p += i - 1;
21015 }
21016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021018 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021021 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021023 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 arg = skipwhite(arg);
21025 }
21026 eap->nextcmd = check_nextcmd(arg);
21027
21028 if (eap->skip)
21029 --emsg_skip;
21030 else
21031 {
21032 /* remove text that may still be there from the command */
21033 if (needclr)
21034 msg_clr_eos();
21035 if (eap->cmdidx == CMD_echo)
21036 msg_end();
21037 }
21038}
21039
21040/*
21041 * ":echohl {name}".
21042 */
21043 void
21044ex_echohl(eap)
21045 exarg_T *eap;
21046{
21047 int id;
21048
21049 id = syn_name2id(eap->arg);
21050 if (id == 0)
21051 echo_attr = 0;
21052 else
21053 echo_attr = syn_id2attr(id);
21054}
21055
21056/*
21057 * ":execute expr1 ..." execute the result of an expression.
21058 * ":echomsg expr1 ..." Print a message
21059 * ":echoerr expr1 ..." Print an error
21060 * Each gets spaces around each argument and a newline at the end for
21061 * echo commands
21062 */
21063 void
21064ex_execute(eap)
21065 exarg_T *eap;
21066{
21067 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021068 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 int ret = OK;
21070 char_u *p;
21071 garray_T ga;
21072 int len;
21073 int save_did_emsg;
21074
21075 ga_init2(&ga, 1, 80);
21076
21077 if (eap->skip)
21078 ++emsg_skip;
21079 while (*arg != NUL && *arg != '|' && *arg != '\n')
21080 {
21081 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021082 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 {
21084 /*
21085 * Report the invalid expression unless the expression evaluation
21086 * has been cancelled due to an aborting error, an interrupt, or an
21087 * exception.
21088 */
21089 if (!aborting())
21090 EMSG2(_(e_invexpr2), p);
21091 ret = FAIL;
21092 break;
21093 }
21094
21095 if (!eap->skip)
21096 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021097 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 len = (int)STRLEN(p);
21099 if (ga_grow(&ga, len + 2) == FAIL)
21100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 ret = FAIL;
21103 break;
21104 }
21105 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 ga.ga_len += len;
21109 }
21110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021111 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 arg = skipwhite(arg);
21113 }
21114
21115 if (ret != FAIL && ga.ga_data != NULL)
21116 {
21117 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021120 out_flush();
21121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 else if (eap->cmdidx == CMD_echoerr)
21123 {
21124 /* We don't want to abort following commands, restore did_emsg. */
21125 save_did_emsg = did_emsg;
21126 EMSG((char_u *)ga.ga_data);
21127 if (!force_abort)
21128 did_emsg = save_did_emsg;
21129 }
21130 else if (eap->cmdidx == CMD_execute)
21131 do_cmdline((char_u *)ga.ga_data,
21132 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21133 }
21134
21135 ga_clear(&ga);
21136
21137 if (eap->skip)
21138 --emsg_skip;
21139
21140 eap->nextcmd = check_nextcmd(arg);
21141}
21142
21143/*
21144 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21145 * "arg" points to the "&" or '+' when called, to "option" when returning.
21146 * Returns NULL when no option name found. Otherwise pointer to the char
21147 * after the option name.
21148 */
21149 static char_u *
21150find_option_end(arg, opt_flags)
21151 char_u **arg;
21152 int *opt_flags;
21153{
21154 char_u *p = *arg;
21155
21156 ++p;
21157 if (*p == 'g' && p[1] == ':')
21158 {
21159 *opt_flags = OPT_GLOBAL;
21160 p += 2;
21161 }
21162 else if (*p == 'l' && p[1] == ':')
21163 {
21164 *opt_flags = OPT_LOCAL;
21165 p += 2;
21166 }
21167 else
21168 *opt_flags = 0;
21169
21170 if (!ASCII_ISALPHA(*p))
21171 return NULL;
21172 *arg = p;
21173
21174 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21175 p += 4; /* termcap option */
21176 else
21177 while (ASCII_ISALPHA(*p))
21178 ++p;
21179 return p;
21180}
21181
21182/*
21183 * ":function"
21184 */
21185 void
21186ex_function(eap)
21187 exarg_T *eap;
21188{
21189 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021190 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 int j;
21192 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021194 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 char_u *name = NULL;
21196 char_u *p;
21197 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021198 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 garray_T newargs;
21200 garray_T newlines;
21201 int varargs = FALSE;
21202 int mustend = FALSE;
21203 int flags = 0;
21204 ufunc_T *fp;
21205 int indent;
21206 int nesting;
21207 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021208 dictitem_T *v;
21209 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021210 static int func_nr = 0; /* number for nameless function */
21211 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021212 hashtab_T *ht;
21213 int todo;
21214 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021215 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216
21217 /*
21218 * ":function" without argument: list functions.
21219 */
21220 if (ends_excmd(*eap->arg))
21221 {
21222 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021224 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021225 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021226 {
21227 if (!HASHITEM_EMPTY(hi))
21228 {
21229 --todo;
21230 fp = HI2UF(hi);
21231 if (!isdigit(*fp->uf_name))
21232 list_func_head(fp, FALSE);
21233 }
21234 }
21235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 eap->nextcmd = check_nextcmd(eap->arg);
21237 return;
21238 }
21239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021240 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021241 * ":function /pat": list functions matching pattern.
21242 */
21243 if (*eap->arg == '/')
21244 {
21245 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21246 if (!eap->skip)
21247 {
21248 regmatch_T regmatch;
21249
21250 c = *p;
21251 *p = NUL;
21252 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21253 *p = c;
21254 if (regmatch.regprog != NULL)
21255 {
21256 regmatch.rm_ic = p_ic;
21257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021258 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021259 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21260 {
21261 if (!HASHITEM_EMPTY(hi))
21262 {
21263 --todo;
21264 fp = HI2UF(hi);
21265 if (!isdigit(*fp->uf_name)
21266 && vim_regexec(&regmatch, fp->uf_name, 0))
21267 list_func_head(fp, FALSE);
21268 }
21269 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021270 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021271 }
21272 }
21273 if (*p == '/')
21274 ++p;
21275 eap->nextcmd = check_nextcmd(p);
21276 return;
21277 }
21278
21279 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021280 * Get the function name. There are these situations:
21281 * func normal function name
21282 * "name" == func, "fudi.fd_dict" == NULL
21283 * dict.func new dictionary entry
21284 * "name" == NULL, "fudi.fd_dict" set,
21285 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21286 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021287 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021288 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21289 * dict.func existing dict entry that's not a Funcref
21290 * "name" == NULL, "fudi.fd_dict" set,
21291 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 name = trans_function_name(&p, eap->skip, 0, &fudi);
21295 paren = (vim_strchr(p, '(') != NULL);
21296 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 {
21298 /*
21299 * Return on an invalid expression in braces, unless the expression
21300 * evaluation has been cancelled due to an aborting error, an
21301 * interrupt, or an exception.
21302 */
21303 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021305 if (!eap->skip && fudi.fd_newkey != NULL)
21306 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021307 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 else
21311 eap->skip = TRUE;
21312 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021313
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 /* An error in a function call during evaluation of an expression in magic
21315 * braces should not cause the function not to be defined. */
21316 saved_did_emsg = did_emsg;
21317 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318
21319 /*
21320 * ":function func" with only function name: list function.
21321 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021322 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021323 {
21324 if (!ends_excmd(*skipwhite(p)))
21325 {
21326 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021327 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 }
21329 eap->nextcmd = check_nextcmd(p);
21330 if (eap->nextcmd != NULL)
21331 *p = NUL;
21332 if (!eap->skip && !got_int)
21333 {
21334 fp = find_func(name);
21335 if (fp != NULL)
21336 {
21337 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021338 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021340 if (FUNCLINE(fp, j) == NULL)
21341 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 msg_putchar('\n');
21343 msg_outnum((long)(j + 1));
21344 if (j < 9)
21345 msg_putchar(' ');
21346 if (j < 99)
21347 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021348 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 out_flush(); /* show a line at a time */
21350 ui_breakcheck();
21351 }
21352 if (!got_int)
21353 {
21354 msg_putchar('\n');
21355 msg_puts((char_u *)" endfunction");
21356 }
21357 }
21358 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021359 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021361 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 }
21363
21364 /*
21365 * ":function name(arg1, arg2)" Define function.
21366 */
21367 p = skipwhite(p);
21368 if (*p != '(')
21369 {
21370 if (!eap->skip)
21371 {
21372 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021373 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 }
21375 /* attempt to continue by skipping some text */
21376 if (vim_strchr(p, '(') != NULL)
21377 p = vim_strchr(p, '(');
21378 }
21379 p = skipwhite(p + 1);
21380
21381 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21382 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21383
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021384 if (!eap->skip)
21385 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021386 /* Check the name of the function. Unless it's a dictionary function
21387 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021388 if (name != NULL)
21389 arg = name;
21390 else
21391 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021392 if (arg != NULL && (fudi.fd_di == NULL
21393 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021394 {
21395 if (*arg == K_SPECIAL)
21396 j = 3;
21397 else
21398 j = 0;
21399 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21400 : eval_isnamec(arg[j])))
21401 ++j;
21402 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021403 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021404 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021405 /* Disallow using the g: dict. */
21406 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21407 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021408 }
21409
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 /*
21411 * Isolate the arguments: "arg1, arg2, ...)"
21412 */
21413 while (*p != ')')
21414 {
21415 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21416 {
21417 varargs = TRUE;
21418 p += 3;
21419 mustend = TRUE;
21420 }
21421 else
21422 {
21423 arg = p;
21424 while (ASCII_ISALNUM(*p) || *p == '_')
21425 ++p;
21426 if (arg == p || isdigit(*arg)
21427 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21428 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21429 {
21430 if (!eap->skip)
21431 EMSG2(_("E125: Illegal argument: %s"), arg);
21432 break;
21433 }
21434 if (ga_grow(&newargs, 1) == FAIL)
21435 goto erret;
21436 c = *p;
21437 *p = NUL;
21438 arg = vim_strsave(arg);
21439 if (arg == NULL)
21440 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021441
21442 /* Check for duplicate argument name. */
21443 for (i = 0; i < newargs.ga_len; ++i)
21444 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21445 {
21446 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21447 goto erret;
21448 }
21449
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21451 *p = c;
21452 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 if (*p == ',')
21454 ++p;
21455 else
21456 mustend = TRUE;
21457 }
21458 p = skipwhite(p);
21459 if (mustend && *p != ')')
21460 {
21461 if (!eap->skip)
21462 EMSG2(_(e_invarg2), eap->arg);
21463 break;
21464 }
21465 }
21466 ++p; /* skip the ')' */
21467
Bram Moolenaare9a41262005-01-15 22:18:47 +000021468 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 for (;;)
21470 {
21471 p = skipwhite(p);
21472 if (STRNCMP(p, "range", 5) == 0)
21473 {
21474 flags |= FC_RANGE;
21475 p += 5;
21476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021477 else if (STRNCMP(p, "dict", 4) == 0)
21478 {
21479 flags |= FC_DICT;
21480 p += 4;
21481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 else if (STRNCMP(p, "abort", 5) == 0)
21483 {
21484 flags |= FC_ABORT;
21485 p += 5;
21486 }
21487 else
21488 break;
21489 }
21490
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021491 /* When there is a line break use what follows for the function body.
21492 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21493 if (*p == '\n')
21494 line_arg = p + 1;
21495 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 EMSG(_(e_trailing));
21497
21498 /*
21499 * Read the body of the function, until ":endfunction" is found.
21500 */
21501 if (KeyTyped)
21502 {
21503 /* Check if the function already exists, don't let the user type the
21504 * whole function before telling him it doesn't work! For a script we
21505 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 if (!eap->skip && !eap->forceit)
21507 {
21508 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21509 EMSG(_(e_funcdict));
21510 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021511 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021514 if (!eap->skip && did_emsg)
21515 goto erret;
21516
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 msg_putchar('\n'); /* don't overwrite the function name */
21518 cmdline_row = msg_row;
21519 }
21520
21521 indent = 2;
21522 nesting = 0;
21523 for (;;)
21524 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021525 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021526 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021527 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021528 saved_wait_return = FALSE;
21529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021531 sourcing_lnum_off = sourcing_lnum;
21532
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021533 if (line_arg != NULL)
21534 {
21535 /* Use eap->arg, split up in parts by line breaks. */
21536 theline = line_arg;
21537 p = vim_strchr(theline, '\n');
21538 if (p == NULL)
21539 line_arg += STRLEN(line_arg);
21540 else
21541 {
21542 *p = NUL;
21543 line_arg = p + 1;
21544 }
21545 }
21546 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 theline = getcmdline(':', 0L, indent);
21548 else
21549 theline = eap->getline(':', eap->cookie, indent);
21550 if (KeyTyped)
21551 lines_left = Rows - 1;
21552 if (theline == NULL)
21553 {
21554 EMSG(_("E126: Missing :endfunction"));
21555 goto erret;
21556 }
21557
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021558 /* Detect line continuation: sourcing_lnum increased more than one. */
21559 if (sourcing_lnum > sourcing_lnum_off + 1)
21560 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21561 else
21562 sourcing_lnum_off = 0;
21563
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 if (skip_until != NULL)
21565 {
21566 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21567 * don't check for ":endfunc". */
21568 if (STRCMP(theline, skip_until) == 0)
21569 {
21570 vim_free(skip_until);
21571 skip_until = NULL;
21572 }
21573 }
21574 else
21575 {
21576 /* skip ':' and blanks*/
21577 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21578 ;
21579
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021580 /* Check for "endfunction". */
21581 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021583 if (line_arg == NULL)
21584 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 break;
21586 }
21587
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021588 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 * at "end". */
21590 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21591 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021592 else if (STRNCMP(p, "if", 2) == 0
21593 || STRNCMP(p, "wh", 2) == 0
21594 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 || STRNCMP(p, "try", 3) == 0)
21596 indent += 2;
21597
21598 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021599 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021601 if (*p == '!')
21602 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 p += eval_fname_script(p);
21604 if (ASCII_ISALPHA(*p))
21605 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021606 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 if (*skipwhite(p) == '(')
21608 {
21609 ++nesting;
21610 indent += 2;
21611 }
21612 }
21613 }
21614
21615 /* Check for ":append" or ":insert". */
21616 p = skip_range(p, NULL);
21617 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21618 || (p[0] == 'i'
21619 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21620 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21621 skip_until = vim_strsave((char_u *)".");
21622
21623 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21624 arg = skipwhite(skiptowhite(p));
21625 if (arg[0] == '<' && arg[1] =='<'
21626 && ((p[0] == 'p' && p[1] == 'y'
21627 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21628 || (p[0] == 'p' && p[1] == 'e'
21629 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21630 || (p[0] == 't' && p[1] == 'c'
21631 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021632 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21633 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21635 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021636 || (p[0] == 'm' && p[1] == 'z'
21637 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 ))
21639 {
21640 /* ":python <<" continues until a dot, like ":append" */
21641 p = skipwhite(arg + 2);
21642 if (*p == NUL)
21643 skip_until = vim_strsave((char_u *)".");
21644 else
21645 skip_until = vim_strsave(p);
21646 }
21647 }
21648
21649 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021650 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021651 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021652 if (line_arg == NULL)
21653 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021655 }
21656
21657 /* Copy the line to newly allocated memory. get_one_sourceline()
21658 * allocates 250 bytes per line, this saves 80% on average. The cost
21659 * is an extra alloc/free. */
21660 p = vim_strsave(theline);
21661 if (p != NULL)
21662 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021663 if (line_arg == NULL)
21664 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021665 theline = p;
21666 }
21667
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021668 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21669
21670 /* Add NULL lines for continuation lines, so that the line count is
21671 * equal to the index in the growarray. */
21672 while (sourcing_lnum_off-- > 0)
21673 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021674
21675 /* Check for end of eap->arg. */
21676 if (line_arg != NULL && *line_arg == NUL)
21677 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 }
21679
21680 /* Don't define the function when skipping commands or when an error was
21681 * detected. */
21682 if (eap->skip || did_emsg)
21683 goto erret;
21684
21685 /*
21686 * If there are no errors, add the function
21687 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021688 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021689 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021690 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021691 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021692 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021693 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021694 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021695 goto erret;
21696 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021697
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021698 fp = find_func(name);
21699 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021701 if (!eap->forceit)
21702 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021703 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021704 goto erret;
21705 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021706 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021707 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021708 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021709 name);
21710 goto erret;
21711 }
21712 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021713 ga_clear_strings(&(fp->uf_args));
21714 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021715 vim_free(name);
21716 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 }
21719 else
21720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021721 char numbuf[20];
21722
21723 fp = NULL;
21724 if (fudi.fd_newkey == NULL && !eap->forceit)
21725 {
21726 EMSG(_(e_funcdict));
21727 goto erret;
21728 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021729 if (fudi.fd_di == NULL)
21730 {
21731 /* Can't add a function to a locked dictionary */
21732 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21733 goto erret;
21734 }
21735 /* Can't change an existing function if it is locked */
21736 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21737 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021738
21739 /* Give the function a sequential number. Can only be used with a
21740 * Funcref! */
21741 vim_free(name);
21742 sprintf(numbuf, "%d", ++func_nr);
21743 name = vim_strsave((char_u *)numbuf);
21744 if (name == NULL)
21745 goto erret;
21746 }
21747
21748 if (fp == NULL)
21749 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021750 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021751 {
21752 int slen, plen;
21753 char_u *scriptname;
21754
21755 /* Check that the autoload name matches the script name. */
21756 j = FAIL;
21757 if (sourcing_name != NULL)
21758 {
21759 scriptname = autoload_name(name);
21760 if (scriptname != NULL)
21761 {
21762 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021763 plen = (int)STRLEN(p);
21764 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021765 if (slen > plen && fnamecmp(p,
21766 sourcing_name + slen - plen) == 0)
21767 j = OK;
21768 vim_free(scriptname);
21769 }
21770 }
21771 if (j == FAIL)
21772 {
21773 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21774 goto erret;
21775 }
21776 }
21777
21778 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 if (fp == NULL)
21780 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021781
21782 if (fudi.fd_dict != NULL)
21783 {
21784 if (fudi.fd_di == NULL)
21785 {
21786 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021787 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021788 if (fudi.fd_di == NULL)
21789 {
21790 vim_free(fp);
21791 goto erret;
21792 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021793 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21794 {
21795 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021796 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021797 goto erret;
21798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021799 }
21800 else
21801 /* overwrite existing dict entry */
21802 clear_tv(&fudi.fd_di->di_tv);
21803 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021804 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021805 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021806 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021807
21808 /* behave like "dict" was used */
21809 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021810 }
21811
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021813 STRCPY(fp->uf_name, name);
21814 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021816 fp->uf_args = newargs;
21817 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021818#ifdef FEAT_PROFILE
21819 fp->uf_tml_count = NULL;
21820 fp->uf_tml_total = NULL;
21821 fp->uf_tml_self = NULL;
21822 fp->uf_profiling = FALSE;
21823 if (prof_def_func())
21824 func_do_profile(fp);
21825#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021826 fp->uf_varargs = varargs;
21827 fp->uf_flags = flags;
21828 fp->uf_calls = 0;
21829 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021830 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831
21832erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 ga_clear_strings(&newargs);
21834 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021835ret_free:
21836 vim_free(skip_until);
21837 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021840 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841}
21842
21843/*
21844 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021845 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021847 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021848 * TFN_INT: internal function name OK
21849 * TFN_QUIET: be quiet
21850 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 * Advances "pp" to just after the function name (if no error).
21852 */
21853 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021854trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 char_u **pp;
21856 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021857 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021860 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 char_u *start;
21862 char_u *end;
21863 int lead;
21864 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021867
21868 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021869 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021871
21872 /* Check for hard coded <SNR>: already translated function ID (from a user
21873 * command). */
21874 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21875 && (*pp)[2] == (int)KE_SNR)
21876 {
21877 *pp += 3;
21878 len = get_id_len(pp) + 3;
21879 return vim_strnsave(start, len);
21880 }
21881
21882 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21883 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021885 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021887
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021888 /* Note that TFN_ flags use the same values as GLV_ flags. */
21889 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021890 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021891 if (end == start)
21892 {
21893 if (!skip)
21894 EMSG(_("E129: Function name required"));
21895 goto theend;
21896 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021897 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021898 {
21899 /*
21900 * Report an invalid expression in braces, unless the expression
21901 * evaluation has been cancelled due to an aborting error, an
21902 * interrupt, or an exception.
21903 */
21904 if (!aborting())
21905 {
21906 if (end != NULL)
21907 EMSG2(_(e_invarg2), start);
21908 }
21909 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021910 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021911 goto theend;
21912 }
21913
21914 if (lv.ll_tv != NULL)
21915 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021916 if (fdp != NULL)
21917 {
21918 fdp->fd_dict = lv.ll_dict;
21919 fdp->fd_newkey = lv.ll_newkey;
21920 lv.ll_newkey = NULL;
21921 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021922 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021923 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21924 {
21925 name = vim_strsave(lv.ll_tv->vval.v_string);
21926 *pp = end;
21927 }
21928 else
21929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021930 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21931 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021932 EMSG(_(e_funcref));
21933 else
21934 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021935 name = NULL;
21936 }
21937 goto theend;
21938 }
21939
21940 if (lv.ll_name == NULL)
21941 {
21942 /* Error found, but continue after the function name. */
21943 *pp = end;
21944 goto theend;
21945 }
21946
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021947 /* Check if the name is a Funcref. If so, use the value. */
21948 if (lv.ll_exp_name != NULL)
21949 {
21950 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010021951 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021952 if (name == lv.ll_exp_name)
21953 name = NULL;
21954 }
21955 else
21956 {
21957 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010021958 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021959 if (name == *pp)
21960 name = NULL;
21961 }
21962 if (name != NULL)
21963 {
21964 name = vim_strsave(name);
21965 *pp = end;
21966 goto theend;
21967 }
21968
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021969 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021971 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021972 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21973 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21974 {
21975 /* When there was "s:" already or the name expanded to get a
21976 * leading "s:" then remove it. */
21977 lv.ll_name += 2;
21978 len -= 2;
21979 lead = 2;
21980 }
21981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021982 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021983 {
21984 if (lead == 2) /* skip over "s:" */
21985 lv.ll_name += 2;
21986 len = (int)(end - lv.ll_name);
21987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021988
21989 /*
21990 * Copy the function name to allocated memory.
21991 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21992 * Accept <SNR>123_name() outside a script.
21993 */
21994 if (skip)
21995 lead = 0; /* do nothing */
21996 else if (lead > 0)
21997 {
21998 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021999 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22000 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022001 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022002 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022003 if (current_SID <= 0)
22004 {
22005 EMSG(_(e_usingsid));
22006 goto theend;
22007 }
22008 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22009 lead += (int)STRLEN(sid_buf);
22010 }
22011 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022012 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022013 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022014 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022015 goto theend;
22016 }
22017 name = alloc((unsigned)(len + lead + 1));
22018 if (name != NULL)
22019 {
22020 if (lead > 0)
22021 {
22022 name[0] = K_SPECIAL;
22023 name[1] = KS_EXTRA;
22024 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022025 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022026 STRCPY(name + 3, sid_buf);
22027 }
22028 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22029 name[len + lead] = NUL;
22030 }
22031 *pp = end;
22032
22033theend:
22034 clear_lval(&lv);
22035 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036}
22037
22038/*
22039 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22040 * Return 2 if "p" starts with "s:".
22041 * Return 0 otherwise.
22042 */
22043 static int
22044eval_fname_script(p)
22045 char_u *p;
22046{
22047 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22048 || STRNICMP(p + 1, "SNR>", 4) == 0))
22049 return 5;
22050 if (p[0] == 's' && p[1] == ':')
22051 return 2;
22052 return 0;
22053}
22054
22055/*
22056 * Return TRUE if "p" starts with "<SID>" or "s:".
22057 * Only works if eval_fname_script() returned non-zero for "p"!
22058 */
22059 static int
22060eval_fname_sid(p)
22061 char_u *p;
22062{
22063 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22064}
22065
22066/*
22067 * List the head of the function: "name(arg1, arg2)".
22068 */
22069 static void
22070list_func_head(fp, indent)
22071 ufunc_T *fp;
22072 int indent;
22073{
22074 int j;
22075
22076 msg_start();
22077 if (indent)
22078 MSG_PUTS(" ");
22079 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022080 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081 {
22082 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022083 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 }
22085 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022086 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022088 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 {
22090 if (j)
22091 MSG_PUTS(", ");
22092 msg_puts(FUNCARG(fp, j));
22093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022094 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 {
22096 if (j)
22097 MSG_PUTS(", ");
22098 MSG_PUTS("...");
22099 }
22100 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022101 if (fp->uf_flags & FC_ABORT)
22102 MSG_PUTS(" abort");
22103 if (fp->uf_flags & FC_RANGE)
22104 MSG_PUTS(" range");
22105 if (fp->uf_flags & FC_DICT)
22106 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022107 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022108 if (p_verbose > 0)
22109 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110}
22111
22112/*
22113 * Find a function by name, return pointer to it in ufuncs.
22114 * Return NULL for unknown function.
22115 */
22116 static ufunc_T *
22117find_func(name)
22118 char_u *name;
22119{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022120 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022122 hi = hash_find(&func_hashtab, name);
22123 if (!HASHITEM_EMPTY(hi))
22124 return HI2UF(hi);
22125 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126}
22127
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022128#if defined(EXITFREE) || defined(PROTO)
22129 void
22130free_all_functions()
22131{
22132 hashitem_T *hi;
22133
22134 /* Need to start all over every time, because func_free() may change the
22135 * hash table. */
22136 while (func_hashtab.ht_used > 0)
22137 for (hi = func_hashtab.ht_array; ; ++hi)
22138 if (!HASHITEM_EMPTY(hi))
22139 {
22140 func_free(HI2UF(hi));
22141 break;
22142 }
22143}
22144#endif
22145
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022146 int
22147translated_function_exists(name)
22148 char_u *name;
22149{
22150 if (builtin_function(name))
22151 return find_internal_func(name) >= 0;
22152 return find_func(name) != NULL;
22153}
22154
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022155/*
22156 * Return TRUE if a function "name" exists.
22157 */
22158 static int
22159function_exists(name)
22160 char_u *name;
22161{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022162 char_u *nm = name;
22163 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022164 int n = FALSE;
22165
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022166 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22167 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022168 nm = skipwhite(nm);
22169
22170 /* Only accept "funcname", "funcname ", "funcname (..." and
22171 * "funcname(...", not "funcname!...". */
22172 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022173 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022174 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022175 return n;
22176}
22177
Bram Moolenaara1544c02013-05-30 12:35:52 +020022178 char_u *
22179get_expanded_name(name, check)
22180 char_u *name;
22181 int check;
22182{
22183 char_u *nm = name;
22184 char_u *p;
22185
22186 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22187
22188 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022189 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022190 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022191
Bram Moolenaara1544c02013-05-30 12:35:52 +020022192 vim_free(p);
22193 return NULL;
22194}
22195
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022196/*
22197 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022198 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022199 */
22200 static int
22201builtin_function(name)
22202 char_u *name;
22203{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022204 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22205 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022206}
22207
Bram Moolenaar05159a02005-02-26 23:04:13 +000022208#if defined(FEAT_PROFILE) || defined(PROTO)
22209/*
22210 * Start profiling function "fp".
22211 */
22212 static void
22213func_do_profile(fp)
22214 ufunc_T *fp;
22215{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022216 int len = fp->uf_lines.ga_len;
22217
22218 if (len == 0)
22219 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022220 fp->uf_tm_count = 0;
22221 profile_zero(&fp->uf_tm_self);
22222 profile_zero(&fp->uf_tm_total);
22223 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022224 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022225 if (fp->uf_tml_total == NULL)
22226 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022227 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022228 if (fp->uf_tml_self == NULL)
22229 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022230 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022231 fp->uf_tml_idx = -1;
22232 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22233 || fp->uf_tml_self == NULL)
22234 return; /* out of memory */
22235
22236 fp->uf_profiling = TRUE;
22237}
22238
22239/*
22240 * Dump the profiling results for all functions in file "fd".
22241 */
22242 void
22243func_dump_profile(fd)
22244 FILE *fd;
22245{
22246 hashitem_T *hi;
22247 int todo;
22248 ufunc_T *fp;
22249 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022250 ufunc_T **sorttab;
22251 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022253 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022254 if (todo == 0)
22255 return; /* nothing to dump */
22256
Bram Moolenaar73830342005-02-28 22:48:19 +000022257 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22258
Bram Moolenaar05159a02005-02-26 23:04:13 +000022259 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22260 {
22261 if (!HASHITEM_EMPTY(hi))
22262 {
22263 --todo;
22264 fp = HI2UF(hi);
22265 if (fp->uf_profiling)
22266 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022267 if (sorttab != NULL)
22268 sorttab[st_len++] = fp;
22269
Bram Moolenaar05159a02005-02-26 23:04:13 +000022270 if (fp->uf_name[0] == K_SPECIAL)
22271 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22272 else
22273 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22274 if (fp->uf_tm_count == 1)
22275 fprintf(fd, "Called 1 time\n");
22276 else
22277 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22278 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22279 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22280 fprintf(fd, "\n");
22281 fprintf(fd, "count total (s) self (s)\n");
22282
22283 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22284 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022285 if (FUNCLINE(fp, i) == NULL)
22286 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022287 prof_func_line(fd, fp->uf_tml_count[i],
22288 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022289 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22290 }
22291 fprintf(fd, "\n");
22292 }
22293 }
22294 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022295
22296 if (sorttab != NULL && st_len > 0)
22297 {
22298 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22299 prof_total_cmp);
22300 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22301 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22302 prof_self_cmp);
22303 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22304 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022305
22306 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022307}
Bram Moolenaar73830342005-02-28 22:48:19 +000022308
22309 static void
22310prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22311 FILE *fd;
22312 ufunc_T **sorttab;
22313 int st_len;
22314 char *title;
22315 int prefer_self; /* when equal print only self time */
22316{
22317 int i;
22318 ufunc_T *fp;
22319
22320 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22321 fprintf(fd, "count total (s) self (s) function\n");
22322 for (i = 0; i < 20 && i < st_len; ++i)
22323 {
22324 fp = sorttab[i];
22325 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22326 prefer_self);
22327 if (fp->uf_name[0] == K_SPECIAL)
22328 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22329 else
22330 fprintf(fd, " %s()\n", fp->uf_name);
22331 }
22332 fprintf(fd, "\n");
22333}
22334
22335/*
22336 * Print the count and times for one function or function line.
22337 */
22338 static void
22339prof_func_line(fd, count, total, self, prefer_self)
22340 FILE *fd;
22341 int count;
22342 proftime_T *total;
22343 proftime_T *self;
22344 int prefer_self; /* when equal print only self time */
22345{
22346 if (count > 0)
22347 {
22348 fprintf(fd, "%5d ", count);
22349 if (prefer_self && profile_equal(total, self))
22350 fprintf(fd, " ");
22351 else
22352 fprintf(fd, "%s ", profile_msg(total));
22353 if (!prefer_self && profile_equal(total, self))
22354 fprintf(fd, " ");
22355 else
22356 fprintf(fd, "%s ", profile_msg(self));
22357 }
22358 else
22359 fprintf(fd, " ");
22360}
22361
22362/*
22363 * Compare function for total time sorting.
22364 */
22365 static int
22366#ifdef __BORLANDC__
22367_RTLENTRYF
22368#endif
22369prof_total_cmp(s1, s2)
22370 const void *s1;
22371 const void *s2;
22372{
22373 ufunc_T *p1, *p2;
22374
22375 p1 = *(ufunc_T **)s1;
22376 p2 = *(ufunc_T **)s2;
22377 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22378}
22379
22380/*
22381 * Compare function for self time sorting.
22382 */
22383 static int
22384#ifdef __BORLANDC__
22385_RTLENTRYF
22386#endif
22387prof_self_cmp(s1, s2)
22388 const void *s1;
22389 const void *s2;
22390{
22391 ufunc_T *p1, *p2;
22392
22393 p1 = *(ufunc_T **)s1;
22394 p2 = *(ufunc_T **)s2;
22395 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22396}
22397
Bram Moolenaar05159a02005-02-26 23:04:13 +000022398#endif
22399
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022400/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022402 * Return TRUE if a package was loaded.
22403 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022404 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022405script_autoload(name, reload)
22406 char_u *name;
22407 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022408{
22409 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022410 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022411 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022412 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022413
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022414 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022415 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022416 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022417 return FALSE;
22418
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022419 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022420
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022421 /* Find the name in the list of previously loaded package names. Skip
22422 * "autoload/", it's always the same. */
22423 for (i = 0; i < ga_loaded.ga_len; ++i)
22424 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22425 break;
22426 if (!reload && i < ga_loaded.ga_len)
22427 ret = FALSE; /* was loaded already */
22428 else
22429 {
22430 /* Remember the name if it wasn't loaded already. */
22431 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22432 {
22433 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22434 tofree = NULL;
22435 }
22436
22437 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022438 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022439 ret = TRUE;
22440 }
22441
22442 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 return ret;
22444}
22445
22446/*
22447 * Return the autoload script name for a function or variable name.
22448 * Returns NULL when out of memory.
22449 */
22450 static char_u *
22451autoload_name(name)
22452 char_u *name;
22453{
22454 char_u *p;
22455 char_u *scriptname;
22456
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022457 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022458 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22459 if (scriptname == NULL)
22460 return FALSE;
22461 STRCPY(scriptname, "autoload/");
22462 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022463 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022464 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022465 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022466 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022467 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022468}
22469
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22471
22472/*
22473 * Function given to ExpandGeneric() to obtain the list of user defined
22474 * function names.
22475 */
22476 char_u *
22477get_user_func_name(xp, idx)
22478 expand_T *xp;
22479 int idx;
22480{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022481 static long_u done;
22482 static hashitem_T *hi;
22483 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484
22485 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022487 done = 0;
22488 hi = func_hashtab.ht_array;
22489 }
22490 if (done < func_hashtab.ht_used)
22491 {
22492 if (done++ > 0)
22493 ++hi;
22494 while (HASHITEM_EMPTY(hi))
22495 ++hi;
22496 fp = HI2UF(hi);
22497
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022498 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022499 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022500
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022501 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22502 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503
22504 cat_func_name(IObuff, fp);
22505 if (xp->xp_context != EXPAND_USER_FUNC)
22506 {
22507 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022508 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 STRCAT(IObuff, ")");
22510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 return IObuff;
22512 }
22513 return NULL;
22514}
22515
22516#endif /* FEAT_CMDL_COMPL */
22517
22518/*
22519 * Copy the function name of "fp" to buffer "buf".
22520 * "buf" must be able to hold the function name plus three bytes.
22521 * Takes care of script-local function names.
22522 */
22523 static void
22524cat_func_name(buf, fp)
22525 char_u *buf;
22526 ufunc_T *fp;
22527{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022528 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 {
22530 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022531 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 }
22533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022534 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535}
22536
22537/*
22538 * ":delfunction {name}"
22539 */
22540 void
22541ex_delfunction(eap)
22542 exarg_T *eap;
22543{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022544 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 char_u *p;
22546 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022547 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548
22549 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022550 name = trans_function_name(&p, eap->skip, 0, &fudi);
22551 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022553 {
22554 if (fudi.fd_dict != NULL && !eap->skip)
22555 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 if (!ends_excmd(*skipwhite(p)))
22559 {
22560 vim_free(name);
22561 EMSG(_(e_trailing));
22562 return;
22563 }
22564 eap->nextcmd = check_nextcmd(p);
22565 if (eap->nextcmd != NULL)
22566 *p = NUL;
22567
22568 if (!eap->skip)
22569 fp = find_func(name);
22570 vim_free(name);
22571
22572 if (!eap->skip)
22573 {
22574 if (fp == NULL)
22575 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022576 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 return;
22578 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022579 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 {
22581 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22582 return;
22583 }
22584
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022585 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022587 /* Delete the dict item that refers to the function, it will
22588 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022589 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022591 else
22592 func_free(fp);
22593 }
22594}
22595
22596/*
22597 * Free a function and remove it from the list of functions.
22598 */
22599 static void
22600func_free(fp)
22601 ufunc_T *fp;
22602{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022603 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604
22605 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022606 ga_clear_strings(&(fp->uf_args));
22607 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022608#ifdef FEAT_PROFILE
22609 vim_free(fp->uf_tml_count);
22610 vim_free(fp->uf_tml_total);
22611 vim_free(fp->uf_tml_self);
22612#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022613
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022614 /* remove the function from the function hashtable */
22615 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22616 if (HASHITEM_EMPTY(hi))
22617 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022618 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022619 hash_remove(&func_hashtab, hi);
22620
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022621 vim_free(fp);
22622}
22623
22624/*
22625 * Unreference a Function: decrement the reference count and free it when it
22626 * becomes zero. Only for numbered functions.
22627 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022628 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022629func_unref(name)
22630 char_u *name;
22631{
22632 ufunc_T *fp;
22633
22634 if (name != NULL && isdigit(*name))
22635 {
22636 fp = find_func(name);
22637 if (fp == NULL)
22638 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022639 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022640 {
22641 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022642 * when "uf_calls" becomes zero. */
22643 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022644 func_free(fp);
22645 }
22646 }
22647}
22648
22649/*
22650 * Count a reference to a Function.
22651 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022652 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022653func_ref(name)
22654 char_u *name;
22655{
22656 ufunc_T *fp;
22657
22658 if (name != NULL && isdigit(*name))
22659 {
22660 fp = find_func(name);
22661 if (fp == NULL)
22662 EMSG2(_(e_intern2), "func_ref()");
22663 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022664 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
22666}
22667
22668/*
22669 * Call a user function.
22670 */
22671 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022672call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 ufunc_T *fp; /* pointer to function */
22674 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 typval_T *argvars; /* arguments */
22676 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 linenr_T firstline; /* first line of range */
22678 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680{
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 char_u *save_sourcing_name;
22682 linenr_T save_sourcing_lnum;
22683 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022684 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022685 int save_did_emsg;
22686 static int depth = 0;
22687 dictitem_T *v;
22688 int fixvar_idx = 0; /* index in fixvar[] */
22689 int i;
22690 int ai;
22691 char_u numbuf[NUMBUFLEN];
22692 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022693#ifdef FEAT_PROFILE
22694 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022695 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697
22698 /* If depth of calling is getting too high, don't execute the function */
22699 if (depth >= p_mfd)
22700 {
22701 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022702 rettv->v_type = VAR_NUMBER;
22703 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 return;
22705 }
22706 ++depth;
22707
22708 line_breakcheck(); /* check for CTRL-C hit */
22709
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022710 fc = (funccall_T *)alloc(sizeof(funccall_T));
22711 fc->caller = current_funccal;
22712 current_funccal = fc;
22713 fc->func = fp;
22714 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022715 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022716 fc->linenr = 0;
22717 fc->returned = FALSE;
22718 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022720 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22721 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722
Bram Moolenaar33570922005-01-25 22:26:29 +000022723 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022724 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022725 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22726 * each argument variable and saves a lot of time.
22727 */
22728 /*
22729 * Init l: variables.
22730 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022731 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022732 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022733 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022734 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22735 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022736 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022737 name = v->di_key;
22738 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022740 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022742 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 v->di_tv.vval.v_dict = selfdict;
22744 ++selfdict->dv_refcount;
22745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022746
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 /*
22748 * Init a: variables.
22749 * Set a:0 to "argcount".
22750 * Set a:000 to a list with room for the "..." arguments.
22751 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022752 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022753 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022754 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022755 /* Use "name" to avoid a warning from some compiler that checks the
22756 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022757 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022758 name = v->di_key;
22759 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022760 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022761 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022762 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022763 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022764 v->di_tv.vval.v_list = &fc->l_varlist;
22765 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22766 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22767 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022768
22769 /*
22770 * Set a:firstline to "firstline" and a:lastline to "lastline".
22771 * Set a:name to named arguments.
22772 * Set a:N to the "..." arguments.
22773 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022774 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022776 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022777 (varnumber_T)lastline);
22778 for (i = 0; i < argcount; ++i)
22779 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022780 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022781 if (ai < 0)
22782 /* named argument a:name */
22783 name = FUNCARG(fp, i);
22784 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022785 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022786 /* "..." argument a:1, a:2, etc. */
22787 sprintf((char *)numbuf, "%d", ai + 1);
22788 name = numbuf;
22789 }
22790 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22791 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022792 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022793 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22794 }
22795 else
22796 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022797 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22798 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 if (v == NULL)
22800 break;
22801 v->di_flags = DI_FLAGS_RO;
22802 }
22803 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022804 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022805
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022806 /* Note: the values are copied directly to avoid alloc/free.
22807 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022808 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022809 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022810
22811 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22812 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022813 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22814 fc->l_listitems[ai].li_tv = argvars[i];
22815 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022816 }
22817 }
22818
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 /* Don't redraw while executing the function. */
22820 ++RedrawingDisabled;
22821 save_sourcing_name = sourcing_name;
22822 save_sourcing_lnum = sourcing_lnum;
22823 sourcing_lnum = 1;
22824 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022825 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 if (sourcing_name != NULL)
22827 {
22828 if (save_sourcing_name != NULL
22829 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22830 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22831 else
22832 STRCPY(sourcing_name, "function ");
22833 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22834
22835 if (p_verbose >= 12)
22836 {
22837 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022838 verbose_enter_scroll();
22839
Bram Moolenaar555b2802005-05-19 21:08:39 +000022840 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 if (p_verbose >= 14)
22842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022844 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022845 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022846 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847
22848 msg_puts((char_u *)"(");
22849 for (i = 0; i < argcount; ++i)
22850 {
22851 if (i > 0)
22852 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022853 if (argvars[i].v_type == VAR_NUMBER)
22854 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 else
22856 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022857 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22858 if (s != NULL)
22859 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022860 if (vim_strsize(s) > MSG_BUF_CLEN)
22861 {
22862 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22863 s = buf;
22864 }
22865 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022866 vim_free(tofree);
22867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 }
22869 }
22870 msg_puts((char_u *)")");
22871 }
22872 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022873
22874 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 --no_wait_return;
22876 }
22877 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022878#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022879 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022880 {
22881 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22882 func_do_profile(fp);
22883 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022884 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022885 {
22886 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022887 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022888 profile_zero(&fp->uf_tm_children);
22889 }
22890 script_prof_save(&wait_start);
22891 }
22892#endif
22893
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022895 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 save_did_emsg = did_emsg;
22897 did_emsg = FALSE;
22898
22899 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022900 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22902
22903 --RedrawingDisabled;
22904
22905 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022908 clear_tv(rettv);
22909 rettv->v_type = VAR_NUMBER;
22910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 }
22912
Bram Moolenaar05159a02005-02-26 23:04:13 +000022913#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022914 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022915 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022916 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022917 profile_end(&call_start);
22918 profile_sub_wait(&wait_start, &call_start);
22919 profile_add(&fp->uf_tm_total, &call_start);
22920 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022921 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022922 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022923 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22924 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022925 }
22926 }
22927#endif
22928
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 /* when being verbose, mention the return value */
22930 if (p_verbose >= 12)
22931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022933 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022936 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022937 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022938 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022939 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022942 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022943 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022944 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022945 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022946
Bram Moolenaar555b2802005-05-19 21:08:39 +000022947 /* The value may be very long. Skip the middle part, so that we
22948 * have some idea how it starts and ends. smsg() would always
22949 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022950 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022951 if (s != NULL)
22952 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022953 if (vim_strsize(s) > MSG_BUF_CLEN)
22954 {
22955 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22956 s = buf;
22957 }
22958 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022959 vim_free(tofree);
22960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 }
22962 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022963
22964 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965 --no_wait_return;
22966 }
22967
22968 vim_free(sourcing_name);
22969 sourcing_name = save_sourcing_name;
22970 sourcing_lnum = save_sourcing_lnum;
22971 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022972#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022973 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022974 script_prof_restore(&wait_start);
22975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976
22977 if (p_verbose >= 12 && sourcing_name != NULL)
22978 {
22979 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022980 verbose_enter_scroll();
22981
Bram Moolenaar555b2802005-05-19 21:08:39 +000022982 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022984
22985 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 --no_wait_return;
22987 }
22988
22989 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022990 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022992
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022993 /* If the a:000 list and the l: and a: dicts are not referenced we can
22994 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022995 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22996 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22997 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22998 {
22999 free_funccal(fc, FALSE);
23000 }
23001 else
23002 {
23003 hashitem_T *hi;
23004 listitem_T *li;
23005 int todo;
23006
23007 /* "fc" is still in use. This can happen when returning "a:000" or
23008 * assigning "l:" to a global variable.
23009 * Link "fc" in the list for garbage collection later. */
23010 fc->caller = previous_funccal;
23011 previous_funccal = fc;
23012
23013 /* Make a copy of the a: variables, since we didn't do that above. */
23014 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23015 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23016 {
23017 if (!HASHITEM_EMPTY(hi))
23018 {
23019 --todo;
23020 v = HI2DI(hi);
23021 copy_tv(&v->di_tv, &v->di_tv);
23022 }
23023 }
23024
23025 /* Make a copy of the a:000 items, since we didn't do that above. */
23026 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23027 copy_tv(&li->li_tv, &li->li_tv);
23028 }
23029}
23030
23031/*
23032 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023033 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023034 */
23035 static int
23036can_free_funccal(fc, copyID)
23037 funccall_T *fc;
23038 int copyID;
23039{
23040 return (fc->l_varlist.lv_copyID != copyID
23041 && fc->l_vars.dv_copyID != copyID
23042 && fc->l_avars.dv_copyID != copyID);
23043}
23044
23045/*
23046 * Free "fc" and what it contains.
23047 */
23048 static void
23049free_funccal(fc, free_val)
23050 funccall_T *fc;
23051 int free_val; /* a: vars were allocated */
23052{
23053 listitem_T *li;
23054
23055 /* The a: variables typevals may not have been allocated, only free the
23056 * allocated variables. */
23057 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23058
23059 /* free all l: variables */
23060 vars_clear(&fc->l_vars.dv_hashtab);
23061
23062 /* Free the a:000 variables if they were allocated. */
23063 if (free_val)
23064 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23065 clear_tv(&li->li_tv);
23066
23067 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068}
23069
23070/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 * Add a number variable "name" to dict "dp" with value "nr".
23072 */
23073 static void
23074add_nr_var(dp, v, name, nr)
23075 dict_T *dp;
23076 dictitem_T *v;
23077 char *name;
23078 varnumber_T nr;
23079{
23080 STRCPY(v->di_key, name);
23081 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23082 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23083 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023084 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 v->di_tv.vval.v_number = nr;
23086}
23087
23088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 * ":return [expr]"
23090 */
23091 void
23092ex_return(eap)
23093 exarg_T *eap;
23094{
23095 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023096 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097 int returning = FALSE;
23098
23099 if (current_funccal == NULL)
23100 {
23101 EMSG(_("E133: :return not inside a function"));
23102 return;
23103 }
23104
23105 if (eap->skip)
23106 ++emsg_skip;
23107
23108 eap->nextcmd = NULL;
23109 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023110 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 {
23112 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023113 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023115 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 }
23117 /* It's safer to return also on error. */
23118 else if (!eap->skip)
23119 {
23120 /*
23121 * Return unless the expression evaluation has been cancelled due to an
23122 * aborting error, an interrupt, or an exception.
23123 */
23124 if (!aborting())
23125 returning = do_return(eap, FALSE, TRUE, NULL);
23126 }
23127
23128 /* When skipping or the return gets pending, advance to the next command
23129 * in this line (!returning). Otherwise, ignore the rest of the line.
23130 * Following lines will be ignored by get_func_line(). */
23131 if (returning)
23132 eap->nextcmd = NULL;
23133 else if (eap->nextcmd == NULL) /* no argument */
23134 eap->nextcmd = check_nextcmd(arg);
23135
23136 if (eap->skip)
23137 --emsg_skip;
23138}
23139
23140/*
23141 * Return from a function. Possibly makes the return pending. Also called
23142 * for a pending return at the ":endtry" or after returning from an extra
23143 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023144 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023145 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 * FALSE when the return gets pending.
23147 */
23148 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023149do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 exarg_T *eap;
23151 int reanimate;
23152 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023153 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154{
23155 int idx;
23156 struct condstack *cstack = eap->cstack;
23157
23158 if (reanimate)
23159 /* Undo the return. */
23160 current_funccal->returned = FALSE;
23161
23162 /*
23163 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23164 * not in its finally clause (which then is to be executed next) is found.
23165 * In this case, make the ":return" pending for execution at the ":endtry".
23166 * Otherwise, return normally.
23167 */
23168 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23169 if (idx >= 0)
23170 {
23171 cstack->cs_pending[idx] = CSTP_RETURN;
23172
23173 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023174 /* A pending return again gets pending. "rettv" points to an
23175 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023177 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178 else
23179 {
23180 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023181 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023183 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023185 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 {
23187 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023188 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023189 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 else
23191 EMSG(_(e_outofmem));
23192 }
23193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023194 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195
23196 if (reanimate)
23197 {
23198 /* The pending return value could be overwritten by a ":return"
23199 * without argument in a finally clause; reset the default
23200 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023201 current_funccal->rettv->v_type = VAR_NUMBER;
23202 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203 }
23204 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023205 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 }
23207 else
23208 {
23209 current_funccal->returned = TRUE;
23210
23211 /* If the return is carried out now, store the return value. For
23212 * a return immediately after reanimation, the value is already
23213 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023214 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023216 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023217 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023219 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 }
23221 }
23222
23223 return idx < 0;
23224}
23225
23226/*
23227 * Free the variable with a pending return value.
23228 */
23229 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023230discard_pending_return(rettv)
23231 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232{
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234}
23235
23236/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023237 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 * is an allocated string. Used by report_pending() for verbose messages.
23239 */
23240 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023241get_return_cmd(rettv)
23242 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023244 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023245 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023246 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023248 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023249 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023250 if (s == NULL)
23251 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023252
23253 STRCPY(IObuff, ":return ");
23254 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23255 if (STRLEN(s) + 8 >= IOSIZE)
23256 STRCPY(IObuff + IOSIZE - 4, "...");
23257 vim_free(tofree);
23258 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259}
23260
23261/*
23262 * Get next function line.
23263 * Called by do_cmdline() to get the next line.
23264 * Returns allocated string, or NULL for end of function.
23265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 char_u *
23267get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023268 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023270 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271{
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023273 ufunc_T *fp = fcp->func;
23274 char_u *retval;
23275 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276
23277 /* If breakpoints have been added/deleted need to check for it. */
23278 if (fcp->dbg_tick != debug_tick)
23279 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023280 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 sourcing_lnum);
23282 fcp->dbg_tick = debug_tick;
23283 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023284#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023285 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023286 func_line_end(cookie);
23287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288
Bram Moolenaar05159a02005-02-26 23:04:13 +000023289 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023290 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23291 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 retval = NULL;
23293 else
23294 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023295 /* Skip NULL lines (continuation lines). */
23296 while (fcp->linenr < gap->ga_len
23297 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23298 ++fcp->linenr;
23299 if (fcp->linenr >= gap->ga_len)
23300 retval = NULL;
23301 else
23302 {
23303 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23304 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023305#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023306 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023307 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023308#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 }
23311
23312 /* Did we encounter a breakpoint? */
23313 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23314 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023315 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023317 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 sourcing_lnum);
23319 fcp->dbg_tick = debug_tick;
23320 }
23321
23322 return retval;
23323}
23324
Bram Moolenaar05159a02005-02-26 23:04:13 +000023325#if defined(FEAT_PROFILE) || defined(PROTO)
23326/*
23327 * Called when starting to read a function line.
23328 * "sourcing_lnum" must be correct!
23329 * When skipping lines it may not actually be executed, but we won't find out
23330 * until later and we need to store the time now.
23331 */
23332 void
23333func_line_start(cookie)
23334 void *cookie;
23335{
23336 funccall_T *fcp = (funccall_T *)cookie;
23337 ufunc_T *fp = fcp->func;
23338
23339 if (fp->uf_profiling && sourcing_lnum >= 1
23340 && sourcing_lnum <= fp->uf_lines.ga_len)
23341 {
23342 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023343 /* Skip continuation lines. */
23344 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23345 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023346 fp->uf_tml_execed = FALSE;
23347 profile_start(&fp->uf_tml_start);
23348 profile_zero(&fp->uf_tml_children);
23349 profile_get_wait(&fp->uf_tml_wait);
23350 }
23351}
23352
23353/*
23354 * Called when actually executing a function line.
23355 */
23356 void
23357func_line_exec(cookie)
23358 void *cookie;
23359{
23360 funccall_T *fcp = (funccall_T *)cookie;
23361 ufunc_T *fp = fcp->func;
23362
23363 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23364 fp->uf_tml_execed = TRUE;
23365}
23366
23367/*
23368 * Called when done with a function line.
23369 */
23370 void
23371func_line_end(cookie)
23372 void *cookie;
23373{
23374 funccall_T *fcp = (funccall_T *)cookie;
23375 ufunc_T *fp = fcp->func;
23376
23377 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23378 {
23379 if (fp->uf_tml_execed)
23380 {
23381 ++fp->uf_tml_count[fp->uf_tml_idx];
23382 profile_end(&fp->uf_tml_start);
23383 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023384 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023385 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23386 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023387 }
23388 fp->uf_tml_idx = -1;
23389 }
23390}
23391#endif
23392
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393/*
23394 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023395 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 */
23397 int
23398func_has_ended(cookie)
23399 void *cookie;
23400{
Bram Moolenaar33570922005-01-25 22:26:29 +000023401 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402
23403 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23404 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023405 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 || fcp->returned);
23407}
23408
23409/*
23410 * return TRUE if cookie indicates a function which "abort"s on errors.
23411 */
23412 int
23413func_has_abort(cookie)
23414 void *cookie;
23415{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023416 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417}
23418
23419#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23420typedef enum
23421{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023422 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23423 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23424 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425} var_flavour_T;
23426
23427static var_flavour_T var_flavour __ARGS((char_u *varname));
23428
23429 static var_flavour_T
23430var_flavour(varname)
23431 char_u *varname;
23432{
23433 char_u *p = varname;
23434
23435 if (ASCII_ISUPPER(*p))
23436 {
23437 while (*(++p))
23438 if (ASCII_ISLOWER(*p))
23439 return VAR_FLAVOUR_SESSION;
23440 return VAR_FLAVOUR_VIMINFO;
23441 }
23442 else
23443 return VAR_FLAVOUR_DEFAULT;
23444}
23445#endif
23446
23447#if defined(FEAT_VIMINFO) || defined(PROTO)
23448/*
23449 * Restore global vars that start with a capital from the viminfo file
23450 */
23451 int
23452read_viminfo_varlist(virp, writing)
23453 vir_T *virp;
23454 int writing;
23455{
23456 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023457 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023458 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459
23460 if (!writing && (find_viminfo_parameter('!') != NULL))
23461 {
23462 tab = vim_strchr(virp->vir_line + 1, '\t');
23463 if (tab != NULL)
23464 {
23465 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023466 switch (*tab)
23467 {
23468 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023469#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023470 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023471#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023472 case 'D': type = VAR_DICT; break;
23473 case 'L': type = VAR_LIST; break;
23474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475
23476 tab = vim_strchr(tab, '\t');
23477 if (tab != NULL)
23478 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023479 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023480 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023481 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023483#ifdef FEAT_FLOAT
23484 else if (type == VAR_FLOAT)
23485 (void)string2float(tab + 1, &tv.vval.v_float);
23486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023488 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023489 if (type == VAR_DICT || type == VAR_LIST)
23490 {
23491 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23492
23493 if (etv == NULL)
23494 /* Failed to parse back the dict or list, use it as a
23495 * string. */
23496 tv.v_type = VAR_STRING;
23497 else
23498 {
23499 vim_free(tv.vval.v_string);
23500 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023501 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023502 }
23503 }
23504
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023505 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023506
23507 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023508 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023509 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23510 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 }
23512 }
23513 }
23514
23515 return viminfo_readline(virp);
23516}
23517
23518/*
23519 * Write global vars that start with a capital to the viminfo file
23520 */
23521 void
23522write_viminfo_varlist(fp)
23523 FILE *fp;
23524{
Bram Moolenaar33570922005-01-25 22:26:29 +000023525 hashitem_T *hi;
23526 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023527 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023528 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023529 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023530 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023531 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532
23533 if (find_viminfo_parameter('!') == NULL)
23534 return;
23535
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023536 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023537
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023538 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023539 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023541 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023543 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023544 this_var = HI2DI(hi);
23545 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023546 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023548 {
23549 case VAR_STRING: s = "STR"; break;
23550 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023551#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023552 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023553#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023554 case VAR_DICT: s = "DIC"; break;
23555 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023556 default: continue;
23557 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023558 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023559 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023560 if (p != NULL)
23561 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023562 vim_free(tofree);
23563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 }
23565 }
23566}
23567#endif
23568
23569#if defined(FEAT_SESSION) || defined(PROTO)
23570 int
23571store_session_globals(fd)
23572 FILE *fd;
23573{
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 hashitem_T *hi;
23575 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023576 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 char_u *p, *t;
23578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023579 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023580 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023584 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023585 this_var = HI2DI(hi);
23586 if ((this_var->di_tv.v_type == VAR_NUMBER
23587 || this_var->di_tv.v_type == VAR_STRING)
23588 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023589 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023590 /* Escape special characters with a backslash. Turn a LF and
23591 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023592 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023593 (char_u *)"\\\"\n\r");
23594 if (p == NULL) /* out of memory */
23595 break;
23596 for (t = p; *t != NUL; ++t)
23597 if (*t == '\n')
23598 *t = 'n';
23599 else if (*t == '\r')
23600 *t = 'r';
23601 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023602 this_var->di_key,
23603 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23604 : ' ',
23605 p,
23606 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23607 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023608 || put_eol(fd) == FAIL)
23609 {
23610 vim_free(p);
23611 return FAIL;
23612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 vim_free(p);
23614 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023615#ifdef FEAT_FLOAT
23616 else if (this_var->di_tv.v_type == VAR_FLOAT
23617 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23618 {
23619 float_T f = this_var->di_tv.vval.v_float;
23620 int sign = ' ';
23621
23622 if (f < 0)
23623 {
23624 f = -f;
23625 sign = '-';
23626 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023627 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023628 this_var->di_key, sign, f) < 0)
23629 || put_eol(fd) == FAIL)
23630 return FAIL;
23631 }
23632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 }
23634 }
23635 return OK;
23636}
23637#endif
23638
Bram Moolenaar661b1822005-07-28 22:36:45 +000023639/*
23640 * Display script name where an item was last set.
23641 * Should only be invoked when 'verbose' is non-zero.
23642 */
23643 void
23644last_set_msg(scriptID)
23645 scid_T scriptID;
23646{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023647 char_u *p;
23648
Bram Moolenaar661b1822005-07-28 22:36:45 +000023649 if (scriptID != 0)
23650 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023651 p = home_replace_save(NULL, get_scriptname(scriptID));
23652 if (p != NULL)
23653 {
23654 verbose_enter();
23655 MSG_PUTS(_("\n\tLast set from "));
23656 MSG_PUTS(p);
23657 vim_free(p);
23658 verbose_leave();
23659 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023660 }
23661}
23662
Bram Moolenaard812df62008-11-09 12:46:09 +000023663/*
23664 * List v:oldfiles in a nice way.
23665 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023666 void
23667ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023668 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023669{
23670 list_T *l = vimvars[VV_OLDFILES].vv_list;
23671 listitem_T *li;
23672 int nr = 0;
23673
23674 if (l == NULL)
23675 msg((char_u *)_("No old files"));
23676 else
23677 {
23678 msg_start();
23679 msg_scroll = TRUE;
23680 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23681 {
23682 msg_outnum((long)++nr);
23683 MSG_PUTS(": ");
23684 msg_outtrans(get_tv_string(&li->li_tv));
23685 msg_putchar('\n');
23686 out_flush(); /* output one line at a time */
23687 ui_breakcheck();
23688 }
23689 /* Assume "got_int" was set to truncate the listing. */
23690 got_int = FALSE;
23691
23692#ifdef FEAT_BROWSE_CMD
23693 if (cmdmod.browse)
23694 {
23695 quit_more = FALSE;
23696 nr = prompt_for_number(FALSE);
23697 msg_starthere();
23698 if (nr > 0)
23699 {
23700 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23701 (long)nr);
23702
23703 if (p != NULL)
23704 {
23705 p = expand_env_save(p);
23706 eap->arg = p;
23707 eap->cmdidx = CMD_edit;
23708 cmdmod.browse = FALSE;
23709 do_exedit(eap, NULL);
23710 vim_free(p);
23711 }
23712 }
23713 }
23714#endif
23715 }
23716}
23717
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718#endif /* FEAT_EVAL */
23719
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023721#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722
23723#ifdef WIN3264
23724/*
23725 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23726 */
23727static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23728static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23729static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23730
23731/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023732 * Get the short path (8.3) for the filename in "fnamep".
23733 * Only works for a valid file name.
23734 * When the path gets longer "fnamep" is changed and the allocated buffer
23735 * is put in "bufp".
23736 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23737 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 */
23739 static int
23740get_short_pathname(fnamep, bufp, fnamelen)
23741 char_u **fnamep;
23742 char_u **bufp;
23743 int *fnamelen;
23744{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023745 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 char_u *newbuf;
23747
23748 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 l = GetShortPathName(*fnamep, *fnamep, len);
23750 if (l > len - 1)
23751 {
23752 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023753 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 newbuf = vim_strnsave(*fnamep, l);
23755 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757
23758 vim_free(*bufp);
23759 *fnamep = *bufp = newbuf;
23760
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023761 /* Really should always succeed, as the buffer is big enough. */
23762 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 }
23764
23765 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023766 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767}
23768
23769/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023770 * Get the short path (8.3) for the filename in "fname". The converted
23771 * path is returned in "bufp".
23772 *
23773 * Some of the directories specified in "fname" may not exist. This function
23774 * will shorten the existing directories at the beginning of the path and then
23775 * append the remaining non-existing path.
23776 *
23777 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023778 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023779 * bufp - Pointer to an allocated buffer for the filename.
23780 * fnamelen - Length of the filename pointed to by fname
23781 *
23782 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 */
23784 static int
23785shortpath_for_invalid_fname(fname, bufp, fnamelen)
23786 char_u **fname;
23787 char_u **bufp;
23788 int *fnamelen;
23789{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023790 char_u *short_fname, *save_fname, *pbuf_unused;
23791 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023793 int old_len, len;
23794 int new_len, sfx_len;
23795 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796
23797 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023798 old_len = *fnamelen;
23799 save_fname = vim_strnsave(*fname, old_len);
23800 pbuf_unused = NULL;
23801 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023803 endp = save_fname + old_len - 1; /* Find the end of the copy */
23804 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023806 /*
23807 * Try shortening the supplied path till it succeeds by removing one
23808 * directory at a time from the tail of the path.
23809 */
23810 len = 0;
23811 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023813 /* go back one path-separator */
23814 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23815 --endp;
23816 if (endp <= save_fname)
23817 break; /* processed the complete path */
23818
23819 /*
23820 * Replace the path separator with a NUL and try to shorten the
23821 * resulting path.
23822 */
23823 ch = *endp;
23824 *endp = 0;
23825 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023826 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023827 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23828 {
23829 retval = FAIL;
23830 goto theend;
23831 }
23832 *endp = ch; /* preserve the string */
23833
23834 if (len > 0)
23835 break; /* successfully shortened the path */
23836
23837 /* failed to shorten the path. Skip the path separator */
23838 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 }
23840
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023841 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023843 /*
23844 * Succeeded in shortening the path. Now concatenate the shortened
23845 * path with the remaining path at the tail.
23846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023848 /* Compute the length of the new path. */
23849 sfx_len = (int)(save_endp - endp) + 1;
23850 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023852 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023854 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023856 /* There is not enough space in the currently allocated string,
23857 * copy it to a buffer big enough. */
23858 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023860 {
23861 retval = FAIL;
23862 goto theend;
23863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 }
23865 else
23866 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023867 /* Transfer short_fname to the main buffer (it's big enough),
23868 * unless get_short_pathname() did its work in-place. */
23869 *fname = *bufp = save_fname;
23870 if (short_fname != save_fname)
23871 vim_strncpy(save_fname, short_fname, len);
23872 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023874
23875 /* concat the not-shortened part of the path */
23876 vim_strncpy(*fname + len, endp, sfx_len);
23877 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023879
23880theend:
23881 vim_free(pbuf_unused);
23882 vim_free(save_fname);
23883
23884 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885}
23886
23887/*
23888 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023889 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 */
23891 static int
23892shortpath_for_partial(fnamep, bufp, fnamelen)
23893 char_u **fnamep;
23894 char_u **bufp;
23895 int *fnamelen;
23896{
23897 int sepcount, len, tflen;
23898 char_u *p;
23899 char_u *pbuf, *tfname;
23900 int hasTilde;
23901
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023902 /* Count up the path separators from the RHS.. so we know which part
23903 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023905 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 if (vim_ispathsep(*p))
23907 ++sepcount;
23908
23909 /* Need full path first (use expand_env() to remove a "~/") */
23910 hasTilde = (**fnamep == '~');
23911 if (hasTilde)
23912 pbuf = tfname = expand_env_save(*fnamep);
23913 else
23914 pbuf = tfname = FullName_save(*fnamep, FALSE);
23915
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023916 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023918 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23919 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920
23921 if (len == 0)
23922 {
23923 /* Don't have a valid filename, so shorten the rest of the
23924 * path if we can. This CAN give us invalid 8.3 filenames, but
23925 * there's not a lot of point in guessing what it might be.
23926 */
23927 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023928 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 }
23931
23932 /* Count the paths backward to find the beginning of the desired string. */
23933 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023934 {
23935#ifdef FEAT_MBYTE
23936 if (has_mbyte)
23937 p -= mb_head_off(tfname, p);
23938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 if (vim_ispathsep(*p))
23940 {
23941 if (sepcount == 0 || (hasTilde && sepcount == 1))
23942 break;
23943 else
23944 sepcount --;
23945 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 if (hasTilde)
23948 {
23949 --p;
23950 if (p >= tfname)
23951 *p = '~';
23952 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023953 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 }
23955 else
23956 ++p;
23957
23958 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23959 vim_free(*bufp);
23960 *fnamelen = (int)STRLEN(p);
23961 *bufp = pbuf;
23962 *fnamep = p;
23963
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023964 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965}
23966#endif /* WIN3264 */
23967
23968/*
23969 * Adjust a filename, according to a string of modifiers.
23970 * *fnamep must be NUL terminated when called. When returning, the length is
23971 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023972 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 * When there is an error, *fnamep is set to NULL.
23974 */
23975 int
23976modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23977 char_u *src; /* string with modifiers */
23978 int *usedlen; /* characters after src that are used */
23979 char_u **fnamep; /* file name so far */
23980 char_u **bufp; /* buffer for allocated file name or NULL */
23981 int *fnamelen; /* length of fnamep */
23982{
23983 int valid = 0;
23984 char_u *tail;
23985 char_u *s, *p, *pbuf;
23986 char_u dirname[MAXPATHL];
23987 int c;
23988 int has_fullname = 0;
23989#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023990 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 int has_shortname = 0;
23992#endif
23993
23994repeat:
23995 /* ":p" - full path/file_name */
23996 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23997 {
23998 has_fullname = 1;
23999
24000 valid |= VALID_PATH;
24001 *usedlen += 2;
24002
24003 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24004 if ((*fnamep)[0] == '~'
24005#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24006 && ((*fnamep)[1] == '/'
24007# ifdef BACKSLASH_IN_FILENAME
24008 || (*fnamep)[1] == '\\'
24009# endif
24010 || (*fnamep)[1] == NUL)
24011
24012#endif
24013 )
24014 {
24015 *fnamep = expand_env_save(*fnamep);
24016 vim_free(*bufp); /* free any allocated file name */
24017 *bufp = *fnamep;
24018 if (*fnamep == NULL)
24019 return -1;
24020 }
24021
24022 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024023 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 {
24025 if (vim_ispathsep(*p)
24026 && p[1] == '.'
24027 && (p[2] == NUL
24028 || vim_ispathsep(p[2])
24029 || (p[2] == '.'
24030 && (p[3] == NUL || vim_ispathsep(p[3])))))
24031 break;
24032 }
24033
24034 /* FullName_save() is slow, don't use it when not needed. */
24035 if (*p != NUL || !vim_isAbsName(*fnamep))
24036 {
24037 *fnamep = FullName_save(*fnamep, *p != NUL);
24038 vim_free(*bufp); /* free any allocated file name */
24039 *bufp = *fnamep;
24040 if (*fnamep == NULL)
24041 return -1;
24042 }
24043
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024044#ifdef WIN3264
24045# if _WIN32_WINNT >= 0x0500
24046 if (vim_strchr(*fnamep, '~') != NULL)
24047 {
24048 /* Expand 8.3 filename to full path. Needed to make sure the same
24049 * file does not have two different names.
24050 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24051 p = alloc(_MAX_PATH + 1);
24052 if (p != NULL)
24053 {
24054 if (GetLongPathName(*fnamep, p, MAXPATHL))
24055 {
24056 vim_free(*bufp);
24057 *bufp = *fnamep = p;
24058 }
24059 else
24060 vim_free(p);
24061 }
24062 }
24063# endif
24064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 /* Append a path separator to a directory. */
24066 if (mch_isdir(*fnamep))
24067 {
24068 /* Make room for one or two extra characters. */
24069 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24070 vim_free(*bufp); /* free any allocated file name */
24071 *bufp = *fnamep;
24072 if (*fnamep == NULL)
24073 return -1;
24074 add_pathsep(*fnamep);
24075 }
24076 }
24077
24078 /* ":." - path relative to the current directory */
24079 /* ":~" - path relative to the home directory */
24080 /* ":8" - shortname path - postponed till after */
24081 while (src[*usedlen] == ':'
24082 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24083 {
24084 *usedlen += 2;
24085 if (c == '8')
24086 {
24087#ifdef WIN3264
24088 has_shortname = 1; /* Postpone this. */
24089#endif
24090 continue;
24091 }
24092 pbuf = NULL;
24093 /* Need full path first (use expand_env() to remove a "~/") */
24094 if (!has_fullname)
24095 {
24096 if (c == '.' && **fnamep == '~')
24097 p = pbuf = expand_env_save(*fnamep);
24098 else
24099 p = pbuf = FullName_save(*fnamep, FALSE);
24100 }
24101 else
24102 p = *fnamep;
24103
24104 has_fullname = 0;
24105
24106 if (p != NULL)
24107 {
24108 if (c == '.')
24109 {
24110 mch_dirname(dirname, MAXPATHL);
24111 s = shorten_fname(p, dirname);
24112 if (s != NULL)
24113 {
24114 *fnamep = s;
24115 if (pbuf != NULL)
24116 {
24117 vim_free(*bufp); /* free any allocated file name */
24118 *bufp = pbuf;
24119 pbuf = NULL;
24120 }
24121 }
24122 }
24123 else
24124 {
24125 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24126 /* Only replace it when it starts with '~' */
24127 if (*dirname == '~')
24128 {
24129 s = vim_strsave(dirname);
24130 if (s != NULL)
24131 {
24132 *fnamep = s;
24133 vim_free(*bufp);
24134 *bufp = s;
24135 }
24136 }
24137 }
24138 vim_free(pbuf);
24139 }
24140 }
24141
24142 tail = gettail(*fnamep);
24143 *fnamelen = (int)STRLEN(*fnamep);
24144
24145 /* ":h" - head, remove "/file_name", can be repeated */
24146 /* Don't remove the first "/" or "c:\" */
24147 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24148 {
24149 valid |= VALID_HEAD;
24150 *usedlen += 2;
24151 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024152 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024153 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 *fnamelen = (int)(tail - *fnamep);
24155#ifdef VMS
24156 if (*fnamelen > 0)
24157 *fnamelen += 1; /* the path separator is part of the path */
24158#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024159 if (*fnamelen == 0)
24160 {
24161 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24162 p = vim_strsave((char_u *)".");
24163 if (p == NULL)
24164 return -1;
24165 vim_free(*bufp);
24166 *bufp = *fnamep = tail = p;
24167 *fnamelen = 1;
24168 }
24169 else
24170 {
24171 while (tail > s && !after_pathsep(s, tail))
24172 mb_ptr_back(*fnamep, tail);
24173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024174 }
24175
24176 /* ":8" - shortname */
24177 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24178 {
24179 *usedlen += 2;
24180#ifdef WIN3264
24181 has_shortname = 1;
24182#endif
24183 }
24184
24185#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024186 /*
24187 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 */
24189 if (has_shortname)
24190 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024191 /* Copy the string if it is shortened by :h and when it wasn't copied
24192 * yet, because we are going to change it in place. Avoids changing
24193 * the buffer name for "%:8". */
24194 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 {
24196 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024197 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 return -1;
24199 vim_free(*bufp);
24200 *bufp = *fnamep = p;
24201 }
24202
24203 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024204 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 if (!has_fullname && !vim_isAbsName(*fnamep))
24206 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024207 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208 return -1;
24209 }
24210 else
24211 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024212 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213
Bram Moolenaardc935552011-08-17 15:23:23 +020024214 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024216 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 return -1;
24218
24219 if (l == 0)
24220 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024221 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024223 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 return -1;
24225 }
24226 *fnamelen = l;
24227 }
24228 }
24229#endif /* WIN3264 */
24230
24231 /* ":t" - tail, just the basename */
24232 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24233 {
24234 *usedlen += 2;
24235 *fnamelen -= (int)(tail - *fnamep);
24236 *fnamep = tail;
24237 }
24238
24239 /* ":e" - extension, can be repeated */
24240 /* ":r" - root, without extension, can be repeated */
24241 while (src[*usedlen] == ':'
24242 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24243 {
24244 /* find a '.' in the tail:
24245 * - for second :e: before the current fname
24246 * - otherwise: The last '.'
24247 */
24248 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24249 s = *fnamep - 2;
24250 else
24251 s = *fnamep + *fnamelen - 1;
24252 for ( ; s > tail; --s)
24253 if (s[0] == '.')
24254 break;
24255 if (src[*usedlen + 1] == 'e') /* :e */
24256 {
24257 if (s > tail)
24258 {
24259 *fnamelen += (int)(*fnamep - (s + 1));
24260 *fnamep = s + 1;
24261#ifdef VMS
24262 /* cut version from the extension */
24263 s = *fnamep + *fnamelen - 1;
24264 for ( ; s > *fnamep; --s)
24265 if (s[0] == ';')
24266 break;
24267 if (s > *fnamep)
24268 *fnamelen = s - *fnamep;
24269#endif
24270 }
24271 else if (*fnamep <= tail)
24272 *fnamelen = 0;
24273 }
24274 else /* :r */
24275 {
24276 if (s > tail) /* remove one extension */
24277 *fnamelen = (int)(s - *fnamep);
24278 }
24279 *usedlen += 2;
24280 }
24281
24282 /* ":s?pat?foo?" - substitute */
24283 /* ":gs?pat?foo?" - global substitute */
24284 if (src[*usedlen] == ':'
24285 && (src[*usedlen + 1] == 's'
24286 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24287 {
24288 char_u *str;
24289 char_u *pat;
24290 char_u *sub;
24291 int sep;
24292 char_u *flags;
24293 int didit = FALSE;
24294
24295 flags = (char_u *)"";
24296 s = src + *usedlen + 2;
24297 if (src[*usedlen + 1] == 'g')
24298 {
24299 flags = (char_u *)"g";
24300 ++s;
24301 }
24302
24303 sep = *s++;
24304 if (sep)
24305 {
24306 /* find end of pattern */
24307 p = vim_strchr(s, sep);
24308 if (p != NULL)
24309 {
24310 pat = vim_strnsave(s, (int)(p - s));
24311 if (pat != NULL)
24312 {
24313 s = p + 1;
24314 /* find end of substitution */
24315 p = vim_strchr(s, sep);
24316 if (p != NULL)
24317 {
24318 sub = vim_strnsave(s, (int)(p - s));
24319 str = vim_strnsave(*fnamep, *fnamelen);
24320 if (sub != NULL && str != NULL)
24321 {
24322 *usedlen = (int)(p + 1 - src);
24323 s = do_string_sub(str, pat, sub, flags);
24324 if (s != NULL)
24325 {
24326 *fnamep = s;
24327 *fnamelen = (int)STRLEN(s);
24328 vim_free(*bufp);
24329 *bufp = s;
24330 didit = TRUE;
24331 }
24332 }
24333 vim_free(sub);
24334 vim_free(str);
24335 }
24336 vim_free(pat);
24337 }
24338 }
24339 /* after using ":s", repeat all the modifiers */
24340 if (didit)
24341 goto repeat;
24342 }
24343 }
24344
24345 return valid;
24346}
24347
24348/*
24349 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24350 * "flags" can be "g" to do a global substitute.
24351 * Returns an allocated string, NULL for error.
24352 */
24353 char_u *
24354do_string_sub(str, pat, sub, flags)
24355 char_u *str;
24356 char_u *pat;
24357 char_u *sub;
24358 char_u *flags;
24359{
24360 int sublen;
24361 regmatch_T regmatch;
24362 int i;
24363 int do_all;
24364 char_u *tail;
24365 garray_T ga;
24366 char_u *ret;
24367 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024368 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369
24370 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24371 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024372 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373
24374 ga_init2(&ga, 1, 200);
24375
24376 do_all = (flags[0] == 'g');
24377
24378 regmatch.rm_ic = p_ic;
24379 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24380 if (regmatch.regprog != NULL)
24381 {
24382 tail = str;
24383 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24384 {
24385 /*
24386 * Get some space for a temporary buffer to do the substitution
24387 * into. It will contain:
24388 * - The text up to where the match is.
24389 * - The substituted text.
24390 * - The text after the match.
24391 */
24392 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24393 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24394 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24395 {
24396 ga_clear(&ga);
24397 break;
24398 }
24399
24400 /* copy the text up to where the match is */
24401 i = (int)(regmatch.startp[0] - tail);
24402 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24403 /* add the substituted text */
24404 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24405 + ga.ga_len + i, TRUE, TRUE, FALSE);
24406 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024407 zero_width = (tail == regmatch.endp[0]
24408 || regmatch.startp[0] == regmatch.endp[0]);
24409 tail = regmatch.endp[0];
24410 if (*tail == NUL)
24411 break;
24412 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024414 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24416 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 if (!do_all)
24419 break;
24420 }
24421
24422 if (ga.ga_data != NULL)
24423 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24424
Bram Moolenaar473de612013-06-08 18:19:48 +020024425 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 }
24427
24428 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24429 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024430 if (p_cpo == empty_option)
24431 p_cpo = save_cpo;
24432 else
24433 /* Darn, evaluating {sub} expression changed the value. */
24434 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435
24436 return ret;
24437}
24438
24439#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */