blob: 085eff88cec6f9a8e8d2d3ea604c76dcd1fb444a [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 Moolenaar8c8de832008-06-24 22:58:06 +0000115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200125/* When using exists() don't auto-load a script. */
126static int no_autoload = FALSE;
127
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128/*
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 */
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
158 * Structure to hold info for a user function.
159 */
160typedef struct ufunc ufunc_T;
161
162struct ufunc
163{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000164 int uf_varargs; /* variable nr of arguments */
165 int uf_flags;
166 int uf_calls; /* nr of active calls */
167 garray_T uf_args; /* arguments */
168 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000169#ifdef FEAT_PROFILE
170 int uf_profiling; /* TRUE when func is being profiled */
171 /* profiling the function as a whole */
172 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000173 proftime_T uf_tm_total; /* time spent in function + children */
174 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000175 proftime_T uf_tm_children; /* time spent in children this call */
176 /* profiling the function per line */
177 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T *uf_tml_total; /* time spent in a line + children */
179 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tml_start; /* start time for current line */
181 proftime_T uf_tml_children; /* time spent in children for this line */
182 proftime_T uf_tml_wait; /* start wait time for current line */
183 int uf_tml_idx; /* index of line being timed; -1 if none */
184 int uf_tml_execed; /* line being timed was executed */
185#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000186 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000188 int uf_refcount; /* for numbered function: reference count */
189 char_u uf_name[1]; /* name of function (actually longer); can
190 start with <SNR>123_ (<SNR> is K_SPECIAL
191 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192};
193
194/* function flags */
195#define FC_ABORT 1 /* abort function on error */
196#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000197#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000200 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000202static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000204/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000205static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
206
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000207/* list heads for garbage collection */
208static dict_T *first_dict = NULL; /* list of all dicts */
209static list_T *first_list = NULL; /* list of all lists */
210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000211/* From user function to hashitem and back. */
212static ufunc_T dumuf;
213#define UF2HIKEY(fp) ((fp)->uf_name)
214#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
215#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
216
217#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
218#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaar33570922005-01-25 22:26:29 +0000220#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
221#define VAR_SHORT_LEN 20 /* short variable name length */
222#define FIXVAR_CNT 12 /* number of fixed variables */
223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000225typedef struct funccall_S funccall_T;
226
227struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 ufunc_T *func; /* function being called */
230 int linenr; /* next line to be executed */
231 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000232 struct /* fixed variables for arguments */
233 {
234 dictitem_T var; /* variable (without room for name) */
235 char_u room[VAR_SHORT_LEN]; /* room for the name */
236 } fixvar[FIXVAR_CNT];
237 dict_T l_vars; /* l: local function variables */
238 dictitem_T l_vars_var; /* variable for l: scope */
239 dict_T l_avars; /* a: argument variables */
240 dictitem_T l_avars_var; /* variable for a: scope */
241 list_T l_varlist; /* list for a:000 */
242 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
243 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 linenr_T breakpoint; /* next line with breakpoint or zero */
245 int dbg_tick; /* debug_tick when breakpoint was set */
246 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000247#ifdef FEAT_PROFILE
248 proftime_T prof_child; /* time spent in a child */
249#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000250 funccall_T *caller; /* calling function or NULL */
251};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000254 * Info used by a ":for" loop.
255 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000256typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257{
258 int fi_semicolon; /* TRUE if ending in '; var]' */
259 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000260 listwatch_T fi_lw; /* keep an eye on the item used. */
261 list_T *fi_list; /* list being used */
262} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000263
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000265 * Struct used by trans_function_name()
266 */
267typedef struct
268{
Bram Moolenaar33570922005-01-25 22:26:29 +0000269 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000270 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000271 dictitem_T *fd_di; /* Dictionary item used */
272} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273
Bram Moolenaara7043832005-01-21 11:56:39 +0000274
275/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 * Array to hold the value of v: variables.
277 * The value is in a dictitem, so that it can also be used in the v: scope.
278 * The reason to use this table anyway is for very quick access to the
279 * variables with the VV_ defines.
280 */
281#include "version.h"
282
283/* values for vv_flags: */
284#define VV_COMPAT 1 /* compatible, also used without "v:" */
285#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000286#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000288#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000289
290static struct vimvar
291{
292 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000293 dictitem_T vv_di; /* value and name for key */
294 char vv_filler[16]; /* space for LONGEST name below!!! */
295 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
296} vimvars[VV_LEN] =
297{
298 /*
299 * The order here must match the VV_ defines in vim.h!
300 * Initializing a union does not work, leave tv.vval empty to get zero's.
301 */
302 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("count1", VAR_NUMBER), VV_RO},
304 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
305 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
306 {VV_NAME("warningmsg", VAR_STRING), 0},
307 {VV_NAME("statusmsg", VAR_STRING), 0},
308 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
310 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
312 {VV_NAME("termresponse", VAR_STRING), VV_RO},
313 {VV_NAME("fname", VAR_STRING), VV_RO},
314 {VV_NAME("lang", VAR_STRING), VV_RO},
315 {VV_NAME("lc_time", VAR_STRING), VV_RO},
316 {VV_NAME("ctype", VAR_STRING), VV_RO},
317 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
318 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
319 {VV_NAME("fname_in", VAR_STRING), VV_RO},
320 {VV_NAME("fname_out", VAR_STRING), VV_RO},
321 {VV_NAME("fname_new", VAR_STRING), VV_RO},
322 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
323 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
324 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
325 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
327 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("progname", VAR_STRING), VV_RO},
329 {VV_NAME("servername", VAR_STRING), VV_RO},
330 {VV_NAME("dying", VAR_NUMBER), VV_RO},
331 {VV_NAME("exception", VAR_STRING), VV_RO},
332 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
333 {VV_NAME("register", VAR_STRING), VV_RO},
334 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
335 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000336 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
337 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000338 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000339 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
340 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000341 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
342 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
343 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
344 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000346 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000347 {VV_NAME("swapname", VAR_STRING), VV_RO},
348 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000349 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200350 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000351 {VV_NAME("mouse_win", VAR_NUMBER), 0},
352 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
353 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000354 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000355 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000356 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200357 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000358};
359
360/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361#define vv_type vv_di.di_tv.v_type
362#define vv_nr vv_di.di_tv.vval.v_number
363#define vv_float vv_di.di_tv.vval.v_float
364#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000365#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000367
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200368static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000369#define vimvarht vimvardict.dv_hashtab
370
Bram Moolenaara40058a2005-07-11 22:42:07 +0000371static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
372static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000373static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
374static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
375static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000376static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
377static void list_glob_vars __ARGS((int *first));
378static void list_buf_vars __ARGS((int *first));
379static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000380#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000381static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000382#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000383static void list_vim_vars __ARGS((int *first));
384static void list_script_vars __ARGS((int *first));
385static void list_func_vars __ARGS((int *first));
386static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000387static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
388static int check_changedtick __ARGS((char_u *arg));
389static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
390static void clear_lval __ARGS((lval_T *lp));
391static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
392static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
393static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
394static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
395static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
396static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
397static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
398static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
399static void item_lock __ARGS((typval_T *tv, int deep, int lock));
400static int tv_islocked __ARGS((typval_T *tv));
401
Bram Moolenaar33570922005-01-25 22:26:29 +0000402static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
403static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000408static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
409static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000411static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000412static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000416static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100419static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
420static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
421static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100429static 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 +0000430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000431static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200432static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000433static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
435static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000436static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
441static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000443#ifdef FEAT_FLOAT
444static int string2float __ARGS((char_u *text, float_T *value));
445#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000446static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
447static int find_internal_func __ARGS((char_u *name));
448static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
449static 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 +0200450static 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 +0000451static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000452static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000453
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000454#ifdef FEAT_FLOAT
455static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200456static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000457#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100459static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200465static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
481static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
482#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000483static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000486static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000487static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000488#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000489static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000490static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
492#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
496static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200497static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000499static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
502static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200513#ifdef FEAT_FLOAT
514static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000518static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000519static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000524#ifdef FEAT_FLOAT
525static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200527static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000529static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000530static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000538static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000540static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000541static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000546static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000554static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000555static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000556static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000557static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200560static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000561static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000569static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000583static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100588static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000590static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000602#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200603static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000604static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
605#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200606#ifdef FEAT_LUA
607static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
608#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000609static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000613static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000614static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000615static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000617static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000621#ifdef vim_mkdir
622static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100625#ifdef FEAT_MZSCHEME
626static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100630static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000631static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000632#ifdef FEAT_FLOAT
633static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
634#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000636static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000637static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200638#ifdef FEAT_PYTHON3
639static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
641#ifdef FEAT_PYTHON
642static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000645static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000646static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
659static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
660#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100661static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000664static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000665static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000666static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000673static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000674static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000675static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000676static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000677static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200678static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000679static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100681#ifdef FEAT_CRYPT
682static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
683#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000684static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200685static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
688static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200689static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000692static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000693static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#ifdef FEAT_FLOAT
697static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
698static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
699#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000700static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200701static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702#ifdef HAVE_STRFTIME
703static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
704#endif
705static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200711static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200712static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000713static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000718static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200719static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000722static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000723static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000724static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000725static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000727static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200728#ifdef FEAT_FLOAT
729static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
731#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
736static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200739static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200740static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100744static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000751static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000753static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000754static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100755static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000757static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000758static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static int get_env_len __ARGS((char_u **arg));
760static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000761static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000762static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
763#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
764#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
765 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000766static 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 +0000767static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000769static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
770static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static typval_T *alloc_tv __ARGS((void));
772static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static void init_tv __ARGS((typval_T *varp));
774static long get_tv_number __ARGS((typval_T *varp));
775static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000776static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static char_u *get_tv_string __ARGS((typval_T *varp));
778static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000779static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200781static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
783static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
784static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000785static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
786static 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 +0000787static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
788static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000789static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200790static int var_check_func_name __ARGS((char_u *name, int new_var));
791static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000792static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000793static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
795static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
796static int eval_fname_script __ARGS((char_u *p));
797static int eval_fname_sid __ARGS((char_u *p));
798static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static ufunc_T *find_func __ARGS((char_u *name));
800static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000801static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802#ifdef FEAT_PROFILE
803static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000804static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
805static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
806static int
807# ifdef __BORLANDC__
808 _RTLENTRYF
809# endif
810 prof_total_cmp __ARGS((const void *s1, const void *s2));
811static int
812# ifdef __BORLANDC__
813 _RTLENTRYF
814# endif
815 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000817static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000818static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000819static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static 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 +0000822static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
823static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
826static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000827static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000828static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200831
832#ifdef EBCDIC
833static int compare_func_name __ARGS((const void *s1, const void *s2));
834static void sortFunctions __ARGS(());
835#endif
836
837
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838/* Character used as separated in autoload function/variable names. */
839#define AUTOLOAD_CHAR '#'
840
Bram Moolenaar33570922005-01-25 22:26:29 +0000841/*
842 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000843 */
844 void
845eval_init()
846{
Bram Moolenaar33570922005-01-25 22:26:29 +0000847 int i;
848 struct vimvar *p;
849
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200850 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
851 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200852 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000853 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000854 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000855
856 for (i = 0; i < VV_LEN; ++i)
857 {
858 p = &vimvars[i];
859 STRCPY(p->vv_di.di_key, p->vv_name);
860 if (p->vv_flags & VV_RO)
861 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
862 else if (p->vv_flags & VV_RO_SBX)
863 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
864 else
865 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000866
867 /* add to v: scope dict, unless the value is not always available */
868 if (p->vv_type != VAR_UNKNOWN)
869 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000871 /* add to compat scope dict */
872 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000874 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200875 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200876
877#ifdef EBCDIC
878 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100879 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200880 */
881 sortFunctions();
882#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000883}
884
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000885#if defined(EXITFREE) || defined(PROTO)
886 void
887eval_clear()
888{
889 int i;
890 struct vimvar *p;
891
892 for (i = 0; i < VV_LEN; ++i)
893 {
894 p = &vimvars[i];
895 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000897 vim_free(p->vv_str);
898 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000899 }
900 else if (p->vv_di.di_tv.v_type == VAR_LIST)
901 {
902 list_unref(p->vv_list);
903 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000904 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 }
906 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000907 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 hash_clear(&compat_hashtab);
909
Bram Moolenaard9fba312005-06-26 22:34:35 +0000910 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100911# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200912 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100913# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914
915 /* global variables */
916 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000918 /* autoloaded script names */
919 ga_clear_strings(&ga_loaded);
920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 /* script-local variables */
922 for (i = 1; i <= ga_scripts.ga_len; ++i)
923 {
924 vars_clear(&SCRIPT_VARS(i));
925 vim_free(SCRIPT_SV(i));
926 }
927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001359 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001360 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001361 if (tv.vval.v_list->lv_len > 0)
1362 ga_append(&ga, NL);
1363 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001364 ga_append(&ga, NUL);
1365 retval = (char_u *)ga.ga_data;
1366 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367#ifdef FEAT_FLOAT
1368 else if (convert && tv.v_type == VAR_FLOAT)
1369 {
1370 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1371 retval = vim_strsave(numbuf);
1372 }
1373#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 else
1375 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378
1379 return retval;
1380}
1381
1382/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 * Call eval_to_string() without using current local variables and using
1384 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 */
1386 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *arg;
1389 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 char_u *retval;
1393 void *save_funccalp;
1394
1395 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 ++sandbox;
1398 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 --sandbox;
1402 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 restore_funccal(save_funccalp);
1404 return retval;
1405}
1406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407/*
1408 * Top level evaluation function, returning a number.
1409 * Evaluates "expr" silently.
1410 * Returns -1 for an error.
1411 */
1412 int
1413eval_to_number(expr)
1414 char_u *expr;
1415{
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001418 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 ++emsg_off;
1421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 retval = -1;
1424 else
1425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001426 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429 --emsg_off;
1430
1431 return retval;
1432}
1433
Bram Moolenaara40058a2005-07-11 22:42:07 +00001434/*
1435 * Prepare v: variable "idx" to be used.
1436 * Save the current typeval in "save_tv".
1437 * When not used yet add the variable to the v: hashtable.
1438 */
1439 static void
1440prepare_vimvar(idx, save_tv)
1441 int idx;
1442 typval_T *save_tv;
1443{
1444 *save_tv = vimvars[idx].vv_tv;
1445 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1446 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1447}
1448
1449/*
1450 * Restore v: variable "idx" to typeval "save_tv".
1451 * When no longer defined, remove the variable from the v: hashtable.
1452 */
1453 static void
1454restore_vimvar(idx, save_tv)
1455 int idx;
1456 typval_T *save_tv;
1457{
1458 hashitem_T *hi;
1459
Bram Moolenaara40058a2005-07-11 22:42:07 +00001460 vimvars[idx].vv_tv = *save_tv;
1461 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1462 {
1463 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1464 if (HASHITEM_EMPTY(hi))
1465 EMSG2(_(e_intern2), "restore_vimvar()");
1466 else
1467 hash_remove(&vimvarht, hi);
1468 }
1469}
1470
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001471#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472/*
1473 * Evaluate an expression to a list with suggestions.
1474 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001475 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476 */
1477 list_T *
1478eval_spell_expr(badword, expr)
1479 char_u *badword;
1480 char_u *expr;
1481{
1482 typval_T save_val;
1483 typval_T rettv;
1484 list_T *list = NULL;
1485 char_u *p = skipwhite(expr);
1486
1487 /* Set "v:val" to the bad word. */
1488 prepare_vimvar(VV_VAL, &save_val);
1489 vimvars[VV_VAL].vv_type = VAR_STRING;
1490 vimvars[VV_VAL].vv_str = badword;
1491 if (p_verbose == 0)
1492 ++emsg_off;
1493
1494 if (eval1(&p, &rettv, TRUE) == OK)
1495 {
1496 if (rettv.v_type != VAR_LIST)
1497 clear_tv(&rettv);
1498 else
1499 list = rettv.vval.v_list;
1500 }
1501
1502 if (p_verbose == 0)
1503 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001504 restore_vimvar(VV_VAL, &save_val);
1505
1506 return list;
1507}
1508
1509/*
1510 * "list" is supposed to contain two items: a word and a number. Return the
1511 * word in "pp" and the number as the return value.
1512 * Return -1 if anything isn't right.
1513 * Used to get the good word and score from the eval_spell_expr() result.
1514 */
1515 int
1516get_spellword(list, pp)
1517 list_T *list;
1518 char_u **pp;
1519{
1520 listitem_T *li;
1521
1522 li = list->lv_first;
1523 if (li == NULL)
1524 return -1;
1525 *pp = get_tv_string(&li->li_tv);
1526
1527 li = li->li_next;
1528 if (li == NULL)
1529 return -1;
1530 return get_tv_number(&li->li_tv);
1531}
1532#endif
1533
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001535 * Top level evaluation function.
1536 * Returns an allocated typval_T with the result.
1537 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538 */
1539 typval_T *
1540eval_expr(arg, nextcmd)
1541 char_u *arg;
1542 char_u **nextcmd;
1543{
1544 typval_T *tv;
1545
1546 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 {
1549 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001550 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551 }
1552
1553 return tv;
1554}
1555
1556
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559 * Uses argv[argc] for the function arguments. Only Number and String
1560 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001563 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001564call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 char_u *func;
1566 int argc;
1567 char_u **argv;
1568 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001569 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaar33570922005-01-25 22:26:29 +00001572 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 long n;
1574 int len;
1575 int i;
1576 int doesrange;
1577 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001580 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 for (i = 0; i < argc; i++)
1585 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001586 /* Pass a NULL or empty argument as an empty string */
1587 if (argv[i] == NULL || *argv[i] == NUL)
1588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001589 argvars[i].v_type = VAR_STRING;
1590 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001591 continue;
1592 }
1593
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594 if (str_arg_only)
1595 len = 0;
1596 else
1597 /* Recognize a number argument, the others must be strings. */
1598 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (len != 0 && len == (int)STRLEN(argv[i]))
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_NUMBER;
1602 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 else
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610
1611 if (safe)
1612 {
1613 save_funccalp = save_funccal();
1614 ++sandbox;
1615 }
1616
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1618 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (safe)
1622 {
1623 --sandbox;
1624 restore_funccal(save_funccalp);
1625 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 vim_free(argvars);
1627
1628 if (ret == FAIL)
1629 clear_tv(rettv);
1630
1631 return ret;
1632}
1633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001634/*
1635 * Call vimL function "func" and return the result as a number.
1636 * Returns -1 when calling the function fails.
1637 * Uses argv[argc] for the function arguments.
1638 */
1639 long
1640call_func_retnr(func, argc, argv, safe)
1641 char_u *func;
1642 int argc;
1643 char_u **argv;
1644 int safe; /* use the sandbox */
1645{
1646 typval_T rettv;
1647 long retval;
1648
1649 /* All arguments are passed as strings, no conversion to number. */
1650 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1651 return -1;
1652
1653 retval = get_tv_number_chk(&rettv, NULL);
1654 clear_tv(&rettv);
1655 return retval;
1656}
1657
1658#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1659 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1660
Bram Moolenaar4f688582007-07-24 12:34:30 +00001661# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001663 * Call vimL function "func" and return the result as a string.
1664 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 * Uses argv[argc] for the function arguments.
1666 */
1667 void *
1668call_func_retstr(func, argc, argv, safe)
1669 char_u *func;
1670 int argc;
1671 char_u **argv;
1672 int safe; /* use the sandbox */
1673{
1674 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001675 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001677 /* All arguments are passed as strings, no conversion to number. */
1678 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 return NULL;
1680
1681 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 return retval;
1684}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001685# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001687/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001688 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001690 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 */
1692 void *
1693call_func_retlist(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
1700
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001701 /* All arguments are passed as strings, no conversion to number. */
1702 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703 return NULL;
1704
1705 if (rettv.v_type != VAR_LIST)
1706 {
1707 clear_tv(&rettv);
1708 return NULL;
1709 }
1710
1711 return rettv.vval.v_list;
1712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#endif
1714
1715/*
1716 * Save the current function call pointer, and set it to NULL.
1717 * Used when executing autocommands and for ":source".
1718 */
1719 void *
1720save_funccal()
1721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 current_funccal = NULL;
1725 return (void *)fc;
1726}
1727
1728 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729restore_funccal(vfc)
1730 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = (funccall_T *)vfc;
1733
1734 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
1736
Bram Moolenaar05159a02005-02-26 23:04:13 +00001737#if defined(FEAT_PROFILE) || defined(PROTO)
1738/*
1739 * Prepare profiling for entering a child or something else that is not
1740 * counted for the script/function itself.
1741 * Should always be called in pair with prof_child_exit().
1742 */
1743 void
1744prof_child_enter(tm)
1745 proftime_T *tm; /* place to store waittime */
1746{
1747 funccall_T *fc = current_funccal;
1748
1749 if (fc != NULL && fc->func->uf_profiling)
1750 profile_start(&fc->prof_child);
1751 script_prof_save(tm);
1752}
1753
1754/*
1755 * Take care of time spent in a child.
1756 * Should always be called after prof_child_enter().
1757 */
1758 void
1759prof_child_exit(tm)
1760 proftime_T *tm; /* where waittime was stored */
1761{
1762 funccall_T *fc = current_funccal;
1763
1764 if (fc != NULL && fc->func->uf_profiling)
1765 {
1766 profile_end(&fc->prof_child);
1767 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1768 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1769 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1770 }
1771 script_prof_restore(tm);
1772}
1773#endif
1774
1775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#ifdef FEAT_FOLDING
1777/*
1778 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1779 * it in "*cp". Doesn't give error messages.
1780 */
1781 int
1782eval_foldexpr(arg, cp)
1783 char_u *arg;
1784 int *cp;
1785{
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int retval;
1788 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001789 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1790 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001793 if (use_sandbox)
1794 ++sandbox;
1795 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 retval = 0;
1799 else
1800 {
1801 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 if (tv.v_type == VAR_NUMBER)
1803 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001804 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 retval = 0;
1806 else
1807 {
1808 /* If the result is a string, check if there is a non-digit before
1809 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (!VIM_ISDIGIT(*s) && *s != '-')
1812 *cp = *s++;
1813 retval = atol((char *)s);
1814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 --sandbox;
1820 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 return retval;
1823}
1824#endif
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 * ":let" list all variable values
1828 * ":let var1 var2" list variable values
1829 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 * ":let var += expr" assignment command.
1831 * ":let var -= expr" assignment command.
1832 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 void
1836ex_let(eap)
1837 exarg_T *eap;
1838{
1839 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 int var_count = 0;
1844 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001847 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaardb552d602006-03-23 22:59:57 +00001849 argend = skip_var_list(arg, &var_count, &semicolon);
1850 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001852 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1853 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001854 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001857 /*
1858 * ":let" without "=": list variables
1859 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 if (*arg == '[')
1861 EMSG(_(e_invarg));
1862 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_glob_vars(&first);
1869 list_buf_vars(&first);
1870 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001871#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001873#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 list_script_vars(&first);
1875 list_func_vars(&first);
1876 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 eap->nextcmd = check_nextcmd(arg);
1879 }
1880 else
1881 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 op[0] = '=';
1883 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001884 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001885 {
1886 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1887 op[0] = expr[-1]; /* +=, -= or .= */
1888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (eap->skip)
1895 {
1896 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 --emsg_skip;
1899 }
1900 else if (i != FAIL)
1901 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 }
1907}
1908
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909/*
1910 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1911 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 * When "nextchars" is not NULL it points to a string with characters that
1913 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1914 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 * Returns OK or FAIL;
1916 */
1917 static int
1918ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1919 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 int copy; /* copy values from "tv", don't move */
1922 int semicolon; /* from skip_var_list() */
1923 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925{
1926 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 listitem_T *item;
1930 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931
1932 if (*arg != '[')
1933 {
1934 /*
1935 * ":let var = expr" or ":for var in list"
1936 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 return FAIL;
1939 return OK;
1940 }
1941
1942 /*
1943 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1944 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001945 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 {
1947 EMSG(_(e_listreq));
1948 return FAIL;
1949 }
1950
1951 i = list_len(l);
1952 if (semicolon == 0 && var_count < i)
1953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001954 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 return FAIL;
1956 }
1957 if (var_count - semicolon > i)
1958 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001959 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 return FAIL;
1961 }
1962
1963 item = l->lv_first;
1964 while (*arg != ']')
1965 {
1966 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 item = item->li_next;
1969 if (arg == NULL)
1970 return FAIL;
1971
1972 arg = skipwhite(arg);
1973 if (*arg == ';')
1974 {
1975 /* Put the rest of the list (may be empty) in the var after ';'.
1976 * Create a new list for this. */
1977 l = list_alloc();
1978 if (l == NULL)
1979 return FAIL;
1980 while (item != NULL)
1981 {
1982 list_append_tv(l, &item->li_tv);
1983 item = item->li_next;
1984 }
1985
1986 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001987 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 ltv.vval.v_list = l;
1989 l->lv_refcount = 1;
1990
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1992 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 clear_tv(&ltv);
1994 if (arg == NULL)
1995 return FAIL;
1996 break;
1997 }
1998 else if (*arg != ',' && *arg != ']')
1999 {
2000 EMSG2(_(e_intern2), "ex_let_vars()");
2001 return FAIL;
2002 }
2003 }
2004
2005 return OK;
2006}
2007
2008/*
2009 * Skip over assignable variable "var" or list of variables "[var, var]".
2010 * Used for ":let varvar = expr" and ":for varvar in expr".
2011 * For "[var, var]" increment "*var_count" for each variable.
2012 * for "[var, var; var]" set "semicolon".
2013 * Return NULL for an error.
2014 */
2015 static char_u *
2016skip_var_list(arg, var_count, semicolon)
2017 char_u *arg;
2018 int *var_count;
2019 int *semicolon;
2020{
2021 char_u *p, *s;
2022
2023 if (*arg == '[')
2024 {
2025 /* "[var, var]": find the matching ']'. */
2026 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002027 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 {
2029 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2030 s = skip_var_one(p);
2031 if (s == p)
2032 {
2033 EMSG2(_(e_invarg2), p);
2034 return NULL;
2035 }
2036 ++*var_count;
2037
2038 p = skipwhite(s);
2039 if (*p == ']')
2040 break;
2041 else if (*p == ';')
2042 {
2043 if (*semicolon == 1)
2044 {
2045 EMSG(_("Double ; in list of variables"));
2046 return NULL;
2047 }
2048 *semicolon = 1;
2049 }
2050 else if (*p != ',')
2051 {
2052 EMSG2(_(e_invarg2), p);
2053 return NULL;
2054 }
2055 }
2056 return p + 1;
2057 }
2058 else
2059 return skip_var_one(arg);
2060}
2061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002063 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002064 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 static char_u *
2067skip_var_one(arg)
2068 char_u *arg;
2069{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002070 if (*arg == '@' && arg[1] != NUL)
2071 return arg + 2;
2072 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2073 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074}
2075
Bram Moolenaara7043832005-01-21 11:56:39 +00002076/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 * List variables for hashtab "ht" with prefix "prefix".
2078 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086{
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 hashitem_T *hi;
2088 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 int todo;
2090
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002091 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2093 {
2094 if (!HASHITEM_EMPTY(hi))
2095 {
2096 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 di = HI2DI(hi);
2098 if (empty || di->di_tv.v_type != VAR_STRING
2099 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 }
2102 }
2103}
2104
2105/*
2106 * List global variables.
2107 */
2108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_glob_vars(first)
2110 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002113}
2114
2115/*
2116 * List buffer variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_buf_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 char_u numbuf[NUMBUFLEN];
2123
Bram Moolenaar429fa852013-04-15 12:27:36 +02002124 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126
2127 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2129 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002130}
2131
2132/*
2133 * List window variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_win_vars(first)
2137 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002138{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002139 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143#ifdef FEAT_WINDOWS
2144/*
2145 * List tab page variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_tab_vars(first)
2149 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002151 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153}
2154#endif
2155
Bram Moolenaara7043832005-01-21 11:56:39 +00002156/*
2157 * List Vim variables.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_vim_vars(first)
2161 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164}
2165
2166/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167 * List script-local variables, if there is a script.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_script_vars(first)
2171 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172{
2173 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2175 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176}
2177
2178/*
2179 * List function variables, if there is a function.
2180 */
2181 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_func_vars(first)
2183 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184{
2185 if (current_funccal != NULL)
2186 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188}
2189
2190/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 * List variables in "arg".
2192 */
2193 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 exarg_T *eap;
2196 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
2199 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 char_u *name_start;
2203 char_u *arg_subsc;
2204 char_u *tofree;
2205 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206
2207 while (!ends_excmd(*arg) && !got_int)
2208 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002211 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2213 {
2214 emsg_severe = TRUE;
2215 EMSG(_(e_trailing));
2216 break;
2217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* get_name_len() takes care of expanding curly braces */
2222 name_start = name = arg;
2223 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2224 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 /* This is mainly to keep test 49 working: when expanding
2227 * curly braces fails overrule the exception error message. */
2228 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 emsg_severe = TRUE;
2231 EMSG2(_(e_invarg2), arg);
2232 break;
2233 }
2234 error = TRUE;
2235 }
2236 else
2237 {
2238 if (tofree != NULL)
2239 name = tofree;
2240 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 else
2243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* handle d.key, l[idx], f(expr) */
2245 arg_subsc = arg;
2246 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 'g': list_glob_vars(first); break;
2255 case 'b': list_buf_vars(first); break;
2256 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 case 'v': list_vim_vars(first); break;
2261 case 's': list_script_vars(first); break;
2262 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 default:
2264 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002265 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 }
2267 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 {
2269 char_u numbuf[NUMBUFLEN];
2270 char_u *tf;
2271 int c;
2272 char_u *s;
2273
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002274 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 c = *arg;
2276 *arg = NUL;
2277 list_one_var_a((char_u *)"",
2278 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002279 tv.v_type,
2280 s == NULL ? (char_u *)"" : s,
2281 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 *arg = c;
2283 vim_free(tf);
2284 }
2285 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
2288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292
2293 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
2295
2296 return arg;
2297}
2298
2299/*
2300 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2301 * Returns a pointer to the char just after the var name.
2302 * Returns NULL if there is an error.
2303 */
2304 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002307 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002309 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311{
2312 int c1;
2313 char_u *name;
2314 char_u *p;
2315 char_u *arg_end = NULL;
2316 int len;
2317 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319
2320 /*
2321 * ":let $VAR = expr": Set environment variable.
2322 */
2323 if (*arg == '$')
2324 {
2325 /* Find the end of the name. */
2326 ++arg;
2327 name = arg;
2328 len = get_env_len(&arg);
2329 if (len == 0)
2330 EMSG2(_(e_invarg2), name - 1);
2331 else
2332 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 if (op != NULL && (*op == '+' || *op == '-'))
2334 EMSG2(_(e_letwrong), op);
2335 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002336 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002338 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
2340 c1 = name[len];
2341 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 p = get_tv_string_chk(tv);
2343 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 {
2345 int mustfree = FALSE;
2346 char_u *s = vim_getenv(name, &mustfree);
2347
2348 if (s != NULL)
2349 {
2350 p = tofree = concat_str(s, p);
2351 if (mustfree)
2352 vim_free(s);
2353 }
2354 }
2355 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002358 if (STRICMP(name, "HOME") == 0)
2359 init_homedir();
2360 else if (didset_vim && STRICMP(name, "VIM") == 0)
2361 didset_vim = FALSE;
2362 else if (didset_vimruntime
2363 && STRICMP(name, "VIMRUNTIME") == 0)
2364 didset_vimruntime = FALSE;
2365 arg_end = arg;
2366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
2371 }
2372
2373 /*
2374 * ":let &option = expr": Set option value.
2375 * ":let &l:option = expr": Set local option value.
2376 * ":let &g:option = expr": Set global option value.
2377 */
2378 else if (*arg == '&')
2379 {
2380 /* Find the end of the name. */
2381 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002382 if (p == NULL || (endchars != NULL
2383 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
2385 else
2386 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 long n;
2388 int opt_type;
2389 long numval;
2390 char_u *stringval = NULL;
2391 char_u *s;
2392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 c1 = *p;
2394 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395
2396 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 s = get_tv_string_chk(tv); /* != NULL if number or string */
2398 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 {
2400 opt_type = get_option_value(arg, &numval,
2401 &stringval, opt_flags);
2402 if ((opt_type == 1 && *op == '.')
2403 || (opt_type == 0 && *op != '.'))
2404 EMSG2(_(e_letwrong), op);
2405 else
2406 {
2407 if (opt_type == 1) /* number */
2408 {
2409 if (*op == '+')
2410 n = numval + n;
2411 else
2412 n = numval - n;
2413 }
2414 else if (opt_type == 0 && stringval != NULL) /* string */
2415 {
2416 s = concat_str(stringval, s);
2417 vim_free(stringval);
2418 stringval = s;
2419 }
2420 }
2421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 if (s != NULL)
2423 {
2424 set_option_value(arg, n, s, opt_flags);
2425 arg_end = p;
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
2430 }
2431
2432 /*
2433 * ":let @r = expr": Set register contents.
2434 */
2435 else if (*arg == '@')
2436 {
2437 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 if (op != NULL && (*op == '+' || *op == '-'))
2439 EMSG2(_(e_letwrong), op);
2440 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 EMSG(_(e_letunexp));
2443 else
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 char_u *s;
2447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 p = get_tv_string_chk(tv);
2449 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002451 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 if (s != NULL)
2453 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 vim_free(s);
2456 }
2457 }
2458 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 arg_end = arg + 1;
2462 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002463 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466
2467 /*
2468 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 arg_end = p;
2484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488
2489 else
2490 EMSG2(_(e_invarg2), arg);
2491
2492 return arg_end;
2493}
2494
2495/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2497 */
2498 static int
2499check_changedtick(arg)
2500 char_u *arg;
2501{
2502 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2503 {
2504 EMSG2(_(e_readonlyvar), arg);
2505 return TRUE;
2506 }
2507 return FALSE;
2508}
2509
2510/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * Get an lval: variable, Dict item or List item that can be assigned a value
2512 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2513 * "name.key", "name.key[expr]" etc.
2514 * Indexing only works if "name" is an existing List or Dictionary.
2515 * "name" points to the start of the name.
2516 * If "rettv" is not NULL it points to the value to be assigned.
2517 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2518 * wrong; must end in space or cmd separator.
2519 *
2520 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002521 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
2524 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002525get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 typval_T *rettv;
2528 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 int unlet;
2530 int skip;
2531 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002532 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 char_u *expr_start, *expr_end;
2536 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 dictitem_T *v;
2538 typval_T var1;
2539 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548
2549 if (skip)
2550 {
2551 /* When skipping just find the end of the name. */
2552 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 }
2555
2556 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002557 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (expr_start != NULL)
2559 {
2560 /* Don't expand the name when we already know there is an error. */
2561 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2562 && *p != '[' && *p != '.')
2563 {
2564 EMSG(_(e_trailing));
2565 return NULL;
2566 }
2567
2568 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2569 if (lp->ll_exp_name == NULL)
2570 {
2571 /* Report an invalid expression in braces, unless the
2572 * expression evaluation has been cancelled due to an
2573 * aborting error, an interrupt, or an exception. */
2574 if (!aborting() && !quiet)
2575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002576 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 EMSG2(_(e_invarg2), name);
2578 return NULL;
2579 }
2580 }
2581 lp->ll_name = lp->ll_exp_name;
2582 }
2583 else
2584 lp->ll_name = name;
2585
2586 /* Without [idx] or .key we are done. */
2587 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2588 return p;
2589
2590 cc = *p;
2591 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002592 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (v == NULL && !quiet)
2594 EMSG2(_(e_undefvar), lp->ll_name);
2595 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 if (v == NULL)
2597 return NULL;
2598
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 /*
2600 * Loop until no more [idx] or .key is following.
2601 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2606 && !(lp->ll_tv->v_type == VAR_DICT
2607 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!quiet)
2610 EMSG(_("E689: Can only index a List or Dictionary"));
2611 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!quiet)
2616 EMSG(_("E708: [:] must come last"));
2617 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 len = -1;
2621 if (*p == '.')
2622 {
2623 key = p + 1;
2624 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2625 ;
2626 if (len == 0)
2627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (!quiet)
2629 EMSG(_(e_emptykey));
2630 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 }
2632 p = key + len;
2633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 else
2635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 if (*p == ':')
2639 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 else
2641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 empty1 = FALSE;
2643 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002645 if (get_tv_string_chk(&var1) == NULL)
2646 {
2647 /* not a number or string */
2648 clear_tv(&var1);
2649 return NULL;
2650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 }
2652
2653 /* Optionally get the second index [ :expr]. */
2654 if (*p == ':')
2655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (rettv != NULL && (rettv->v_type != VAR_LIST
2665 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = skipwhite(p + 1);
2674 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 else
2677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 if (!empty1)
2682 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 if (get_tv_string_chk(&var2) == NULL)
2686 {
2687 /* not a number or string */
2688 if (!empty1)
2689 clear_tv(&var1);
2690 clear_tv(&var2);
2691 return NULL;
2692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (!empty1)
2704 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709
2710 /* Skip to past ']'. */
2711 ++p;
2712 }
2713
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
2716 if (len == -1)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (*key == NUL)
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
2723 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 lp->ll_list = NULL;
2729 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002730 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002731
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002732 /* When assigning to a scope dictionary check that a function and
2733 * variable name is valid (only variable name unless it is l: or
2734 * g: dictionary). Disallow overwriting a builtin function. */
2735 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002736 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002737 int prevval;
2738 int wrong;
2739
2740 if (len != -1)
2741 {
2742 prevval = key[len];
2743 key[len] = NUL;
2744 }
2745 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2746 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002747 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002748 || !valid_varname(key);
2749 if (len != -1)
2750 key[len] = prevval;
2751 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002752 return NULL;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 /* Can't add "v:" variable. */
2758 if (lp->ll_dict == &vimvardict)
2759 {
2760 EMSG2(_(e_illvar), name);
2761 return NULL;
2762 }
2763
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002764 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (len == -1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (len == -1)
2778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 p = NULL;
2781 break;
2782 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* existing variable, need to check if it can be changed */
2784 else if (var_check_ro(lp->ll_di->di_flags, name))
2785 return NULL;
2786
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 else
2792 {
2793 /*
2794 * Get the number and item for the only or first index of the List.
2795 */
2796 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 else
2799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002800 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 clear_tv(&var1);
2802 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 lp->ll_list = lp->ll_tv->vval.v_list;
2805 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2806 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002808 if (lp->ll_n1 < 0)
2809 {
2810 lp->ll_n1 = 0;
2811 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2812 }
2813 }
2814 if (lp->ll_li == NULL)
2815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002818 if (!quiet)
2819 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 }
2822
2823 /*
2824 * May need to find the item or absolute index for the second
2825 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 * When no index given: "lp->ll_empty2" is TRUE.
2827 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002831 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 {
2838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2846 if (lp->ll_n1 < 0)
2847 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2848 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 {
2850 if (!quiet)
2851 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 }
2859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return p;
2861}
2862
2863/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 */
2866 static void
2867clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869{
2870 vim_free(lp->ll_exp_name);
2871 vim_free(lp->ll_newkey);
2872}
2873
2874/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002875 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 */
2879 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002885 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886{
2887 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 listitem_T *ri;
2889 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890
2891 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 cc = *endp;
2896 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 if (op != NULL && *op != '=')
2898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002902 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002903 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 {
2905 if (tv_op(&tv, rettv, op) == OK)
2906 set_var(lp->ll_name, &tv, FALSE);
2907 clear_tv(&tv);
2908 }
2909 }
2910 else
2911 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 else if (tv_check_lock(lp->ll_newkey == NULL
2916 ? lp->ll_tv->v_lock
2917 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2918 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 else if (lp->ll_range)
2920 {
2921 /*
2922 * Assign the List values to the list items.
2923 */
2924 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 if (op != NULL && *op != '=')
2927 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2928 else
2929 {
2930 clear_tv(&lp->ll_li->li_tv);
2931 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 ri = ri->li_next;
2934 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2935 break;
2936 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002939 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 ri = NULL;
2942 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 lp->ll_li = lp->ll_li->li_next;
2946 ++lp->ll_n1;
2947 }
2948 if (ri != NULL)
2949 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 else if (lp->ll_empty2
2951 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 : lp->ll_n1 != lp->ll_n2)
2953 EMSG(_("E711: List value has not enough items"));
2954 }
2955 else
2956 {
2957 /*
2958 * Assign to a List or Dictionary item.
2959 */
2960 if (lp->ll_newkey != NULL)
2961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 if (op != NULL && *op != '=')
2963 {
2964 EMSG2(_(e_letwrong), op);
2965 return;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002969 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 if (di == NULL)
2971 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002972 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2973 {
2974 vim_free(di);
2975 return;
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 else if (op != NULL && *op != '=')
2980 {
2981 tv_op(lp->ll_tv, rettv, op);
2982 return;
2983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002986
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /*
2988 * Assign the value to the variable or list item.
2989 */
2990 if (copy)
2991 copy_tv(rettv, lp->ll_tv);
2992 else
2993 {
2994 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002995 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 }
2998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999}
3000
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3003 * Returns OK or FAIL.
3004 */
3005 static int
3006tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003007 typval_T *tv1;
3008 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 char_u *op;
3010{
3011 long n;
3012 char_u numbuf[NUMBUFLEN];
3013 char_u *s;
3014
3015 /* Can't do anything with a Funcref or a Dict on the right. */
3016 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3017 {
3018 switch (tv1->v_type)
3019 {
3020 case VAR_DICT:
3021 case VAR_FUNC:
3022 break;
3023
3024 case VAR_LIST:
3025 if (*op != '+' || tv2->v_type != VAR_LIST)
3026 break;
3027 /* List += List */
3028 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3029 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3030 return OK;
3031
3032 case VAR_NUMBER:
3033 case VAR_STRING:
3034 if (tv2->v_type == VAR_LIST)
3035 break;
3036 if (*op == '+' || *op == '-')
3037 {
3038 /* nr += nr or nr -= nr*/
3039 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040#ifdef FEAT_FLOAT
3041 if (tv2->v_type == VAR_FLOAT)
3042 {
3043 float_T f = n;
3044
3045 if (*op == '+')
3046 f += tv2->vval.v_float;
3047 else
3048 f -= tv2->vval.v_float;
3049 clear_tv(tv1);
3050 tv1->v_type = VAR_FLOAT;
3051 tv1->vval.v_float = f;
3052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054#endif
3055 {
3056 if (*op == '+')
3057 n += get_tv_number(tv2);
3058 else
3059 n -= get_tv_number(tv2);
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_NUMBER;
3062 tv1->vval.v_number = n;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 }
3065 else
3066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067 if (tv2->v_type == VAR_FLOAT)
3068 break;
3069
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 /* str .= str */
3071 s = get_tv_string(tv1);
3072 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3073 clear_tv(tv1);
3074 tv1->v_type = VAR_STRING;
3075 tv1->vval.v_string = s;
3076 }
3077 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078
3079#ifdef FEAT_FLOAT
3080 case VAR_FLOAT:
3081 {
3082 float_T f;
3083
3084 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3085 && tv2->v_type != VAR_NUMBER
3086 && tv2->v_type != VAR_STRING))
3087 break;
3088 if (tv2->v_type == VAR_FLOAT)
3089 f = tv2->vval.v_float;
3090 else
3091 f = get_tv_number(tv2);
3092 if (*op == '+')
3093 tv1->vval.v_float += f;
3094 else
3095 tv1->vval.v_float -= f;
3096 }
3097 return OK;
3098#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 }
3100 }
3101
3102 EMSG2(_(e_letwrong), op);
3103 return FAIL;
3104}
3105
3106/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 * Add a watcher to a list.
3108 */
3109 static void
3110list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 list_T *l;
3112 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113{
3114 lw->lw_next = l->lv_watch;
3115 l->lv_watch = lw;
3116}
3117
3118/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003119 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 * No warning when it isn't found...
3121 */
3122 static void
3123list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003124 list_T *l;
3125 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126{
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128
3129 lwp = &l->lv_watch;
3130 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3131 {
3132 if (lw == lwrem)
3133 {
3134 *lwp = lw->lw_next;
3135 break;
3136 }
3137 lwp = &lw->lw_next;
3138 }
3139}
3140
3141/*
3142 * Just before removing an item from a list: advance watchers to the next
3143 * item.
3144 */
3145 static void
3146list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 list_T *l;
3148 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151
3152 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3153 if (lw->lw_item == item)
3154 lw->lw_item = item->li_next;
3155}
3156
3157/*
3158 * Evaluate the expression used in a ":for var in expr" command.
3159 * "arg" points to "var".
3160 * Set "*errp" to TRUE for an error, FALSE otherwise;
3161 * Return a pointer that holds the info. Null when there is an error.
3162 */
3163 void *
3164eval_for_line(arg, errp, nextcmdp, skip)
3165 char_u *arg;
3166 int *errp;
3167 char_u **nextcmdp;
3168 int skip;
3169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 typval_T tv;
3173 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174
3175 *errp = TRUE; /* default: there is an error */
3176
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 if (fi == NULL)
3179 return NULL;
3180
3181 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3182 if (expr == NULL)
3183 return fi;
3184
3185 expr = skipwhite(expr);
3186 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3187 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003188 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 return fi;
3190 }
3191
3192 if (skip)
3193 ++emsg_skip;
3194 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3195 {
3196 *errp = FALSE;
3197 if (!skip)
3198 {
3199 l = tv.vval.v_list;
3200 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 clear_tv(&tv);
3204 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 else
3206 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003207 /* No need to increment the refcount, it's already set for the
3208 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 fi->fi_list = l;
3210 list_add_watch(l, &fi->fi_lw);
3211 fi->fi_lw.lw_item = l->lv_first;
3212 }
3213 }
3214 }
3215 if (skip)
3216 --emsg_skip;
3217
3218 return fi;
3219}
3220
3221/*
3222 * Use the first item in a ":for" list. Advance to the next.
3223 * Assign the values to the variable (list). "arg" points to the first one.
3224 * Return TRUE when a valid item was found, FALSE when at end of list or
3225 * something wrong.
3226 */
3227 int
3228next_for_item(fi_void, arg)
3229 void *fi_void;
3230 char_u *arg;
3231{
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 item = fi->fi_lw.lw_item;
3237 if (item == NULL)
3238 result = FALSE;
3239 else
3240 {
3241 fi->fi_lw.lw_item = item->li_next;
3242 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3243 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3244 }
3245 return result;
3246}
3247
3248/*
3249 * Free the structure used to store info used by ":for".
3250 */
3251 void
3252free_for_info(fi_void)
3253 void *fi_void;
3254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003257 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 list_unref(fi->fi_list);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 vim_free(fi);
3263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3266
3267 void
3268set_context_for_expression(xp, arg, cmdidx)
3269 expand_T *xp;
3270 char_u *arg;
3271 cmdidx_T cmdidx;
3272{
3273 int got_eq = FALSE;
3274 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 if (cmdidx == CMD_let)
3278 {
3279 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003280 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 {
3282 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003283 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 {
3285 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (vim_iswhite(*p))
3288 break;
3289 }
3290 return;
3291 }
3292 }
3293 else
3294 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3295 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 while ((xp->xp_pattern = vim_strpbrk(arg,
3297 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3298 {
3299 c = *xp->xp_pattern;
3300 if (c == '&')
3301 {
3302 c = xp->xp_pattern[1];
3303 if (c == '&')
3304 {
3305 ++xp->xp_pattern;
3306 xp->xp_context = cmdidx != CMD_let || got_eq
3307 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3308 }
3309 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003312 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3313 xp->xp_pattern += 2;
3314
3315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 else if (c == '$')
3318 {
3319 /* environment variable */
3320 xp->xp_context = EXPAND_ENV_VARS;
3321 }
3322 else if (c == '=')
3323 {
3324 got_eq = TRUE;
3325 xp->xp_context = EXPAND_EXPRESSION;
3326 }
3327 else if (c == '<'
3328 && xp->xp_context == EXPAND_FUNCTIONS
3329 && vim_strchr(xp->xp_pattern, '(') == NULL)
3330 {
3331 /* Function name can start with "<SNR>" */
3332 break;
3333 }
3334 else if (cmdidx != CMD_let || got_eq)
3335 {
3336 if (c == '"') /* string */
3337 {
3338 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3339 if (c == '\\' && xp->xp_pattern[1] != NUL)
3340 ++xp->xp_pattern;
3341 xp->xp_context = EXPAND_NOTHING;
3342 }
3343 else if (c == '\'') /* literal string */
3344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3347 /* skip */ ;
3348 xp->xp_context = EXPAND_NOTHING;
3349 }
3350 else if (c == '|')
3351 {
3352 if (xp->xp_pattern[1] == '|')
3353 {
3354 ++xp->xp_pattern;
3355 xp->xp_context = EXPAND_EXPRESSION;
3356 }
3357 else
3358 xp->xp_context = EXPAND_COMMANDS;
3359 }
3360 else
3361 xp->xp_context = EXPAND_EXPRESSION;
3362 }
3363 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364 /* Doesn't look like something valid, expand as an expression
3365 * anyway. */
3366 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 arg = xp->xp_pattern;
3368 if (*arg != NUL)
3369 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3370 /* skip */ ;
3371 }
3372 xp->xp_pattern = arg;
3373}
3374
3375#endif /* FEAT_CMDL_COMPL */
3376
3377/*
3378 * ":1,25call func(arg1, arg2)" function call.
3379 */
3380 void
3381ex_call(eap)
3382 exarg_T *eap;
3383{
3384 char_u *arg = eap->arg;
3385 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003387 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 linenr_T lnum;
3391 int doesrange;
3392 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003395 if (eap->skip)
3396 {
3397 /* trans_function_name() doesn't work well when skipping, use eval0()
3398 * instead to skip to any following command, e.g. for:
3399 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003400 ++emsg_skip;
3401 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3402 clear_tv(&rettv);
3403 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003404 return;
3405 }
3406
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003407 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003408 if (fudi.fd_newkey != NULL)
3409 {
3410 /* Still need to give an error message for missing key. */
3411 EMSG2(_(e_dictkey), fudi.fd_newkey);
3412 vim_free(fudi.fd_newkey);
3413 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003414 if (tofree == NULL)
3415 return;
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 /* Increase refcount on dictionary, it could get deleted when evaluating
3418 * the arguments. */
3419 if (fudi.fd_dict != NULL)
3420 ++fudi.fd_dict->dv_refcount;
3421
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003422 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar532c7802005-01-27 14:44:31 +00003426 /* Skip white space to allow ":call func ()". Not good, but required for
3427 * backward compatibility. */
3428 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 if (*startarg != '(')
3432 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003433 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 goto end;
3435 }
3436
3437 /*
3438 * When skipping, evaluate the function once, to find the end of the
3439 * arguments.
3440 * When the function takes a range, this is discovered after the first
3441 * call, and the loop is broken.
3442 */
3443 if (eap->skip)
3444 {
3445 ++emsg_skip;
3446 lnum = eap->line2; /* do it once, also with an invalid range */
3447 }
3448 else
3449 lnum = eap->line1;
3450 for ( ; lnum <= eap->line2; ++lnum)
3451 {
3452 if (!eap->skip && eap->addr_count > 0)
3453 {
3454 curwin->w_cursor.lnum = lnum;
3455 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003456#ifdef FEAT_VIRTUALEDIT
3457 curwin->w_cursor.coladd = 0;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003461 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003462 eap->line1, eap->line2, &doesrange,
3463 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 failed = TRUE;
3466 break;
3467 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003468
3469 /* Handle a function returning a Funcref, Dictionary or List. */
3470 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3471 {
3472 failed = TRUE;
3473 break;
3474 }
3475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (doesrange || eap->skip)
3478 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (aborting())
3485 break;
3486 }
3487 if (eap->skip)
3488 --emsg_skip;
3489
3490 if (!failed)
3491 {
3492 /* Check for trailing illegal characters and a following command. */
3493 if (!ends_excmd(*arg))
3494 {
3495 emsg_severe = TRUE;
3496 EMSG(_(e_trailing));
3497 }
3498 else
3499 eap->nextcmd = check_nextcmd(arg);
3500 }
3501
3502end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003503 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003504 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505}
3506
3507/*
3508 * ":unlet[!] var1 ... " command.
3509 */
3510 void
3511ex_unlet(eap)
3512 exarg_T *eap;
3513{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003514 ex_unletlock(eap, eap->arg, 0);
3515}
3516
3517/*
3518 * ":lockvar" and ":unlockvar" commands
3519 */
3520 void
3521ex_lockvar(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 int deep = 2;
3526
3527 if (eap->forceit)
3528 deep = -1;
3529 else if (vim_isdigit(*arg))
3530 {
3531 deep = getdigits(&arg);
3532 arg = skipwhite(arg);
3533 }
3534
3535 ex_unletlock(eap, arg, deep);
3536}
3537
3538/*
3539 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3540 */
3541 static void
3542ex_unletlock(eap, argstart, deep)
3543 exarg_T *eap;
3544 char_u *argstart;
3545 int deep;
3546{
3547 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 do
3553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003555 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3556 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 if (lv.ll_name == NULL)
3558 error = TRUE; /* error but continue parsing */
3559 if (name_end == NULL || (!vim_iswhite(*name_end)
3560 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 if (name_end != NULL)
3563 {
3564 emsg_severe = TRUE;
3565 EMSG(_(e_trailing));
3566 }
3567 if (!(eap->skip || error))
3568 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
3570 }
3571
3572 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 {
3574 if (eap->cmdidx == CMD_unlet)
3575 {
3576 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3577 error = TRUE;
3578 }
3579 else
3580 {
3581 if (do_lock_var(&lv, name_end, deep,
3582 eap->cmdidx == CMD_lockvar) == FAIL)
3583 error = TRUE;
3584 }
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 if (!eap->skip)
3588 clear_lval(&lv);
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 arg = skipwhite(name_end);
3591 } while (!ends_excmd(*arg));
3592
3593 eap->nextcmd = check_nextcmd(arg);
3594}
3595
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 int forceit;
3601{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 int ret = OK;
3603 int cc;
3604
3605 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 cc = *name_end;
3608 *name_end = NUL;
3609
3610 /* Normal name or expanded name. */
3611 if (check_changedtick(lp->ll_name))
3612 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3618 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 else if (lp->ll_range)
3620 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622
3623 /* Delete a range of List items. */
3624 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3625 {
3626 li = lp->ll_li->li_next;
3627 listitem_remove(lp->ll_list, lp->ll_li);
3628 lp->ll_li = li;
3629 ++lp->ll_n1;
3630 }
3631 }
3632 else
3633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 /* unlet a List item. */
3636 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003639 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 }
3641
3642 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645/*
3646 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
3649 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 hashtab_T *ht;
3655 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003657 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 ht = find_var_ht(name, &varname);
3660 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 hi = hash_find(ht, varname);
3663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003664 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003665 di = HI2DI(hi);
3666 if (var_check_fixed(di->di_flags, name)
3667 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 return FAIL;
3669 delete_var(ht, hi);
3670 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 if (forceit)
3674 return OK;
3675 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677}
3678
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679/*
3680 * Lock or unlock variable indicated by "lp".
3681 * "deep" is the levels to go (-1 for unlimited);
3682 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3683 */
3684 static int
3685do_lock_var(lp, name_end, deep, lock)
3686 lval_T *lp;
3687 char_u *name_end;
3688 int deep;
3689 int lock;
3690{
3691 int ret = OK;
3692 int cc;
3693 dictitem_T *di;
3694
3695 if (deep == 0) /* nothing to do */
3696 return OK;
3697
3698 if (lp->ll_tv == NULL)
3699 {
3700 cc = *name_end;
3701 *name_end = NUL;
3702
3703 /* Normal name or expanded name. */
3704 if (check_changedtick(lp->ll_name))
3705 ret = FAIL;
3706 else
3707 {
3708 di = find_var(lp->ll_name, NULL);
3709 if (di == NULL)
3710 ret = FAIL;
3711 else
3712 {
3713 if (lock)
3714 di->di_flags |= DI_FLAGS_LOCK;
3715 else
3716 di->di_flags &= ~DI_FLAGS_LOCK;
3717 item_lock(&di->di_tv, deep, lock);
3718 }
3719 }
3720 *name_end = cc;
3721 }
3722 else if (lp->ll_range)
3723 {
3724 listitem_T *li = lp->ll_li;
3725
3726 /* (un)lock a range of List items. */
3727 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3728 {
3729 item_lock(&li->li_tv, deep, lock);
3730 li = li->li_next;
3731 ++lp->ll_n1;
3732 }
3733 }
3734 else if (lp->ll_list != NULL)
3735 /* (un)lock a List item. */
3736 item_lock(&lp->ll_li->li_tv, deep, lock);
3737 else
3738 /* un(lock) a Dictionary item. */
3739 item_lock(&lp->ll_di->di_tv, deep, lock);
3740
3741 return ret;
3742}
3743
3744/*
3745 * Lock or unlock an item. "deep" is nr of levels to go.
3746 */
3747 static void
3748item_lock(tv, deep, lock)
3749 typval_T *tv;
3750 int deep;
3751 int lock;
3752{
3753 static int recurse = 0;
3754 list_T *l;
3755 listitem_T *li;
3756 dict_T *d;
3757 hashitem_T *hi;
3758 int todo;
3759
3760 if (recurse >= DICT_MAXNEST)
3761 {
3762 EMSG(_("E743: variable nested too deep for (un)lock"));
3763 return;
3764 }
3765 if (deep == 0)
3766 return;
3767 ++recurse;
3768
3769 /* lock/unlock the item itself */
3770 if (lock)
3771 tv->v_lock |= VAR_LOCKED;
3772 else
3773 tv->v_lock &= ~VAR_LOCKED;
3774
3775 switch (tv->v_type)
3776 {
3777 case VAR_LIST:
3778 if ((l = tv->vval.v_list) != NULL)
3779 {
3780 if (lock)
3781 l->lv_lock |= VAR_LOCKED;
3782 else
3783 l->lv_lock &= ~VAR_LOCKED;
3784 if (deep < 0 || deep > 1)
3785 /* recursive: lock/unlock the items the List contains */
3786 for (li = l->lv_first; li != NULL; li = li->li_next)
3787 item_lock(&li->li_tv, deep - 1, lock);
3788 }
3789 break;
3790 case VAR_DICT:
3791 if ((d = tv->vval.v_dict) != NULL)
3792 {
3793 if (lock)
3794 d->dv_lock |= VAR_LOCKED;
3795 else
3796 d->dv_lock &= ~VAR_LOCKED;
3797 if (deep < 0 || deep > 1)
3798 {
3799 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003800 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3802 {
3803 if (!HASHITEM_EMPTY(hi))
3804 {
3805 --todo;
3806 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3807 }
3808 }
3809 }
3810 }
3811 }
3812 --recurse;
3813}
3814
Bram Moolenaara40058a2005-07-11 22:42:07 +00003815/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003816 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3817 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003818 */
3819 static int
3820tv_islocked(tv)
3821 typval_T *tv;
3822{
3823 return (tv->v_lock & VAR_LOCKED)
3824 || (tv->v_type == VAR_LIST
3825 && tv->vval.v_list != NULL
3826 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3827 || (tv->v_type == VAR_DICT
3828 && tv->vval.v_dict != NULL
3829 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3830}
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3833/*
3834 * Delete all "menutrans_" variables.
3835 */
3836 void
3837del_menutrans_vars()
3838{
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003840 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003843 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003844 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 {
3846 if (!HASHITEM_EMPTY(hi))
3847 {
3848 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3850 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 }
3852 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854}
3855#endif
3856
3857#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3858
3859/*
3860 * Local string buffer for the next two functions to store a variable name
3861 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3862 * get_user_var_name().
3863 */
3864
3865static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3866
3867static char_u *varnamebuf = NULL;
3868static int varnamebuflen = 0;
3869
3870/*
3871 * Function to concatenate a prefix and a variable name.
3872 */
3873 static char_u *
3874cat_prefix_varname(prefix, name)
3875 int prefix;
3876 char_u *name;
3877{
3878 int len;
3879
3880 len = (int)STRLEN(name) + 3;
3881 if (len > varnamebuflen)
3882 {
3883 vim_free(varnamebuf);
3884 len += 10; /* some additional space */
3885 varnamebuf = alloc(len);
3886 if (varnamebuf == NULL)
3887 {
3888 varnamebuflen = 0;
3889 return NULL;
3890 }
3891 varnamebuflen = len;
3892 }
3893 *varnamebuf = prefix;
3894 varnamebuf[1] = ':';
3895 STRCPY(varnamebuf + 2, name);
3896 return varnamebuf;
3897}
3898
3899/*
3900 * Function given to ExpandGeneric() to obtain the list of user defined
3901 * (global/buffer/window/built-in) variable names.
3902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 char_u *
3904get_user_var_name(xp, idx)
3905 expand_T *xp;
3906 int idx;
3907{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 static long_u gdone;
3909 static long_u bdone;
3910 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003911#ifdef FEAT_WINDOWS
3912 static long_u tdone;
3913#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003914 static int vidx;
3915 static hashitem_T *hi;
3916 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003919 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003920 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 tdone = 0;
3923#endif
3924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003925
3926 /* Global variables */
3927 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 else
3932 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 while (HASHITEM_EMPTY(hi))
3934 ++hi;
3935 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3936 return cat_prefix_varname('g', hi->hi_key);
3937 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003939
3940 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003941 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003946 else
3947 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 while (HASHITEM_EMPTY(hi))
3949 ++hi;
3950 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return (char_u *)"b:changedtick";
3956 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003957
3958 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003959 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003962 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003964 else
3965 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003966 while (HASHITEM_EMPTY(hi))
3967 ++hi;
3968 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003970
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003971#ifdef FEAT_WINDOWS
3972 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003973 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 if (tdone < ht->ht_used)
3975 {
3976 if (tdone++ == 0)
3977 hi = ht->ht_array;
3978 else
3979 ++hi;
3980 while (HASHITEM_EMPTY(hi))
3981 ++hi;
3982 return cat_prefix_varname('t', hi->hi_key);
3983 }
3984#endif
3985
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 /* v: variables */
3987 if (vidx < VV_LEN)
3988 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 vim_free(varnamebuf);
3991 varnamebuf = NULL;
3992 varnamebuflen = 0;
3993 return NULL;
3994}
3995
3996#endif /* FEAT_CMDL_COMPL */
3997
3998/*
3999 * types for expressions.
4000 */
4001typedef enum
4002{
4003 TYPE_UNKNOWN = 0
4004 , TYPE_EQUAL /* == */
4005 , TYPE_NEQUAL /* != */
4006 , TYPE_GREATER /* > */
4007 , TYPE_GEQUAL /* >= */
4008 , TYPE_SMALLER /* < */
4009 , TYPE_SEQUAL /* <= */
4010 , TYPE_MATCH /* =~ */
4011 , TYPE_NOMATCH /* !~ */
4012} exptype_T;
4013
4014/*
4015 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4018 */
4019
4020/*
4021 * Handle zero level expression.
4022 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004024 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 * Return OK or FAIL.
4026 */
4027 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 char_u **nextcmd;
4032 int evaluate;
4033{
4034 int ret;
4035 char_u *p;
4036
4037 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (ret == FAIL || !ends_excmd(*p))
4040 {
4041 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /*
4044 * Report the invalid expression unless the expression evaluation has
4045 * been cancelled due to an aborting error, an interrupt, or an
4046 * exception.
4047 */
4048 if (!aborting())
4049 EMSG2(_(e_invexpr2), arg);
4050 ret = FAIL;
4051 }
4052 if (nextcmd != NULL)
4053 *nextcmd = check_nextcmd(p);
4054
4055 return ret;
4056}
4057
4058/*
4059 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004060 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 *
4062 * "arg" must point to the first non-white of the expression.
4063 * "arg" is advanced to the next non-white after the recognized expression.
4064 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004065 * Note: "rettv.v_lock" is not set.
4066 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Return OK or FAIL.
4068 */
4069 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 int evaluate;
4074{
4075 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 /*
4079 * Get the first variable.
4080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return FAIL;
4083
4084 if ((*arg)[0] == '?')
4085 {
4086 result = FALSE;
4087 if (evaluate)
4088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 int error = FALSE;
4090
4091 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (error)
4095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 /*
4099 * Get the second variable.
4100 */
4101 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return FAIL;
4104
4105 /*
4106 * Check for the ":".
4107 */
4108 if ((*arg)[0] != ':')
4109 {
4110 EMSG(_("E109: Missing ':' after '?'"));
4111 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114 }
4115
4116 /*
4117 * Get the third variable.
4118 */
4119 *arg = skipwhite(*arg + 1);
4120 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4121 {
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 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129
4130 return OK;
4131}
4132
4133/*
4134 * Handle first level expression:
4135 * expr2 || expr2 || expr2 logical OR
4136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
4140 * Return OK or FAIL.
4141 */
4142 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int evaluate;
4147{
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 long result;
4150 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004151 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Repeat until there is no following "||".
4161 */
4162 first = TRUE;
4163 result = FALSE;
4164 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4165 {
4166 if (evaluate && first)
4167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (error)
4172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 first = FALSE;
4174 }
4175
4176 /*
4177 * Get the second variable.
4178 */
4179 *arg = skipwhite(*arg + 2);
4180 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4181 return FAIL;
4182
4183 /*
4184 * Compute the result.
4185 */
4186 if (evaluate && !result)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 if (evaluate)
4195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 rettv->v_type = VAR_NUMBER;
4197 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200
4201 return OK;
4202}
4203
4204/*
4205 * Handle second level expression:
4206 * expr3 && expr3 && expr3 logical AND
4207 *
4208 * "arg" must point to the first non-white of the expression.
4209 * "arg" is advanced to the next non-white after the recognized expression.
4210 *
4211 * Return OK or FAIL.
4212 */
4213 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 int evaluate;
4218{
Bram Moolenaar33570922005-01-25 22:26:29 +00004219 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 long result;
4221 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 /*
4225 * Get the first variable.
4226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229
4230 /*
4231 * Repeat until there is no following "&&".
4232 */
4233 first = TRUE;
4234 result = TRUE;
4235 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4236 {
4237 if (evaluate && first)
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (error)
4243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 first = FALSE;
4245 }
4246
4247 /*
4248 * Get the second variable.
4249 */
4250 *arg = skipwhite(*arg + 2);
4251 if (eval4(arg, &var2, evaluate && result) == FAIL)
4252 return FAIL;
4253
4254 /*
4255 * Compute the result.
4256 */
4257 if (evaluate && result)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 if (evaluate)
4266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 rettv->v_type = VAR_NUMBER;
4268 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 }
4271
4272 return OK;
4273}
4274
4275/*
4276 * Handle third level expression:
4277 * var1 == var2
4278 * var1 =~ var2
4279 * var1 != var2
4280 * var1 !~ var2
4281 * var1 > var2
4282 * var1 >= var2
4283 * var1 < var2
4284 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004285 * var1 is var2
4286 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 *
4288 * "arg" must point to the first non-white of the expression.
4289 * "arg" is advanced to the next non-white after the recognized expression.
4290 *
4291 * Return OK or FAIL.
4292 */
4293 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 int evaluate;
4298{
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 char_u *p;
4301 int i;
4302 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004303 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 int len = 2;
4305 long n1, n2;
4306 char_u *s1, *s2;
4307 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4308 regmatch_T regmatch;
4309 int ic;
4310 char_u *save_cpo;
4311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 p = *arg;
4319 switch (p[0])
4320 {
4321 case '=': if (p[1] == '=')
4322 type = TYPE_EQUAL;
4323 else if (p[1] == '~')
4324 type = TYPE_MATCH;
4325 break;
4326 case '!': if (p[1] == '=')
4327 type = TYPE_NEQUAL;
4328 else if (p[1] == '~')
4329 type = TYPE_NOMATCH;
4330 break;
4331 case '>': if (p[1] != '=')
4332 {
4333 type = TYPE_GREATER;
4334 len = 1;
4335 }
4336 else
4337 type = TYPE_GEQUAL;
4338 break;
4339 case '<': if (p[1] != '=')
4340 {
4341 type = TYPE_SMALLER;
4342 len = 1;
4343 }
4344 else
4345 type = TYPE_SEQUAL;
4346 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004347 case 'i': if (p[1] == 's')
4348 {
4349 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4350 len = 5;
4351 if (!vim_isIDc(p[len]))
4352 {
4353 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4354 type_is = TRUE;
4355 }
4356 }
4357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359
4360 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
4363 if (type != TYPE_UNKNOWN)
4364 {
4365 /* extra question mark appended: ignore case */
4366 if (p[len] == '?')
4367 {
4368 ic = TRUE;
4369 ++len;
4370 }
4371 /* extra '#' appended: match case */
4372 else if (p[len] == '#')
4373 {
4374 ic = FALSE;
4375 ++len;
4376 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004377 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 else
4379 ic = p_ic;
4380
4381 /*
4382 * Get the second variable.
4383 */
4384 *arg = skipwhite(p + len);
4385 if (eval5(arg, &var2, evaluate) == FAIL)
4386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 return FAIL;
4389 }
4390
4391 if (evaluate)
4392 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 if (type_is && rettv->v_type != var2.v_type)
4394 {
4395 /* For "is" a different type always means FALSE, for "notis"
4396 * it means TRUE. */
4397 n1 = (type == TYPE_NEQUAL);
4398 }
4399 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4400 {
4401 if (type_is)
4402 {
4403 n1 = (rettv->v_type == var2.v_type
4404 && rettv->vval.v_list == var2.vval.v_list);
4405 if (type == TYPE_NEQUAL)
4406 n1 = !n1;
4407 }
4408 else if (rettv->v_type != var2.v_type
4409 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4410 {
4411 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004412 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004414 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004415 clear_tv(rettv);
4416 clear_tv(&var2);
4417 return FAIL;
4418 }
4419 else
4420 {
4421 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004422 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4423 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 if (type == TYPE_NEQUAL)
4425 n1 = !n1;
4426 }
4427 }
4428
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004429 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4430 {
4431 if (type_is)
4432 {
4433 n1 = (rettv->v_type == var2.v_type
4434 && rettv->vval.v_dict == var2.vval.v_dict);
4435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 else if (rettv->v_type != var2.v_type
4439 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4440 {
4441 if (rettv->v_type != var2.v_type)
4442 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4443 else
4444 EMSG(_("E736: Invalid operation for Dictionary"));
4445 clear_tv(rettv);
4446 clear_tv(&var2);
4447 return FAIL;
4448 }
4449 else
4450 {
4451 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004452 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4453 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004454 if (type == TYPE_NEQUAL)
4455 n1 = !n1;
4456 }
4457 }
4458
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004459 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4460 {
4461 if (rettv->v_type != var2.v_type
4462 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4463 {
4464 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004465 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004467 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 clear_tv(rettv);
4469 clear_tv(&var2);
4470 return FAIL;
4471 }
4472 else
4473 {
4474 /* Compare two Funcrefs for being equal or unequal. */
4475 if (rettv->vval.v_string == NULL
4476 || var2.vval.v_string == NULL)
4477 n1 = FALSE;
4478 else
4479 n1 = STRCMP(rettv->vval.v_string,
4480 var2.vval.v_string) == 0;
4481 if (type == TYPE_NEQUAL)
4482 n1 = !n1;
4483 }
4484 }
4485
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004486#ifdef FEAT_FLOAT
4487 /*
4488 * If one of the two variables is a float, compare as a float.
4489 * When using "=~" or "!~", always compare as string.
4490 */
4491 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4492 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4493 {
4494 float_T f1, f2;
4495
4496 if (rettv->v_type == VAR_FLOAT)
4497 f1 = rettv->vval.v_float;
4498 else
4499 f1 = get_tv_number(rettv);
4500 if (var2.v_type == VAR_FLOAT)
4501 f2 = var2.vval.v_float;
4502 else
4503 f2 = get_tv_number(&var2);
4504 n1 = FALSE;
4505 switch (type)
4506 {
4507 case TYPE_EQUAL: n1 = (f1 == f2); break;
4508 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4509 case TYPE_GREATER: n1 = (f1 > f2); break;
4510 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4511 case TYPE_SMALLER: n1 = (f1 < f2); break;
4512 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4513 case TYPE_UNKNOWN:
4514 case TYPE_MATCH:
4515 case TYPE_NOMATCH: break; /* avoid gcc warning */
4516 }
4517 }
4518#endif
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /*
4521 * If one of the two variables is a number, compare as a number.
4522 * When using "=~" or "!~", always compare as string.
4523 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527 n1 = get_tv_number(rettv);
4528 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 switch (type)
4530 {
4531 case TYPE_EQUAL: n1 = (n1 == n2); break;
4532 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4533 case TYPE_GREATER: n1 = (n1 > n2); break;
4534 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4535 case TYPE_SMALLER: n1 = (n1 < n2); break;
4536 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4537 case TYPE_UNKNOWN:
4538 case TYPE_MATCH:
4539 case TYPE_NOMATCH: break; /* avoid gcc warning */
4540 }
4541 }
4542 else
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 s1 = get_tv_string_buf(rettv, buf1);
4545 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4547 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4548 else
4549 i = 0;
4550 n1 = FALSE;
4551 switch (type)
4552 {
4553 case TYPE_EQUAL: n1 = (i == 0); break;
4554 case TYPE_NEQUAL: n1 = (i != 0); break;
4555 case TYPE_GREATER: n1 = (i > 0); break;
4556 case TYPE_GEQUAL: n1 = (i >= 0); break;
4557 case TYPE_SMALLER: n1 = (i < 0); break;
4558 case TYPE_SEQUAL: n1 = (i <= 0); break;
4559
4560 case TYPE_MATCH:
4561 case TYPE_NOMATCH:
4562 /* avoid 'l' flag in 'cpoptions' */
4563 save_cpo = p_cpo;
4564 p_cpo = (char_u *)"";
4565 regmatch.regprog = vim_regcomp(s2,
4566 RE_MAGIC + RE_STRING);
4567 regmatch.rm_ic = ic;
4568 if (regmatch.regprog != NULL)
4569 {
4570 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4571 vim_free(regmatch.regprog);
4572 if (type == TYPE_NOMATCH)
4573 n1 = !n1;
4574 }
4575 p_cpo = save_cpo;
4576 break;
4577
4578 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4579 }
4580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 clear_tv(rettv);
4582 clear_tv(&var2);
4583 rettv->v_type = VAR_NUMBER;
4584 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 }
4587
4588 return OK;
4589}
4590
4591/*
4592 * Handle fourth level expression:
4593 * + number addition
4594 * - number subtraction
4595 * . string concatenation
4596 *
4597 * "arg" must point to the first non-white of the expression.
4598 * "arg" is advanced to the next non-white after the recognized expression.
4599 *
4600 * Return OK or FAIL.
4601 */
4602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 int evaluate;
4607{
Bram Moolenaar33570922005-01-25 22:26:29 +00004608 typval_T var2;
4609 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 int op;
4611 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004612#ifdef FEAT_FLOAT
4613 float_T f1 = 0, f2 = 0;
4614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u *s1, *s2;
4616 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4617 char_u *p;
4618
4619 /*
4620 * Get the first variable.
4621 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004622 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 return FAIL;
4624
4625 /*
4626 * Repeat computing, until no '+', '-' or '.' is following.
4627 */
4628 for (;;)
4629 {
4630 op = **arg;
4631 if (op != '+' && op != '-' && op != '.')
4632 break;
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 if ((op != '+' || rettv->v_type != VAR_LIST)
4635#ifdef FEAT_FLOAT
4636 && (op == '.' || rettv->v_type != VAR_FLOAT)
4637#endif
4638 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 {
4640 /* For "list + ...", an illegal use of the first operand as
4641 * a number cannot be determined before evaluating the 2nd
4642 * operand: if this is also a list, all is ok.
4643 * For "something . ...", "something - ..." or "non-list + ...",
4644 * we know that the first operand needs to be a string or number
4645 * without evaluating the 2nd operand. So check before to avoid
4646 * side effects after an error. */
4647 if (evaluate && get_tv_string_chk(rettv) == NULL)
4648 {
4649 clear_tv(rettv);
4650 return FAIL;
4651 }
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
4655 * Get the second variable.
4656 */
4657 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004658 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return FAIL;
4662 }
4663
4664 if (evaluate)
4665 {
4666 /*
4667 * Compute the result.
4668 */
4669 if (op == '.')
4670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4672 s2 = get_tv_string_buf_chk(&var2, buf2);
4673 if (s2 == NULL) /* type error ? */
4674 {
4675 clear_tv(rettv);
4676 clear_tv(&var2);
4677 return FAIL;
4678 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004679 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
4681 rettv->v_type = VAR_STRING;
4682 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 else if (op == '+' && rettv->v_type == VAR_LIST
4685 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004686 {
4687 /* concatenate Lists */
4688 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4689 &var3) == FAIL)
4690 {
4691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 return FAIL;
4694 }
4695 clear_tv(rettv);
4696 *rettv = var3;
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 else
4699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 int error = FALSE;
4701
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 f1 = rettv->vval.v_float;
4706 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708 else
4709#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 n1 = get_tv_number_chk(rettv, &error);
4712 if (error)
4713 {
4714 /* This can only happen for "list + non-list". For
4715 * "non-list + ..." or "something - ...", we returned
4716 * before evaluating the 2nd operand. */
4717 clear_tv(rettv);
4718 return FAIL;
4719 }
4720#ifdef FEAT_FLOAT
4721 if (var2.v_type == VAR_FLOAT)
4722 f1 = n1;
4723#endif
4724 }
4725#ifdef FEAT_FLOAT
4726 if (var2.v_type == VAR_FLOAT)
4727 {
4728 f2 = var2.vval.v_float;
4729 n2 = 0;
4730 }
4731 else
4732#endif
4733 {
4734 n2 = get_tv_number_chk(&var2, &error);
4735 if (error)
4736 {
4737 clear_tv(rettv);
4738 clear_tv(&var2);
4739 return FAIL;
4740 }
4741#ifdef FEAT_FLOAT
4742 if (rettv->v_type == VAR_FLOAT)
4743 f2 = n2;
4744#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747
4748#ifdef FEAT_FLOAT
4749 /* If there is a float on either side the result is a float. */
4750 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4751 {
4752 if (op == '+')
4753 f1 = f1 + f2;
4754 else
4755 f1 = f1 - f2;
4756 rettv->v_type = VAR_FLOAT;
4757 rettv->vval.v_float = f1;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760#endif
4761 {
4762 if (op == '+')
4763 n1 = n1 + n2;
4764 else
4765 n1 = n1 - n2;
4766 rettv->v_type = VAR_NUMBER;
4767 rettv->vval.v_number = n1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 }
4773 return OK;
4774}
4775
4776/*
4777 * Handle fifth level expression:
4778 * * number multiplication
4779 * / number division
4780 * % number modulo
4781 *
4782 * "arg" must point to the first non-white of the expression.
4783 * "arg" is advanced to the next non-white after the recognized expression.
4784 *
4785 * Return OK or FAIL.
4786 */
4787 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004788eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int op;
4796 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797#ifdef FEAT_FLOAT
4798 int use_float = FALSE;
4799 float_T f1 = 0, f2;
4800#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004801 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 /*
4804 * Get the first variable.
4805 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004806 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return FAIL;
4808
4809 /*
4810 * Repeat computing, until no '*', '/' or '%' is following.
4811 */
4812 for (;;)
4813 {
4814 op = **arg;
4815 if (op != '*' && op != '/' && op != '%')
4816 break;
4817
4818 if (evaluate)
4819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
4822 {
4823 f1 = rettv->vval.v_float;
4824 use_float = TRUE;
4825 n1 = 0;
4826 }
4827 else
4828#endif
4829 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 if (error)
4832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 else
4835 n1 = 0;
4836
4837 /*
4838 * Get the second variable.
4839 */
4840 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004841 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 return FAIL;
4843
4844 if (evaluate)
4845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#ifdef FEAT_FLOAT
4847 if (var2.v_type == VAR_FLOAT)
4848 {
4849 if (!use_float)
4850 {
4851 f1 = n1;
4852 use_float = TRUE;
4853 }
4854 f2 = var2.vval.v_float;
4855 n2 = 0;
4856 }
4857 else
4858#endif
4859 {
4860 n2 = get_tv_number_chk(&var2, &error);
4861 clear_tv(&var2);
4862 if (error)
4863 return FAIL;
4864#ifdef FEAT_FLOAT
4865 if (use_float)
4866 f2 = n2;
4867#endif
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 /*
4871 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 if (op == '*')
4878 f1 = f1 * f2;
4879 else if (op == '/')
4880 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004881# ifdef VMS
4882 /* VMS crashes on divide by zero, work around it */
4883 if (f2 == 0.0)
4884 {
4885 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004888 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004889 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004890 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891 }
4892 else
4893 f1 = f1 / f2;
4894# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 /* We rely on the floating point library to handle divide
4896 * by zero to result in "inf" and not a crash. */
4897 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004902 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 return FAIL;
4904 }
4905 rettv->v_type = VAR_FLOAT;
4906 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 if (op == '*')
4912 n1 = n1 * n2;
4913 else if (op == '/')
4914 {
4915 if (n2 == 0) /* give an error message? */
4916 {
4917 if (n1 == 0)
4918 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4919 else if (n1 < 0)
4920 n1 = -0x7fffffffL;
4921 else
4922 n1 = 0x7fffffffL;
4923 }
4924 else
4925 n1 = n1 / n2;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 {
4929 if (n2 == 0) /* give an error message? */
4930 n1 = 0;
4931 else
4932 n1 = n1 % n2;
4933 }
4934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 }
4939
4940 return OK;
4941}
4942
4943/*
4944 * Handle sixth level expression:
4945 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004946 * "string" string constant
4947 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 * &option-name option value
4949 * @r register contents
4950 * identifier variable value
4951 * function() function call
4952 * $VAR environment variable
4953 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004954 * [expr, expr] List
4955 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 *
4957 * Also handle:
4958 * ! in front logical NOT
4959 * - in front unary minus
4960 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 * trailing [] subscript in String or List
4962 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 *
4964 * "arg" must point to the first non-white of the expression.
4965 * "arg" is advanced to the next non-white after the recognized expression.
4966 *
4967 * Return OK or FAIL.
4968 */
4969 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004970eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004974 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 long n;
4977 int len;
4978 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 char_u *start_leader, *end_leader;
4980 int ret = OK;
4981 char_u *alias;
4982
4983 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 /*
4990 * Skip '!' and '-' characters. They are handled later.
4991 */
4992 start_leader = *arg;
4993 while (**arg == '!' || **arg == '-' || **arg == '+')
4994 *arg = skipwhite(*arg + 1);
4995 end_leader = *arg;
4996
4997 switch (**arg)
4998 {
4999 /*
5000 * Number constant.
5001 */
5002 case '0':
5003 case '1':
5004 case '2':
5005 case '3':
5006 case '4':
5007 case '5':
5008 case '6':
5009 case '7':
5010 case '8':
5011 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013#ifdef FEAT_FLOAT
5014 char_u *p = skipdigits(*arg + 1);
5015 int get_float = FALSE;
5016
5017 /* We accept a float when the format matches
5018 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005019 * strict to avoid backwards compatibility problems.
5020 * Don't look for a float after the "." operator, so that
5021 * ":let vers = 1.2.3" doesn't fail. */
5022 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 get_float = TRUE;
5025 p = skipdigits(p + 2);
5026 if (*p == 'e' || *p == 'E')
5027 {
5028 ++p;
5029 if (*p == '-' || *p == '+')
5030 ++p;
5031 if (!vim_isdigit(*p))
5032 get_float = FALSE;
5033 else
5034 p = skipdigits(p + 1);
5035 }
5036 if (ASCII_ISALPHA(*p) || *p == '.')
5037 get_float = FALSE;
5038 }
5039 if (get_float)
5040 {
5041 float_T f;
5042
5043 *arg += string2float(*arg, &f);
5044 if (evaluate)
5045 {
5046 rettv->v_type = VAR_FLOAT;
5047 rettv->vval.v_float = f;
5048 }
5049 }
5050 else
5051#endif
5052 {
5053 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5054 *arg += len;
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_NUMBER;
5058 rettv->vval.v_number = n;
5059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * String constant: "string".
5066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069
5070 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 break;
5075
5076 /*
5077 * List: [expr, expr]
5078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081
5082 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 * Dictionary: {key: val, key: val}
5084 */
5085 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5086 break;
5087
5088 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005089 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005091 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093
5094 /*
5095 * Environment variable: $VAR.
5096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /*
5101 * Register contents: @r.
5102 */
5103 case '@': ++*arg;
5104 if (evaluate)
5105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005107 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 if (**arg != NUL)
5110 ++*arg;
5111 break;
5112
5113 /*
5114 * nested expression: (expression).
5115 */
5116 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (**arg == ')')
5119 ++*arg;
5120 else if (ret == OK)
5121 {
5122 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ret = FAIL;
5125 }
5126 break;
5127
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131
5132 if (ret == NOTDONE)
5133 {
5134 /*
5135 * Must be a variable or function name.
5136 * Can also be a curly-braces kind of name: {expr}.
5137 */
5138 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 if (alias != NULL)
5141 s = alias;
5142
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 ret = FAIL;
5145 else
5146 {
5147 if (**arg == '(') /* recursive! */
5148 {
5149 /* If "s" is the name of a variable of type VAR_FUNC
5150 * use its contents. */
5151 s = deref_func_name(s, &len);
5152
5153 /* Invoke the function. */
5154 ret = get_func_tv(s, len, rettv, arg,
5155 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005156 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005157
5158 /* If evaluate is FALSE rettv->v_type was not set in
5159 * get_func_tv, but it's needed in handle_subscript() to parse
5160 * what follows. So set it here. */
5161 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5162 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005163 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005164 rettv->v_type = VAR_FUNC;
5165 }
5166
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 /* Stop the expression evaluation when immediately
5168 * aborting on error, or when an interrupt occurred or
5169 * an exception was thrown but not caught. */
5170 if (aborting())
5171 {
5172 if (ret == OK)
5173 clear_tv(rettv);
5174 ret = FAIL;
5175 }
5176 }
5177 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005178 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005179 else
5180 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005182 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 }
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 *arg = skipwhite(*arg);
5186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5188 * expr(expr). */
5189 if (ret == OK)
5190 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192 /*
5193 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5194 */
5195 if (ret == OK && evaluate && end_leader > start_leader)
5196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 int val = 0;
5199#ifdef FEAT_FLOAT
5200 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 if (rettv->v_type == VAR_FLOAT)
5203 f = rettv->vval.v_float;
5204 else
5205#endif
5206 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 clear_tv(rettv);
5210 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 else
5213 {
5214 while (end_leader > start_leader)
5215 {
5216 --end_leader;
5217 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 {
5219#ifdef FEAT_FLOAT
5220 if (rettv->v_type == VAR_FLOAT)
5221 f = !f;
5222 else
5223#endif
5224 val = !val;
5225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005227 {
5228#ifdef FEAT_FLOAT
5229 if (rettv->v_type == VAR_FLOAT)
5230 f = -f;
5231 else
5232#endif
5233 val = -val;
5234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005236#ifdef FEAT_FLOAT
5237 if (rettv->v_type == VAR_FLOAT)
5238 {
5239 clear_tv(rettv);
5240 rettv->vval.v_float = f;
5241 }
5242 else
5243#endif
5244 {
5245 clear_tv(rettv);
5246 rettv->v_type = VAR_NUMBER;
5247 rettv->vval.v_number = val;
5248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251
5252 return ret;
5253}
5254
5255/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005256 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5257 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5259 */
5260 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266{
5267 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 long len = -1;
5271 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005275 if (rettv->v_type == VAR_FUNC
5276#ifdef FEAT_FLOAT
5277 || rettv->v_type == VAR_FLOAT
5278#endif
5279 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005281 if (verbose)
5282 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 return FAIL;
5284 }
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 /*
5289 * dict.name
5290 */
5291 key = *arg + 1;
5292 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5293 ;
5294 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 }
5298 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 /*
5301 * something[idx]
5302 *
5303 * Get the (first) variable from inside the [].
5304 */
5305 *arg = skipwhite(*arg + 1);
5306 if (**arg == ':')
5307 empty1 = TRUE;
5308 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5309 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5311 {
5312 /* not a number or string */
5313 clear_tv(&var1);
5314 return FAIL;
5315 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005316
5317 /*
5318 * Get the second variable from inside the [:].
5319 */
5320 if (**arg == ':')
5321 {
5322 range = TRUE;
5323 *arg = skipwhite(*arg + 1);
5324 if (**arg == ']')
5325 empty2 = TRUE;
5326 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 if (!empty1)
5329 clear_tv(&var1);
5330 return FAIL;
5331 }
5332 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5333 {
5334 /* not a number or string */
5335 if (!empty1)
5336 clear_tv(&var1);
5337 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 return FAIL;
5339 }
5340 }
5341
5342 /* Check for the ']'. */
5343 if (**arg != ']')
5344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345 if (verbose)
5346 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 clear_tv(&var1);
5348 if (range)
5349 clear_tv(&var2);
5350 return FAIL;
5351 }
5352 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353 }
5354
5355 if (evaluate)
5356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 n1 = 0;
5358 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005360 n1 = get_tv_number(&var1);
5361 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 }
5363 if (range)
5364 {
5365 if (empty2)
5366 n2 = -1;
5367 else
5368 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005369 n2 = get_tv_number(&var2);
5370 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 }
5372 }
5373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 {
5376 case VAR_NUMBER:
5377 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 len = (long)STRLEN(s);
5380 if (range)
5381 {
5382 /* The resulting variable is a substring. If the indexes
5383 * are out of range the result is empty. */
5384 if (n1 < 0)
5385 {
5386 n1 = len + n1;
5387 if (n1 < 0)
5388 n1 = 0;
5389 }
5390 if (n2 < 0)
5391 n2 = len + n2;
5392 else if (n2 >= len)
5393 n2 = len;
5394 if (n1 >= len || n2 < 0 || n1 > n2)
5395 s = NULL;
5396 else
5397 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5398 }
5399 else
5400 {
5401 /* The resulting variable is a string of a single
5402 * character. If the index is too big or negative the
5403 * result is empty. */
5404 if (n1 >= len || n1 < 0)
5405 s = NULL;
5406 else
5407 s = vim_strnsave(s + n1, 1);
5408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005409 clear_tv(rettv);
5410 rettv->v_type = VAR_STRING;
5411 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 break;
5413
5414 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 if (n1 < 0)
5417 n1 = len + n1;
5418 if (!empty1 && (n1 < 0 || n1 >= len))
5419 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005420 /* For a range we allow invalid values and return an empty
5421 * list. A list index out of range is an error. */
5422 if (!range)
5423 {
5424 if (verbose)
5425 EMSGN(_(e_listidx), n1);
5426 return FAIL;
5427 }
5428 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 }
5430 if (range)
5431 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005432 list_T *l;
5433 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434
5435 if (n2 < 0)
5436 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005437 else if (n2 >= len)
5438 n2 = len - 1;
5439 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005440 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005441 l = list_alloc();
5442 if (l == NULL)
5443 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 n1 <= n2; ++n1)
5446 {
5447 if (list_append_tv(l, &item->li_tv) == FAIL)
5448 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005449 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 return FAIL;
5451 }
5452 item = item->li_next;
5453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 clear_tv(rettv);
5455 rettv->v_type = VAR_LIST;
5456 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005457 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 }
5459 else
5460 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005461 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005462 clear_tv(rettv);
5463 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 }
5465 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466
5467 case VAR_DICT:
5468 if (range)
5469 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 if (verbose)
5471 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 if (len == -1)
5473 clear_tv(&var1);
5474 return FAIL;
5475 }
5476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478
5479 if (len == -1)
5480 {
5481 key = get_tv_string(&var1);
5482 if (*key == NUL)
5483 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005484 if (verbose)
5485 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486 clear_tv(&var1);
5487 return FAIL;
5488 }
5489 }
5490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005491 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005493 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005494 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495 if (len == -1)
5496 clear_tv(&var1);
5497 if (item == NULL)
5498 return FAIL;
5499
5500 copy_tv(&item->di_tv, &var1);
5501 clear_tv(rettv);
5502 *rettv = var1;
5503 }
5504 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 }
5506 }
5507
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 return OK;
5509}
5510
5511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * Get an option value.
5513 * "arg" points to the '&' or '+' before the option name.
5514 * "arg" is advanced to character after the option name.
5515 * Return OK or FAIL.
5516 */
5517 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 int evaluate;
5522{
5523 char_u *option_end;
5524 long numval;
5525 char_u *stringval;
5526 int opt_type;
5527 int c;
5528 int working = (**arg == '+'); /* has("+option") */
5529 int ret = OK;
5530 int opt_flags;
5531
5532 /*
5533 * Isolate the option name and find its value.
5534 */
5535 option_end = find_option_end(arg, &opt_flags);
5536 if (option_end == NULL)
5537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005538 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 EMSG2(_("E112: Option name missing: %s"), *arg);
5540 return FAIL;
5541 }
5542
5543 if (!evaluate)
5544 {
5545 *arg = option_end;
5546 return OK;
5547 }
5548
5549 c = *option_end;
5550 *option_end = NUL;
5551 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 if (opt_type == -3) /* invalid name */
5555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 EMSG2(_("E113: Unknown option: %s"), *arg);
5558 ret = FAIL;
5559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 if (opt_type == -2) /* hidden string option */
5563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 rettv->v_type = VAR_STRING;
5565 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 else if (opt_type == -1) /* hidden number option */
5568 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 rettv->v_type = VAR_NUMBER;
5570 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 else if (opt_type == 1) /* number option */
5573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 rettv->v_type = VAR_NUMBER;
5575 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else /* string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 }
5583 else if (working && (opt_type == -2 || opt_type == -1))
5584 ret = FAIL;
5585
5586 *option_end = c; /* put back for error messages */
5587 *arg = option_end;
5588
5589 return ret;
5590}
5591
5592/*
5593 * Allocate a variable for a string constant.
5594 * Return OK or FAIL.
5595 */
5596 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005599 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int evaluate;
5601{
5602 char_u *p;
5603 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 int extra = 0;
5605
5606 /*
5607 * Find the end of the string, skipping backslashed characters.
5608 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005609 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 {
5611 if (*p == '\\' && p[1] != NUL)
5612 {
5613 ++p;
5614 /* A "\<x>" form occupies at least 4 characters, and produces up
5615 * to 6 characters: reserve space for 2 extra */
5616 if (*p == '<')
5617 extra += 2;
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620
5621 if (*p != '"')
5622 {
5623 EMSG2(_("E114: Missing quote: %s"), *arg);
5624 return FAIL;
5625 }
5626
5627 /* If only parsing, set *arg and return here */
5628 if (!evaluate)
5629 {
5630 *arg = p + 1;
5631 return OK;
5632 }
5633
5634 /*
5635 * Copy the string into allocated memory, handling backslashed
5636 * characters.
5637 */
5638 name = alloc((unsigned)(p - *arg + extra));
5639 if (name == NULL)
5640 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 rettv->v_type = VAR_STRING;
5642 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 if (*p == '\\')
5647 {
5648 switch (*++p)
5649 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 case 'b': *name++ = BS; ++p; break;
5651 case 'e': *name++ = ESC; ++p; break;
5652 case 'f': *name++ = FF; ++p; break;
5653 case 'n': *name++ = NL; ++p; break;
5654 case 'r': *name++ = CAR; ++p; break;
5655 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 case 'X': /* hex: "\x1", "\x12" */
5658 case 'x':
5659 case 'u': /* Unicode: "\u0023" */
5660 case 'U':
5661 if (vim_isxdigit(p[1]))
5662 {
5663 int n, nr;
5664 int c = toupper(*p);
5665
5666 if (c == 'X')
5667 n = 2;
5668 else
5669 n = 4;
5670 nr = 0;
5671 while (--n >= 0 && vim_isxdigit(p[1]))
5672 {
5673 ++p;
5674 nr = (nr << 4) + hex2nr(*p);
5675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#ifdef FEAT_MBYTE
5678 /* For "\u" store the number according to
5679 * 'encoding'. */
5680 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 else
5683#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 break;
5687
5688 /* octal: "\1", "\12", "\123" */
5689 case '0':
5690 case '1':
5691 case '2':
5692 case '3':
5693 case '4':
5694 case '5':
5695 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 case '7': *name = *p++ - '0';
5697 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name = (*name << 3) + *p++ - '0';
5700 if (*p >= '0' && *p <= '7')
5701 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 break;
5705
5706 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (extra != 0)
5709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 break;
5712 }
5713 /* FALLTHROUGH */
5714
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 break;
5717 }
5718 }
5719 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005720 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 *arg = p + 1;
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 return OK;
5727}
5728
5729/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 * Return OK or FAIL.
5732 */
5733 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 int evaluate;
5738{
5739 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 int reduce = 0;
5742
5743 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 */
5746 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5747 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 break;
5752 ++reduce;
5753 ++p;
5754 }
5755 }
5756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 return FAIL;
5761 }
5762
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 if (!evaluate)
5765 {
5766 *arg = p + 1;
5767 return OK;
5768 }
5769
5770 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 */
5773 str = alloc((unsigned)((p - *arg) - reduce));
5774 if (str == NULL)
5775 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005776 rettv->v_type = VAR_STRING;
5777 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 break;
5785 ++p;
5786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 *arg = p + 1;
5791
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 return OK;
5793}
5794
5795/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005796 * Allocate a variable for a List and fill it from "*arg".
5797 * Return OK or FAIL.
5798 */
5799 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005800get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005802 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005803 int evaluate;
5804{
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 list_T *l = NULL;
5806 typval_T tv;
5807 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808
5809 if (evaluate)
5810 {
5811 l = list_alloc();
5812 if (l == NULL)
5813 return FAIL;
5814 }
5815
5816 *arg = skipwhite(*arg + 1);
5817 while (**arg != ']' && **arg != NUL)
5818 {
5819 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5820 goto failret;
5821 if (evaluate)
5822 {
5823 item = listitem_alloc();
5824 if (item != NULL)
5825 {
5826 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005827 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005828 list_append(l, item);
5829 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005830 else
5831 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 }
5833
5834 if (**arg == ']')
5835 break;
5836 if (**arg != ',')
5837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839 goto failret;
5840 }
5841 *arg = skipwhite(*arg + 1);
5842 }
5843
5844 if (**arg != ']')
5845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847failret:
5848 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005849 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 return FAIL;
5851 }
5852
5853 *arg = skipwhite(*arg + 1);
5854 if (evaluate)
5855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005856 rettv->v_type = VAR_LIST;
5857 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 ++l->lv_refcount;
5859 }
5860
5861 return OK;
5862}
5863
5864/*
5865 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005866 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005868 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005869list_alloc()
5870{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005871 list_T *l;
5872
5873 l = (list_T *)alloc_clear(sizeof(list_T));
5874 if (l != NULL)
5875 {
5876 /* Prepend the list to the list of lists for garbage collection. */
5877 if (first_list != NULL)
5878 first_list->lv_used_prev = l;
5879 l->lv_used_prev = NULL;
5880 l->lv_used_next = first_list;
5881 first_list = l;
5882 }
5883 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884}
5885
5886/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005887 * Allocate an empty list for a return value.
5888 * Returns OK or FAIL.
5889 */
5890 static int
5891rettv_list_alloc(rettv)
5892 typval_T *rettv;
5893{
5894 list_T *l = list_alloc();
5895
5896 if (l == NULL)
5897 return FAIL;
5898
5899 rettv->vval.v_list = l;
5900 rettv->v_type = VAR_LIST;
5901 ++l->lv_refcount;
5902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Unreference a list: decrement the reference count and free it when it
5907 * becomes zero.
5908 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005909 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005911 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005913 if (l != NULL && --l->lv_refcount <= 0)
5914 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915}
5916
5917/*
5918 * Free a list, including all items it points to.
5919 * Ignores the reference count.
5920 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005921 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005922list_free(l, recurse)
5923 list_T *l;
5924 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925{
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005928 /* Remove the list from the list of lists for garbage collection. */
5929 if (l->lv_used_prev == NULL)
5930 first_list = l->lv_used_next;
5931 else
5932 l->lv_used_prev->lv_used_next = l->lv_used_next;
5933 if (l->lv_used_next != NULL)
5934 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5935
Bram Moolenaard9fba312005-06-26 22:34:35 +00005936 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005938 /* Remove the item before deleting it. */
5939 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005940 if (recurse || (item->li_tv.v_type != VAR_LIST
5941 && item->li_tv.v_type != VAR_DICT))
5942 clear_tv(&item->li_tv);
5943 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 }
5945 vim_free(l);
5946}
5947
5948/*
5949 * Allocate a list item.
5950 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005951 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952listitem_alloc()
5953{
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955}
5956
5957/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005958 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 */
5960 static void
5961listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005962 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 vim_free(item);
5966}
5967
5968/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005969 * Remove a list item from a List and free it. Also clears the value.
5970 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005971 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 list_T *l;
5974 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975{
5976 list_remove(l, item, item);
5977 listitem_free(item);
5978}
5979
5980/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981 * Get the number of items in a list.
5982 */
5983 static long
5984list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 if (l == NULL)
5988 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005989 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990}
5991
5992/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993 * Return TRUE when two lists have exactly the same values.
5994 */
5995 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005996list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005997 list_T *l1;
5998 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001{
Bram Moolenaar33570922005-01-25 22:26:29 +00006002 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006004 if (l1 == NULL || l2 == NULL)
6005 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006006 if (l1 == l2)
6007 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006008 if (list_len(l1) != list_len(l2))
6009 return FALSE;
6010
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 for (item1 = l1->lv_first, item2 = l2->lv_first;
6012 item1 != NULL && item2 != NULL;
6013 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006014 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015 return FALSE;
6016 return item1 == NULL && item2 == NULL;
6017}
6018
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006019#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6020 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006021/*
6022 * Return the dictitem that an entry in a hashtable points to.
6023 */
6024 dictitem_T *
6025dict_lookup(hi)
6026 hashitem_T *hi;
6027{
6028 return HI2DI(hi);
6029}
6030#endif
6031
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006032/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033 * Return TRUE when two dictionaries have exactly the same key/values.
6034 */
6035 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006036dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 dict_T *d1;
6038 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006041{
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 hashitem_T *hi;
6043 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006044 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006046 if (d1 == NULL || d2 == NULL)
6047 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006048 if (d1 == d2)
6049 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006050 if (dict_len(d1) != dict_len(d2))
6051 return FALSE;
6052
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006053 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006056 if (!HASHITEM_EMPTY(hi))
6057 {
6058 item2 = dict_find(d2, hi->hi_key, -1);
6059 if (item2 == NULL)
6060 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006062 return FALSE;
6063 --todo;
6064 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 }
6066 return TRUE;
6067}
6068
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006069static int tv_equal_recurse_limit;
6070
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006072 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006073 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006074 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 */
6076 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006078 typval_T *tv1;
6079 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080 int ic; /* ignore case */
6081 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006082{
6083 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006084 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006085 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006086 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006088 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006089 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092 * recursiveness to a limit. We guess they are equal then.
6093 * A fixed limit has the problem of still taking an awful long time.
6094 * Reduce the limit every time running into it. That should work fine for
6095 * deeply linked structures that are not recursively linked and catch
6096 * recursiveness quickly. */
6097 if (!recursive)
6098 tv_equal_recurse_limit = 1000;
6099 if (recursive_cnt >= tv_equal_recurse_limit)
6100 {
6101 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006102 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006103 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104
6105 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006108 ++recursive_cnt;
6109 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6110 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006111 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006112
6113 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 ++recursive_cnt;
6115 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6116 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006117 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006118
6119 case VAR_FUNC:
6120 return (tv1->vval.v_string != NULL
6121 && tv2->vval.v_string != NULL
6122 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6123
6124 case VAR_NUMBER:
6125 return tv1->vval.v_number == tv2->vval.v_number;
6126
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006127#ifdef FEAT_FLOAT
6128 case VAR_FLOAT:
6129 return tv1->vval.v_float == tv2->vval.v_float;
6130#endif
6131
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006132 case VAR_STRING:
6133 s1 = get_tv_string_buf(tv1, buf1);
6134 s2 = get_tv_string_buf(tv2, buf2);
6135 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006137
6138 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 return TRUE;
6140}
6141
6142/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143 * Locate item with index "n" in list "l" and return it.
6144 * A negative index is counted from the end; -1 is the last item.
6145 * Returns NULL when "n" is out of range.
6146 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006147 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 long n;
6151{
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 long idx;
6154
6155 if (l == NULL)
6156 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006157
6158 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160 n = l->lv_len + n;
6161
6162 /* Check for index out of range. */
6163 if (n < 0 || n >= l->lv_len)
6164 return NULL;
6165
6166 /* When there is a cached index may start search from there. */
6167 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006169 if (n < l->lv_idx / 2)
6170 {
6171 /* closest to the start of the list */
6172 item = l->lv_first;
6173 idx = 0;
6174 }
6175 else if (n > (l->lv_idx + l->lv_len) / 2)
6176 {
6177 /* closest to the end of the list */
6178 item = l->lv_last;
6179 idx = l->lv_len - 1;
6180 }
6181 else
6182 {
6183 /* closest to the cached index */
6184 item = l->lv_idx_item;
6185 idx = l->lv_idx;
6186 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006187 }
6188 else
6189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006190 if (n < l->lv_len / 2)
6191 {
6192 /* closest to the start of the list */
6193 item = l->lv_first;
6194 idx = 0;
6195 }
6196 else
6197 {
6198 /* closest to the end of the list */
6199 item = l->lv_last;
6200 idx = l->lv_len - 1;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006203
6204 while (n > idx)
6205 {
6206 /* search forward */
6207 item = item->li_next;
6208 ++idx;
6209 }
6210 while (n < idx)
6211 {
6212 /* search backward */
6213 item = item->li_prev;
6214 --idx;
6215 }
6216
6217 /* cache the used index */
6218 l->lv_idx = idx;
6219 l->lv_idx_item = item;
6220
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 return item;
6222}
6223
6224/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006225 * Get list item "l[idx]" as a number.
6226 */
6227 static long
6228list_find_nr(l, idx, errorp)
6229 list_T *l;
6230 long idx;
6231 int *errorp; /* set to TRUE when something wrong */
6232{
6233 listitem_T *li;
6234
6235 li = list_find(l, idx);
6236 if (li == NULL)
6237 {
6238 if (errorp != NULL)
6239 *errorp = TRUE;
6240 return -1L;
6241 }
6242 return get_tv_number_chk(&li->li_tv, errorp);
6243}
6244
6245/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006246 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6247 */
6248 char_u *
6249list_find_str(l, idx)
6250 list_T *l;
6251 long idx;
6252{
6253 listitem_T *li;
6254
6255 li = list_find(l, idx - 1);
6256 if (li == NULL)
6257 {
6258 EMSGN(_(e_listidx), idx);
6259 return NULL;
6260 }
6261 return get_tv_string(&li->li_tv);
6262}
6263
6264/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006265 * Locate "item" list "l" and return its index.
6266 * Returns -1 when "item" is not in the list.
6267 */
6268 static long
6269list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 list_T *l;
6271 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006272{
6273 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275
6276 if (l == NULL)
6277 return -1;
6278 idx = 0;
6279 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6280 ++idx;
6281 if (li == NULL)
6282 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006283 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006284}
6285
6286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 * Append item "item" to the end of list "l".
6288 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006291 list_T *l;
6292 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293{
6294 if (l->lv_last == NULL)
6295 {
6296 /* empty list */
6297 l->lv_first = item;
6298 l->lv_last = item;
6299 item->li_prev = NULL;
6300 }
6301 else
6302 {
6303 l->lv_last->li_next = item;
6304 item->li_prev = l->lv_last;
6305 l->lv_last = item;
6306 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 item->li_next = NULL;
6309}
6310
6311/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006315 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006317 list_T *l;
6318 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321
Bram Moolenaar05159a02005-02-26 23:04:13 +00006322 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006324 copy_tv(tv, &li->li_tv);
6325 list_append(l, li);
6326 return OK;
6327}
6328
6329/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006330 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006331 * Return FAIL when out of memory.
6332 */
6333 int
6334list_append_dict(list, dict)
6335 list_T *list;
6336 dict_T *dict;
6337{
6338 listitem_T *li = listitem_alloc();
6339
6340 if (li == NULL)
6341 return FAIL;
6342 li->li_tv.v_type = VAR_DICT;
6343 li->li_tv.v_lock = 0;
6344 li->li_tv.vval.v_dict = dict;
6345 list_append(list, li);
6346 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 return OK;
6348}
6349
6350/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006352 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006353 * Returns FAIL when out of memory.
6354 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006355 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006356list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006357 list_T *l;
6358 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006359 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006360{
6361 listitem_T *li = listitem_alloc();
6362
6363 if (li == NULL)
6364 return FAIL;
6365 list_append(l, li);
6366 li->li_tv.v_type = VAR_STRING;
6367 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006368 if (str == NULL)
6369 li->li_tv.vval.v_string = NULL;
6370 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006371 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 return FAIL;
6373 return OK;
6374}
6375
6376/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006377 * Append "n" to list "l".
6378 * Returns FAIL when out of memory.
6379 */
6380 static int
6381list_append_number(l, n)
6382 list_T *l;
6383 varnumber_T n;
6384{
6385 listitem_T *li;
6386
6387 li = listitem_alloc();
6388 if (li == NULL)
6389 return FAIL;
6390 li->li_tv.v_type = VAR_NUMBER;
6391 li->li_tv.v_lock = 0;
6392 li->li_tv.vval.v_number = n;
6393 list_append(l, li);
6394 return OK;
6395}
6396
6397/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399 * If "item" is NULL append at the end.
6400 * Return FAIL when out of memory.
6401 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006402 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 list_T *l;
6405 typval_T *tv;
6406 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407{
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409
6410 if (ni == NULL)
6411 return FAIL;
6412 copy_tv(tv, &ni->li_tv);
6413 if (item == NULL)
6414 /* Append new item at end of list. */
6415 list_append(l, ni);
6416 else
6417 {
6418 /* Insert new item before existing item. */
6419 ni->li_prev = item->li_prev;
6420 ni->li_next = item;
6421 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006422 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006424 ++l->lv_idx;
6425 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006429 l->lv_idx_item = NULL;
6430 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006433 }
6434 return OK;
6435}
6436
6437/*
6438 * Extend "l1" with "l2".
6439 * If "bef" is NULL append at the end, otherwise insert before this item.
6440 * Returns FAIL when out of memory.
6441 */
6442 static int
6443list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 list_T *l1;
6445 list_T *l2;
6446 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447{
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006449 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006451 /* We also quit the loop when we have inserted the original item count of
6452 * the list, avoid a hang when we extend a list with itself. */
6453 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6455 return FAIL;
6456 return OK;
6457}
6458
6459/*
6460 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6461 * Return FAIL when out of memory.
6462 */
6463 static int
6464list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l1;
6466 list_T *l2;
6467 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468{
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006471 if (l1 == NULL || l2 == NULL)
6472 return FAIL;
6473
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006475 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476 if (l == NULL)
6477 return FAIL;
6478 tv->v_type = VAR_LIST;
6479 tv->vval.v_list = l;
6480
6481 /* append all items from the second list */
6482 return list_extend(l, l2, NULL);
6483}
6484
6485/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006486 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006488 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489 * Returns NULL when out of memory.
6490 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 list_T *copy;
6498 listitem_T *item;
6499 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500
6501 if (orig == NULL)
6502 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503
6504 copy = list_alloc();
6505 if (copy != NULL)
6506 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006507 if (copyID != 0)
6508 {
6509 /* Do this before adding the items, because one of the items may
6510 * refer back to this list. */
6511 orig->lv_copyID = copyID;
6512 orig->lv_copylist = copy;
6513 }
6514 for (item = orig->lv_first; item != NULL && !got_int;
6515 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006516 {
6517 ni = listitem_alloc();
6518 if (ni == NULL)
6519 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006521 {
6522 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6523 {
6524 vim_free(ni);
6525 break;
6526 }
6527 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006529 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 list_append(copy, ni);
6531 }
6532 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006533 if (item != NULL)
6534 {
6535 list_unref(copy);
6536 copy = NULL;
6537 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 }
6539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 return copy;
6541}
6542
6543/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006545 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006547 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006548list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 list_T *l;
6550 listitem_T *item;
6551 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552{
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 /* notify watchers */
6556 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006558 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 list_fix_watch(l, ip);
6560 if (ip == item2)
6561 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
6564 if (item2->li_next == NULL)
6565 l->lv_last = item->li_prev;
6566 else
6567 item2->li_next->li_prev = item->li_prev;
6568 if (item->li_prev == NULL)
6569 l->lv_first = item2->li_next;
6570 else
6571 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006572 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573}
6574
6575/*
6576 * Return an allocated string with the string representation of a list.
6577 * May return NULL.
6578 */
6579 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006580list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006582 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583{
6584 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585
6586 if (tv->vval.v_list == NULL)
6587 return NULL;
6588 ga_init2(&ga, (int)sizeof(char), 80);
6589 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006590 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 {
6592 vim_free(ga.ga_data);
6593 return NULL;
6594 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 ga_append(&ga, ']');
6596 ga_append(&ga, NUL);
6597 return (char_u *)ga.ga_data;
6598}
6599
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006600typedef struct join_S {
6601 char_u *s;
6602 char_u *tofree;
6603} join_T;
6604
6605 static int
6606list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6607 garray_T *gap; /* to store the result in */
6608 list_T *l;
6609 char_u *sep;
6610 int echo_style;
6611 int copyID;
6612 garray_T *join_gap; /* to keep each list item string */
6613{
6614 int i;
6615 join_T *p;
6616 int len;
6617 int sumlen = 0;
6618 int first = TRUE;
6619 char_u *tofree;
6620 char_u numbuf[NUMBUFLEN];
6621 listitem_T *item;
6622 char_u *s;
6623
6624 /* Stringify each item in the list. */
6625 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6626 {
6627 if (echo_style)
6628 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6629 else
6630 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6631 if (s == NULL)
6632 return FAIL;
6633
6634 len = (int)STRLEN(s);
6635 sumlen += len;
6636
6637 ga_grow(join_gap, 1);
6638 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6639 if (tofree != NULL || s != numbuf)
6640 {
6641 p->s = s;
6642 p->tofree = tofree;
6643 }
6644 else
6645 {
6646 p->s = vim_strnsave(s, len);
6647 p->tofree = p->s;
6648 }
6649
6650 line_breakcheck();
6651 }
6652
6653 /* Allocate result buffer with its total size, avoid re-allocation and
6654 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6655 if (join_gap->ga_len >= 2)
6656 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6657 if (ga_grow(gap, sumlen + 2) == FAIL)
6658 return FAIL;
6659
6660 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6661 {
6662 if (first)
6663 first = FALSE;
6664 else
6665 ga_concat(gap, sep);
6666 p = ((join_T *)join_gap->ga_data) + i;
6667
6668 if (p->s != NULL)
6669 ga_concat(gap, p->s);
6670 line_breakcheck();
6671 }
6672
6673 return OK;
6674}
6675
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006686 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006687 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006689 garray_T join_ga;
6690 int retval;
6691 join_T *p;
6692 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006693
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6695 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6696
6697 /* Dispose each item in join_ga. */
6698 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700 p = (join_T *)join_ga.ga_data;
6701 for (i = 0; i < join_ga.ga_len; ++i)
6702 {
6703 vim_free(p->tofree);
6704 ++p;
6705 }
6706 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006708
6709 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710}
6711
6712/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006713 * Garbage collection for lists and dictionaries.
6714 *
6715 * We use reference counts to be able to free most items right away when they
6716 * are no longer used. But for composite items it's possible that it becomes
6717 * unused while the reference count is > 0: When there is a recursive
6718 * reference. Example:
6719 * :let l = [1, 2, 3]
6720 * :let d = {9: l}
6721 * :let l[1] = d
6722 *
6723 * Since this is quite unusual we handle this with garbage collection: every
6724 * once in a while find out which lists and dicts are not referenced from any
6725 * variable.
6726 *
6727 * Here is a good reference text about garbage collection (refers to Python
6728 * but it applies to all reference-counting mechanisms):
6729 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006730 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006731
6732/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 * Do garbage collection for lists and dicts.
6734 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 int
6737garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006739 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 buf_T *buf;
6741 win_T *wp;
6742 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006743 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006744 int did_free;
6745 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006746#ifdef FEAT_WINDOWS
6747 tabpage_T *tp;
6748#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006749
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006750 /* Only do this once. */
6751 want_garbage_collect = FALSE;
6752 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006753 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006754
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006755 /* We advance by two because we add one for items referenced through
6756 * previous_funccal. */
6757 current_copyID += COPYID_INC;
6758 copyID = current_copyID;
6759
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 /*
6761 * 1. Go through all accessible variables and mark all lists and dicts
6762 * with copyID.
6763 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006764
6765 /* Don't free variables in the previous_funccal list unless they are only
6766 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006767 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6769 {
6770 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6771 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6772 }
6773
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006774 /* script-local variables */
6775 for (i = 1; i <= ga_scripts.ga_len; ++i)
6776 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6777
6778 /* buffer-local variables */
6779 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006780 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781
6782 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006783 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006784 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006785#ifdef FEAT_AUTOCMD
6786 if (aucmd_win != NULL)
6787 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6788#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006790#ifdef FEAT_WINDOWS
6791 /* tabpage-local variables */
6792 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006793 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006794#endif
6795
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006796 /* global variables */
6797 set_ref_in_ht(&globvarht, copyID);
6798
6799 /* function-local variables */
6800 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6801 {
6802 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6803 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6804 }
6805
Bram Moolenaard812df62008-11-09 12:46:09 +00006806 /* v: vars */
6807 set_ref_in_ht(&vimvarht, copyID);
6808
Bram Moolenaar1dced572012-04-05 16:54:08 +02006809#ifdef FEAT_LUA
6810 set_ref_in_lua(copyID);
6811#endif
6812
Bram Moolenaardb913952012-06-29 12:54:53 +02006813#ifdef FEAT_PYTHON
6814 set_ref_in_python(copyID);
6815#endif
6816
6817#ifdef FEAT_PYTHON3
6818 set_ref_in_python3(copyID);
6819#endif
6820
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006821 /*
6822 * 2. Free lists and dictionaries that are not referenced.
6823 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006824 did_free = free_unref_items(copyID);
6825
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006826 /*
6827 * 3. Check if any funccal can be freed now.
6828 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006829 for (pfc = &previous_funccal; *pfc != NULL; )
6830 {
6831 if (can_free_funccal(*pfc, copyID))
6832 {
6833 fc = *pfc;
6834 *pfc = fc->caller;
6835 free_funccal(fc, TRUE);
6836 did_free = TRUE;
6837 did_free_funccal = TRUE;
6838 }
6839 else
6840 pfc = &(*pfc)->caller;
6841 }
6842 if (did_free_funccal)
6843 /* When a funccal was freed some more items might be garbage
6844 * collected, so run again. */
6845 (void)garbage_collect();
6846
6847 return did_free;
6848}
6849
6850/*
6851 * Free lists and dictionaries that are no longer referenced.
6852 */
6853 static int
6854free_unref_items(copyID)
6855 int copyID;
6856{
6857 dict_T *dd;
6858 list_T *ll;
6859 int did_free = FALSE;
6860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863 */
6864 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006867 /* Free the Dictionary and ordinary items it contains, but don't
6868 * recurse into Lists and Dictionaries, they will be in the list
6869 * of dicts or list of lists. */
6870 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871 did_free = TRUE;
6872
6873 /* restart, next dict may also have been freed */
6874 dd = first_dict;
6875 }
6876 else
6877 dd = dd->dv_used_next;
6878
6879 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 * Go through the list of lists and free items without the copyID.
6881 * But don't free a list that has a watcher (used in a for loop), these
6882 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 */
6884 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006885 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6886 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006888 /* Free the List and ordinary items it contains, but don't recurse
6889 * into Lists and Dictionaries, they will be in the list of dicts
6890 * or list of lists. */
6891 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 did_free = TRUE;
6893
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006894 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 ll = first_list;
6896 }
6897 else
6898 ll = ll->lv_used_next;
6899
6900 return did_free;
6901}
6902
6903/*
6904 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6905 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006906 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907set_ref_in_ht(ht, copyID)
6908 hashtab_T *ht;
6909 int copyID;
6910{
6911 int todo;
6912 hashitem_T *hi;
6913
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006914 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 for (hi = ht->ht_array; todo > 0; ++hi)
6916 if (!HASHITEM_EMPTY(hi))
6917 {
6918 --todo;
6919 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6920 }
6921}
6922
6923/*
6924 * Mark all lists and dicts referenced through list "l" with "copyID".
6925 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006926 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927set_ref_in_list(l, copyID)
6928 list_T *l;
6929 int copyID;
6930{
6931 listitem_T *li;
6932
6933 for (li = l->lv_first; li != NULL; li = li->li_next)
6934 set_ref_in_item(&li->li_tv, copyID);
6935}
6936
6937/*
6938 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6939 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006940 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941set_ref_in_item(tv, copyID)
6942 typval_T *tv;
6943 int copyID;
6944{
6945 dict_T *dd;
6946 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006947
6948 switch (tv->v_type)
6949 {
6950 case VAR_DICT:
6951 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006952 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954 /* Didn't see this dict yet. */
6955 dd->dv_copyID = copyID;
6956 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959
6960 case VAR_LIST:
6961 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006962 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 /* Didn't see this list yet. */
6965 ll->lv_copyID = copyID;
6966 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006969 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971}
6972
6973/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974 * Allocate an empty header for a dictionary.
6975 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006976 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977dict_alloc()
6978{
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006980
Bram Moolenaar33570922005-01-25 22:26:29 +00006981 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006982 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006983 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006984 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 if (first_dict != NULL)
6986 first_dict->dv_used_prev = d;
6987 d->dv_used_next = first_dict;
6988 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006989 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006993 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006994 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006995 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006996 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006997 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998}
6999
7000/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007001 * Allocate an empty dict for a return value.
7002 * Returns OK or FAIL.
7003 */
7004 static int
7005rettv_dict_alloc(rettv)
7006 typval_T *rettv;
7007{
7008 dict_T *d = dict_alloc();
7009
7010 if (d == NULL)
7011 return FAIL;
7012
7013 rettv->vval.v_dict = d;
7014 rettv->v_type = VAR_DICT;
7015 ++d->dv_refcount;
7016 return OK;
7017}
7018
7019
7020/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007021 * Unreference a Dictionary: decrement the reference count and free it when it
7022 * becomes zero.
7023 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007024 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007026 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 if (d != NULL && --d->dv_refcount <= 0)
7029 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030}
7031
7032/*
7033 * Free a Dictionary, including all items it contains.
7034 * Ignores the reference count.
7035 */
7036 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007037dict_free(d, recurse)
7038 dict_T *d;
7039 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007041 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007042 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007043 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 /* Remove the dict from the list of dicts for garbage collection. */
7046 if (d->dv_used_prev == NULL)
7047 first_dict = d->dv_used_next;
7048 else
7049 d->dv_used_prev->dv_used_next = d->dv_used_next;
7050 if (d->dv_used_next != NULL)
7051 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7052
7053 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007054 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007055 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007058 if (!HASHITEM_EMPTY(hi))
7059 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007060 /* Remove the item before deleting it, just in case there is
7061 * something recursive causing trouble. */
7062 di = HI2DI(hi);
7063 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007064 if (recurse || (di->di_tv.v_type != VAR_LIST
7065 && di->di_tv.v_type != VAR_DICT))
7066 clear_tv(&di->di_tv);
7067 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007068 --todo;
7069 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072 vim_free(d);
7073}
7074
7075/*
7076 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007077 * The "key" is copied to the new item.
7078 * Note that the value of the item "di_tv" still needs to be initialized!
7079 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007080 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007081 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082dictitem_alloc(key)
7083 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084{
Bram Moolenaar33570922005-01-25 22:26:29 +00007085 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007087 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007090 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007091 di->di_flags = 0;
7092 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094}
7095
7096/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007097 * Make a copy of a Dictionary item.
7098 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007102{
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007105 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7106 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107 if (di != NULL)
7108 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007110 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 copy_tv(&org->di_tv, &di->di_tv);
7112 }
7113 return di;
7114}
7115
7116/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 * Remove item "item" from Dictionary "dict" and free it.
7118 */
7119 static void
7120dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007121 dict_T *dict;
7122 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123{
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127 if (HASHITEM_EMPTY(hi))
7128 EMSG2(_(e_intern2), "dictitem_remove()");
7129 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 dictitem_free(item);
7132}
7133
7134/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135 * Free a dict item. Also clears the value.
7136 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007137 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007139 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007140{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 clear_tv(&item->di_tv);
7142 vim_free(item);
7143}
7144
7145/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7147 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007148 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 * Returns NULL when out of memory.
7150 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007152dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007153 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007156{
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *copy;
7158 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007161
7162 if (orig == NULL)
7163 return NULL;
7164
7165 copy = dict_alloc();
7166 if (copy != NULL)
7167 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007168 if (copyID != 0)
7169 {
7170 orig->dv_copyID = copyID;
7171 orig->dv_copydict = copy;
7172 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007173 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007174 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007177 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007178 --todo;
7179
7180 di = dictitem_alloc(hi->hi_key);
7181 if (di == NULL)
7182 break;
7183 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007184 {
7185 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7186 copyID) == FAIL)
7187 {
7188 vim_free(di);
7189 break;
7190 }
7191 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 else
7193 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7194 if (dict_add(copy, di) == FAIL)
7195 {
7196 dictitem_free(di);
7197 break;
7198 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007203 if (todo > 0)
7204 {
7205 dict_unref(copy);
7206 copy = NULL;
7207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 }
7209
7210 return copy;
7211}
7212
7213/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007215 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007217 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007219 dict_T *d;
7220 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221{
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223}
7224
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007226 * Add a number or string entry to dictionary "d".
7227 * When "str" is NULL use number "nr", otherwise use "str".
7228 * Returns FAIL when out of memory and when key already exists.
7229 */
7230 int
7231dict_add_nr_str(d, key, nr, str)
7232 dict_T *d;
7233 char *key;
7234 long nr;
7235 char_u *str;
7236{
7237 dictitem_T *item;
7238
7239 item = dictitem_alloc((char_u *)key);
7240 if (item == NULL)
7241 return FAIL;
7242 item->di_tv.v_lock = 0;
7243 if (str == NULL)
7244 {
7245 item->di_tv.v_type = VAR_NUMBER;
7246 item->di_tv.vval.v_number = nr;
7247 }
7248 else
7249 {
7250 item->di_tv.v_type = VAR_STRING;
7251 item->di_tv.vval.v_string = vim_strsave(str);
7252 }
7253 if (dict_add(d, item) == FAIL)
7254 {
7255 dictitem_free(item);
7256 return FAIL;
7257 }
7258 return OK;
7259}
7260
7261/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007262 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007263 * Returns FAIL when out of memory and when key already exists.
7264 */
7265 int
7266dict_add_list(d, key, list)
7267 dict_T *d;
7268 char *key;
7269 list_T *list;
7270{
7271 dictitem_T *item;
7272
7273 item = dictitem_alloc((char_u *)key);
7274 if (item == NULL)
7275 return FAIL;
7276 item->di_tv.v_lock = 0;
7277 item->di_tv.v_type = VAR_LIST;
7278 item->di_tv.vval.v_list = list;
7279 if (dict_add(d, item) == FAIL)
7280 {
7281 dictitem_free(item);
7282 return FAIL;
7283 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007284 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007285 return OK;
7286}
7287
7288/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007289 * Get the number of items in a Dictionary.
7290 */
7291 static long
7292dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 if (d == NULL)
7296 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298}
7299
7300/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 * Find item "key[len]" in Dictionary "d".
7302 * If "len" is negative use strlen(key).
7303 * Returns NULL when not found.
7304 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007305 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 char_u *key;
7309 int len;
7310{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311#define AKEYLEN 200
7312 char_u buf[AKEYLEN];
7313 char_u *akey;
7314 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 if (len < 0)
7318 akey = key;
7319 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007320 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 tofree = akey = vim_strnsave(key, len);
7322 if (akey == NULL)
7323 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 else
7326 {
7327 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007328 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 akey = buf;
7330 }
7331
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 vim_free(tofree);
7334 if (HASHITEM_EMPTY(hi))
7335 return NULL;
7336 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337}
7338
7339/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340 * Get a string item from a dictionary.
7341 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007342 * Returns NULL if the entry doesn't exist or out of memory.
7343 */
7344 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007345get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007346 dict_T *d;
7347 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007349{
7350 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352
7353 di = dict_find(d, key, -1);
7354 if (di == NULL)
7355 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007356 s = get_tv_string(&di->di_tv);
7357 if (save && s != NULL)
7358 s = vim_strsave(s);
7359 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007360}
7361
7362/*
7363 * Get a number item from a dictionary.
7364 * Returns 0 if the entry doesn't exist or out of memory.
7365 */
7366 long
7367get_dict_number(d, key)
7368 dict_T *d;
7369 char_u *key;
7370{
7371 dictitem_T *di;
7372
7373 di = dict_find(d, key, -1);
7374 if (di == NULL)
7375 return 0;
7376 return get_tv_number(&di->di_tv);
7377}
7378
7379/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380 * Return an allocated string with the string representation of a Dictionary.
7381 * May return NULL.
7382 */
7383 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007384dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007385 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007386 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387{
7388 garray_T ga;
7389 int first = TRUE;
7390 char_u *tofree;
7391 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007394 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 return NULL;
7399 ga_init2(&ga, (int)sizeof(char), 80);
7400 ga_append(&ga, '{');
7401
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007402 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007403 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 --todo;
7408
7409 if (first)
7410 first = FALSE;
7411 else
7412 ga_concat(&ga, (char_u *)", ");
7413
7414 tofree = string_quote(hi->hi_key, FALSE);
7415 if (tofree != NULL)
7416 {
7417 ga_concat(&ga, tofree);
7418 vim_free(tofree);
7419 }
7420 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007421 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007422 if (s != NULL)
7423 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 if (s == NULL)
7426 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 if (todo > 0)
7430 {
7431 vim_free(ga.ga_data);
7432 return NULL;
7433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434
7435 ga_append(&ga, '}');
7436 ga_append(&ga, NUL);
7437 return (char_u *)ga.ga_data;
7438}
7439
7440/*
7441 * Allocate a variable for a Dictionary and fill it from "*arg".
7442 * Return OK or FAIL. Returns NOTDONE for {expr}.
7443 */
7444 static int
7445get_dict_tv(arg, rettv, evaluate)
7446 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 int evaluate;
7449{
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 dict_T *d = NULL;
7451 typval_T tvkey;
7452 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007453 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007456 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457
7458 /*
7459 * First check if it's not a curly-braces thing: {expr}.
7460 * Must do this without evaluating, otherwise a function may be called
7461 * twice. Unfortunately this means we need to call eval1() twice for the
7462 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007465 if (*start != '}')
7466 {
7467 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7468 return FAIL;
7469 if (*start == '}')
7470 return NOTDONE;
7471 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472
7473 if (evaluate)
7474 {
7475 d = dict_alloc();
7476 if (d == NULL)
7477 return FAIL;
7478 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 tvkey.v_type = VAR_UNKNOWN;
7480 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 *arg = skipwhite(*arg + 1);
7483 while (**arg != '}' && **arg != NUL)
7484 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 goto failret;
7487 if (**arg != ':')
7488 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007489 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 goto failret;
7492 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007493 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007495 key = get_tv_string_buf_chk(&tvkey, buf);
7496 if (key == NULL || *key == NUL)
7497 {
7498 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7499 if (key != NULL)
7500 EMSG(_(e_emptykey));
7501 clear_tv(&tvkey);
7502 goto failret;
7503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
7506 *arg = skipwhite(*arg + 1);
7507 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7508 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007509 if (evaluate)
7510 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 goto failret;
7512 }
7513 if (evaluate)
7514 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 if (item != NULL)
7517 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007518 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 clear_tv(&tv);
7521 goto failret;
7522 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 item = dictitem_alloc(key);
7524 clear_tv(&tvkey);
7525 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007528 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 if (dict_add(d, item) == FAIL)
7530 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 }
7532 }
7533
7534 if (**arg == '}')
7535 break;
7536 if (**arg != ',')
7537 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007538 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 goto failret;
7540 }
7541 *arg = skipwhite(*arg + 1);
7542 }
7543
7544 if (**arg != '}')
7545 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007546 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547failret:
7548 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007549 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 return FAIL;
7551 }
7552
7553 *arg = skipwhite(*arg + 1);
7554 if (evaluate)
7555 {
7556 rettv->v_type = VAR_DICT;
7557 rettv->vval.v_dict = d;
7558 ++d->dv_refcount;
7559 }
7560
7561 return OK;
7562}
7563
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007565 * Return a string with the string representation of a variable.
7566 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007567 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007568 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007570 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 */
7572 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007576 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007577 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 static int recurse = 0;
7580 char_u *r = NULL;
7581
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007584 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 *tofree = NULL;
7586 return NULL;
7587 }
7588 ++recurse;
7589
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007590 switch (tv->v_type)
7591 {
7592 case VAR_FUNC:
7593 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007594 r = tv->vval.v_string;
7595 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007597 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 if (tv->vval.v_list == NULL)
7599 {
7600 *tofree = NULL;
7601 r = NULL;
7602 }
7603 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7604 {
7605 *tofree = NULL;
7606 r = (char_u *)"[...]";
7607 }
7608 else
7609 {
7610 tv->vval.v_list->lv_copyID = copyID;
7611 *tofree = list2string(tv, copyID);
7612 r = *tofree;
7613 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007615
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007617 if (tv->vval.v_dict == NULL)
7618 {
7619 *tofree = NULL;
7620 r = NULL;
7621 }
7622 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7623 {
7624 *tofree = NULL;
7625 r = (char_u *)"{...}";
7626 }
7627 else
7628 {
7629 tv->vval.v_dict->dv_copyID = copyID;
7630 *tofree = dict2string(tv, copyID);
7631 r = *tofree;
7632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007634
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635 case VAR_STRING:
7636 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 *tofree = NULL;
7638 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007640
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007641#ifdef FEAT_FLOAT
7642 case VAR_FLOAT:
7643 *tofree = NULL;
7644 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7645 r = numbuf;
7646 break;
7647#endif
7648
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007652 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007653
7654 --recurse;
7655 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656}
7657
7658/*
7659 * Return a string with the string representation of a variable.
7660 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7661 * "numbuf" is used for a number.
7662 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007663 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007664 */
7665 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007666tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007668 char_u **tofree;
7669 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007671{
7672 switch (tv->v_type)
7673 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 case VAR_FUNC:
7675 *tofree = string_quote(tv->vval.v_string, TRUE);
7676 return *tofree;
7677 case VAR_STRING:
7678 *tofree = string_quote(tv->vval.v_string, FALSE);
7679 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007680#ifdef FEAT_FLOAT
7681 case VAR_FLOAT:
7682 *tofree = NULL;
7683 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7684 return numbuf;
7685#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007686 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007687 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007693 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007694}
7695
7696/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 * Return string "str" in ' quotes, doubling ' characters.
7698 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007700 */
7701 static char_u *
7702string_quote(str, function)
7703 char_u *str;
7704 int function;
7705{
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007707 char_u *p, *r, *s;
7708
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 len = (function ? 13 : 3);
7710 if (str != NULL)
7711 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007712 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 for (p = str; *p != NUL; mb_ptr_adv(p))
7714 if (*p == '\'')
7715 ++len;
7716 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 s = r = alloc(len);
7718 if (r != NULL)
7719 {
7720 if (function)
7721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 r += 10;
7724 }
7725 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 if (str != NULL)
7728 for (p = str; *p != NUL; )
7729 {
7730 if (*p == '\'')
7731 *r++ = '\'';
7732 MB_COPY_CHAR(p, r);
7733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007735 if (function)
7736 *r++ = ')';
7737 *r++ = NUL;
7738 }
7739 return s;
7740}
7741
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007742#ifdef FEAT_FLOAT
7743/*
7744 * Convert the string "text" to a floating point number.
7745 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7746 * this always uses a decimal point.
7747 * Returns the length of the text that was consumed.
7748 */
7749 static int
7750string2float(text, value)
7751 char_u *text;
7752 float_T *value; /* result stored here */
7753{
7754 char *s = (char *)text;
7755 float_T f;
7756
7757 f = strtod(s, &s);
7758 *value = f;
7759 return (int)((char_u *)s - text);
7760}
7761#endif
7762
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 * Get the value of an environment variable.
7765 * "arg" is pointing to the '$'. It is advanced to after the name.
7766 * If the environment variable was not set, silently assume it is empty.
7767 * Always return OK.
7768 */
7769 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007770get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 int evaluate;
7774{
7775 char_u *string = NULL;
7776 int len;
7777 int cc;
7778 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007779 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780
7781 ++*arg;
7782 name = *arg;
7783 len = get_env_len(arg);
7784 if (evaluate)
7785 {
7786 if (len != 0)
7787 {
7788 cc = name[len];
7789 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007790 /* first try vim_getenv(), fast for normal environment vars */
7791 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 {
7794 if (!mustfree)
7795 string = vim_strsave(string);
7796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 else
7798 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799 if (mustfree)
7800 vim_free(string);
7801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 /* next try expanding things like $VIM and ${HOME} */
7803 string = expand_env_save(name - 1);
7804 if (string != NULL && *string == '$')
7805 {
7806 vim_free(string);
7807 string = NULL;
7808 }
7809 }
7810 name[len] = cc;
7811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007812 rettv->v_type = VAR_STRING;
7813 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815
7816 return OK;
7817}
7818
7819/*
7820 * Array with names and number of arguments of all internal functions
7821 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7822 */
7823static struct fst
7824{
7825 char *f_name; /* function name */
7826 char f_min_argc; /* minimal number of arguments */
7827 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007828 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007829 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830} functions[] =
7831{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007832#ifdef FEAT_FLOAT
7833 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007834 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007835#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007836 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007837 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {"append", 2, 2, f_append},
7839 {"argc", 0, 0, f_argc},
7840 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007841 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007843 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007845 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007848 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"bufexists", 1, 1, f_bufexists},
7850 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7851 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7852 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7853 {"buflisted", 1, 1, f_buflisted},
7854 {"bufloaded", 1, 1, f_bufloaded},
7855 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007856 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {"bufwinnr", 1, 1, f_bufwinnr},
7858 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007859 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007861#ifdef FEAT_FLOAT
7862 {"ceil", 1, 1, f_ceil},
7863#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007864 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007865 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007867 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007869#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007870 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007871 {"complete_add", 1, 1, f_complete_add},
7872 {"complete_check", 0, 0, f_complete_check},
7873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876#ifdef FEAT_FLOAT
7877 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007878 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007880 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007882 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007883 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"delete", 1, 1, f_delete},
7885 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007886 {"diff_filler", 1, 1, f_diff_filler},
7887 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007888 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"eventhandler", 0, 0, f_eventhandler},
7892 {"executable", 1, 1, f_executable},
7893 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007894#ifdef FEAT_FLOAT
7895 {"exp", 1, 1, f_exp},
7896#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007897 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007898 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007899 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7901 {"filereadable", 1, 1, f_filereadable},
7902 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007903 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007904 {"finddir", 1, 3, f_finddir},
7905 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906#ifdef FEAT_FLOAT
7907 {"float2nr", 1, 1, f_float2nr},
7908 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007909 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007911 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"fnamemodify", 2, 2, f_fnamemodify},
7913 {"foldclosed", 1, 1, f_foldclosed},
7914 {"foldclosedend", 1, 1, f_foldclosedend},
7915 {"foldlevel", 1, 1, f_foldlevel},
7916 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007917 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007920 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007921 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007922 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007923 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getchar", 0, 1, f_getchar},
7925 {"getcharmod", 0, 0, f_getcharmod},
7926 {"getcmdline", 0, 0, f_getcmdline},
7927 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007928 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007930 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007931 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"getfsize", 1, 1, f_getfsize},
7933 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007934 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007935 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007936 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007937 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007938 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007939 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007940 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007941 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007943 {"gettabvar", 2, 3, f_gettabvar},
7944 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"getwinposx", 0, 0, f_getwinposx},
7946 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007947 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007948 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007949 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007951 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007952 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007953 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7955 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7956 {"histadd", 2, 2, f_histadd},
7957 {"histdel", 1, 2, f_histdel},
7958 {"histget", 1, 2, f_histget},
7959 {"histnr", 1, 1, f_histnr},
7960 {"hlID", 1, 1, f_hlID},
7961 {"hlexists", 1, 1, f_hlexists},
7962 {"hostname", 0, 0, f_hostname},
7963 {"iconv", 3, 3, f_iconv},
7964 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007965 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007966 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007968 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {"inputrestore", 0, 0, f_inputrestore},
7970 {"inputsave", 0, 0, f_inputsave},
7971 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007973 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007975 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007980 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"libcall", 3, 3, f_libcall},
7982 {"libcallnr", 3, 3, f_libcallnr},
7983 {"line", 1, 1, f_line},
7984 {"line2byte", 1, 1, f_line2byte},
7985 {"lispindent", 1, 1, f_lispindent},
7986 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007988 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007989 {"log10", 1, 1, f_log10},
7990#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007991#ifdef FEAT_LUA
7992 {"luaeval", 1, 2, f_luaeval},
7993#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007995 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007996 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007997 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007998 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007999 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008000 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008001 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008002 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008003 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008004 {"max", 1, 1, f_max},
8005 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008006#ifdef vim_mkdir
8007 {"mkdir", 1, 3, f_mkdir},
8008#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008009 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008010#ifdef FEAT_MZSCHEME
8011 {"mzeval", 1, 1, f_mzeval},
8012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008014 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008015 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008016 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008017#ifdef FEAT_FLOAT
8018 {"pow", 2, 2, f_pow},
8019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008021 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008022 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008023#ifdef FEAT_PYTHON3
8024 {"py3eval", 1, 1, f_py3eval},
8025#endif
8026#ifdef FEAT_PYTHON
8027 {"pyeval", 1, 1, f_pyeval},
8028#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008030 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008031 {"reltime", 0, 2, f_reltime},
8032 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"remote_expr", 2, 3, f_remote_expr},
8034 {"remote_foreground", 1, 1, f_remote_foreground},
8035 {"remote_peek", 1, 2, f_remote_peek},
8036 {"remote_read", 1, 1, f_remote_read},
8037 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008038 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008040 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008042 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008043#ifdef FEAT_FLOAT
8044 {"round", 1, 1, f_round},
8045#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008046 {"screencol", 0, 0, f_screencol},
8047 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008048 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008049 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008050 {"searchpair", 3, 7, f_searchpair},
8051 {"searchpairpos", 3, 7, f_searchpairpos},
8052 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {"server2client", 2, 2, f_server2client},
8054 {"serverlist", 0, 0, f_serverlist},
8055 {"setbufvar", 3, 3, f_setbufvar},
8056 {"setcmdpos", 1, 1, f_setcmdpos},
8057 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008058 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008059 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008060 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008061 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008063 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008064 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008066#ifdef FEAT_CRYPT
8067 {"sha256", 1, 1, f_sha256},
8068#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008069 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008070 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#ifdef FEAT_FLOAT
8073 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008074 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008076 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008077 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008078 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008079 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008080 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#ifdef FEAT_FLOAT
8082 {"sqrt", 1, 1, f_sqrt},
8083 {"str2float", 1, 1, f_str2float},
8084#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008085 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008086 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008087 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088#ifdef HAVE_STRFTIME
8089 {"strftime", 1, 2, f_strftime},
8090#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008091 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"strlen", 1, 1, f_strlen},
8094 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008095 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008097 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"submatch", 1, 1, f_submatch},
8099 {"substitute", 4, 4, f_substitute},
8100 {"synID", 3, 3, f_synID},
8101 {"synIDattr", 2, 3, f_synIDattr},
8102 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008103 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008104 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008105 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008106 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008107 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008108 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008109 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008110 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008111#ifdef FEAT_FLOAT
8112 {"tan", 1, 1, f_tan},
8113 {"tanh", 1, 1, f_tanh},
8114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008116 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"tolower", 1, 1, f_tolower},
8118 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008119 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120#ifdef FEAT_FLOAT
8121 {"trunc", 1, 1, f_trunc},
8122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008124 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008125 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008126 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"virtcol", 1, 1, f_virtcol},
8128 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008129 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"winbufnr", 1, 1, f_winbufnr},
8131 {"wincol", 0, 0, f_wincol},
8132 {"winheight", 1, 1, f_winheight},
8133 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008134 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008136 {"winrestview", 1, 1, f_winrestview},
8137 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008139 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008140 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141};
8142
8143#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8144
8145/*
8146 * Function given to ExpandGeneric() to obtain the list of internal
8147 * or user defined function names.
8148 */
8149 char_u *
8150get_function_name(xp, idx)
8151 expand_T *xp;
8152 int idx;
8153{
8154 static int intidx = -1;
8155 char_u *name;
8156
8157 if (idx == 0)
8158 intidx = -1;
8159 if (intidx < 0)
8160 {
8161 name = get_user_func_name(xp, idx);
8162 if (name != NULL)
8163 return name;
8164 }
8165 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8166 {
8167 STRCPY(IObuff, functions[intidx].f_name);
8168 STRCAT(IObuff, "(");
8169 if (functions[intidx].f_max_argc == 0)
8170 STRCAT(IObuff, ")");
8171 return IObuff;
8172 }
8173
8174 return NULL;
8175}
8176
8177/*
8178 * Function given to ExpandGeneric() to obtain the list of internal or
8179 * user defined variable or function names.
8180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 char_u *
8182get_expr_name(xp, idx)
8183 expand_T *xp;
8184 int idx;
8185{
8186 static int intidx = -1;
8187 char_u *name;
8188
8189 if (idx == 0)
8190 intidx = -1;
8191 if (intidx < 0)
8192 {
8193 name = get_function_name(xp, idx);
8194 if (name != NULL)
8195 return name;
8196 }
8197 return get_user_var_name(xp, ++intidx);
8198}
8199
8200#endif /* FEAT_CMDL_COMPL */
8201
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008202#if defined(EBCDIC) || defined(PROTO)
8203/*
8204 * Compare struct fst by function name.
8205 */
8206 static int
8207compare_func_name(s1, s2)
8208 const void *s1;
8209 const void *s2;
8210{
8211 struct fst *p1 = (struct fst *)s1;
8212 struct fst *p2 = (struct fst *)s2;
8213
8214 return STRCMP(p1->f_name, p2->f_name);
8215}
8216
8217/*
8218 * Sort the function table by function name.
8219 * The sorting of the table above is ASCII dependant.
8220 * On machines using EBCDIC we have to sort it.
8221 */
8222 static void
8223sortFunctions()
8224{
8225 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8226
8227 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8228}
8229#endif
8230
8231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232/*
8233 * Find internal function in table above.
8234 * Return index, or -1 if not found
8235 */
8236 static int
8237find_internal_func(name)
8238 char_u *name; /* name of the function */
8239{
8240 int first = 0;
8241 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8242 int cmp;
8243 int x;
8244
8245 /*
8246 * Find the function name in the table. Binary search.
8247 */
8248 while (first <= last)
8249 {
8250 x = first + ((unsigned)(last - first) >> 1);
8251 cmp = STRCMP(name, functions[x].f_name);
8252 if (cmp < 0)
8253 last = x - 1;
8254 else if (cmp > 0)
8255 first = x + 1;
8256 else
8257 return x;
8258 }
8259 return -1;
8260}
8261
8262/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008263 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8264 * name it contains, otherwise return "name".
8265 */
8266 static char_u *
8267deref_func_name(name, lenp)
8268 char_u *name;
8269 int *lenp;
8270{
Bram Moolenaar33570922005-01-25 22:26:29 +00008271 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 int cc;
8273
8274 cc = name[*lenp];
8275 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008276 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008278 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008279 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 {
8282 *lenp = 0;
8283 return (char_u *)""; /* just in case */
8284 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008285 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287 }
8288
8289 return name;
8290}
8291
8292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 * Allocate a variable for the result of a function.
8294 * Return OK or FAIL.
8295 */
8296 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008297get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8298 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 char_u *name; /* name of the function */
8300 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 char_u **arg; /* argument, pointing to the '(' */
8303 linenr_T firstline; /* first line of range */
8304 linenr_T lastline; /* last line of range */
8305 int *doesrange; /* return: function handled range */
8306 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
8309 char_u *argp;
8310 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008311 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 int argcount = 0; /* number of arguments found */
8313
8314 /*
8315 * Get the arguments.
8316 */
8317 argp = *arg;
8318 while (argcount < MAX_FUNC_ARGS)
8319 {
8320 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8321 if (*argp == ')' || *argp == ',' || *argp == NUL)
8322 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8324 {
8325 ret = FAIL;
8326 break;
8327 }
8328 ++argcount;
8329 if (*argp != ',')
8330 break;
8331 }
8332 if (*argp == ')')
8333 ++argp;
8334 else
8335 ret = FAIL;
8336
8337 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008338 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008339 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008341 {
8342 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008343 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008345 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008349 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
8351 *arg = skipwhite(argp);
8352 return ret;
8353}
8354
8355
8356/*
8357 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008358 * Return OK when the function can't be called, FAIL otherwise.
8359 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 */
8361 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008362call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008363 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008364 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008366 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008368 typval_T *argvars; /* vars for arguments, must have "argcount"
8369 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 linenr_T firstline; /* first line of range */
8371 linenr_T lastline; /* last line of range */
8372 int *doesrange; /* return: function handled range */
8373 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377#define ERROR_UNKNOWN 0
8378#define ERROR_TOOMANY 1
8379#define ERROR_TOOFEW 2
8380#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381#define ERROR_DICT 4
8382#define ERROR_NONE 5
8383#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 int error = ERROR_NONE;
8385 int i;
8386 int llen;
8387 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#define FLEN_FIXED 40
8389 char_u fname_buf[FLEN_FIXED + 1];
8390 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008391 char_u *name;
8392
8393 /* Make a copy of the name, if it comes from a funcref variable it could
8394 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008395 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 if (name == NULL)
8397 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398
8399 /*
8400 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8401 * Change <SNR>123_name() to K_SNR 123_name().
8402 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 llen = eval_fname_script(name);
8405 if (llen > 0)
8406 {
8407 fname_buf[0] = K_SPECIAL;
8408 fname_buf[1] = KS_EXTRA;
8409 fname_buf[2] = (int)KE_SNR;
8410 i = 3;
8411 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8412 {
8413 if (current_SID <= 0)
8414 error = ERROR_SCRIPT;
8415 else
8416 {
8417 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8418 i = (int)STRLEN(fname_buf);
8419 }
8420 }
8421 if (i + STRLEN(name + llen) < FLEN_FIXED)
8422 {
8423 STRCPY(fname_buf + i, name + llen);
8424 fname = fname_buf;
8425 }
8426 else
8427 {
8428 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8429 if (fname == NULL)
8430 error = ERROR_OTHER;
8431 else
8432 {
8433 mch_memmove(fname, fname_buf, (size_t)i);
8434 STRCPY(fname + i, name + llen);
8435 }
8436 }
8437 }
8438 else
8439 fname = name;
8440
8441 *doesrange = FALSE;
8442
8443
8444 /* execute the function if no errors detected and executing */
8445 if (evaluate && error == ERROR_NONE)
8446 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008447 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8448 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 error = ERROR_UNKNOWN;
8450
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008451 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
8453 /*
8454 * User defined function.
8455 */
8456 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008459 /* Trigger FuncUndefined event, may load the function. */
8460 if (fp == NULL
8461 && apply_autocmds(EVENT_FUNCUNDEFINED,
8462 fname, fname, TRUE, NULL)
8463 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 fp = find_func(fname);
8467 }
8468#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008469 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008470 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471 {
8472 /* loaded a package, search for the function again */
8473 fp = find_func(fname);
8474 }
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 if (fp != NULL)
8477 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008480 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008482 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008484 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008485 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 else
8487 {
8488 /*
8489 * Call the user function.
8490 * Save and restore search patterns, script variables and
8491 * redo buffer.
8492 */
8493 save_search_patterns();
8494 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008495 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008498 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8499 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8500 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008501 /* Function was unreferenced while being used, free it
8502 * now. */
8503 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 restoreRedobuff();
8505 restore_search_patterns();
8506 error = ERROR_NONE;
8507 }
8508 }
8509 }
8510 else
8511 {
8512 /*
8513 * Find the function name in the table, call its implementation.
8514 */
8515 i = find_internal_func(fname);
8516 if (i >= 0)
8517 {
8518 if (argcount < functions[i].f_min_argc)
8519 error = ERROR_TOOFEW;
8520 else if (argcount > functions[i].f_max_argc)
8521 error = ERROR_TOOMANY;
8522 else
8523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008525 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 error = ERROR_NONE;
8527 }
8528 }
8529 }
8530 /*
8531 * The function call (or "FuncUndefined" autocommand sequence) might
8532 * have been aborted by an error, an interrupt, or an explicitly thrown
8533 * exception that has not been caught so far. This situation can be
8534 * tested for by calling aborting(). For an error in an internal
8535 * function or for the "E132" error in call_user_func(), however, the
8536 * throw point at which the "force_abort" flag (temporarily reset by
8537 * emsg()) is normally updated has not been reached yet. We need to
8538 * update that flag first to make aborting() reliable.
8539 */
8540 update_force_abort();
8541 }
8542 if (error == ERROR_NONE)
8543 ret = OK;
8544
8545 /*
8546 * Report an error unless the argument evaluation or function call has been
8547 * cancelled due to an aborting error, an interrupt, or an exception.
8548 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008549 if (!aborting())
8550 {
8551 switch (error)
8552 {
8553 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008554 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 break;
8556 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008557 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008558 break;
8559 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 name);
8562 break;
8563 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008564 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 name);
8566 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008568 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008569 name);
8570 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008571 }
8572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (fname != name && fname != fname_buf)
8575 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008576 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577
8578 return ret;
8579}
8580
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008581/*
8582 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008583 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584 */
8585 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008586emsg_funcname(ermsg, name)
8587 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008588 char_u *name;
8589{
8590 char_u *p;
8591
8592 if (*name == K_SPECIAL)
8593 p = concat_str((char_u *)"<SNR>", name + 3);
8594 else
8595 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008596 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008597 if (p != name)
8598 vim_free(p);
8599}
8600
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008601/*
8602 * Return TRUE for a non-zero Number and a non-empty String.
8603 */
8604 static int
8605non_zero_arg(argvars)
8606 typval_T *argvars;
8607{
8608 return ((argvars[0].v_type == VAR_NUMBER
8609 && argvars[0].vval.v_number != 0)
8610 || (argvars[0].v_type == VAR_STRING
8611 && argvars[0].vval.v_string != NULL
8612 && *argvars[0].vval.v_string != NUL));
8613}
8614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615/*********************************************
8616 * Implementation of the built-in functions
8617 */
8618
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008619#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008620static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8621
8622/*
8623 * Get the float value of "argvars[0]" into "f".
8624 * Returns FAIL when the argument is not a Number or Float.
8625 */
8626 static int
8627get_float_arg(argvars, f)
8628 typval_T *argvars;
8629 float_T *f;
8630{
8631 if (argvars[0].v_type == VAR_FLOAT)
8632 {
8633 *f = argvars[0].vval.v_float;
8634 return OK;
8635 }
8636 if (argvars[0].v_type == VAR_NUMBER)
8637 {
8638 *f = (float_T)argvars[0].vval.v_number;
8639 return OK;
8640 }
8641 EMSG(_("E808: Number or Float required"));
8642 return FAIL;
8643}
8644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008645/*
8646 * "abs(expr)" function
8647 */
8648 static void
8649f_abs(argvars, rettv)
8650 typval_T *argvars;
8651 typval_T *rettv;
8652{
8653 if (argvars[0].v_type == VAR_FLOAT)
8654 {
8655 rettv->v_type = VAR_FLOAT;
8656 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8657 }
8658 else
8659 {
8660 varnumber_T n;
8661 int error = FALSE;
8662
8663 n = get_tv_number_chk(&argvars[0], &error);
8664 if (error)
8665 rettv->vval.v_number = -1;
8666 else if (n > 0)
8667 rettv->vval.v_number = n;
8668 else
8669 rettv->vval.v_number = -n;
8670 }
8671}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008672
8673/*
8674 * "acos()" function
8675 */
8676 static void
8677f_acos(argvars, rettv)
8678 typval_T *argvars;
8679 typval_T *rettv;
8680{
8681 float_T f;
8682
8683 rettv->v_type = VAR_FLOAT;
8684 if (get_float_arg(argvars, &f) == OK)
8685 rettv->vval.v_float = acos(f);
8686 else
8687 rettv->vval.v_float = 0.0;
8688}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008689#endif
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008692 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 */
8694 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008695f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *argvars;
8697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008701 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008702 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008704 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008705 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008706 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 }
8709 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008710 EMSG(_(e_listreq));
8711}
8712
8713/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008714 * "and(expr, expr)" function
8715 */
8716 static void
8717f_and(argvars, rettv)
8718 typval_T *argvars;
8719 typval_T *rettv;
8720{
8721 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8722 & get_tv_number_chk(&argvars[1], NULL);
8723}
8724
8725/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008726 * "append(lnum, string/list)" function
8727 */
8728 static void
8729f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 typval_T *argvars;
8731 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732{
8733 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008734 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 list_T *l = NULL;
8736 listitem_T *li = NULL;
8737 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 long added = 0;
8739
Bram Moolenaar0d660222005-01-07 21:51:51 +00008740 lnum = get_tv_lnum(argvars);
8741 if (lnum >= 0
8742 && lnum <= curbuf->b_ml.ml_line_count
8743 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008746 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 l = argvars[1].vval.v_list;
8748 if (l == NULL)
8749 return;
8750 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008752 for (;;)
8753 {
8754 if (l == NULL)
8755 tv = &argvars[1]; /* append a string */
8756 else if (li == NULL)
8757 break; /* end of list */
8758 else
8759 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008760 line = get_tv_string_chk(tv);
8761 if (line == NULL) /* type error */
8762 {
8763 rettv->vval.v_number = 1; /* Failed */
8764 break;
8765 }
8766 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008767 ++added;
8768 if (l == NULL)
8769 break;
8770 li = li->li_next;
8771 }
8772
8773 appended_lines_mark(lnum, added);
8774 if (curwin->w_cursor.lnum > lnum)
8775 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008777 else
8778 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779}
8780
8781/*
8782 * "argc()" function
8783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008785f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008786 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790}
8791
8792/*
8793 * "argidx()" function
8794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008797 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008800 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801}
8802
8803/*
8804 * "argv(nr)" function
8805 */
8806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 typval_T *argvars;
8809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
8811 int idx;
8812
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008813 if (argvars[0].v_type != VAR_UNKNOWN)
8814 {
8815 idx = get_tv_number_chk(&argvars[0], NULL);
8816 if (idx >= 0 && idx < ARGCOUNT)
8817 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8818 else
8819 rettv->vval.v_string = NULL;
8820 rettv->v_type = VAR_STRING;
8821 }
8822 else if (rettv_list_alloc(rettv) == OK)
8823 for (idx = 0; idx < ARGCOUNT; ++idx)
8824 list_append_string(rettv->vval.v_list,
8825 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008829/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008831 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008832 static void
8833f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008836{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008837 float_T f;
8838
8839 rettv->v_type = VAR_FLOAT;
8840 if (get_float_arg(argvars, &f) == OK)
8841 rettv->vval.v_float = asin(f);
8842 else
8843 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008844}
8845
8846/*
8847 * "atan()" function
8848 */
8849 static void
8850f_atan(argvars, rettv)
8851 typval_T *argvars;
8852 typval_T *rettv;
8853{
8854 float_T f;
8855
8856 rettv->v_type = VAR_FLOAT;
8857 if (get_float_arg(argvars, &f) == OK)
8858 rettv->vval.v_float = atan(f);
8859 else
8860 rettv->vval.v_float = 0.0;
8861}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008862
8863/*
8864 * "atan2()" function
8865 */
8866 static void
8867f_atan2(argvars, rettv)
8868 typval_T *argvars;
8869 typval_T *rettv;
8870{
8871 float_T fx, fy;
8872
8873 rettv->v_type = VAR_FLOAT;
8874 if (get_float_arg(argvars, &fx) == OK
8875 && get_float_arg(&argvars[1], &fy) == OK)
8876 rettv->vval.v_float = atan2(fx, fy);
8877 else
8878 rettv->vval.v_float = 0.0;
8879}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008880#endif
8881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882/*
8883 * "browse(save, title, initdir, default)" function
8884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008887 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008888 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889{
8890#ifdef FEAT_BROWSE
8891 int save;
8892 char_u *title;
8893 char_u *initdir;
8894 char_u *defname;
8895 char_u buf[NUMBUFLEN];
8896 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008899 save = get_tv_number_chk(&argvars[0], &error);
8900 title = get_tv_string_chk(&argvars[1]);
8901 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8902 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 if (error || title == NULL || initdir == NULL || defname == NULL)
8905 rettv->vval.v_string = NULL;
8906 else
8907 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008908 do_browse(save ? BROWSE_SAVE : 0,
8909 title, defname, NULL, initdir, NULL, curbuf);
8910#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008912#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008914}
8915
8916/*
8917 * "browsedir(title, initdir)" function
8918 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008921 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008923{
8924#ifdef FEAT_BROWSE
8925 char_u *title;
8926 char_u *initdir;
8927 char_u buf[NUMBUFLEN];
8928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008929 title = get_tv_string_chk(&argvars[0]);
8930 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 if (title == NULL || initdir == NULL)
8933 rettv->vval.v_string = NULL;
8934 else
8935 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008936 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941}
8942
Bram Moolenaar33570922005-01-25 22:26:29 +00008943static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945/*
8946 * Find a buffer by number or exact name.
8947 */
8948 static buf_T *
8949find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008950 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951{
8952 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008954 if (avar->v_type == VAR_NUMBER)
8955 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008956 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008958 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008959 if (buf == NULL)
8960 {
8961 /* No full path name match, try a match with a URL or a "nofile"
8962 * buffer, these don't use the full path. */
8963 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8964 if (buf->b_fname != NULL
8965 && (path_with_url(buf->b_fname)
8966#ifdef FEAT_QUICKFIX
8967 || bt_nofile(buf)
8968#endif
8969 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008970 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008971 break;
8972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 }
8974 return buf;
8975}
8976
8977/*
8978 * "bufexists(expr)" function
8979 */
8980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008982 typval_T *argvars;
8983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008985 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986}
8987
8988/*
8989 * "buflisted(expr)" function
8990 */
8991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008992f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008993 typval_T *argvars;
8994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
8996 buf_T *buf;
8997
8998 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008999 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002/*
9003 * "bufloaded(expr)" function
9004 */
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 buf_T *buf;
9011
9012 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014}
9015
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009016static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009017
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018/*
9019 * Get buffer by number or pattern.
9020 */
9021 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009022get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009023 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009024 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 int save_magic;
9028 char_u *save_cpo;
9029 buf_T *buf;
9030
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 if (tv->v_type == VAR_NUMBER)
9032 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009033 if (tv->v_type != VAR_STRING)
9034 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 if (name == NULL || *name == NUL)
9036 return curbuf;
9037 if (name[0] == '$' && name[1] == NUL)
9038 return lastbuf;
9039
9040 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9041 save_magic = p_magic;
9042 p_magic = TRUE;
9043 save_cpo = p_cpo;
9044 p_cpo = (char_u *)"";
9045
9046 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009047 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048
9049 p_magic = save_magic;
9050 p_cpo = save_cpo;
9051
9052 /* If not found, try expanding the name, like done for bufexists(). */
9053 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009054 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055
9056 return buf;
9057}
9058
9059/*
9060 * "bufname(expr)" function
9061 */
9062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009063f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 typval_T *argvars;
9065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
9067 buf_T *buf;
9068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009071 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 --emsg_off;
9078}
9079
9080/*
9081 * "bufnr(expr)" function
9082 */
9083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 typval_T *argvars;
9086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
9088 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009089 int error = FALSE;
9090 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009092 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009094 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009095 --emsg_off;
9096
9097 /* If the buffer isn't found and the second argument is not zero create a
9098 * new buffer. */
9099 if (buf == NULL
9100 && argvars[1].v_type != VAR_UNKNOWN
9101 && get_tv_number_chk(&argvars[1], &error) != 0
9102 && !error
9103 && (name = get_tv_string_chk(&argvars[0])) != NULL
9104 && !error)
9105 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9106
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
9112
9113/*
9114 * "bufwinnr(nr)" function
9115 */
9116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 typval_T *argvars;
9119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121#ifdef FEAT_WINDOWS
9122 win_T *wp;
9123 int winnr = 0;
9124#endif
9125 buf_T *buf;
9126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009129 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130#ifdef FEAT_WINDOWS
9131 for (wp = firstwin; wp; wp = wp->w_next)
9132 {
9133 ++winnr;
9134 if (wp->w_buffer == buf)
9135 break;
9136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009139 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#endif
9141 --emsg_off;
9142}
9143
9144/*
9145 * "byte2line(byte)" function
9146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009149 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
9152#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#else
9155 long boff = 0;
9156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 (linenr_T)0, &boff);
9163#endif
9164}
9165
9166/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167 * "byteidx()" function
9168 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009171 typval_T *argvars;
9172 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009173{
9174#ifdef FEAT_MBYTE
9175 char_u *t;
9176#endif
9177 char_u *str;
9178 long idx;
9179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 str = get_tv_string_chk(&argvars[0]);
9181 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009183 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009184 return;
9185
9186#ifdef FEAT_MBYTE
9187 t = str;
9188 for ( ; idx > 0; idx--)
9189 {
9190 if (*t == NUL) /* EOL reached */
9191 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009192 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009193 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009194 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009195#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009196 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198#endif
9199}
9200
Bram Moolenaardb913952012-06-29 12:54:53 +02009201 int
9202func_call(name, args, selfdict, rettv)
9203 char_u *name;
9204 typval_T *args;
9205 dict_T *selfdict;
9206 typval_T *rettv;
9207{
9208 listitem_T *item;
9209 typval_T argv[MAX_FUNC_ARGS + 1];
9210 int argc = 0;
9211 int dummy;
9212 int r = 0;
9213
9214 for (item = args->vval.v_list->lv_first; item != NULL;
9215 item = item->li_next)
9216 {
9217 if (argc == MAX_FUNC_ARGS)
9218 {
9219 EMSG(_("E699: Too many arguments"));
9220 break;
9221 }
9222 /* Make a copy of each argument. This is needed to be able to set
9223 * v_lock to VAR_FIXED in the copy without changing the original list.
9224 */
9225 copy_tv(&item->li_tv, &argv[argc++]);
9226 }
9227
9228 if (item == NULL)
9229 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9230 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9231 &dummy, TRUE, selfdict);
9232
9233 /* Free the arguments. */
9234 while (argc > 0)
9235 clear_tv(&argv[--argc]);
9236
9237 return r;
9238}
9239
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009240/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009241 * "call(func, arglist)" function
9242 */
9243 static void
9244f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009245 typval_T *argvars;
9246 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009247{
9248 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009249 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009250
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009251 if (argvars[1].v_type != VAR_LIST)
9252 {
9253 EMSG(_(e_listreq));
9254 return;
9255 }
9256 if (argvars[1].vval.v_list == NULL)
9257 return;
9258
9259 if (argvars[0].v_type == VAR_FUNC)
9260 func = argvars[0].vval.v_string;
9261 else
9262 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 if (*func == NUL)
9264 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009265
Bram Moolenaare9a41262005-01-15 22:18:47 +00009266 if (argvars[2].v_type != VAR_UNKNOWN)
9267 {
9268 if (argvars[2].v_type != VAR_DICT)
9269 {
9270 EMSG(_(e_dictreq));
9271 return;
9272 }
9273 selfdict = argvars[2].vval.v_dict;
9274 }
9275
Bram Moolenaardb913952012-06-29 12:54:53 +02009276 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009277}
9278
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009279#ifdef FEAT_FLOAT
9280/*
9281 * "ceil({float})" function
9282 */
9283 static void
9284f_ceil(argvars, rettv)
9285 typval_T *argvars;
9286 typval_T *rettv;
9287{
9288 float_T f;
9289
9290 rettv->v_type = VAR_FLOAT;
9291 if (get_float_arg(argvars, &f) == OK)
9292 rettv->vval.v_float = ceil(f);
9293 else
9294 rettv->vval.v_float = 0.0;
9295}
9296#endif
9297
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009298/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009299 * "changenr()" function
9300 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009301 static void
9302f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009303 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009304 typval_T *rettv;
9305{
9306 rettv->vval.v_number = curbuf->b_u_seq_cur;
9307}
9308
9309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 * "char2nr(string)" function
9311 */
9312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009314 typval_T *argvars;
9315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316{
9317#ifdef FEAT_MBYTE
9318 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009319 {
9320 int utf8 = 0;
9321
9322 if (argvars[1].v_type != VAR_UNKNOWN)
9323 utf8 = get_tv_number_chk(&argvars[1], NULL);
9324
9325 if (utf8)
9326 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9327 else
9328 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 else
9331#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333}
9334
9335/*
9336 * "cindent(lnum)" function
9337 */
9338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009340 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
9343#ifdef FEAT_CINDENT
9344 pos_T pos;
9345 linenr_T lnum;
9346
9347 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9350 {
9351 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 curwin->w_cursor = pos;
9354 }
9355 else
9356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358}
9359
9360/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009361 * "clearmatches()" function
9362 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009363 static void
9364f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009365 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009366 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009367{
9368#ifdef FEAT_SEARCH_EXTRA
9369 clear_matches(curwin);
9370#endif
9371}
9372
9373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 * "col(string)" function
9375 */
9376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009378 typval_T *argvars;
9379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380{
9381 colnr_T col = 0;
9382 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009383 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009385 fp = var2fpos(&argvars[0], FALSE, &fnum);
9386 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 {
9388 if (fp->col == MAXCOL)
9389 {
9390 /* '> can be MAXCOL, get the length of the line then */
9391 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009392 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 else
9394 col = MAXCOL;
9395 }
9396 else
9397 {
9398 col = fp->col + 1;
9399#ifdef FEAT_VIRTUALEDIT
9400 /* col(".") when the cursor is on the NUL at the end of the line
9401 * because of "coladd" can be seen as an extra column. */
9402 if (virtual_active() && fp == &curwin->w_cursor)
9403 {
9404 char_u *p = ml_get_cursor();
9405
9406 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9407 curwin->w_virtcol - curwin->w_cursor.coladd))
9408 {
9409# ifdef FEAT_MBYTE
9410 int l;
9411
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009412 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 col += l;
9414# else
9415 if (*p != NUL && p[1] == NUL)
9416 ++col;
9417# endif
9418 }
9419 }
9420#endif
9421 }
9422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425
Bram Moolenaar572cb562005-08-05 21:35:02 +00009426#if defined(FEAT_INS_EXPAND)
9427/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009428 * "complete()" function
9429 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009430 static void
9431f_complete(argvars, rettv)
9432 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009433 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009434{
9435 int startcol;
9436
9437 if ((State & INSERT) == 0)
9438 {
9439 EMSG(_("E785: complete() can only be used in Insert mode"));
9440 return;
9441 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009442
9443 /* Check for undo allowed here, because if something was already inserted
9444 * the line was already saved for undo and this check isn't done. */
9445 if (!undo_allowed())
9446 return;
9447
Bram Moolenaarade00832006-03-10 21:46:58 +00009448 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9449 {
9450 EMSG(_(e_invarg));
9451 return;
9452 }
9453
9454 startcol = get_tv_number_chk(&argvars[0], NULL);
9455 if (startcol <= 0)
9456 return;
9457
9458 set_completion(startcol - 1, argvars[1].vval.v_list);
9459}
9460
9461/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009462 * "complete_add()" function
9463 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009464 static void
9465f_complete_add(argvars, rettv)
9466 typval_T *argvars;
9467 typval_T *rettv;
9468{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009469 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009470}
9471
9472/*
9473 * "complete_check()" function
9474 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475 static void
9476f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009477 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009478 typval_T *rettv;
9479{
9480 int saved = RedrawingDisabled;
9481
9482 RedrawingDisabled = 0;
9483 ins_compl_check_keys(0);
9484 rettv->vval.v_number = compl_interrupted;
9485 RedrawingDisabled = saved;
9486}
9487#endif
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489/*
9490 * "confirm(message, buttons[, default [, type]])" function
9491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009494 typval_T *argvars UNUSED;
9495 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9498 char_u *message;
9499 char_u *buttons = NULL;
9500 char_u buf[NUMBUFLEN];
9501 char_u buf2[NUMBUFLEN];
9502 int def = 1;
9503 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009504 char_u *typestr;
9505 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009507 message = get_tv_string_chk(&argvars[0]);
9508 if (message == NULL)
9509 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9513 if (buttons == NULL)
9514 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009518 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9521 if (typestr == NULL)
9522 error = TRUE;
9523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 switch (TOUPPER_ASC(*typestr))
9526 {
9527 case 'E': type = VIM_ERROR; break;
9528 case 'Q': type = VIM_QUESTION; break;
9529 case 'I': type = VIM_INFO; break;
9530 case 'W': type = VIM_WARNING; break;
9531 case 'G': type = VIM_GENERIC; break;
9532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 }
9534 }
9535 }
9536 }
9537
9538 if (buttons == NULL || *buttons == NUL)
9539 buttons = (char_u *)_("&Ok");
9540
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009541 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009543 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544#endif
9545}
9546
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009547/*
9548 * "copy()" function
9549 */
9550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009552 typval_T *argvars;
9553 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009554{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009555 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009556}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009558#ifdef FEAT_FLOAT
9559/*
9560 * "cos()" function
9561 */
9562 static void
9563f_cos(argvars, rettv)
9564 typval_T *argvars;
9565 typval_T *rettv;
9566{
9567 float_T f;
9568
9569 rettv->v_type = VAR_FLOAT;
9570 if (get_float_arg(argvars, &f) == OK)
9571 rettv->vval.v_float = cos(f);
9572 else
9573 rettv->vval.v_float = 0.0;
9574}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009575
9576/*
9577 * "cosh()" function
9578 */
9579 static void
9580f_cosh(argvars, rettv)
9581 typval_T *argvars;
9582 typval_T *rettv;
9583{
9584 float_T f;
9585
9586 rettv->v_type = VAR_FLOAT;
9587 if (get_float_arg(argvars, &f) == OK)
9588 rettv->vval.v_float = cosh(f);
9589 else
9590 rettv->vval.v_float = 0.0;
9591}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009592#endif
9593
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009595 * "count()" function
9596 */
9597 static void
9598f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009599 typval_T *argvars;
9600 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009601{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009602 long n = 0;
9603 int ic = FALSE;
9604
Bram Moolenaare9a41262005-01-15 22:18:47 +00009605 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009607 listitem_T *li;
9608 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009610
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 if ((l = argvars[0].vval.v_list) != NULL)
9612 {
9613 li = l->lv_first;
9614 if (argvars[2].v_type != VAR_UNKNOWN)
9615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 int error = FALSE;
9617
9618 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 if (argvars[3].v_type != VAR_UNKNOWN)
9620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 idx = get_tv_number_chk(&argvars[3], &error);
9622 if (!error)
9623 {
9624 li = list_find(l, idx);
9625 if (li == NULL)
9626 EMSGN(_(e_listidx), idx);
9627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 if (error)
9630 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009631 }
9632
9633 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009634 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635 ++n;
9636 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009638 else if (argvars[0].v_type == VAR_DICT)
9639 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009640 int todo;
9641 dict_T *d;
9642 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009644 if ((d = argvars[0].vval.v_dict) != NULL)
9645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 int error = FALSE;
9647
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 if (argvars[2].v_type != VAR_UNKNOWN)
9649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009650 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 if (argvars[3].v_type != VAR_UNKNOWN)
9652 EMSG(_(e_invarg));
9653 }
9654
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009655 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009656 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009657 {
9658 if (!HASHITEM_EMPTY(hi))
9659 {
9660 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009661 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009662 ++n;
9663 }
9664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009665 }
9666 }
9667 else
9668 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009669 rettv->vval.v_number = n;
9670}
9671
9672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9674 *
9675 * Checks the existence of a cscope connection.
9676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009679 typval_T *argvars UNUSED;
9680 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682#ifdef FEAT_CSCOPE
9683 int num = 0;
9684 char_u *dbpath = NULL;
9685 char_u *prepend = NULL;
9686 char_u buf[NUMBUFLEN];
9687
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009688 if (argvars[0].v_type != VAR_UNKNOWN
9689 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691 num = (int)get_tv_number(&argvars[0]);
9692 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 }
9696
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698#endif
9699}
9700
9701/*
9702 * "cursor(lnum, col)" function
9703 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009704 * Moves the cursor to the specified line and column.
9705 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *argvars;
9710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009713#ifdef FEAT_VIRTUALEDIT
9714 long coladd = 0;
9715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009717 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009718 if (argvars[1].v_type == VAR_UNKNOWN)
9719 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009721
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009722 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009723 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009724 line = pos.lnum;
9725 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009728#endif
9729 }
9730 else
9731 {
9732 line = get_tv_lnum(argvars);
9733 col = get_tv_number_chk(&argvars[1], NULL);
9734#ifdef FEAT_VIRTUALEDIT
9735 if (argvars[2].v_type != VAR_UNKNOWN)
9736 coladd = get_tv_number_chk(&argvars[2], NULL);
9737#endif
9738 }
9739 if (line < 0 || col < 0
9740#ifdef FEAT_VIRTUALEDIT
9741 || coladd < 0
9742#endif
9743 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (line > 0)
9746 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 if (col > 0)
9748 curwin->w_cursor.col = col - 1;
9749#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009750 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751#endif
9752
9753 /* Make sure the cursor is in a valid position. */
9754 check_cursor();
9755#ifdef FEAT_MBYTE
9756 /* Correct cursor for multi-byte character. */
9757 if (has_mbyte)
9758 mb_adjust_cursor();
9759#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009760
9761 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009762 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009766 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 */
9768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009770 typval_T *argvars;
9771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009773 int noref = 0;
9774
9775 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009776 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009777 if (noref < 0 || noref > 1)
9778 EMSG(_(e_invarg));
9779 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009780 {
9781 current_copyID += COPYID_INC;
9782 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784}
9785
9786/*
9787 * "delete()" function
9788 */
9789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009791 typval_T *argvars;
9792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * "did_filetype()" function
9802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009805 typval_T *argvars UNUSED;
9806 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#endif
9811}
9812
9813/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009814 * "diff_filler()" function
9815 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009820{
9821#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009823#endif
9824}
9825
9826/*
9827 * "diff_hlID()" function
9828 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009831 typval_T *argvars UNUSED;
9832 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009833{
9834#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009836 static linenr_T prev_lnum = 0;
9837 static int changedtick = 0;
9838 static int fnum = 0;
9839 static int change_start = 0;
9840 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009841 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 int filler_lines;
9843 int col;
9844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009845 if (lnum < 0) /* ignore type error in {lnum} arg */
9846 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847 if (lnum != prev_lnum
9848 || changedtick != curbuf->b_changedtick
9849 || fnum != curbuf->b_fnum)
9850 {
9851 /* New line, buffer, change: need to get the values. */
9852 filler_lines = diff_check(curwin, lnum);
9853 if (filler_lines < 0)
9854 {
9855 if (filler_lines == -1)
9856 {
9857 change_start = MAXCOL;
9858 change_end = -1;
9859 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9860 hlID = HLF_ADD; /* added line */
9861 else
9862 hlID = HLF_CHD; /* changed line */
9863 }
9864 else
9865 hlID = HLF_ADD; /* added line */
9866 }
9867 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009868 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009869 prev_lnum = lnum;
9870 changedtick = curbuf->b_changedtick;
9871 fnum = curbuf->b_fnum;
9872 }
9873
9874 if (hlID == HLF_CHD || hlID == HLF_TXD)
9875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009877 if (col >= change_start && col <= change_end)
9878 hlID = HLF_TXD; /* changed text */
9879 else
9880 hlID = HLF_CHD; /* changed line */
9881 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009882 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883#endif
9884}
9885
9886/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009887 * "empty({expr})" function
9888 */
9889 static void
9890f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009891 typval_T *argvars;
9892 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009893{
9894 int n;
9895
9896 switch (argvars[0].v_type)
9897 {
9898 case VAR_STRING:
9899 case VAR_FUNC:
9900 n = argvars[0].vval.v_string == NULL
9901 || *argvars[0].vval.v_string == NUL;
9902 break;
9903 case VAR_NUMBER:
9904 n = argvars[0].vval.v_number == 0;
9905 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009906#ifdef FEAT_FLOAT
9907 case VAR_FLOAT:
9908 n = argvars[0].vval.v_float == 0.0;
9909 break;
9910#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009911 case VAR_LIST:
9912 n = argvars[0].vval.v_list == NULL
9913 || argvars[0].vval.v_list->lv_first == NULL;
9914 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009915 case VAR_DICT:
9916 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009917 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009919 default:
9920 EMSG2(_(e_intern2), "f_empty()");
9921 n = 0;
9922 }
9923
9924 rettv->vval.v_number = n;
9925}
9926
9927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 * "escape({string}, {chars})" function
9929 */
9930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009932 typval_T *argvars;
9933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
9935 char_u buf[NUMBUFLEN];
9936
Bram Moolenaar758711c2005-02-02 23:11:38 +00009937 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9938 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940}
9941
9942/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009943 * "eval()" function
9944 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009945 static void
9946f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009947 typval_T *argvars;
9948 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009949{
9950 char_u *s;
9951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 s = get_tv_string_chk(&argvars[0]);
9953 if (s != NULL)
9954 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9957 {
9958 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009960 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009961 else if (*s != NUL)
9962 EMSG(_(e_trailing));
9963}
9964
9965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 * "eventhandler()" function
9967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009969f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009970 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009973 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974}
9975
9976/*
9977 * "executable()" function
9978 */
9979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009981 typval_T *argvars;
9982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009984 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985}
9986
9987/*
9988 * "exists()" function
9989 */
9990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009992 typval_T *argvars;
9993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994{
9995 char_u *p;
9996 char_u *name;
9997 int n = FALSE;
9998 int len = 0;
9999
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010000 no_autoload = TRUE;
10001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010002 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 if (*p == '$') /* environment variable */
10004 {
10005 /* first try "normal" environment variables (fast) */
10006 if (mch_getenv(p + 1) != NULL)
10007 n = TRUE;
10008 else
10009 {
10010 /* try expanding things like $VIM and ${HOME} */
10011 p = expand_env_save(p);
10012 if (p != NULL && *p != '$')
10013 n = TRUE;
10014 vim_free(p);
10015 }
10016 }
10017 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010018 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010020 if (*skipwhite(p) != NUL)
10021 n = FALSE; /* trailing garbage */
10022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 else if (*p == '*') /* internal or user defined function */
10024 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010025 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 }
10027 else if (*p == ':')
10028 {
10029 n = cmd_exists(p + 1);
10030 }
10031 else if (*p == '#')
10032 {
10033#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010034 if (p[1] == '#')
10035 n = autocmd_supported(p + 2);
10036 else
10037 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038#endif
10039 }
10040 else /* internal variable */
10041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010042 char_u *tofree;
10043 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010045 /* get_name_len() takes care of expanding curly braces */
10046 name = p;
10047 len = get_name_len(&p, &tofree, TRUE, FALSE);
10048 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010050 if (tofree != NULL)
10051 name = tofree;
10052 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10053 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 /* handle d.key, l[idx], f(expr) */
10056 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10057 if (n)
10058 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 }
10060 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010061 if (*p != NUL)
10062 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010064 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 }
10066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010068
10069 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070}
10071
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010072#ifdef FEAT_FLOAT
10073/*
10074 * "exp()" function
10075 */
10076 static void
10077f_exp(argvars, rettv)
10078 typval_T *argvars;
10079 typval_T *rettv;
10080{
10081 float_T f;
10082
10083 rettv->v_type = VAR_FLOAT;
10084 if (get_float_arg(argvars, &f) == OK)
10085 rettv->vval.v_float = exp(f);
10086 else
10087 rettv->vval.v_float = 0.0;
10088}
10089#endif
10090
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091/*
10092 * "expand()" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010096 typval_T *argvars;
10097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
10099 char_u *s;
10100 int len;
10101 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010102 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010104 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010105 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010108 if (argvars[1].v_type != VAR_UNKNOWN
10109 && argvars[2].v_type != VAR_UNKNOWN
10110 && get_tv_number_chk(&argvars[2], &error)
10111 && !error)
10112 {
10113 rettv->v_type = VAR_LIST;
10114 rettv->vval.v_list = NULL;
10115 }
10116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 if (*s == '%' || *s == '#' || *s == '<')
10119 {
10120 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010121 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010123 if (rettv->v_type == VAR_LIST)
10124 {
10125 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10126 list_append_string(rettv->vval.v_list, result, -1);
10127 else
10128 vim_free(result);
10129 }
10130 else
10131 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 else
10134 {
10135 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010136 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 if (argvars[1].v_type != VAR_UNKNOWN
10138 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010139 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 if (!error)
10141 {
10142 ExpandInit(&xpc);
10143 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010144 if (p_wic)
10145 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010146 if (rettv->v_type == VAR_STRING)
10147 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10148 options, WILD_ALL);
10149 else if (rettv_list_alloc(rettv) != FAIL)
10150 {
10151 int i;
10152
10153 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10154 for (i = 0; i < xpc.xp_numfiles; i++)
10155 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10156 ExpandCleanup(&xpc);
10157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 }
10159 else
10160 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 }
10162}
10163
10164/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010165 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010166 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010167 */
10168 static void
10169f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010170 typval_T *argvars;
10171 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010172{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010173 char *arg_errmsg = N_("extend() argument");
10174
Bram Moolenaare9a41262005-01-15 22:18:47 +000010175 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010176 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010177 list_T *l1, *l2;
10178 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010180 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010181
Bram Moolenaare9a41262005-01-15 22:18:47 +000010182 l1 = argvars[0].vval.v_list;
10183 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010184 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010185 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010186 {
10187 if (argvars[2].v_type != VAR_UNKNOWN)
10188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010189 before = get_tv_number_chk(&argvars[2], &error);
10190 if (error)
10191 return; /* type error; errmsg already given */
10192
Bram Moolenaar758711c2005-02-02 23:11:38 +000010193 if (before == l1->lv_len)
10194 item = NULL;
10195 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010196 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010197 item = list_find(l1, before);
10198 if (item == NULL)
10199 {
10200 EMSGN(_(e_listidx), before);
10201 return;
10202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 }
10204 }
10205 else
10206 item = NULL;
10207 list_extend(l1, l2, item);
10208
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 copy_tv(&argvars[0], rettv);
10210 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010211 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10213 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010214 dict_T *d1, *d2;
10215 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 char_u *action;
10217 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010219 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010220
10221 d1 = argvars[0].vval.v_dict;
10222 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010223 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010224 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010225 {
10226 /* Check the third argument. */
10227 if (argvars[2].v_type != VAR_UNKNOWN)
10228 {
10229 static char *(av[]) = {"keep", "force", "error"};
10230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 action = get_tv_string_chk(&argvars[2]);
10232 if (action == NULL)
10233 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010234 for (i = 0; i < 3; ++i)
10235 if (STRCMP(action, av[i]) == 0)
10236 break;
10237 if (i == 3)
10238 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010239 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 return;
10241 }
10242 }
10243 else
10244 action = (char_u *)"force";
10245
10246 /* Go over all entries in the second dict and add them to the
10247 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010248 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010253 --todo;
10254 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010255 if (d1->dv_scope != 0)
10256 {
10257 /* Disallow replacing a builtin function in l: and g:.
10258 * Check the key to be valid when adding to any
10259 * scope. */
10260 if (d1->dv_scope == VAR_DEF_SCOPE
10261 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10262 && var_check_func_name(hi2->hi_key,
10263 di1 == NULL))
10264 break;
10265 if (!valid_varname(hi2->hi_key))
10266 break;
10267 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010268 if (di1 == NULL)
10269 {
10270 di1 = dictitem_copy(HI2DI(hi2));
10271 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10272 dictitem_free(di1);
10273 }
10274 else if (*action == 'e')
10275 {
10276 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10277 break;
10278 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010279 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010280 {
10281 clear_tv(&di1->di_tv);
10282 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 }
10285 }
10286
Bram Moolenaare9a41262005-01-15 22:18:47 +000010287 copy_tv(&argvars[0], rettv);
10288 }
10289 }
10290 else
10291 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010292}
10293
10294/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010295 * "feedkeys()" function
10296 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010297 static void
10298f_feedkeys(argvars, rettv)
10299 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010300 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010301{
10302 int remap = TRUE;
10303 char_u *keys, *flags;
10304 char_u nbuf[NUMBUFLEN];
10305 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010306 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010307
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010308 /* This is not allowed in the sandbox. If the commands would still be
10309 * executed in the sandbox it would be OK, but it probably happens later,
10310 * when "sandbox" is no longer set. */
10311 if (check_secure())
10312 return;
10313
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010314 keys = get_tv_string(&argvars[0]);
10315 if (*keys != NUL)
10316 {
10317 if (argvars[1].v_type != VAR_UNKNOWN)
10318 {
10319 flags = get_tv_string_buf(&argvars[1], nbuf);
10320 for ( ; *flags != NUL; ++flags)
10321 {
10322 switch (*flags)
10323 {
10324 case 'n': remap = FALSE; break;
10325 case 'm': remap = TRUE; break;
10326 case 't': typed = TRUE; break;
10327 }
10328 }
10329 }
10330
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010331 /* Need to escape K_SPECIAL and CSI before putting the string in the
10332 * typeahead buffer. */
10333 keys_esc = vim_strsave_escape_csi(keys);
10334 if (keys_esc != NULL)
10335 {
10336 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010337 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010338 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010339 if (vgetc_busy)
10340 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010341 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010342 }
10343}
10344
10345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 * "filereadable()" function
10347 */
10348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010349f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010350 typval_T *argvars;
10351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010353 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 char_u *p;
10355 int n;
10356
Bram Moolenaarc236c162008-07-13 17:41:49 +000010357#ifndef O_NONBLOCK
10358# define O_NONBLOCK 0
10359#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010360 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010361 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10362 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
10364 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010365 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 }
10367 else
10368 n = FALSE;
10369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371}
10372
10373/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010374 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 * rights to write into.
10376 */
10377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010379 typval_T *argvars;
10380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010382 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383}
10384
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010385static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010386
10387 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010388findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010389 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010390 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010391 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010392{
10393#ifdef FEAT_SEARCHPATH
10394 char_u *fname;
10395 char_u *fresult = NULL;
10396 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10397 char_u *p;
10398 char_u pathbuf[NUMBUFLEN];
10399 int count = 1;
10400 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010401 int error = FALSE;
10402#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010403
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010404 rettv->vval.v_string = NULL;
10405 rettv->v_type = VAR_STRING;
10406
10407#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010409
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010410 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10413 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010414 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010415 else
10416 {
10417 if (*p != NUL)
10418 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010421 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010423 }
10424
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010425 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10426 error = TRUE;
10427
10428 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 do
10431 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010432 if (rettv->v_type == VAR_STRING)
10433 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 fresult = find_file_in_path_option(first ? fname : NULL,
10435 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010436 0, first, path,
10437 find_what,
10438 curbuf->b_ffname,
10439 find_what == FINDFILE_DIR
10440 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010442
10443 if (fresult != NULL && rettv->v_type == VAR_LIST)
10444 list_append_string(rettv->vval.v_list, fresult, -1);
10445
10446 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010448
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010449 if (rettv->v_type == VAR_STRING)
10450 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010451#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010452}
10453
Bram Moolenaar33570922005-01-25 22:26:29 +000010454static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10455static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010456
10457/*
10458 * Implementation of map() and filter().
10459 */
10460 static void
10461filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010462 typval_T *argvars;
10463 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010464 int map;
10465{
10466 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010467 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 listitem_T *li, *nli;
10469 list_T *l = NULL;
10470 dictitem_T *di;
10471 hashtab_T *ht;
10472 hashitem_T *hi;
10473 dict_T *d = NULL;
10474 typval_T save_val;
10475 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010477 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010478 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10479 char *arg_errmsg = (map ? N_("map() argument")
10480 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010481 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010482 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483
Bram Moolenaare9a41262005-01-15 22:18:47 +000010484 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010485 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010486 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010487 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010488 return;
10489 }
10490 else if (argvars[0].v_type == VAR_DICT)
10491 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010492 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010493 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 return;
10495 }
10496 else
10497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010498 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010499 return;
10500 }
10501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 expr = get_tv_string_buf_chk(&argvars[1], buf);
10503 /* On type errors, the preceding call has already displayed an error
10504 * message. Avoid a misleading error message for an empty string that
10505 * was not passed as argument. */
10506 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 prepare_vimvar(VV_VAL, &save_val);
10509 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010510
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010511 /* We reset "did_emsg" to be able to detect whether an error
10512 * occurred during evaluation of the expression. */
10513 save_did_emsg = did_emsg;
10514 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010515
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010516 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010518 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010519 vimvars[VV_KEY].vv_type = VAR_STRING;
10520
10521 ht = &d->dv_hashtab;
10522 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010523 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010526 if (!HASHITEM_EMPTY(hi))
10527 {
10528 --todo;
10529 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010530 if (tv_check_lock(di->di_tv.v_lock,
10531 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 break;
10533 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010534 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010535 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 break;
10537 if (!map && rem)
10538 dictitem_remove(d, di);
10539 clear_tv(&vimvars[VV_KEY].vv_tv);
10540 }
10541 }
10542 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 }
10544 else
10545 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010546 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010548 for (li = l->lv_first; li != NULL; li = nli)
10549 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010550 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010551 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010553 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010554 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010555 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010556 break;
10557 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010559 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010560 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010561 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010562
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010563 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010565
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010566 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010567 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010568
10569 copy_tv(&argvars[0], rettv);
10570}
10571
10572 static int
10573filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010574 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575 char_u *expr;
10576 int map;
10577 int *remp;
10578{
Bram Moolenaar33570922005-01-25 22:26:29 +000010579 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010581 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010584 s = expr;
10585 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010586 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 if (*s != NUL) /* check for trailing chars after expr */
10588 {
10589 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010590 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 }
10592 if (map)
10593 {
10594 /* map(): replace the list item value */
10595 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010596 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 *tv = rettv;
10598 }
10599 else
10600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 int error = FALSE;
10602
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 /* On type error, nothing has been removed; return FAIL to stop the
10607 * loop. The error message was given by get_tv_number_chk(). */
10608 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010611 retval = OK;
10612theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010613 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010614 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010615}
10616
10617/*
10618 * "filter()" function
10619 */
10620 static void
10621f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010624{
10625 filter_map(argvars, rettv, FALSE);
10626}
10627
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010628/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010629 * "finddir({fname}[, {path}[, {count}]])" function
10630 */
10631 static void
10632f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010633 typval_T *argvars;
10634 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010636 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010637}
10638
10639/*
10640 * "findfile({fname}[, {path}[, {count}]])" function
10641 */
10642 static void
10643f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010644 typval_T *argvars;
10645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010646{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010647 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010648}
10649
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010650#ifdef FEAT_FLOAT
10651/*
10652 * "float2nr({float})" function
10653 */
10654 static void
10655f_float2nr(argvars, rettv)
10656 typval_T *argvars;
10657 typval_T *rettv;
10658{
10659 float_T f;
10660
10661 if (get_float_arg(argvars, &f) == OK)
10662 {
10663 if (f < -0x7fffffff)
10664 rettv->vval.v_number = -0x7fffffff;
10665 else if (f > 0x7fffffff)
10666 rettv->vval.v_number = 0x7fffffff;
10667 else
10668 rettv->vval.v_number = (varnumber_T)f;
10669 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010670}
10671
10672/*
10673 * "floor({float})" function
10674 */
10675 static void
10676f_floor(argvars, rettv)
10677 typval_T *argvars;
10678 typval_T *rettv;
10679{
10680 float_T f;
10681
10682 rettv->v_type = VAR_FLOAT;
10683 if (get_float_arg(argvars, &f) == OK)
10684 rettv->vval.v_float = floor(f);
10685 else
10686 rettv->vval.v_float = 0.0;
10687}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010688
10689/*
10690 * "fmod()" function
10691 */
10692 static void
10693f_fmod(argvars, rettv)
10694 typval_T *argvars;
10695 typval_T *rettv;
10696{
10697 float_T fx, fy;
10698
10699 rettv->v_type = VAR_FLOAT;
10700 if (get_float_arg(argvars, &fx) == OK
10701 && get_float_arg(&argvars[1], &fy) == OK)
10702 rettv->vval.v_float = fmod(fx, fy);
10703 else
10704 rettv->vval.v_float = 0.0;
10705}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010706#endif
10707
Bram Moolenaar0d660222005-01-07 21:51:51 +000010708/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010709 * "fnameescape({string})" function
10710 */
10711 static void
10712f_fnameescape(argvars, rettv)
10713 typval_T *argvars;
10714 typval_T *rettv;
10715{
10716 rettv->vval.v_string = vim_strsave_fnameescape(
10717 get_tv_string(&argvars[0]), FALSE);
10718 rettv->v_type = VAR_STRING;
10719}
10720
10721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 * "fnamemodify({fname}, {mods})" function
10723 */
10724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010726 typval_T *argvars;
10727 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728{
10729 char_u *fname;
10730 char_u *mods;
10731 int usedlen = 0;
10732 int len;
10733 char_u *fbuf = NULL;
10734 char_u buf[NUMBUFLEN];
10735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 fname = get_tv_string_chk(&argvars[0]);
10737 mods = get_tv_string_buf_chk(&argvars[1], buf);
10738 if (fname == NULL || mods == NULL)
10739 fname = NULL;
10740 else
10741 {
10742 len = (int)STRLEN(fname);
10743 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 vim_free(fbuf);
10752}
10753
Bram Moolenaar33570922005-01-25 22:26:29 +000010754static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755
10756/*
10757 * "foldclosed()" function
10758 */
10759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010760foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010761 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010763 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764{
10765#ifdef FEAT_FOLDING
10766 linenr_T lnum;
10767 linenr_T first, last;
10768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10771 {
10772 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10773 {
10774 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 return;
10779 }
10780 }
10781#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783}
10784
10785/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010786 * "foldclosed()" function
10787 */
10788 static void
10789f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *argvars;
10791 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792{
10793 foldclosed_both(argvars, rettv, FALSE);
10794}
10795
10796/*
10797 * "foldclosedend()" function
10798 */
10799 static void
10800f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010801 typval_T *argvars;
10802 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010803{
10804 foldclosed_both(argvars, rettv, TRUE);
10805}
10806
10807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 * "foldlevel()" function
10809 */
10810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010811f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010812 typval_T *argvars UNUSED;
10813 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814{
10815#ifdef FEAT_FOLDING
10816 linenr_T lnum;
10817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010820 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822}
10823
10824/*
10825 * "foldtext()" function
10826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010829 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832#ifdef FEAT_FOLDING
10833 linenr_T lnum;
10834 char_u *s;
10835 char_u *r;
10836 int len;
10837 char *txt;
10838#endif
10839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->v_type = VAR_STRING;
10841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010843 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10844 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10845 <= curbuf->b_ml.ml_line_count
10846 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 {
10848 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10850 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 {
10852 if (!linewhite(lnum))
10853 break;
10854 ++lnum;
10855 }
10856
10857 /* Find interesting text in this line. */
10858 s = skipwhite(ml_get(lnum));
10859 /* skip C comment-start */
10860 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010863 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010865 {
10866 s = skipwhite(ml_get(lnum + 1));
10867 if (*s == '*')
10868 s = skipwhite(s + 1);
10869 }
10870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 txt = _("+-%s%3ld lines: ");
10872 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010873 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 + 20 /* for %3ld */
10875 + STRLEN(s))); /* concatenated */
10876 if (r != NULL)
10877 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010878 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10879 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10880 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 len = (int)STRLEN(r);
10882 STRCAT(r, s);
10883 /* remove 'foldmarker' and 'commentstring' */
10884 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 }
10887 }
10888#endif
10889}
10890
10891/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010892 * "foldtextresult(lnum)" function
10893 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010895f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010897 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010898{
10899#ifdef FEAT_FOLDING
10900 linenr_T lnum;
10901 char_u *text;
10902 char_u buf[51];
10903 foldinfo_T foldinfo;
10904 int fold_count;
10905#endif
10906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010907 rettv->v_type = VAR_STRING;
10908 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010909#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 /* treat illegal types and illegal string values for {lnum} the same */
10912 if (lnum < 0)
10913 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010914 fold_count = foldedCount(curwin, lnum, &foldinfo);
10915 if (fold_count > 0)
10916 {
10917 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10918 &foldinfo, buf);
10919 if (text == buf)
10920 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010922 }
10923#endif
10924}
10925
10926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 * "foreground()" function
10928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010931 typval_T *argvars UNUSED;
10932 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#ifdef FEAT_GUI
10935 if (gui.in_use)
10936 gui_mch_set_foreground();
10937#else
10938# ifdef WIN32
10939 win32_set_foreground();
10940# endif
10941#endif
10942}
10943
10944/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010945 * "function()" function
10946 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010949 typval_T *argvars;
10950 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951{
10952 char_u *s;
10953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010955 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010956 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010957 /* Don't check an autoload name for existence here. */
10958 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010959 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010960 else
10961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010962 rettv->vval.v_string = vim_strsave(s);
10963 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 }
10965}
10966
10967/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010968 * "garbagecollect()" function
10969 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010970 static void
10971f_garbagecollect(argvars, rettv)
10972 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010973 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010974{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010975 /* This is postponed until we are back at the toplevel, because we may be
10976 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10977 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010978
10979 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10980 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010981}
10982
10983/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010984 * "get()" function
10985 */
10986 static void
10987f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010988 typval_T *argvars;
10989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010990{
Bram Moolenaar33570922005-01-25 22:26:29 +000010991 listitem_T *li;
10992 list_T *l;
10993 dictitem_T *di;
10994 dict_T *d;
10995 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010996
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010998 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010999 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011000 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011001 int error = FALSE;
11002
11003 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11004 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011005 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011006 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011008 else if (argvars[0].v_type == VAR_DICT)
11009 {
11010 if ((d = argvars[0].vval.v_dict) != NULL)
11011 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011012 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011013 if (di != NULL)
11014 tv = &di->di_tv;
11015 }
11016 }
11017 else
11018 EMSG2(_(e_listdictarg), "get()");
11019
11020 if (tv == NULL)
11021 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011022 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011023 copy_tv(&argvars[2], rettv);
11024 }
11025 else
11026 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011027}
11028
Bram Moolenaar342337a2005-07-21 21:11:17 +000011029static 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 +000011030
11031/*
11032 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011033 * Return a range (from start to end) of lines in rettv from the specified
11034 * buffer.
11035 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011036 */
11037 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011038get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011039 buf_T *buf;
11040 linenr_T start;
11041 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011042 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011043 typval_T *rettv;
11044{
11045 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011046
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011047 if (retlist && rettv_list_alloc(rettv) == FAIL)
11048 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011049
11050 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11051 return;
11052
11053 if (!retlist)
11054 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011055 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11056 p = ml_get_buf(buf, start, FALSE);
11057 else
11058 p = (char_u *)"";
11059
11060 rettv->v_type = VAR_STRING;
11061 rettv->vval.v_string = vim_strsave(p);
11062 }
11063 else
11064 {
11065 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011066 return;
11067
11068 if (start < 1)
11069 start = 1;
11070 if (end > buf->b_ml.ml_line_count)
11071 end = buf->b_ml.ml_line_count;
11072 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011073 if (list_append_string(rettv->vval.v_list,
11074 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011075 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011076 }
11077}
11078
11079/*
11080 * "getbufline()" function
11081 */
11082 static void
11083f_getbufline(argvars, rettv)
11084 typval_T *argvars;
11085 typval_T *rettv;
11086{
11087 linenr_T lnum;
11088 linenr_T end;
11089 buf_T *buf;
11090
11091 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11092 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011093 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011094 --emsg_off;
11095
Bram Moolenaar661b1822005-07-28 22:36:45 +000011096 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011097 if (argvars[2].v_type == VAR_UNKNOWN)
11098 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011099 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011100 end = get_tv_lnum_buf(&argvars[2], buf);
11101
Bram Moolenaar342337a2005-07-21 21:11:17 +000011102 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011103}
11104
Bram Moolenaar0d660222005-01-07 21:51:51 +000011105/*
11106 * "getbufvar()" function
11107 */
11108 static void
11109f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 typval_T *argvars;
11111 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011112{
11113 buf_T *buf;
11114 buf_T *save_curbuf;
11115 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011116 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011117 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11120 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011122 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011124 rettv->v_type = VAR_STRING;
11125 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011126
11127 if (buf != NULL && varname != NULL)
11128 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011129 /* set curbuf to be our buf, temporarily */
11130 save_curbuf = curbuf;
11131 curbuf = buf;
11132
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011134 {
11135 if (get_option_tv(&varname, rettv, TRUE) == OK)
11136 done = TRUE;
11137 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011138 else if (STRCMP(varname, "changedtick") == 0)
11139 {
11140 rettv->v_type = VAR_NUMBER;
11141 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011142 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011143 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011144 else
11145 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011146 /* Look up the variable. */
11147 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11148 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11149 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011151 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011153 done = TRUE;
11154 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011155 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011156
11157 /* restore previous notion of curbuf */
11158 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 }
11160
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011161 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11162 /* use the default value */
11163 copy_tv(&argvars[2], rettv);
11164
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 --emsg_off;
11166}
11167
11168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 * "getchar()" function
11170 */
11171 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011173 typval_T *argvars;
11174 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175{
11176 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011179 /* Position the cursor. Needed after a message that ends in a space. */
11180 windgoto(msg_row, msg_col);
11181
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 ++no_mapping;
11183 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011184 for (;;)
11185 {
11186 if (argvars[0].v_type == VAR_UNKNOWN)
11187 /* getchar(): blocking wait. */
11188 n = safe_vgetc();
11189 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11190 /* getchar(1): only check if char avail */
11191 n = vpeekc();
11192 else if (error || vpeekc() == NUL)
11193 /* illegal argument or getchar(0) and no char avail: return zero */
11194 n = 0;
11195 else
11196 /* getchar(0) and char avail: return char */
11197 n = safe_vgetc();
11198 if (n == K_IGNORE)
11199 continue;
11200 break;
11201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 --no_mapping;
11203 --allow_keys;
11204
Bram Moolenaar219b8702006-11-01 14:32:36 +000011205 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11206 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11207 vimvars[VV_MOUSE_COL].vv_nr = 0;
11208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 if (IS_SPECIAL(n) || mod_mask != 0)
11211 {
11212 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11213 int i = 0;
11214
11215 /* Turn a special key into three bytes, plus modifier. */
11216 if (mod_mask != 0)
11217 {
11218 temp[i++] = K_SPECIAL;
11219 temp[i++] = KS_MODIFIER;
11220 temp[i++] = mod_mask;
11221 }
11222 if (IS_SPECIAL(n))
11223 {
11224 temp[i++] = K_SPECIAL;
11225 temp[i++] = K_SECOND(n);
11226 temp[i++] = K_THIRD(n);
11227 }
11228#ifdef FEAT_MBYTE
11229 else if (has_mbyte)
11230 i += (*mb_char2bytes)(n, temp + i);
11231#endif
11232 else
11233 temp[i++] = n;
11234 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235 rettv->v_type = VAR_STRING;
11236 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011237
11238#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011239 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011240 {
11241 int row = mouse_row;
11242 int col = mouse_col;
11243 win_T *win;
11244 linenr_T lnum;
11245# ifdef FEAT_WINDOWS
11246 win_T *wp;
11247# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011248 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011249
11250 if (row >= 0 && col >= 0)
11251 {
11252 /* Find the window at the mouse coordinates and compute the
11253 * text position. */
11254 win = mouse_find_win(&row, &col);
11255 (void)mouse_comp_pos(win, &row, &col, &lnum);
11256# ifdef FEAT_WINDOWS
11257 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011258 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011259# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011260 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011261 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11262 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11263 }
11264 }
11265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 }
11267}
11268
11269/*
11270 * "getcharmod()" function
11271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011274 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278}
11279
11280/*
11281 * "getcmdline()" function
11282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011284f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011285 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288 rettv->v_type = VAR_STRING;
11289 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290}
11291
11292/*
11293 * "getcmdpos()" function
11294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011297 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301}
11302
11303/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011304 * "getcmdtype()" function
11305 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011306 static void
11307f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011308 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011309 typval_T *rettv;
11310{
11311 rettv->v_type = VAR_STRING;
11312 rettv->vval.v_string = alloc(2);
11313 if (rettv->vval.v_string != NULL)
11314 {
11315 rettv->vval.v_string[0] = get_cmdline_type();
11316 rettv->vval.v_string[1] = NUL;
11317 }
11318}
11319
11320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 * "getcwd()" function
11322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011325 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011328 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011331 rettv->vval.v_string = NULL;
11332 cwd = alloc(MAXPATHL);
11333 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011335 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11336 {
11337 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011339 if (rettv->vval.v_string != NULL)
11340 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011342 }
11343 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 }
11345}
11346
11347/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011348 * "getfontname()" function
11349 */
11350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011352 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011353 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011354{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 rettv->v_type = VAR_STRING;
11356 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011357#ifdef FEAT_GUI
11358 if (gui.in_use)
11359 {
11360 GuiFont font;
11361 char_u *name = NULL;
11362
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011363 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011364 {
11365 /* Get the "Normal" font. Either the name saved by
11366 * hl_set_font_name() or from the font ID. */
11367 font = gui.norm_font;
11368 name = hl_get_font_name();
11369 }
11370 else
11371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011373 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11374 return;
11375 font = gui_mch_get_font(name, FALSE);
11376 if (font == NOFONT)
11377 return; /* Invalid font name, return empty string. */
11378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011380 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011381 gui_mch_free_font(font);
11382 }
11383#endif
11384}
11385
11386/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011387 * "getfperm({fname})" function
11388 */
11389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *argvars;
11392 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011393{
11394 char_u *fname;
11395 struct stat st;
11396 char_u *perm = NULL;
11397 char_u flags[] = "rwx";
11398 int i;
11399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011403 if (mch_stat((char *)fname, &st) >= 0)
11404 {
11405 perm = vim_strsave((char_u *)"---------");
11406 if (perm != NULL)
11407 {
11408 for (i = 0; i < 9; i++)
11409 {
11410 if (st.st_mode & (1 << (8 - i)))
11411 perm[i] = flags[i % 3];
11412 }
11413 }
11414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011416}
11417
11418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 * "getfsize({fname})" function
11420 */
11421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011423 typval_T *argvars;
11424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425{
11426 char_u *fname;
11427 struct stat st;
11428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432
11433 if (mch_stat((char *)fname, &st) >= 0)
11434 {
11435 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011440
11441 /* non-perfect check for overflow */
11442 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11443 rettv->vval.v_number = -2;
11444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 }
11446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448}
11449
11450/*
11451 * "getftime({fname})" function
11452 */
11453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011455 typval_T *argvars;
11456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457{
11458 char_u *fname;
11459 struct stat st;
11460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462
11463 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467}
11468
11469/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011470 * "getftype({fname})" function
11471 */
11472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 typval_T *argvars;
11475 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011476{
11477 char_u *fname;
11478 struct stat st;
11479 char_u *type = NULL;
11480 char *t;
11481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011485 if (mch_lstat((char *)fname, &st) >= 0)
11486 {
11487#ifdef S_ISREG
11488 if (S_ISREG(st.st_mode))
11489 t = "file";
11490 else if (S_ISDIR(st.st_mode))
11491 t = "dir";
11492# ifdef S_ISLNK
11493 else if (S_ISLNK(st.st_mode))
11494 t = "link";
11495# endif
11496# ifdef S_ISBLK
11497 else if (S_ISBLK(st.st_mode))
11498 t = "bdev";
11499# endif
11500# ifdef S_ISCHR
11501 else if (S_ISCHR(st.st_mode))
11502 t = "cdev";
11503# endif
11504# ifdef S_ISFIFO
11505 else if (S_ISFIFO(st.st_mode))
11506 t = "fifo";
11507# endif
11508# ifdef S_ISSOCK
11509 else if (S_ISSOCK(st.st_mode))
11510 t = "fifo";
11511# endif
11512 else
11513 t = "other";
11514#else
11515# ifdef S_IFMT
11516 switch (st.st_mode & S_IFMT)
11517 {
11518 case S_IFREG: t = "file"; break;
11519 case S_IFDIR: t = "dir"; break;
11520# ifdef S_IFLNK
11521 case S_IFLNK: t = "link"; break;
11522# endif
11523# ifdef S_IFBLK
11524 case S_IFBLK: t = "bdev"; break;
11525# endif
11526# ifdef S_IFCHR
11527 case S_IFCHR: t = "cdev"; break;
11528# endif
11529# ifdef S_IFIFO
11530 case S_IFIFO: t = "fifo"; break;
11531# endif
11532# ifdef S_IFSOCK
11533 case S_IFSOCK: t = "socket"; break;
11534# endif
11535 default: t = "other";
11536 }
11537# else
11538 if (mch_isdir(fname))
11539 t = "dir";
11540 else
11541 t = "file";
11542# endif
11543#endif
11544 type = vim_strsave((char_u *)t);
11545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011547}
11548
11549/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011550 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011551 */
11552 static void
11553f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011554 typval_T *argvars;
11555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011556{
11557 linenr_T lnum;
11558 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011559 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011560
11561 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011563 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011564 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011565 retlist = FALSE;
11566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011568 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011570 retlist = TRUE;
11571 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011572
Bram Moolenaar342337a2005-07-21 21:11:17 +000011573 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011574}
11575
11576/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011577 * "getmatches()" function
11578 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011579 static void
11580f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011581 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011582 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011583{
11584#ifdef FEAT_SEARCH_EXTRA
11585 dict_T *dict;
11586 matchitem_T *cur = curwin->w_match_head;
11587
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011588 if (rettv_list_alloc(rettv) == OK)
11589 {
11590 while (cur != NULL)
11591 {
11592 dict = dict_alloc();
11593 if (dict == NULL)
11594 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011595 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11596 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11597 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11598 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11599 list_append_dict(rettv->vval.v_list, dict);
11600 cur = cur->next;
11601 }
11602 }
11603#endif
11604}
11605
11606/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011607 * "getpid()" function
11608 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011609 static void
11610f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011611 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011612 typval_T *rettv;
11613{
11614 rettv->vval.v_number = mch_get_pid();
11615}
11616
11617/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011618 * "getpos(string)" function
11619 */
11620 static void
11621f_getpos(argvars, rettv)
11622 typval_T *argvars;
11623 typval_T *rettv;
11624{
11625 pos_T *fp;
11626 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011627 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011628
11629 if (rettv_list_alloc(rettv) == OK)
11630 {
11631 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011632 fp = var2fpos(&argvars[0], TRUE, &fnum);
11633 if (fnum != -1)
11634 list_append_number(l, (varnumber_T)fnum);
11635 else
11636 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011637 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11638 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011639 list_append_number(l, (fp != NULL)
11640 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011641 : (varnumber_T)0);
11642 list_append_number(l,
11643#ifdef FEAT_VIRTUALEDIT
11644 (fp != NULL) ? (varnumber_T)fp->coladd :
11645#endif
11646 (varnumber_T)0);
11647 }
11648 else
11649 rettv->vval.v_number = FALSE;
11650}
11651
11652/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011653 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011654 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011655 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011656f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011657 typval_T *argvars UNUSED;
11658 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011659{
11660#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011661 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011662#endif
11663
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011665 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011666 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011667 wp = NULL;
11668 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11669 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011670 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011671 if (wp == NULL)
11672 return;
11673 }
11674
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011675 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011676 }
11677#endif
11678}
11679
11680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 * "getreg()" function
11682 */
11683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011685 typval_T *argvars;
11686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687{
11688 char_u *strregname;
11689 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011690 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 strregname = get_tv_string_chk(&argvars[0]);
11696 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011697 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011698 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011701 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 regname = (strregname == NULL ? '"' : *strregname);
11703 if (regname == 0)
11704 regname = '"';
11705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 rettv->vval.v_string = error ? NULL :
11708 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709}
11710
11711/*
11712 * "getregtype()" function
11713 */
11714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 typval_T *argvars;
11717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718{
11719 char_u *strregname;
11720 int regname;
11721 char_u buf[NUMBUFLEN + 2];
11722 long reglen = 0;
11723
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011724 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011725 {
11726 strregname = get_tv_string_chk(&argvars[0]);
11727 if (strregname == NULL) /* type error; errmsg already given */
11728 {
11729 rettv->v_type = VAR_STRING;
11730 rettv->vval.v_string = NULL;
11731 return;
11732 }
11733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 else
11735 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011736 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737
11738 regname = (strregname == NULL ? '"' : *strregname);
11739 if (regname == 0)
11740 regname = '"';
11741
11742 buf[0] = NUL;
11743 buf[1] = NUL;
11744 switch (get_reg_type(regname, &reglen))
11745 {
11746 case MLINE: buf[0] = 'V'; break;
11747 case MCHAR: buf[0] = 'v'; break;
11748#ifdef FEAT_VISUAL
11749 case MBLOCK:
11750 buf[0] = Ctrl_V;
11751 sprintf((char *)buf + 1, "%ld", reglen + 1);
11752 break;
11753#endif
11754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755 rettv->v_type = VAR_STRING;
11756 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757}
11758
11759/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011760 * "gettabvar()" function
11761 */
11762 static void
11763f_gettabvar(argvars, rettv)
11764 typval_T *argvars;
11765 typval_T *rettv;
11766{
11767 tabpage_T *tp;
11768 dictitem_T *v;
11769 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011770 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011771
11772 rettv->v_type = VAR_STRING;
11773 rettv->vval.v_string = NULL;
11774
11775 varname = get_tv_string_chk(&argvars[1]);
11776 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11777 if (tp != NULL && varname != NULL)
11778 {
11779 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011780 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011781 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011782 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011783 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011784 done = TRUE;
11785 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011786 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011787
11788 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011789 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011790}
11791
11792/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011793 * "gettabwinvar()" function
11794 */
11795 static void
11796f_gettabwinvar(argvars, rettv)
11797 typval_T *argvars;
11798 typval_T *rettv;
11799{
11800 getwinvar(argvars, rettv, 1);
11801}
11802
11803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 * "getwinposx()" function
11805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812#ifdef FEAT_GUI
11813 if (gui.in_use)
11814 {
11815 int x, y;
11816
11817 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 }
11820#endif
11821}
11822
11823/*
11824 * "getwinposy()" function
11825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011828 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832#ifdef FEAT_GUI
11833 if (gui.in_use)
11834 {
11835 int x, y;
11836
11837 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011838 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 }
11840#endif
11841}
11842
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011843/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011844 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011845 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011846 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011847find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011848 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011849 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011850{
11851#ifdef FEAT_WINDOWS
11852 win_T *wp;
11853#endif
11854 int nr;
11855
11856 nr = get_tv_number_chk(vp, NULL);
11857
11858#ifdef FEAT_WINDOWS
11859 if (nr < 0)
11860 return NULL;
11861 if (nr == 0)
11862 return curwin;
11863
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011864 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11865 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011866 if (--nr <= 0)
11867 break;
11868 return wp;
11869#else
11870 if (nr == 0 || nr == 1)
11871 return curwin;
11872 return NULL;
11873#endif
11874}
11875
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876/*
11877 * "getwinvar()" function
11878 */
11879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 typval_T *argvars;
11882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011884 getwinvar(argvars, rettv, 0);
11885}
11886
11887/*
11888 * getwinvar() and gettabwinvar()
11889 */
11890 static void
11891getwinvar(argvars, rettv, off)
11892 typval_T *argvars;
11893 typval_T *rettv;
11894 int off; /* 1 for gettabwinvar() */
11895{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 win_T *win, *oldcurwin;
11897 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011898 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011899 tabpage_T *tp;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011900 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011902#ifdef FEAT_WINDOWS
11903 if (off == 1)
11904 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11905 else
11906 tp = curtab;
11907#endif
11908 win = find_win_by_nr(&argvars[off], tp);
11909 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011912 rettv->v_type = VAR_STRING;
11913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914
11915 if (win != NULL && varname != NULL)
11916 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011917 /* Set curwin to be our win, temporarily. Also set curbuf, so
11918 * that we can get buffer-local options. */
11919 oldcurwin = curwin;
11920 curwin = win;
11921 curbuf = win->w_buffer;
11922
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011924 {
11925 if (get_option_tv(&varname, rettv, 1) == OK)
11926 done = TRUE;
11927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011928 else
11929 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011930 /* Look up the variable. */
11931 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11932 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011934 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011935 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011936 done = TRUE;
11937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011939
11940 /* restore previous notion of curwin */
11941 curwin = oldcurwin;
11942 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 }
11944
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011945 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11946 /* use the default return value */
11947 copy_tv(&argvars[off + 2], rettv);
11948
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949 --emsg_off;
11950}
11951
11952/*
11953 * "glob()" function
11954 */
11955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011956f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011957 typval_T *argvars;
11958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011960 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011962 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011964 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011965 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011966 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011967 if (argvars[1].v_type != VAR_UNKNOWN)
11968 {
11969 if (get_tv_number_chk(&argvars[1], &error))
11970 options |= WILD_KEEP_ALL;
11971 if (argvars[2].v_type != VAR_UNKNOWN
11972 && get_tv_number_chk(&argvars[2], &error))
11973 {
11974 rettv->v_type = VAR_LIST;
11975 rettv->vval.v_list = NULL;
11976 }
11977 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011978 if (!error)
11979 {
11980 ExpandInit(&xpc);
11981 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011982 if (p_wic)
11983 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011984 if (rettv->v_type == VAR_STRING)
11985 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011986 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011987 else if (rettv_list_alloc(rettv) != FAIL)
11988 {
11989 int i;
11990
11991 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11992 NULL, options, WILD_ALL_KEEP);
11993 for (i = 0; i < xpc.xp_numfiles; i++)
11994 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11995
11996 ExpandCleanup(&xpc);
11997 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011998 }
11999 else
12000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001}
12002
12003/*
12004 * "globpath()" function
12005 */
12006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012008 typval_T *argvars;
12009 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012011 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012014 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012016 /* When the optional second argument is non-zero, don't remove matches
12017 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12018 if (argvars[2].v_type != VAR_UNKNOWN
12019 && get_tv_number_chk(&argvars[2], &error))
12020 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012022 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023 rettv->vval.v_string = NULL;
12024 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012025 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12026 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027}
12028
12029/*
12030 * "has()" function
12031 */
12032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012034 typval_T *argvars;
12035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036{
12037 int i;
12038 char_u *name;
12039 int n = FALSE;
12040 static char *(has_list[]) =
12041 {
12042#ifdef AMIGA
12043 "amiga",
12044# ifdef FEAT_ARP
12045 "arp",
12046# endif
12047#endif
12048#ifdef __BEOS__
12049 "beos",
12050#endif
12051#ifdef MSDOS
12052# ifdef DJGPP
12053 "dos32",
12054# else
12055 "dos16",
12056# endif
12057#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012058#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 "mac",
12060#endif
12061#if defined(MACOS_X_UNIX)
12062 "macunix",
12063#endif
12064#ifdef OS2
12065 "os2",
12066#endif
12067#ifdef __QNX__
12068 "qnx",
12069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070#ifdef UNIX
12071 "unix",
12072#endif
12073#ifdef VMS
12074 "vms",
12075#endif
12076#ifdef WIN16
12077 "win16",
12078#endif
12079#ifdef WIN32
12080 "win32",
12081#endif
12082#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12083 "win32unix",
12084#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012085#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 "win64",
12087#endif
12088#ifdef EBCDIC
12089 "ebcdic",
12090#endif
12091#ifndef CASE_INSENSITIVE_FILENAME
12092 "fname_case",
12093#endif
12094#ifdef FEAT_ARABIC
12095 "arabic",
12096#endif
12097#ifdef FEAT_AUTOCMD
12098 "autocmd",
12099#endif
12100#ifdef FEAT_BEVAL
12101 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012102# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12103 "balloon_multiline",
12104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105#endif
12106#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12107 "builtin_terms",
12108# ifdef ALL_BUILTIN_TCAPS
12109 "all_builtin_terms",
12110# endif
12111#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012112#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12113 || defined(FEAT_GUI_W32) \
12114 || defined(FEAT_GUI_MOTIF))
12115 "browsefilter",
12116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#ifdef FEAT_BYTEOFF
12118 "byte_offset",
12119#endif
12120#ifdef FEAT_CINDENT
12121 "cindent",
12122#endif
12123#ifdef FEAT_CLIENTSERVER
12124 "clientserver",
12125#endif
12126#ifdef FEAT_CLIPBOARD
12127 "clipboard",
12128#endif
12129#ifdef FEAT_CMDL_COMPL
12130 "cmdline_compl",
12131#endif
12132#ifdef FEAT_CMDHIST
12133 "cmdline_hist",
12134#endif
12135#ifdef FEAT_COMMENTS
12136 "comments",
12137#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012138#ifdef FEAT_CONCEAL
12139 "conceal",
12140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#ifdef FEAT_CRYPT
12142 "cryptv",
12143#endif
12144#ifdef FEAT_CSCOPE
12145 "cscope",
12146#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012147#ifdef FEAT_CURSORBIND
12148 "cursorbind",
12149#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012150#ifdef CURSOR_SHAPE
12151 "cursorshape",
12152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153#ifdef DEBUG
12154 "debug",
12155#endif
12156#ifdef FEAT_CON_DIALOG
12157 "dialog_con",
12158#endif
12159#ifdef FEAT_GUI_DIALOG
12160 "dialog_gui",
12161#endif
12162#ifdef FEAT_DIFF
12163 "diff",
12164#endif
12165#ifdef FEAT_DIGRAPHS
12166 "digraphs",
12167#endif
12168#ifdef FEAT_DND
12169 "dnd",
12170#endif
12171#ifdef FEAT_EMACS_TAGS
12172 "emacs_tags",
12173#endif
12174 "eval", /* always present, of course! */
12175#ifdef FEAT_EX_EXTRA
12176 "ex_extra",
12177#endif
12178#ifdef FEAT_SEARCH_EXTRA
12179 "extra_search",
12180#endif
12181#ifdef FEAT_FKMAP
12182 "farsi",
12183#endif
12184#ifdef FEAT_SEARCHPATH
12185 "file_in_path",
12186#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012187#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012188 "filterpipe",
12189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190#ifdef FEAT_FIND_ID
12191 "find_in_path",
12192#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012193#ifdef FEAT_FLOAT
12194 "float",
12195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196#ifdef FEAT_FOLDING
12197 "folding",
12198#endif
12199#ifdef FEAT_FOOTER
12200 "footer",
12201#endif
12202#if !defined(USE_SYSTEM) && defined(UNIX)
12203 "fork",
12204#endif
12205#ifdef FEAT_GETTEXT
12206 "gettext",
12207#endif
12208#ifdef FEAT_GUI
12209 "gui",
12210#endif
12211#ifdef FEAT_GUI_ATHENA
12212# ifdef FEAT_GUI_NEXTAW
12213 "gui_neXtaw",
12214# else
12215 "gui_athena",
12216# endif
12217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218#ifdef FEAT_GUI_GTK
12219 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012222#ifdef FEAT_GUI_GNOME
12223 "gui_gnome",
12224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225#ifdef FEAT_GUI_MAC
12226 "gui_mac",
12227#endif
12228#ifdef FEAT_GUI_MOTIF
12229 "gui_motif",
12230#endif
12231#ifdef FEAT_GUI_PHOTON
12232 "gui_photon",
12233#endif
12234#ifdef FEAT_GUI_W16
12235 "gui_win16",
12236#endif
12237#ifdef FEAT_GUI_W32
12238 "gui_win32",
12239#endif
12240#ifdef FEAT_HANGULIN
12241 "hangul_input",
12242#endif
12243#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12244 "iconv",
12245#endif
12246#ifdef FEAT_INS_EXPAND
12247 "insert_expand",
12248#endif
12249#ifdef FEAT_JUMPLIST
12250 "jumplist",
12251#endif
12252#ifdef FEAT_KEYMAP
12253 "keymap",
12254#endif
12255#ifdef FEAT_LANGMAP
12256 "langmap",
12257#endif
12258#ifdef FEAT_LIBCALL
12259 "libcall",
12260#endif
12261#ifdef FEAT_LINEBREAK
12262 "linebreak",
12263#endif
12264#ifdef FEAT_LISP
12265 "lispindent",
12266#endif
12267#ifdef FEAT_LISTCMDS
12268 "listcmds",
12269#endif
12270#ifdef FEAT_LOCALMAP
12271 "localmap",
12272#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012273#ifdef FEAT_LUA
12274# ifndef DYNAMIC_LUA
12275 "lua",
12276# endif
12277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278#ifdef FEAT_MENU
12279 "menu",
12280#endif
12281#ifdef FEAT_SESSION
12282 "mksession",
12283#endif
12284#ifdef FEAT_MODIFY_FNAME
12285 "modify_fname",
12286#endif
12287#ifdef FEAT_MOUSE
12288 "mouse",
12289#endif
12290#ifdef FEAT_MOUSESHAPE
12291 "mouseshape",
12292#endif
12293#if defined(UNIX) || defined(VMS)
12294# ifdef FEAT_MOUSE_DEC
12295 "mouse_dec",
12296# endif
12297# ifdef FEAT_MOUSE_GPM
12298 "mouse_gpm",
12299# endif
12300# ifdef FEAT_MOUSE_JSB
12301 "mouse_jsbterm",
12302# endif
12303# ifdef FEAT_MOUSE_NET
12304 "mouse_netterm",
12305# endif
12306# ifdef FEAT_MOUSE_PTERM
12307 "mouse_pterm",
12308# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012309# ifdef FEAT_MOUSE_SGR
12310 "mouse_sgr",
12311# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012312# ifdef FEAT_SYSMOUSE
12313 "mouse_sysmouse",
12314# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012315# ifdef FEAT_MOUSE_URXVT
12316 "mouse_urxvt",
12317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318# ifdef FEAT_MOUSE_XTERM
12319 "mouse_xterm",
12320# endif
12321#endif
12322#ifdef FEAT_MBYTE
12323 "multi_byte",
12324#endif
12325#ifdef FEAT_MBYTE_IME
12326 "multi_byte_ime",
12327#endif
12328#ifdef FEAT_MULTI_LANG
12329 "multi_lang",
12330#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012331#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012332#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012333 "mzscheme",
12334#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336#ifdef FEAT_OLE
12337 "ole",
12338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339#ifdef FEAT_PATH_EXTRA
12340 "path_extra",
12341#endif
12342#ifdef FEAT_PERL
12343#ifndef DYNAMIC_PERL
12344 "perl",
12345#endif
12346#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012347#ifdef FEAT_PERSISTENT_UNDO
12348 "persistent_undo",
12349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350#ifdef FEAT_PYTHON
12351#ifndef DYNAMIC_PYTHON
12352 "python",
12353#endif
12354#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012355#ifdef FEAT_PYTHON3
12356#ifndef DYNAMIC_PYTHON3
12357 "python3",
12358#endif
12359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360#ifdef FEAT_POSTSCRIPT
12361 "postscript",
12362#endif
12363#ifdef FEAT_PRINTER
12364 "printer",
12365#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012366#ifdef FEAT_PROFILE
12367 "profile",
12368#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012369#ifdef FEAT_RELTIME
12370 "reltime",
12371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372#ifdef FEAT_QUICKFIX
12373 "quickfix",
12374#endif
12375#ifdef FEAT_RIGHTLEFT
12376 "rightleft",
12377#endif
12378#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12379 "ruby",
12380#endif
12381#ifdef FEAT_SCROLLBIND
12382 "scrollbind",
12383#endif
12384#ifdef FEAT_CMDL_INFO
12385 "showcmd",
12386 "cmdline_info",
12387#endif
12388#ifdef FEAT_SIGNS
12389 "signs",
12390#endif
12391#ifdef FEAT_SMARTINDENT
12392 "smartindent",
12393#endif
12394#ifdef FEAT_SNIFF
12395 "sniff",
12396#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012397#ifdef STARTUPTIME
12398 "startuptime",
12399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#ifdef FEAT_STL_OPT
12401 "statusline",
12402#endif
12403#ifdef FEAT_SUN_WORKSHOP
12404 "sun_workshop",
12405#endif
12406#ifdef FEAT_NETBEANS_INTG
12407 "netbeans_intg",
12408#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012409#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012410 "spell",
12411#endif
12412#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413 "syntax",
12414#endif
12415#if defined(USE_SYSTEM) || !defined(UNIX)
12416 "system",
12417#endif
12418#ifdef FEAT_TAG_BINS
12419 "tag_binary",
12420#endif
12421#ifdef FEAT_TAG_OLDSTATIC
12422 "tag_old_static",
12423#endif
12424#ifdef FEAT_TAG_ANYWHITE
12425 "tag_any_white",
12426#endif
12427#ifdef FEAT_TCL
12428# ifndef DYNAMIC_TCL
12429 "tcl",
12430# endif
12431#endif
12432#ifdef TERMINFO
12433 "terminfo",
12434#endif
12435#ifdef FEAT_TERMRESPONSE
12436 "termresponse",
12437#endif
12438#ifdef FEAT_TEXTOBJ
12439 "textobjects",
12440#endif
12441#ifdef HAVE_TGETENT
12442 "tgetent",
12443#endif
12444#ifdef FEAT_TITLE
12445 "title",
12446#endif
12447#ifdef FEAT_TOOLBAR
12448 "toolbar",
12449#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012450#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12451 "unnamedplus",
12452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453#ifdef FEAT_USR_CMDS
12454 "user-commands", /* was accidentally included in 5.4 */
12455 "user_commands",
12456#endif
12457#ifdef FEAT_VIMINFO
12458 "viminfo",
12459#endif
12460#ifdef FEAT_VERTSPLIT
12461 "vertsplit",
12462#endif
12463#ifdef FEAT_VIRTUALEDIT
12464 "virtualedit",
12465#endif
12466#ifdef FEAT_VISUAL
12467 "visual",
12468#endif
12469#ifdef FEAT_VISUALEXTRA
12470 "visualextra",
12471#endif
12472#ifdef FEAT_VREPLACE
12473 "vreplace",
12474#endif
12475#ifdef FEAT_WILDIGN
12476 "wildignore",
12477#endif
12478#ifdef FEAT_WILDMENU
12479 "wildmenu",
12480#endif
12481#ifdef FEAT_WINDOWS
12482 "windows",
12483#endif
12484#ifdef FEAT_WAK
12485 "winaltkeys",
12486#endif
12487#ifdef FEAT_WRITEBACKUP
12488 "writebackup",
12489#endif
12490#ifdef FEAT_XIM
12491 "xim",
12492#endif
12493#ifdef FEAT_XFONTSET
12494 "xfontset",
12495#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012496#ifdef FEAT_XPM_W32
12497 "xpm_w32",
12498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499#ifdef USE_XSMP
12500 "xsmp",
12501#endif
12502#ifdef USE_XSMP_INTERACT
12503 "xsmp_interact",
12504#endif
12505#ifdef FEAT_XCLIPBOARD
12506 "xterm_clipboard",
12507#endif
12508#ifdef FEAT_XTERM_SAVE
12509 "xterm_save",
12510#endif
12511#if defined(UNIX) && defined(FEAT_X11)
12512 "X11",
12513#endif
12514 NULL
12515 };
12516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012518 for (i = 0; has_list[i] != NULL; ++i)
12519 if (STRICMP(name, has_list[i]) == 0)
12520 {
12521 n = TRUE;
12522 break;
12523 }
12524
12525 if (n == FALSE)
12526 {
12527 if (STRNICMP(name, "patch", 5) == 0)
12528 n = has_patch(atoi((char *)name + 5));
12529 else if (STRICMP(name, "vim_starting") == 0)
12530 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012531#ifdef FEAT_MBYTE
12532 else if (STRICMP(name, "multi_byte_encoding") == 0)
12533 n = has_mbyte;
12534#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012535#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12536 else if (STRICMP(name, "balloon_multiline") == 0)
12537 n = multiline_balloon_available();
12538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539#ifdef DYNAMIC_TCL
12540 else if (STRICMP(name, "tcl") == 0)
12541 n = tcl_enabled(FALSE);
12542#endif
12543#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12544 else if (STRICMP(name, "iconv") == 0)
12545 n = iconv_enabled(FALSE);
12546#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012547#ifdef DYNAMIC_LUA
12548 else if (STRICMP(name, "lua") == 0)
12549 n = lua_enabled(FALSE);
12550#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012551#ifdef DYNAMIC_MZSCHEME
12552 else if (STRICMP(name, "mzscheme") == 0)
12553 n = mzscheme_enabled(FALSE);
12554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555#ifdef DYNAMIC_RUBY
12556 else if (STRICMP(name, "ruby") == 0)
12557 n = ruby_enabled(FALSE);
12558#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012559#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560#ifdef DYNAMIC_PYTHON
12561 else if (STRICMP(name, "python") == 0)
12562 n = python_enabled(FALSE);
12563#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012564#endif
12565#ifdef FEAT_PYTHON3
12566#ifdef DYNAMIC_PYTHON3
12567 else if (STRICMP(name, "python3") == 0)
12568 n = python3_enabled(FALSE);
12569#endif
12570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571#ifdef DYNAMIC_PERL
12572 else if (STRICMP(name, "perl") == 0)
12573 n = perl_enabled(FALSE);
12574#endif
12575#ifdef FEAT_GUI
12576 else if (STRICMP(name, "gui_running") == 0)
12577 n = (gui.in_use || gui.starting);
12578# ifdef FEAT_GUI_W32
12579 else if (STRICMP(name, "gui_win32s") == 0)
12580 n = gui_is_win32s();
12581# endif
12582# ifdef FEAT_BROWSE
12583 else if (STRICMP(name, "browse") == 0)
12584 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12585# endif
12586#endif
12587#ifdef FEAT_SYN_HL
12588 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012589 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590#endif
12591#if defined(WIN3264)
12592 else if (STRICMP(name, "win95") == 0)
12593 n = mch_windows95();
12594#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012595#ifdef FEAT_NETBEANS_INTG
12596 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012597 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599 }
12600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012601 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602}
12603
12604/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012605 * "has_key()" function
12606 */
12607 static void
12608f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012609 typval_T *argvars;
12610 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012611{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012612 if (argvars[0].v_type != VAR_DICT)
12613 {
12614 EMSG(_(e_dictreq));
12615 return;
12616 }
12617 if (argvars[0].vval.v_dict == NULL)
12618 return;
12619
12620 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012621 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012622}
12623
12624/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012625 * "haslocaldir()" function
12626 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012627 static void
12628f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012629 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012630 typval_T *rettv;
12631{
12632 rettv->vval.v_number = (curwin->w_localdir != NULL);
12633}
12634
12635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 * "hasmapto()" function
12637 */
12638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012640 typval_T *argvars;
12641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642{
12643 char_u *name;
12644 char_u *mode;
12645 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012646 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012649 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 mode = (char_u *)"nvo";
12651 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012654 if (argvars[2].v_type != VAR_UNKNOWN)
12655 abbr = get_tv_number(&argvars[2]);
12656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaar2c932302006-03-18 21:42:09 +000012658 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662}
12663
12664/*
12665 * "histadd()" function
12666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012669 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671{
12672#ifdef FEAT_CMDHIST
12673 int histype;
12674 char_u *str;
12675 char_u buf[NUMBUFLEN];
12676#endif
12677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 if (check_restricted() || check_secure())
12680 return;
12681#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012682 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12683 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684 if (histype >= 0)
12685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 if (*str != NUL)
12688 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012689 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 return;
12693 }
12694 }
12695#endif
12696}
12697
12698/*
12699 * "histdel()" function
12700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012703 typval_T *argvars UNUSED;
12704 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705{
12706#ifdef FEAT_CMDHIST
12707 int n;
12708 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012711 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12712 if (str == NULL)
12713 n = 0;
12714 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012716 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012717 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 else
12722 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 get_tv_string_buf(&argvars[1], buf));
12725 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726#endif
12727}
12728
12729/*
12730 * "histget()" function
12731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012734 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736{
12737#ifdef FEAT_CMDHIST
12738 int type;
12739 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12743 if (str == NULL)
12744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 {
12747 type = get_histtype(str);
12748 if (argvars[1].v_type == VAR_UNKNOWN)
12749 idx = get_history_idx(type);
12750 else
12751 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12752 /* -1 on type error */
12753 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759}
12760
12761/*
12762 * "histnr()" function
12763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012766 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768{
12769 int i;
12770
12771#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 char_u *history = get_tv_string_chk(&argvars[0]);
12773
12774 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 if (i >= HIST_CMD && i < HIST_COUNT)
12776 i = get_history_idx(i);
12777 else
12778#endif
12779 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781}
12782
12783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 * "highlightID(name)" function
12785 */
12786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *argvars;
12789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012791 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792}
12793
12794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012795 * "highlight_exists()" function
12796 */
12797 static void
12798f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012799 typval_T *argvars;
12800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801{
12802 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12803}
12804
12805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 * "hostname()" function
12807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812{
12813 char_u hostname[256];
12814
12815 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->v_type = VAR_STRING;
12817 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818}
12819
12820/*
12821 * iconv() function
12822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012825 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827{
12828#ifdef FEAT_MBYTE
12829 char_u buf1[NUMBUFLEN];
12830 char_u buf2[NUMBUFLEN];
12831 char_u *from, *to, *str;
12832 vimconv_T vimconv;
12833#endif
12834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->v_type = VAR_STRING;
12836 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837
12838#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 str = get_tv_string(&argvars[0]);
12840 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12841 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 vimconv.vc_type = CONV_NONE;
12843 convert_setup(&vimconv, from, to);
12844
12845 /* If the encodings are equal, no conversion needed. */
12846 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850
12851 convert_setup(&vimconv, NULL, NULL);
12852 vim_free(from);
12853 vim_free(to);
12854#endif
12855}
12856
12857/*
12858 * "indent()" function
12859 */
12860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012862 typval_T *argvars;
12863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
12865 linenr_T lnum;
12866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872}
12873
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012874/*
12875 * "index()" function
12876 */
12877 static void
12878f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012879 typval_T *argvars;
12880 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012881{
Bram Moolenaar33570922005-01-25 22:26:29 +000012882 list_T *l;
12883 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012884 long idx = 0;
12885 int ic = FALSE;
12886
12887 rettv->vval.v_number = -1;
12888 if (argvars[0].v_type != VAR_LIST)
12889 {
12890 EMSG(_(e_listreq));
12891 return;
12892 }
12893 l = argvars[0].vval.v_list;
12894 if (l != NULL)
12895 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012896 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012897 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012899 int error = FALSE;
12900
Bram Moolenaar758711c2005-02-02 23:11:38 +000012901 /* Start at specified item. Use the cached index that list_find()
12902 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012904 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012905 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012906 ic = get_tv_number_chk(&argvars[3], &error);
12907 if (error)
12908 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012909 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012910
Bram Moolenaar758711c2005-02-02 23:11:38 +000012911 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012912 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012913 {
12914 rettv->vval.v_number = idx;
12915 break;
12916 }
12917 }
12918}
12919
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920static int inputsecret_flag = 0;
12921
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012922static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12923
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012925 * This function is used by f_input() and f_inputdialog() functions. The third
12926 * argument to f_input() specifies the type of completion to use at the
12927 * prompt. The third argument to f_inputdialog() specifies the value to return
12928 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 */
12930 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012931get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012934 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012936 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 char_u *p = NULL;
12938 int c;
12939 char_u buf[NUMBUFLEN];
12940 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012941 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012942 int xp_type = EXPAND_NOTHING;
12943 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012946 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947
12948#ifdef NO_CONSOLE_INPUT
12949 /* While starting up, there is no place to enter text. */
12950 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952#endif
12953
12954 cmd_silent = FALSE; /* Want to see the prompt. */
12955 if (prompt != NULL)
12956 {
12957 /* Only the part of the message after the last NL is considered as
12958 * prompt for the command line */
12959 p = vim_strrchr(prompt, '\n');
12960 if (p == NULL)
12961 p = prompt;
12962 else
12963 {
12964 ++p;
12965 c = *p;
12966 *p = NUL;
12967 msg_start();
12968 msg_clr_eos();
12969 msg_puts_attr(prompt, echo_attr);
12970 msg_didout = FALSE;
12971 msg_starthere();
12972 *p = c;
12973 }
12974 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012976 if (argvars[1].v_type != VAR_UNKNOWN)
12977 {
12978 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12979 if (defstr != NULL)
12980 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012982 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012983 {
12984 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012985 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012986 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012987
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012988 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012989 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012990
Bram Moolenaar4463f292005-09-25 22:20:24 +000012991 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12992 if (xp_name == NULL)
12993 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012994
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012995 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012996
Bram Moolenaar4463f292005-09-25 22:20:24 +000012997 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12998 &xp_arg) == FAIL)
12999 return;
13000 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013001 }
13002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013003 if (defstr != NULL)
13004 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013005 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13006 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013007 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013008 && argvars[1].v_type != VAR_UNKNOWN
13009 && argvars[2].v_type != VAR_UNKNOWN)
13010 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13011 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013012
13013 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 /* since the user typed this, no need to wait for return */
13016 need_wait_return = FALSE;
13017 msg_didout = FALSE;
13018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 cmd_silent = cmd_silent_save;
13020}
13021
13022/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013023 * "input()" function
13024 * Also handles inputsecret() when inputsecret is set.
13025 */
13026 static void
13027f_input(argvars, rettv)
13028 typval_T *argvars;
13029 typval_T *rettv;
13030{
13031 get_user_input(argvars, rettv, FALSE);
13032}
13033
13034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 * "inputdialog()" function
13036 */
13037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013039 typval_T *argvars;
13040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041{
13042#if defined(FEAT_GUI_TEXTDIALOG)
13043 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13044 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13045 {
13046 char_u *message;
13047 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013048 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013050 message = get_tv_string_chk(&argvars[0]);
13051 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013052 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013053 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 else
13055 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 if (message != NULL && defstr != NULL
13057 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013058 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 else
13061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 if (message != NULL && defstr != NULL
13063 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013064 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 rettv->vval.v_string = vim_strsave(
13066 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 }
13072 else
13073#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013074 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075}
13076
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013077/*
13078 * "inputlist()" function
13079 */
13080 static void
13081f_inputlist(argvars, rettv)
13082 typval_T *argvars;
13083 typval_T *rettv;
13084{
13085 listitem_T *li;
13086 int selected;
13087 int mouse_used;
13088
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013089#ifdef NO_CONSOLE_INPUT
13090 /* While starting up, there is no place to enter text. */
13091 if (no_console_input())
13092 return;
13093#endif
13094 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13095 {
13096 EMSG2(_(e_listarg), "inputlist()");
13097 return;
13098 }
13099
13100 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013101 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013102 lines_left = Rows; /* avoid more prompt */
13103 msg_scroll = TRUE;
13104 msg_clr_eos();
13105
13106 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13107 {
13108 msg_puts(get_tv_string(&li->li_tv));
13109 msg_putchar('\n');
13110 }
13111
13112 /* Ask for choice. */
13113 selected = prompt_for_number(&mouse_used);
13114 if (mouse_used)
13115 selected -= lines_left;
13116
13117 rettv->vval.v_number = selected;
13118}
13119
13120
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13122
13123/*
13124 * "inputrestore()" function
13125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013128 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130{
13131 if (ga_userinput.ga_len > 0)
13132 {
13133 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13135 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013136 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 }
13138 else if (p_verbose > 1)
13139 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013140 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 }
13143}
13144
13145/*
13146 * "inputsave()" function
13147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013150 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013153 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 if (ga_grow(&ga_userinput, 1) == OK)
13155 {
13156 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13157 + ga_userinput.ga_len);
13158 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013159 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 }
13161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163}
13164
13165/*
13166 * "inputsecret()" function
13167 */
13168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013170 typval_T *argvars;
13171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172{
13173 ++cmdline_star;
13174 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 --cmdline_star;
13177 --inputsecret_flag;
13178}
13179
13180/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013181 * "insert()" function
13182 */
13183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *argvars;
13186 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187{
13188 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013189 listitem_T *item;
13190 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013191 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192
13193 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013194 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013195 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013196 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197 {
13198 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013199 before = get_tv_number_chk(&argvars[2], &error);
13200 if (error)
13201 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013202
Bram Moolenaar758711c2005-02-02 23:11:38 +000013203 if (before == l->lv_len)
13204 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 else
13206 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013207 item = list_find(l, before);
13208 if (item == NULL)
13209 {
13210 EMSGN(_(e_listidx), before);
13211 l = NULL;
13212 }
13213 }
13214 if (l != NULL)
13215 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013216 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013217 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013218 }
13219 }
13220}
13221
13222/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013223 * "invert(expr)" function
13224 */
13225 static void
13226f_invert(argvars, rettv)
13227 typval_T *argvars;
13228 typval_T *rettv;
13229{
13230 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13231}
13232
13233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 * "isdirectory()" function
13235 */
13236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013238 typval_T *argvars;
13239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013241 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242}
13243
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013244/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013245 * "islocked()" function
13246 */
13247 static void
13248f_islocked(argvars, rettv)
13249 typval_T *argvars;
13250 typval_T *rettv;
13251{
13252 lval_T lv;
13253 char_u *end;
13254 dictitem_T *di;
13255
13256 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013257 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13258 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013259 if (end != NULL && lv.ll_name != NULL)
13260 {
13261 if (*end != NUL)
13262 EMSG(_(e_trailing));
13263 else
13264 {
13265 if (lv.ll_tv == NULL)
13266 {
13267 if (check_changedtick(lv.ll_name))
13268 rettv->vval.v_number = 1; /* always locked */
13269 else
13270 {
13271 di = find_var(lv.ll_name, NULL);
13272 if (di != NULL)
13273 {
13274 /* Consider a variable locked when:
13275 * 1. the variable itself is locked
13276 * 2. the value of the variable is locked.
13277 * 3. the List or Dict value is locked.
13278 */
13279 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13280 || tv_islocked(&di->di_tv));
13281 }
13282 }
13283 }
13284 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013285 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013286 else if (lv.ll_newkey != NULL)
13287 EMSG2(_(e_dictkey), lv.ll_newkey);
13288 else if (lv.ll_list != NULL)
13289 /* List item. */
13290 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13291 else
13292 /* Dictionary item. */
13293 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13294 }
13295 }
13296
13297 clear_lval(&lv);
13298}
13299
Bram Moolenaar33570922005-01-25 22:26:29 +000013300static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013301
13302/*
13303 * Turn a dict into a list:
13304 * "what" == 0: list of keys
13305 * "what" == 1: list of values
13306 * "what" == 2: list of items
13307 */
13308 static void
13309dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013310 typval_T *argvars;
13311 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013312 int what;
13313{
Bram Moolenaar33570922005-01-25 22:26:29 +000013314 list_T *l2;
13315 dictitem_T *di;
13316 hashitem_T *hi;
13317 listitem_T *li;
13318 listitem_T *li2;
13319 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013320 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013321
Bram Moolenaar8c711452005-01-14 21:53:12 +000013322 if (argvars[0].v_type != VAR_DICT)
13323 {
13324 EMSG(_(e_dictreq));
13325 return;
13326 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013327 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013328 return;
13329
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013330 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013331 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013333 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013334 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013336 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013337 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013338 --todo;
13339 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013341 li = listitem_alloc();
13342 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013344 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013346 if (what == 0)
13347 {
13348 /* keys() */
13349 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013350 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013351 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13352 }
13353 else if (what == 1)
13354 {
13355 /* values() */
13356 copy_tv(&di->di_tv, &li->li_tv);
13357 }
13358 else
13359 {
13360 /* items() */
13361 l2 = list_alloc();
13362 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013363 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013364 li->li_tv.vval.v_list = l2;
13365 if (l2 == NULL)
13366 break;
13367 ++l2->lv_refcount;
13368
13369 li2 = listitem_alloc();
13370 if (li2 == NULL)
13371 break;
13372 list_append(l2, li2);
13373 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013374 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013375 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13376
13377 li2 = listitem_alloc();
13378 if (li2 == NULL)
13379 break;
13380 list_append(l2, li2);
13381 copy_tv(&di->di_tv, &li2->li_tv);
13382 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013383 }
13384 }
13385}
13386
13387/*
13388 * "items(dict)" function
13389 */
13390 static void
13391f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013392 typval_T *argvars;
13393 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013394{
13395 dict_list(argvars, rettv, 2);
13396}
13397
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013399 * "join()" function
13400 */
13401 static void
13402f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013403 typval_T *argvars;
13404 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013405{
13406 garray_T ga;
13407 char_u *sep;
13408
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013409 if (argvars[0].v_type != VAR_LIST)
13410 {
13411 EMSG(_(e_listreq));
13412 return;
13413 }
13414 if (argvars[0].vval.v_list == NULL)
13415 return;
13416 if (argvars[1].v_type == VAR_UNKNOWN)
13417 sep = (char_u *)" ";
13418 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013419 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013420
13421 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422
13423 if (sep != NULL)
13424 {
13425 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013426 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013427 ga_append(&ga, NUL);
13428 rettv->vval.v_string = (char_u *)ga.ga_data;
13429 }
13430 else
13431 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013432}
13433
13434/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013435 * "keys()" function
13436 */
13437 static void
13438f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013441{
13442 dict_list(argvars, rettv, 0);
13443}
13444
13445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 * "last_buffer_nr()" function.
13447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013450 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
13453 int n = 0;
13454 buf_T *buf;
13455
13456 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13457 if (n < buf->b_fnum)
13458 n = buf->b_fnum;
13459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013460 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013461}
13462
13463/*
13464 * "len()" function
13465 */
13466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013467f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013468 typval_T *argvars;
13469 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013470{
13471 switch (argvars[0].v_type)
13472 {
13473 case VAR_STRING:
13474 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 rettv->vval.v_number = (varnumber_T)STRLEN(
13476 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013477 break;
13478 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013480 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013481 case VAR_DICT:
13482 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13483 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013485 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486 break;
13487 }
13488}
13489
Bram Moolenaar33570922005-01-25 22:26:29 +000013490static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491
13492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013496 int type;
13497{
13498#ifdef FEAT_LIBCALL
13499 char_u *string_in;
13500 char_u **string_result;
13501 int nr_result;
13502#endif
13503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013505 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013506 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013507
13508 if (check_restricted() || check_secure())
13509 return;
13510
13511#ifdef FEAT_LIBCALL
13512 /* The first two args must be strings, otherwise its meaningless */
13513 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13514 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013515 string_in = NULL;
13516 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013517 string_in = argvars[2].vval.v_string;
13518 if (type == VAR_NUMBER)
13519 string_result = NULL;
13520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013522 if (mch_libcall(argvars[0].vval.v_string,
13523 argvars[1].vval.v_string,
13524 string_in,
13525 argvars[2].vval.v_number,
13526 string_result,
13527 &nr_result) == OK
13528 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013530 }
13531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532}
13533
13534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013535 * "libcall()" function
13536 */
13537 static void
13538f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013539 typval_T *argvars;
13540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013541{
13542 libcall_common(argvars, rettv, VAR_STRING);
13543}
13544
13545/*
13546 * "libcallnr()" function
13547 */
13548 static void
13549f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013550 typval_T *argvars;
13551 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013552{
13553 libcall_common(argvars, rettv, VAR_NUMBER);
13554}
13555
13556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 * "line(string)" function
13558 */
13559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013560f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013561 typval_T *argvars;
13562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563{
13564 linenr_T lnum = 0;
13565 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013566 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013568 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 if (fp != NULL)
13570 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572}
13573
13574/*
13575 * "line2byte(lnum)" function
13576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013579 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581{
13582#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584#else
13585 linenr_T lnum;
13586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013591 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13592 if (rettv->vval.v_number >= 0)
13593 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594#endif
13595}
13596
13597/*
13598 * "lispindent(lnum)" function
13599 */
13600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013602 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604{
13605#ifdef FEAT_LISP
13606 pos_T pos;
13607 linenr_T lnum;
13608
13609 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013610 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13612 {
13613 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615 curwin->w_cursor = pos;
13616 }
13617 else
13618#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620}
13621
13622/*
13623 * "localtime()" function
13624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631}
13632
Bram Moolenaar33570922005-01-25 22:26:29 +000013633static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634
13635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013637 typval_T *argvars;
13638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 int exact;
13640{
13641 char_u *keys;
13642 char_u *which;
13643 char_u buf[NUMBUFLEN];
13644 char_u *keys_buf = NULL;
13645 char_u *rhs;
13646 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013647 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013648 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013649 mapblock_T *mp;
13650 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651
13652 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653 rettv->v_type = VAR_STRING;
13654 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013656 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 if (*keys == NUL)
13658 return;
13659
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013660 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013662 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013663 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013664 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013665 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013666 if (argvars[3].v_type != VAR_UNKNOWN)
13667 get_dict = get_tv_number(&argvars[3]);
13668 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 else
13671 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013672 if (which == NULL)
13673 return;
13674
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 mode = get_map_mode(&which, 0);
13676
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013677 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013680
13681 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013683 /* Return a string. */
13684 if (rhs != NULL)
13685 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686
Bram Moolenaarbd743252010-10-20 21:23:33 +020013687 }
13688 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13689 {
13690 /* Return a dictionary. */
13691 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13692 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13693 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694
Bram Moolenaarbd743252010-10-20 21:23:33 +020013695 dict_add_nr_str(dict, "lhs", 0L, lhs);
13696 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13697 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13698 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13699 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13700 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13701 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13702 dict_add_nr_str(dict, "mode", 0L, mapmode);
13703
13704 vim_free(lhs);
13705 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 }
13707}
13708
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013709#ifdef FEAT_FLOAT
13710/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013711 * "log()" function
13712 */
13713 static void
13714f_log(argvars, rettv)
13715 typval_T *argvars;
13716 typval_T *rettv;
13717{
13718 float_T f;
13719
13720 rettv->v_type = VAR_FLOAT;
13721 if (get_float_arg(argvars, &f) == OK)
13722 rettv->vval.v_float = log(f);
13723 else
13724 rettv->vval.v_float = 0.0;
13725}
13726
13727/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013728 * "log10()" function
13729 */
13730 static void
13731f_log10(argvars, rettv)
13732 typval_T *argvars;
13733 typval_T *rettv;
13734{
13735 float_T f;
13736
13737 rettv->v_type = VAR_FLOAT;
13738 if (get_float_arg(argvars, &f) == OK)
13739 rettv->vval.v_float = log10(f);
13740 else
13741 rettv->vval.v_float = 0.0;
13742}
13743#endif
13744
Bram Moolenaar1dced572012-04-05 16:54:08 +020013745#ifdef FEAT_LUA
13746/*
13747 * "luaeval()" function
13748 */
13749 static void
13750f_luaeval(argvars, rettv)
13751 typval_T *argvars;
13752 typval_T *rettv;
13753{
13754 char_u *str;
13755 char_u buf[NUMBUFLEN];
13756
13757 str = get_tv_string_buf(&argvars[0], buf);
13758 do_luaeval(str, argvars + 1, rettv);
13759}
13760#endif
13761
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013763 * "map()" function
13764 */
13765 static void
13766f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 typval_T *argvars;
13768 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013769{
13770 filter_map(argvars, rettv, TRUE);
13771}
13772
13773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013774 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 */
13776 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013777f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013778 typval_T *argvars;
13779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013781 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782}
13783
13784/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013785 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 */
13787 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013788f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013789 typval_T *argvars;
13790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013792 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013793}
13794
Bram Moolenaar33570922005-01-25 22:26:29 +000013795static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796
13797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013798find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 typval_T *argvars;
13800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 int type;
13802{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013803 char_u *str = NULL;
13804 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 char_u *pat;
13806 regmatch_T regmatch;
13807 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013808 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 char_u *save_cpo;
13810 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013811 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013812 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013813 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 list_T *l = NULL;
13815 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013816 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013817 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818
13819 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13820 save_cpo = p_cpo;
13821 p_cpo = (char_u *)"";
13822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013823 rettv->vval.v_number = -1;
13824 if (type == 3)
13825 {
13826 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013827 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013828 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013829 }
13830 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832 rettv->v_type = VAR_STRING;
13833 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013836 if (argvars[0].v_type == VAR_LIST)
13837 {
13838 if ((l = argvars[0].vval.v_list) == NULL)
13839 goto theend;
13840 li = l->lv_first;
13841 }
13842 else
13843 expr = str = get_tv_string(&argvars[0]);
13844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013845 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13846 if (pat == NULL)
13847 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013848
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013849 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 int error = FALSE;
13852
13853 start = get_tv_number_chk(&argvars[2], &error);
13854 if (error)
13855 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013856 if (l != NULL)
13857 {
13858 li = list_find(l, start);
13859 if (li == NULL)
13860 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013861 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013862 }
13863 else
13864 {
13865 if (start < 0)
13866 start = 0;
13867 if (start > (long)STRLEN(str))
13868 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013869 /* When "count" argument is there ignore matches before "start",
13870 * otherwise skip part of the string. Differs when pattern is "^"
13871 * or "\<". */
13872 if (argvars[3].v_type != VAR_UNKNOWN)
13873 startcol = start;
13874 else
13875 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013876 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013877
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013878 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013879 nth = get_tv_number_chk(&argvars[3], &error);
13880 if (error)
13881 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 }
13883
13884 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13885 if (regmatch.regprog != NULL)
13886 {
13887 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013888
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013889 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013890 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013891 if (l != NULL)
13892 {
13893 if (li == NULL)
13894 {
13895 match = FALSE;
13896 break;
13897 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013898 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013899 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013900 if (str == NULL)
13901 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013902 }
13903
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013904 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013906 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013907 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013908 if (l == NULL && !match)
13909 break;
13910
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013911 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013912 if (l != NULL)
13913 {
13914 li = li->li_next;
13915 ++idx;
13916 }
13917 else
13918 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013919#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013920 startcol = (colnr_T)(regmatch.startp[0]
13921 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013922#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013923 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013924#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013925 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013926 }
13927
13928 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013930 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013932 int i;
13933
13934 /* return list with matched string and submatches */
13935 for (i = 0; i < NSUBEXP; ++i)
13936 {
13937 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013938 {
13939 if (list_append_string(rettv->vval.v_list,
13940 (char_u *)"", 0) == FAIL)
13941 break;
13942 }
13943 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013944 regmatch.startp[i],
13945 (int)(regmatch.endp[i] - regmatch.startp[i]))
13946 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013947 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013948 }
13949 }
13950 else if (type == 2)
13951 {
13952 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013953 if (l != NULL)
13954 copy_tv(&li->li_tv, rettv);
13955 else
13956 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013958 }
13959 else if (l != NULL)
13960 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 else
13962 {
13963 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 (varnumber_T)(regmatch.startp[0] - str);
13966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013969 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 }
13971 }
13972 vim_free(regmatch.regprog);
13973 }
13974
13975theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013976 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 p_cpo = save_cpo;
13978}
13979
13980/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981 * "match()" function
13982 */
13983 static void
13984f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013985 typval_T *argvars;
13986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013987{
13988 find_some_match(argvars, rettv, 1);
13989}
13990
13991/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013992 * "matchadd()" function
13993 */
13994 static void
13995f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013996 typval_T *argvars UNUSED;
13997 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013998{
13999#ifdef FEAT_SEARCH_EXTRA
14000 char_u buf[NUMBUFLEN];
14001 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14002 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14003 int prio = 10; /* default priority */
14004 int id = -1;
14005 int error = FALSE;
14006
14007 rettv->vval.v_number = -1;
14008
14009 if (grp == NULL || pat == NULL)
14010 return;
14011 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014012 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014013 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014014 if (argvars[3].v_type != VAR_UNKNOWN)
14015 id = get_tv_number_chk(&argvars[3], &error);
14016 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014017 if (error == TRUE)
14018 return;
14019 if (id >= 1 && id <= 3)
14020 {
14021 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14022 return;
14023 }
14024
14025 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14026#endif
14027}
14028
14029/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014030 * "matcharg()" function
14031 */
14032 static void
14033f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014034 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014035 typval_T *rettv;
14036{
14037 if (rettv_list_alloc(rettv) == OK)
14038 {
14039#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014040 int id = get_tv_number(&argvars[0]);
14041 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014042
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014043 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014044 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014045 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14046 {
14047 list_append_string(rettv->vval.v_list,
14048 syn_id2name(m->hlg_id), -1);
14049 list_append_string(rettv->vval.v_list, m->pattern, -1);
14050 }
14051 else
14052 {
14053 list_append_string(rettv->vval.v_list, NUL, -1);
14054 list_append_string(rettv->vval.v_list, NUL, -1);
14055 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014056 }
14057#endif
14058 }
14059}
14060
14061/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014062 * "matchdelete()" function
14063 */
14064 static void
14065f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014066 typval_T *argvars UNUSED;
14067 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014068{
14069#ifdef FEAT_SEARCH_EXTRA
14070 rettv->vval.v_number = match_delete(curwin,
14071 (int)get_tv_number(&argvars[0]), TRUE);
14072#endif
14073}
14074
14075/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076 * "matchend()" function
14077 */
14078 static void
14079f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014080 typval_T *argvars;
14081 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014082{
14083 find_some_match(argvars, rettv, 0);
14084}
14085
14086/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014087 * "matchlist()" function
14088 */
14089 static void
14090f_matchlist(argvars, rettv)
14091 typval_T *argvars;
14092 typval_T *rettv;
14093{
14094 find_some_match(argvars, rettv, 3);
14095}
14096
14097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098 * "matchstr()" function
14099 */
14100 static void
14101f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014102 typval_T *argvars;
14103 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014104{
14105 find_some_match(argvars, rettv, 2);
14106}
14107
Bram Moolenaar33570922005-01-25 22:26:29 +000014108static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014109
14110 static void
14111max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014112 typval_T *argvars;
14113 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014114 int domax;
14115{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014116 long n = 0;
14117 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014119
14120 if (argvars[0].v_type == VAR_LIST)
14121 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 list_T *l;
14123 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014124
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014125 l = argvars[0].vval.v_list;
14126 if (l != NULL)
14127 {
14128 li = l->lv_first;
14129 if (li != NULL)
14130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014132 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014133 {
14134 li = li->li_next;
14135 if (li == NULL)
14136 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014137 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014138 if (domax ? i > n : i < n)
14139 n = i;
14140 }
14141 }
14142 }
14143 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014144 else if (argvars[0].v_type == VAR_DICT)
14145 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014147 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014149 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014150
14151 d = argvars[0].vval.v_dict;
14152 if (d != NULL)
14153 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014154 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014158 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014159 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014161 if (first)
14162 {
14163 n = i;
14164 first = FALSE;
14165 }
14166 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014167 n = i;
14168 }
14169 }
14170 }
14171 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014172 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014173 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014175}
14176
14177/*
14178 * "max()" function
14179 */
14180 static void
14181f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 typval_T *argvars;
14183 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014184{
14185 max_min(argvars, rettv, TRUE);
14186}
14187
14188/*
14189 * "min()" function
14190 */
14191 static void
14192f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 typval_T *argvars;
14194 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014195{
14196 max_min(argvars, rettv, FALSE);
14197}
14198
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014199static int mkdir_recurse __ARGS((char_u *dir, int prot));
14200
14201/*
14202 * Create the directory in which "dir" is located, and higher levels when
14203 * needed.
14204 */
14205 static int
14206mkdir_recurse(dir, prot)
14207 char_u *dir;
14208 int prot;
14209{
14210 char_u *p;
14211 char_u *updir;
14212 int r = FAIL;
14213
14214 /* Get end of directory name in "dir".
14215 * We're done when it's "/" or "c:/". */
14216 p = gettail_sep(dir);
14217 if (p <= get_past_head(dir))
14218 return OK;
14219
14220 /* If the directory exists we're done. Otherwise: create it.*/
14221 updir = vim_strnsave(dir, (int)(p - dir));
14222 if (updir == NULL)
14223 return FAIL;
14224 if (mch_isdir(updir))
14225 r = OK;
14226 else if (mkdir_recurse(updir, prot) == OK)
14227 r = vim_mkdir_emsg(updir, prot);
14228 vim_free(updir);
14229 return r;
14230}
14231
14232#ifdef vim_mkdir
14233/*
14234 * "mkdir()" function
14235 */
14236 static void
14237f_mkdir(argvars, rettv)
14238 typval_T *argvars;
14239 typval_T *rettv;
14240{
14241 char_u *dir;
14242 char_u buf[NUMBUFLEN];
14243 int prot = 0755;
14244
14245 rettv->vval.v_number = FAIL;
14246 if (check_restricted() || check_secure())
14247 return;
14248
14249 dir = get_tv_string_buf(&argvars[0], buf);
14250 if (argvars[1].v_type != VAR_UNKNOWN)
14251 {
14252 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253 prot = get_tv_number_chk(&argvars[2], NULL);
14254 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014255 mkdir_recurse(dir, prot);
14256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014257 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014258}
14259#endif
14260
Bram Moolenaar0d660222005-01-07 21:51:51 +000014261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 * "mode()" function
14263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014265f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014266 typval_T *argvars;
14267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014269 char_u buf[3];
14270
14271 buf[1] = NUL;
14272 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273
14274#ifdef FEAT_VISUAL
14275 if (VIsual_active)
14276 {
14277 if (VIsual_select)
14278 buf[0] = VIsual_mode + 's' - 'v';
14279 else
14280 buf[0] = VIsual_mode;
14281 }
14282 else
14283#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014284 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14285 || State == CONFIRM)
14286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014288 if (State == ASKMORE)
14289 buf[1] = 'm';
14290 else if (State == CONFIRM)
14291 buf[1] = '?';
14292 }
14293 else if (State == EXTERNCMD)
14294 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 else if (State & INSERT)
14296 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014297#ifdef FEAT_VREPLACE
14298 if (State & VREPLACE_FLAG)
14299 {
14300 buf[0] = 'R';
14301 buf[1] = 'v';
14302 }
14303 else
14304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 if (State & REPLACE_FLAG)
14306 buf[0] = 'R';
14307 else
14308 buf[0] = 'i';
14309 }
14310 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014313 if (exmode_active)
14314 buf[1] = 'v';
14315 }
14316 else if (exmode_active)
14317 {
14318 buf[0] = 'c';
14319 buf[1] = 'e';
14320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014324 if (finish_op)
14325 buf[1] = 'o';
14326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014328 /* Clear out the minor mode when the argument is not a non-zero number or
14329 * non-empty string. */
14330 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014331 buf[1] = NUL;
14332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 rettv->vval.v_string = vim_strsave(buf);
14334 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335}
14336
Bram Moolenaar429fa852013-04-15 12:27:36 +020014337#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014338/*
14339 * "mzeval()" function
14340 */
14341 static void
14342f_mzeval(argvars, rettv)
14343 typval_T *argvars;
14344 typval_T *rettv;
14345{
14346 char_u *str;
14347 char_u buf[NUMBUFLEN];
14348
14349 str = get_tv_string_buf(&argvars[0], buf);
14350 do_mzeval(str, rettv);
14351}
Bram Moolenaar75676462013-01-30 14:55:42 +010014352
14353 void
14354mzscheme_call_vim(name, args, rettv)
14355 char_u *name;
14356 typval_T *args;
14357 typval_T *rettv;
14358{
14359 typval_T argvars[3];
14360
14361 argvars[0].v_type = VAR_STRING;
14362 argvars[0].vval.v_string = name;
14363 copy_tv(args, &argvars[1]);
14364 argvars[2].v_type = VAR_UNKNOWN;
14365 f_call(argvars, rettv);
14366 clear_tv(&argvars[1]);
14367}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014368#endif
14369
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014371 * "nextnonblank()" function
14372 */
14373 static void
14374f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014375 typval_T *argvars;
14376 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014377{
14378 linenr_T lnum;
14379
14380 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014382 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383 {
14384 lnum = 0;
14385 break;
14386 }
14387 if (*skipwhite(ml_get(lnum)) != NUL)
14388 break;
14389 }
14390 rettv->vval.v_number = lnum;
14391}
14392
14393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 * "nr2char()" function
14395 */
14396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014398 typval_T *argvars;
14399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400{
14401 char_u buf[NUMBUFLEN];
14402
14403#ifdef FEAT_MBYTE
14404 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014405 {
14406 int utf8 = 0;
14407
14408 if (argvars[1].v_type != VAR_UNKNOWN)
14409 utf8 = get_tv_number_chk(&argvars[1], NULL);
14410 if (utf8)
14411 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14412 else
14413 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 else
14416#endif
14417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 buf[1] = NUL;
14420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 rettv->v_type = VAR_STRING;
14422 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014423}
14424
14425/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014426 * "or(expr, expr)" function
14427 */
14428 static void
14429f_or(argvars, rettv)
14430 typval_T *argvars;
14431 typval_T *rettv;
14432{
14433 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14434 | get_tv_number_chk(&argvars[1], NULL);
14435}
14436
14437/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014438 * "pathshorten()" function
14439 */
14440 static void
14441f_pathshorten(argvars, rettv)
14442 typval_T *argvars;
14443 typval_T *rettv;
14444{
14445 char_u *p;
14446
14447 rettv->v_type = VAR_STRING;
14448 p = get_tv_string_chk(&argvars[0]);
14449 if (p == NULL)
14450 rettv->vval.v_string = NULL;
14451 else
14452 {
14453 p = vim_strsave(p);
14454 rettv->vval.v_string = p;
14455 if (p != NULL)
14456 shorten_dir(p);
14457 }
14458}
14459
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014460#ifdef FEAT_FLOAT
14461/*
14462 * "pow()" function
14463 */
14464 static void
14465f_pow(argvars, rettv)
14466 typval_T *argvars;
14467 typval_T *rettv;
14468{
14469 float_T fx, fy;
14470
14471 rettv->v_type = VAR_FLOAT;
14472 if (get_float_arg(argvars, &fx) == OK
14473 && get_float_arg(&argvars[1], &fy) == OK)
14474 rettv->vval.v_float = pow(fx, fy);
14475 else
14476 rettv->vval.v_float = 0.0;
14477}
14478#endif
14479
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014480/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014481 * "prevnonblank()" function
14482 */
14483 static void
14484f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014485 typval_T *argvars;
14486 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014487{
14488 linenr_T lnum;
14489
14490 lnum = get_tv_lnum(argvars);
14491 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14492 lnum = 0;
14493 else
14494 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14495 --lnum;
14496 rettv->vval.v_number = lnum;
14497}
14498
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014499#ifdef HAVE_STDARG_H
14500/* This dummy va_list is here because:
14501 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14502 * - locally in the function results in a "used before set" warning
14503 * - using va_start() to initialize it gives "function with fixed args" error */
14504static va_list ap;
14505#endif
14506
Bram Moolenaar8c711452005-01-14 21:53:12 +000014507/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014508 * "printf()" function
14509 */
14510 static void
14511f_printf(argvars, rettv)
14512 typval_T *argvars;
14513 typval_T *rettv;
14514{
14515 rettv->v_type = VAR_STRING;
14516 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014517#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014518 {
14519 char_u buf[NUMBUFLEN];
14520 int len;
14521 char_u *s;
14522 int saved_did_emsg = did_emsg;
14523 char *fmt;
14524
14525 /* Get the required length, allocate the buffer and do it for real. */
14526 did_emsg = FALSE;
14527 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014528 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014529 if (!did_emsg)
14530 {
14531 s = alloc(len + 1);
14532 if (s != NULL)
14533 {
14534 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014535 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014536 }
14537 }
14538 did_emsg |= saved_did_emsg;
14539 }
14540#endif
14541}
14542
14543/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014544 * "pumvisible()" function
14545 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014546 static void
14547f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014548 typval_T *argvars UNUSED;
14549 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014550{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014551#ifdef FEAT_INS_EXPAND
14552 if (pum_visible())
14553 rettv->vval.v_number = 1;
14554#endif
14555}
14556
Bram Moolenaardb913952012-06-29 12:54:53 +020014557#ifdef FEAT_PYTHON3
14558/*
14559 * "py3eval()" function
14560 */
14561 static void
14562f_py3eval(argvars, rettv)
14563 typval_T *argvars;
14564 typval_T *rettv;
14565{
14566 char_u *str;
14567 char_u buf[NUMBUFLEN];
14568
14569 str = get_tv_string_buf(&argvars[0], buf);
14570 do_py3eval(str, rettv);
14571}
14572#endif
14573
14574#ifdef FEAT_PYTHON
14575/*
14576 * "pyeval()" function
14577 */
14578 static void
14579f_pyeval(argvars, rettv)
14580 typval_T *argvars;
14581 typval_T *rettv;
14582{
14583 char_u *str;
14584 char_u buf[NUMBUFLEN];
14585
14586 str = get_tv_string_buf(&argvars[0], buf);
14587 do_pyeval(str, rettv);
14588}
14589#endif
14590
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014591/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014592 * "range()" function
14593 */
14594 static void
14595f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014596 typval_T *argvars;
14597 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014598{
14599 long start;
14600 long end;
14601 long stride = 1;
14602 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606 if (argvars[1].v_type == VAR_UNKNOWN)
14607 {
14608 end = start - 1;
14609 start = 0;
14610 }
14611 else
14612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616 }
14617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 if (error)
14619 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014621 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014622 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014623 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 else
14625 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014626 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014627 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014628 if (list_append_number(rettv->vval.v_list,
14629 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014631 }
14632}
14633
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014634/*
14635 * "readfile()" function
14636 */
14637 static void
14638f_readfile(argvars, rettv)
14639 typval_T *argvars;
14640 typval_T *rettv;
14641{
14642 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014643 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014644 char_u *fname;
14645 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014646 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14647 int io_size = sizeof(buf);
14648 int readlen; /* size of last fread() */
14649 char_u *prev = NULL; /* previously read bytes, if any */
14650 long prevlen = 0; /* length of data in prev */
14651 long prevsize = 0; /* size of prev buffer */
14652 long maxline = MAXLNUM;
14653 long cnt = 0;
14654 char_u *p; /* position in buf */
14655 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014656
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014657 if (argvars[1].v_type != VAR_UNKNOWN)
14658 {
14659 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14660 binary = TRUE;
14661 if (argvars[2].v_type != VAR_UNKNOWN)
14662 maxline = get_tv_number(&argvars[2]);
14663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014665 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014666 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014667
14668 /* Always open the file in binary mode, library functions have a mind of
14669 * their own about CR-LF conversion. */
14670 fname = get_tv_string(&argvars[0]);
14671 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14672 {
14673 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14674 return;
14675 }
14676
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014677 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014679 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014680
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014681 /* This for loop processes what was read, but is also entered at end
14682 * of file so that either:
14683 * - an incomplete line gets written
14684 * - a "binary" file gets an empty line at the end if it ends in a
14685 * newline. */
14686 for (p = buf, start = buf;
14687 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14688 ++p)
14689 {
14690 if (*p == '\n' || readlen <= 0)
14691 {
14692 listitem_T *li;
14693 char_u *s = NULL;
14694 long_u len = p - start;
14695
14696 /* Finished a line. Remove CRs before NL. */
14697 if (readlen > 0 && !binary)
14698 {
14699 while (len > 0 && start[len - 1] == '\r')
14700 --len;
14701 /* removal may cross back to the "prev" string */
14702 if (len == 0)
14703 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14704 --prevlen;
14705 }
14706 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014707 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014708 else
14709 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014710 /* Change "prev" buffer to be the right size. This way
14711 * the bytes are only copied once, and very long lines are
14712 * allocated only once. */
14713 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014717 prev = NULL; /* the list will own the string */
14718 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014719 }
14720 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014721 if (s == NULL)
14722 {
14723 do_outofmem_msg((long_u) prevlen + len + 1);
14724 failed = TRUE;
14725 break;
14726 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014728 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729 {
14730 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014731 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014732 break;
14733 }
14734 li->li_tv.v_type = VAR_STRING;
14735 li->li_tv.v_lock = 0;
14736 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014737 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014738
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014739 start = p + 1; /* step over newline */
14740 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741 break;
14742 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 else if (*p == NUL)
14744 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014745#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014746 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14747 * when finding the BF and check the previous two bytes. */
14748 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014749 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014750 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14751 * + 1, these may be in the "prev" string. */
14752 char_u back1 = p >= buf + 1 ? p[-1]
14753 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14754 char_u back2 = p >= buf + 2 ? p[-2]
14755 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14756 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 char_u *dest = p - 2;
14761
14762 /* Usually a BOM is at the beginning of a file, and so at
14763 * the beginning of a line; then we can just step over it.
14764 */
14765 if (start == dest)
14766 start = p + 1;
14767 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014768 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014769 /* have to shuffle buf to close gap */
14770 int adjust_prevlen = 0;
14771
14772 if (dest < buf)
14773 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014774 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775 dest = buf;
14776 }
14777 if (readlen > p - buf + 1)
14778 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14779 readlen -= 3 - adjust_prevlen;
14780 prevlen -= adjust_prevlen;
14781 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014782 }
14783 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014784 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014785#endif
14786 } /* for */
14787
14788 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14789 break;
14790 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014792 /* There's part of a line in buf, store it in "prev". */
14793 if (p - start + prevlen >= prevsize)
14794 {
14795 /* need bigger "prev" buffer */
14796 char_u *newprev;
14797
14798 /* A common use case is ordinary text files and "prev" gets a
14799 * fragment of a line, so the first allocation is made
14800 * small, to avoid repeatedly 'allocing' large and
14801 * 'reallocing' small. */
14802 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014803 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014804 else
14805 {
14806 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014807 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014808 prevsize = grow50pc > growmin ? grow50pc : growmin;
14809 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014810 newprev = prev == NULL ? alloc(prevsize)
14811 : vim_realloc(prev, prevsize);
14812 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014813 {
14814 do_outofmem_msg((long_u)prevsize);
14815 failed = TRUE;
14816 break;
14817 }
14818 prev = newprev;
14819 }
14820 /* Add the line part to end of "prev". */
14821 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014822 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014823 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014824 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014826 /*
14827 * For a negative line count use only the lines at the end of the file,
14828 * free the rest.
14829 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014831 while (cnt > -maxline)
14832 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014833 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014834 --cnt;
14835 }
14836
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014837 if (failed)
14838 {
14839 list_free(rettv->vval.v_list, TRUE);
14840 /* readfile doc says an empty list is returned on error */
14841 rettv->vval.v_list = list_alloc();
14842 }
14843
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014844 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014845 fclose(fd);
14846}
14847
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014848#if defined(FEAT_RELTIME)
14849static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14850
14851/*
14852 * Convert a List to proftime_T.
14853 * Return FAIL when there is something wrong.
14854 */
14855 static int
14856list2proftime(arg, tm)
14857 typval_T *arg;
14858 proftime_T *tm;
14859{
14860 long n1, n2;
14861 int error = FALSE;
14862
14863 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14864 || arg->vval.v_list->lv_len != 2)
14865 return FAIL;
14866 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14867 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14868# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014869 tm->HighPart = n1;
14870 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014871# else
14872 tm->tv_sec = n1;
14873 tm->tv_usec = n2;
14874# endif
14875 return error ? FAIL : OK;
14876}
14877#endif /* FEAT_RELTIME */
14878
14879/*
14880 * "reltime()" function
14881 */
14882 static void
14883f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014884 typval_T *argvars UNUSED;
14885 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014886{
14887#ifdef FEAT_RELTIME
14888 proftime_T res;
14889 proftime_T start;
14890
14891 if (argvars[0].v_type == VAR_UNKNOWN)
14892 {
14893 /* No arguments: get current time. */
14894 profile_start(&res);
14895 }
14896 else if (argvars[1].v_type == VAR_UNKNOWN)
14897 {
14898 if (list2proftime(&argvars[0], &res) == FAIL)
14899 return;
14900 profile_end(&res);
14901 }
14902 else
14903 {
14904 /* Two arguments: compute the difference. */
14905 if (list2proftime(&argvars[0], &start) == FAIL
14906 || list2proftime(&argvars[1], &res) == FAIL)
14907 return;
14908 profile_sub(&res, &start);
14909 }
14910
14911 if (rettv_list_alloc(rettv) == OK)
14912 {
14913 long n1, n2;
14914
14915# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014916 n1 = res.HighPart;
14917 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014918# else
14919 n1 = res.tv_sec;
14920 n2 = res.tv_usec;
14921# endif
14922 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14923 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14924 }
14925#endif
14926}
14927
14928/*
14929 * "reltimestr()" function
14930 */
14931 static void
14932f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014933 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014934 typval_T *rettv;
14935{
14936#ifdef FEAT_RELTIME
14937 proftime_T tm;
14938#endif
14939
14940 rettv->v_type = VAR_STRING;
14941 rettv->vval.v_string = NULL;
14942#ifdef FEAT_RELTIME
14943 if (list2proftime(&argvars[0], &tm) == OK)
14944 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14945#endif
14946}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014947
Bram Moolenaar0d660222005-01-07 21:51:51 +000014948#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14949static void make_connection __ARGS((void));
14950static int check_connection __ARGS((void));
14951
14952 static void
14953make_connection()
14954{
14955 if (X_DISPLAY == NULL
14956# ifdef FEAT_GUI
14957 && !gui.in_use
14958# endif
14959 )
14960 {
14961 x_force_connect = TRUE;
14962 setup_term_clip();
14963 x_force_connect = FALSE;
14964 }
14965}
14966
14967 static int
14968check_connection()
14969{
14970 make_connection();
14971 if (X_DISPLAY == NULL)
14972 {
14973 EMSG(_("E240: No connection to Vim server"));
14974 return FAIL;
14975 }
14976 return OK;
14977}
14978#endif
14979
14980#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014981static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982
14983 static void
14984remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 typval_T *argvars;
14986 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014987 int expr;
14988{
14989 char_u *server_name;
14990 char_u *keys;
14991 char_u *r = NULL;
14992 char_u buf[NUMBUFLEN];
14993# ifdef WIN32
14994 HWND w;
14995# else
14996 Window w;
14997# endif
14998
14999 if (check_restricted() || check_secure())
15000 return;
15001
15002# ifdef FEAT_X11
15003 if (check_connection() == FAIL)
15004 return;
15005# endif
15006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015007 server_name = get_tv_string_chk(&argvars[0]);
15008 if (server_name == NULL)
15009 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015010 keys = get_tv_string_buf(&argvars[1], buf);
15011# ifdef WIN32
15012 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15013# else
15014 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15015 < 0)
15016# endif
15017 {
15018 if (r != NULL)
15019 EMSG(r); /* sending worked but evaluation failed */
15020 else
15021 EMSG2(_("E241: Unable to send to %s"), server_name);
15022 return;
15023 }
15024
15025 rettv->vval.v_string = r;
15026
15027 if (argvars[2].v_type != VAR_UNKNOWN)
15028 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015030 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015032
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015033 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 v.di_tv.v_type = VAR_STRING;
15035 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015036 idvar = get_tv_string_chk(&argvars[2]);
15037 if (idvar != NULL)
15038 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015039 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015040 }
15041}
15042#endif
15043
15044/*
15045 * "remote_expr()" function
15046 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015047 static void
15048f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015049 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015050 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051{
15052 rettv->v_type = VAR_STRING;
15053 rettv->vval.v_string = NULL;
15054#ifdef FEAT_CLIENTSERVER
15055 remote_common(argvars, rettv, TRUE);
15056#endif
15057}
15058
15059/*
15060 * "remote_foreground()" function
15061 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015062 static void
15063f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015064 typval_T *argvars UNUSED;
15065 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015066{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067#ifdef FEAT_CLIENTSERVER
15068# ifdef WIN32
15069 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 {
15071 char_u *server_name = get_tv_string_chk(&argvars[0]);
15072
15073 if (server_name != NULL)
15074 serverForeground(server_name);
15075 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076# else
15077 /* Send a foreground() expression to the server. */
15078 argvars[1].v_type = VAR_STRING;
15079 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15080 argvars[2].v_type = VAR_UNKNOWN;
15081 remote_common(argvars, rettv, TRUE);
15082 vim_free(argvars[1].vval.v_string);
15083# endif
15084#endif
15085}
15086
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087 static void
15088f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015089 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015090 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091{
15092#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015093 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094 char_u *s = NULL;
15095# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015096 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099
15100 if (check_restricted() || check_secure())
15101 {
15102 rettv->vval.v_number = -1;
15103 return;
15104 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015105 serverid = get_tv_string_chk(&argvars[0]);
15106 if (serverid == NULL)
15107 {
15108 rettv->vval.v_number = -1;
15109 return; /* type error; errmsg already given */
15110 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015112 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113 if (n == 0)
15114 rettv->vval.v_number = -1;
15115 else
15116 {
15117 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15118 rettv->vval.v_number = (s != NULL);
15119 }
15120# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 if (check_connection() == FAIL)
15122 return;
15123
15124 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015126# endif
15127
15128 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015130 char_u *retvar;
15131
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 v.di_tv.v_type = VAR_STRING;
15133 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 retvar = get_tv_string_chk(&argvars[1]);
15135 if (retvar != NULL)
15136 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138 }
15139#else
15140 rettv->vval.v_number = -1;
15141#endif
15142}
15143
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 static void
15145f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015146 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148{
15149 char_u *r = NULL;
15150
15151#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 char_u *serverid = get_tv_string_chk(&argvars[0]);
15153
15154 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015155 {
15156# ifdef WIN32
15157 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015158 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015160 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161 if (n != 0)
15162 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15163 if (r == NULL)
15164# else
15165 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167# endif
15168 EMSG(_("E277: Unable to read a server reply"));
15169 }
15170#endif
15171 rettv->v_type = VAR_STRING;
15172 rettv->vval.v_string = r;
15173}
15174
15175/*
15176 * "remote_send()" function
15177 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178 static void
15179f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015180 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015182{
15183 rettv->v_type = VAR_STRING;
15184 rettv->vval.v_string = NULL;
15185#ifdef FEAT_CLIENTSERVER
15186 remote_common(argvars, rettv, FALSE);
15187#endif
15188}
15189
15190/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015191 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015192 */
15193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 typval_T *argvars;
15196 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015197{
Bram Moolenaar33570922005-01-25 22:26:29 +000015198 list_T *l;
15199 listitem_T *item, *item2;
15200 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015201 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015202 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015203 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015204 dict_T *d;
15205 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015206 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207
Bram Moolenaar8c711452005-01-14 21:53:12 +000015208 if (argvars[0].v_type == VAR_DICT)
15209 {
15210 if (argvars[2].v_type != VAR_UNKNOWN)
15211 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015212 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015213 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 key = get_tv_string_chk(&argvars[1]);
15216 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 di = dict_find(d, key, -1);
15219 if (di == NULL)
15220 EMSG2(_(e_dictkey), key);
15221 else
15222 {
15223 *rettv = di->di_tv;
15224 init_tv(&di->di_tv);
15225 dictitem_remove(d, di);
15226 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015228 }
15229 }
15230 else if (argvars[0].v_type != VAR_LIST)
15231 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015232 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015233 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015235 int error = FALSE;
15236
15237 idx = get_tv_number_chk(&argvars[1], &error);
15238 if (error)
15239 ; /* type error: do nothing, errmsg already given */
15240 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015241 EMSGN(_(e_listidx), idx);
15242 else
15243 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015244 if (argvars[2].v_type == VAR_UNKNOWN)
15245 {
15246 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015247 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015248 *rettv = item->li_tv;
15249 vim_free(item);
15250 }
15251 else
15252 {
15253 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 end = get_tv_number_chk(&argvars[2], &error);
15255 if (error)
15256 ; /* type error: do nothing */
15257 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015258 EMSGN(_(e_listidx), end);
15259 else
15260 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015261 int cnt = 0;
15262
15263 for (li = item; li != NULL; li = li->li_next)
15264 {
15265 ++cnt;
15266 if (li == item2)
15267 break;
15268 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015269 if (li == NULL) /* didn't find "item2" after "item" */
15270 EMSG(_(e_invrange));
15271 else
15272 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015273 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015274 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015276 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015277 l->lv_first = item;
15278 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015279 item->li_prev = NULL;
15280 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015281 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015282 }
15283 }
15284 }
15285 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015286 }
15287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288}
15289
15290/*
15291 * "rename({from}, {to})" function
15292 */
15293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015295 typval_T *argvars;
15296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297{
15298 char_u buf[NUMBUFLEN];
15299
15300 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15304 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305}
15306
15307/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015308 * "repeat()" function
15309 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015310 static void
15311f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015312 typval_T *argvars;
15313 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314{
15315 char_u *p;
15316 int n;
15317 int slen;
15318 int len;
15319 char_u *r;
15320 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015321
15322 n = get_tv_number(&argvars[1]);
15323 if (argvars[0].v_type == VAR_LIST)
15324 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015325 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015326 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015327 if (list_extend(rettv->vval.v_list,
15328 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015329 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015330 }
15331 else
15332 {
15333 p = get_tv_string(&argvars[0]);
15334 rettv->v_type = VAR_STRING;
15335 rettv->vval.v_string = NULL;
15336
15337 slen = (int)STRLEN(p);
15338 len = slen * n;
15339 if (len <= 0)
15340 return;
15341
15342 r = alloc(len + 1);
15343 if (r != NULL)
15344 {
15345 for (i = 0; i < n; i++)
15346 mch_memmove(r + i * slen, p, (size_t)slen);
15347 r[len] = NUL;
15348 }
15349
15350 rettv->vval.v_string = r;
15351 }
15352}
15353
15354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 * "resolve()" function
15356 */
15357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015358f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015359 typval_T *argvars;
15360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361{
15362 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015363#ifdef HAVE_READLINK
15364 char_u *buf = NULL;
15365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368#ifdef FEAT_SHORTCUT
15369 {
15370 char_u *v = NULL;
15371
15372 v = mch_resolve_shortcut(p);
15373 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015376 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 }
15378#else
15379# ifdef HAVE_READLINK
15380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 char_u *cpy;
15382 int len;
15383 char_u *remain = NULL;
15384 char_u *q;
15385 int is_relative_to_current = FALSE;
15386 int has_trailing_pathsep = FALSE;
15387 int limit = 100;
15388
15389 p = vim_strsave(p);
15390
15391 if (p[0] == '.' && (vim_ispathsep(p[1])
15392 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15393 is_relative_to_current = TRUE;
15394
15395 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015396 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015397 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015399 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
15402 q = getnextcomp(p);
15403 if (*q != NUL)
15404 {
15405 /* Separate the first path component in "p", and keep the
15406 * remainder (beginning with the path separator). */
15407 remain = vim_strsave(q - 1);
15408 q[-1] = NUL;
15409 }
15410
Bram Moolenaard9462e32011-04-11 21:35:11 +020015411 buf = alloc(MAXPATHL + 1);
15412 if (buf == NULL)
15413 goto fail;
15414
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 for (;;)
15416 {
15417 for (;;)
15418 {
15419 len = readlink((char *)p, (char *)buf, MAXPATHL);
15420 if (len <= 0)
15421 break;
15422 buf[len] = NUL;
15423
15424 if (limit-- == 0)
15425 {
15426 vim_free(p);
15427 vim_free(remain);
15428 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430 goto fail;
15431 }
15432
15433 /* Ensure that the result will have a trailing path separator
15434 * if the argument has one. */
15435 if (remain == NULL && has_trailing_pathsep)
15436 add_pathsep(buf);
15437
15438 /* Separate the first path component in the link value and
15439 * concatenate the remainders. */
15440 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15441 if (*q != NUL)
15442 {
15443 if (remain == NULL)
15444 remain = vim_strsave(q - 1);
15445 else
15446 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015447 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 if (cpy != NULL)
15449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 vim_free(remain);
15451 remain = cpy;
15452 }
15453 }
15454 q[-1] = NUL;
15455 }
15456
15457 q = gettail(p);
15458 if (q > p && *q == NUL)
15459 {
15460 /* Ignore trailing path separator. */
15461 q[-1] = NUL;
15462 q = gettail(p);
15463 }
15464 if (q > p && !mch_isFullName(buf))
15465 {
15466 /* symlink is relative to directory of argument */
15467 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15468 if (cpy != NULL)
15469 {
15470 STRCPY(cpy, p);
15471 STRCPY(gettail(cpy), buf);
15472 vim_free(p);
15473 p = cpy;
15474 }
15475 }
15476 else
15477 {
15478 vim_free(p);
15479 p = vim_strsave(buf);
15480 }
15481 }
15482
15483 if (remain == NULL)
15484 break;
15485
15486 /* Append the first path component of "remain" to "p". */
15487 q = getnextcomp(remain + 1);
15488 len = q - remain - (*q != NUL);
15489 cpy = vim_strnsave(p, STRLEN(p) + len);
15490 if (cpy != NULL)
15491 {
15492 STRNCAT(cpy, remain, len);
15493 vim_free(p);
15494 p = cpy;
15495 }
15496 /* Shorten "remain". */
15497 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015498 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 else
15500 {
15501 vim_free(remain);
15502 remain = NULL;
15503 }
15504 }
15505
15506 /* If the result is a relative path name, make it explicitly relative to
15507 * the current directory if and only if the argument had this form. */
15508 if (!vim_ispathsep(*p))
15509 {
15510 if (is_relative_to_current
15511 && *p != NUL
15512 && !(p[0] == '.'
15513 && (p[1] == NUL
15514 || vim_ispathsep(p[1])
15515 || (p[1] == '.'
15516 && (p[2] == NUL
15517 || vim_ispathsep(p[2]))))))
15518 {
15519 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015520 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 if (cpy != NULL)
15522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 vim_free(p);
15524 p = cpy;
15525 }
15526 }
15527 else if (!is_relative_to_current)
15528 {
15529 /* Strip leading "./". */
15530 q = p;
15531 while (q[0] == '.' && vim_ispathsep(q[1]))
15532 q += 2;
15533 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015534 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 }
15536 }
15537
15538 /* Ensure that the result will have no trailing path separator
15539 * if the argument had none. But keep "/" or "//". */
15540 if (!has_trailing_pathsep)
15541 {
15542 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015543 if (after_pathsep(p, q))
15544 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 }
15546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 }
15549# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015550 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551# endif
15552#endif
15553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015554 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555
15556#ifdef HAVE_READLINK
15557fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015558 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561}
15562
15563/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015564 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 */
15566 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015568 typval_T *argvars;
15569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570{
Bram Moolenaar33570922005-01-25 22:26:29 +000015571 list_T *l;
15572 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573
Bram Moolenaar0d660222005-01-07 21:51:51 +000015574 if (argvars[0].v_type != VAR_LIST)
15575 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015576 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015577 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015578 {
15579 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015580 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015581 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 while (li != NULL)
15583 {
15584 ni = li->li_prev;
15585 list_append(l, li);
15586 li = ni;
15587 }
15588 rettv->vval.v_list = l;
15589 rettv->v_type = VAR_LIST;
15590 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015591 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593}
15594
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015595#define SP_NOMOVE 0x01 /* don't move cursor */
15596#define SP_REPEAT 0x02 /* repeat to find outer pair */
15597#define SP_RETCOUNT 0x04 /* return matchcount */
15598#define SP_SETPCMARK 0x08 /* set previous context mark */
15599#define SP_START 0x10 /* accept match at start position */
15600#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15601#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015602
Bram Moolenaar33570922005-01-25 22:26:29 +000015603static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604
15605/*
15606 * Get flags for a search function.
15607 * Possibly sets "p_ws".
15608 * Returns BACKWARD, FORWARD or zero (for an error).
15609 */
15610 static int
15611get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015612 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015613 int *flagsp;
15614{
15615 int dir = FORWARD;
15616 char_u *flags;
15617 char_u nbuf[NUMBUFLEN];
15618 int mask;
15619
15620 if (varp->v_type != VAR_UNKNOWN)
15621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015622 flags = get_tv_string_buf_chk(varp, nbuf);
15623 if (flags == NULL)
15624 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015625 while (*flags != NUL)
15626 {
15627 switch (*flags)
15628 {
15629 case 'b': dir = BACKWARD; break;
15630 case 'w': p_ws = TRUE; break;
15631 case 'W': p_ws = FALSE; break;
15632 default: mask = 0;
15633 if (flagsp != NULL)
15634 switch (*flags)
15635 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015636 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015637 case 'e': mask = SP_END; break;
15638 case 'm': mask = SP_RETCOUNT; break;
15639 case 'n': mask = SP_NOMOVE; break;
15640 case 'p': mask = SP_SUBPAT; break;
15641 case 'r': mask = SP_REPEAT; break;
15642 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015643 }
15644 if (mask == 0)
15645 {
15646 EMSG2(_(e_invarg2), flags);
15647 dir = 0;
15648 }
15649 else
15650 *flagsp |= mask;
15651 }
15652 if (dir == 0)
15653 break;
15654 ++flags;
15655 }
15656 }
15657 return dir;
15658}
15659
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015661 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015663 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015664search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015665 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015666 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015667 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015669 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 char_u *pat;
15671 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015672 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 int save_p_ws = p_ws;
15674 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015675 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015676 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015677 proftime_T tm;
15678#ifdef FEAT_RELTIME
15679 long time_limit = 0;
15680#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015681 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015682 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015684 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015685 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015686 if (dir == 0)
15687 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015688 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015689 if (flags & SP_START)
15690 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015691 if (flags & SP_END)
15692 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015693
Bram Moolenaar76929292008-01-06 19:07:36 +000015694 /* Optional arguments: line number to stop searching and timeout. */
15695 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015696 {
15697 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15698 if (lnum_stop < 0)
15699 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015700#ifdef FEAT_RELTIME
15701 if (argvars[3].v_type != VAR_UNKNOWN)
15702 {
15703 time_limit = get_tv_number_chk(&argvars[3], NULL);
15704 if (time_limit < 0)
15705 goto theend;
15706 }
15707#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 }
15709
Bram Moolenaar76929292008-01-06 19:07:36 +000015710#ifdef FEAT_RELTIME
15711 /* Set the time limit, if there is one. */
15712 profile_setlimit(time_limit, &tm);
15713#endif
15714
Bram Moolenaar231334e2005-07-25 20:46:57 +000015715 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015716 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015717 * Check to make sure only those flags are set.
15718 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15719 * flags cannot be set. Check for that condition also.
15720 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015721 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015722 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015724 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015725 goto theend;
15726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015728 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015729 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015730 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015731 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015733 if (flags & SP_SUBPAT)
15734 retval = subpatnum;
15735 else
15736 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015737 if (flags & SP_SETPCMARK)
15738 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015740 if (match_pos != NULL)
15741 {
15742 /* Store the match cursor position */
15743 match_pos->lnum = pos.lnum;
15744 match_pos->col = pos.col + 1;
15745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 /* "/$" will put the cursor after the end of the line, may need to
15747 * correct that here */
15748 check_cursor();
15749 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015750
15751 /* If 'n' flag is used: restore cursor position. */
15752 if (flags & SP_NOMOVE)
15753 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015754 else
15755 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015756theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015758
15759 return retval;
15760}
15761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015762#ifdef FEAT_FLOAT
15763/*
15764 * "round({float})" function
15765 */
15766 static void
15767f_round(argvars, rettv)
15768 typval_T *argvars;
15769 typval_T *rettv;
15770{
15771 float_T f;
15772
15773 rettv->v_type = VAR_FLOAT;
15774 if (get_float_arg(argvars, &f) == OK)
15775 /* round() is not in C90, use ceil() or floor() instead. */
15776 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15777 else
15778 rettv->vval.v_float = 0.0;
15779}
15780#endif
15781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015782/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015783 * "screencol()" function
15784 *
15785 * First column is 1 to be consistent with virtcol().
15786 */
15787 static void
15788f_screencol(argvars, rettv)
15789 typval_T *argvars UNUSED;
15790 typval_T *rettv;
15791{
15792 rettv->vval.v_number = screen_screencol() + 1;
15793}
15794
15795/*
15796 * "screenrow()" function
15797 */
15798 static void
15799f_screenrow(argvars, rettv)
15800 typval_T *argvars UNUSED;
15801 typval_T *rettv;
15802{
15803 rettv->vval.v_number = screen_screenrow() + 1;
15804}
15805
15806/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015807 * "search()" function
15808 */
15809 static void
15810f_search(argvars, rettv)
15811 typval_T *argvars;
15812 typval_T *rettv;
15813{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015814 int flags = 0;
15815
15816 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817}
15818
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015820 * "searchdecl()" function
15821 */
15822 static void
15823f_searchdecl(argvars, rettv)
15824 typval_T *argvars;
15825 typval_T *rettv;
15826{
15827 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015828 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015829 int error = FALSE;
15830 char_u *name;
15831
15832 rettv->vval.v_number = 1; /* default: FAIL */
15833
15834 name = get_tv_string_chk(&argvars[0]);
15835 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015836 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015837 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015838 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15839 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15840 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015841 if (!error && name != NULL)
15842 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015843 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015844}
15845
15846/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015849 static int
15850searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015851 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015852 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853{
15854 char_u *spat, *mpat, *epat;
15855 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 int dir;
15858 int flags = 0;
15859 char_u nbuf1[NUMBUFLEN];
15860 char_u nbuf2[NUMBUFLEN];
15861 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015862 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015863 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015864 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015867 spat = get_tv_string_chk(&argvars[0]);
15868 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15869 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15870 if (spat == NULL || mpat == NULL || epat == NULL)
15871 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 /* Handle the optional fourth argument: flags */
15874 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015875 if (dir == 0)
15876 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015877
15878 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015879 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15880 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015881 if ((flags & (SP_END | SP_SUBPAT)) != 0
15882 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015883 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015884 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015885 goto theend;
15886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015888 /* Using 'r' implies 'W', otherwise it doesn't work. */
15889 if (flags & SP_REPEAT)
15890 p_ws = FALSE;
15891
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015892 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015893 if (argvars[3].v_type == VAR_UNKNOWN
15894 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 skip = (char_u *)"";
15896 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015898 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015899 if (argvars[5].v_type != VAR_UNKNOWN)
15900 {
15901 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15902 if (lnum_stop < 0)
15903 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015904#ifdef FEAT_RELTIME
15905 if (argvars[6].v_type != VAR_UNKNOWN)
15906 {
15907 time_limit = get_tv_number_chk(&argvars[6], NULL);
15908 if (time_limit < 0)
15909 goto theend;
15910 }
15911#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015912 }
15913 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015914 if (skip == NULL)
15915 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015917 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015918 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015919
15920theend:
15921 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015922
15923 return retval;
15924}
15925
15926/*
15927 * "searchpair()" function
15928 */
15929 static void
15930f_searchpair(argvars, rettv)
15931 typval_T *argvars;
15932 typval_T *rettv;
15933{
15934 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15935}
15936
15937/*
15938 * "searchpairpos()" function
15939 */
15940 static void
15941f_searchpairpos(argvars, rettv)
15942 typval_T *argvars;
15943 typval_T *rettv;
15944{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015945 pos_T match_pos;
15946 int lnum = 0;
15947 int col = 0;
15948
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015949 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015950 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015951
15952 if (searchpair_cmn(argvars, &match_pos) > 0)
15953 {
15954 lnum = match_pos.lnum;
15955 col = match_pos.col;
15956 }
15957
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015958 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15959 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015960}
15961
15962/*
15963 * Search for a start/middle/end thing.
15964 * Used by searchpair(), see its documentation for the details.
15965 * Returns 0 or -1 for no match,
15966 */
15967 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015968do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15969 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015970 char_u *spat; /* start pattern */
15971 char_u *mpat; /* middle pattern */
15972 char_u *epat; /* end pattern */
15973 int dir; /* BACKWARD or FORWARD */
15974 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015975 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015976 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015977 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015978 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015979{
15980 char_u *save_cpo;
15981 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15982 long retval = 0;
15983 pos_T pos;
15984 pos_T firstpos;
15985 pos_T foundpos;
15986 pos_T save_cursor;
15987 pos_T save_pos;
15988 int n;
15989 int r;
15990 int nest = 1;
15991 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015992 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015993 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015994
15995 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15996 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015997 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015998
Bram Moolenaar76929292008-01-06 19:07:36 +000015999#ifdef FEAT_RELTIME
16000 /* Set the time limit, if there is one. */
16001 profile_setlimit(time_limit, &tm);
16002#endif
16003
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016004 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16005 * start/middle/end (pat3, for the top pair). */
16006 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16007 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16008 if (pat2 == NULL || pat3 == NULL)
16009 goto theend;
16010 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16011 if (*mpat == NUL)
16012 STRCPY(pat3, pat2);
16013 else
16014 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16015 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016016 if (flags & SP_START)
16017 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016018
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019 save_cursor = curwin->w_cursor;
16020 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016021 clearpos(&firstpos);
16022 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 pat = pat3;
16024 for (;;)
16025 {
16026 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016027 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16029 /* didn't find it or found the first match again: FAIL */
16030 break;
16031
16032 if (firstpos.lnum == 0)
16033 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016034 if (equalpos(pos, foundpos))
16035 {
16036 /* Found the same position again. Can happen with a pattern that
16037 * has "\zs" at the end and searching backwards. Advance one
16038 * character and try again. */
16039 if (dir == BACKWARD)
16040 decl(&pos);
16041 else
16042 incl(&pos);
16043 }
16044 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016046 /* clear the start flag to avoid getting stuck here */
16047 options &= ~SEARCH_START;
16048
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 /* If the skip pattern matches, ignore this match. */
16050 if (*skip != NUL)
16051 {
16052 save_pos = curwin->w_cursor;
16053 curwin->w_cursor = pos;
16054 r = eval_to_bool(skip, &err, NULL, FALSE);
16055 curwin->w_cursor = save_pos;
16056 if (err)
16057 {
16058 /* Evaluating {skip} caused an error, break here. */
16059 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016060 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 break;
16062 }
16063 if (r)
16064 continue;
16065 }
16066
16067 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16068 {
16069 /* Found end when searching backwards or start when searching
16070 * forward: nested pair. */
16071 ++nest;
16072 pat = pat2; /* nested, don't search for middle */
16073 }
16074 else
16075 {
16076 /* Found end when searching forward or start when searching
16077 * backward: end of (nested) pair; or found middle in outer pair. */
16078 if (--nest == 1)
16079 pat = pat3; /* outer level, search for middle */
16080 }
16081
16082 if (nest == 0)
16083 {
16084 /* Found the match: return matchcount or line number. */
16085 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016086 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016088 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016089 if (flags & SP_SETPCMARK)
16090 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 curwin->w_cursor = pos;
16092 if (!(flags & SP_REPEAT))
16093 break;
16094 nest = 1; /* search for next unmatched */
16095 }
16096 }
16097
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016098 if (match_pos != NULL)
16099 {
16100 /* Store the match cursor position */
16101 match_pos->lnum = curwin->w_cursor.lnum;
16102 match_pos->col = curwin->w_cursor.col + 1;
16103 }
16104
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016106 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 curwin->w_cursor = save_cursor;
16108
16109theend:
16110 vim_free(pat2);
16111 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016112 if (p_cpo == empty_option)
16113 p_cpo = save_cpo;
16114 else
16115 /* Darn, evaluating the {skip} expression changed the value. */
16116 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016117
16118 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119}
16120
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016121/*
16122 * "searchpos()" function
16123 */
16124 static void
16125f_searchpos(argvars, rettv)
16126 typval_T *argvars;
16127 typval_T *rettv;
16128{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016129 pos_T match_pos;
16130 int lnum = 0;
16131 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016132 int n;
16133 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016134
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016135 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016136 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016137
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016138 n = search_cmn(argvars, &match_pos, &flags);
16139 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016140 {
16141 lnum = match_pos.lnum;
16142 col = match_pos.col;
16143 }
16144
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016145 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16146 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016147 if (flags & SP_SUBPAT)
16148 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016149}
16150
16151
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152 static void
16153f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016157#ifdef FEAT_CLIENTSERVER
16158 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016159 char_u *server = get_tv_string_chk(&argvars[0]);
16160 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161
Bram Moolenaar0d660222005-01-07 21:51:51 +000016162 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016163 if (server == NULL || reply == NULL)
16164 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016165 if (check_restricted() || check_secure())
16166 return;
16167# ifdef FEAT_X11
16168 if (check_connection() == FAIL)
16169 return;
16170# endif
16171
16172 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016174 EMSG(_("E258: Unable to send to client"));
16175 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 rettv->vval.v_number = 0;
16178#else
16179 rettv->vval.v_number = -1;
16180#endif
16181}
16182
Bram Moolenaar0d660222005-01-07 21:51:51 +000016183 static void
16184f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016185 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187{
16188 char_u *r = NULL;
16189
16190#ifdef FEAT_CLIENTSERVER
16191# ifdef WIN32
16192 r = serverGetVimNames();
16193# else
16194 make_connection();
16195 if (X_DISPLAY != NULL)
16196 r = serverGetVimNames(X_DISPLAY);
16197# endif
16198#endif
16199 rettv->v_type = VAR_STRING;
16200 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201}
16202
16203/*
16204 * "setbufvar()" function
16205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016207f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016208 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016209 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210{
16211 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016214 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 char_u nbuf[NUMBUFLEN];
16216
16217 if (check_restricted() || check_secure())
16218 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016219 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16220 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016221 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 varp = &argvars[2];
16223
16224 if (buf != NULL && varname != NULL && varp != NULL)
16225 {
16226 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016228
16229 if (*varname == '&')
16230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016231 long numval;
16232 char_u *strval;
16233 int error = FALSE;
16234
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 numval = get_tv_number_chk(varp, &error);
16237 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016238 if (!error && strval != NULL)
16239 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 }
16241 else
16242 {
16243 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16244 if (bufvarname != NULL)
16245 {
16246 STRCPY(bufvarname, "b:");
16247 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016248 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 vim_free(bufvarname);
16250 }
16251 }
16252
16253 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256}
16257
16258/*
16259 * "setcmdpos()" function
16260 */
16261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016263 typval_T *argvars;
16264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016266 int pos = (int)get_tv_number(&argvars[0]) - 1;
16267
16268 if (pos >= 0)
16269 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270}
16271
16272/*
16273 * "setline()" function
16274 */
16275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016276f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016277 typval_T *argvars;
16278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279{
16280 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016281 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016282 list_T *l = NULL;
16283 listitem_T *li = NULL;
16284 long added = 0;
16285 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016287 lnum = get_tv_lnum(&argvars[0]);
16288 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016290 l = argvars[1].vval.v_list;
16291 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016293 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016295
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016296 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016297 for (;;)
16298 {
16299 if (l != NULL)
16300 {
16301 /* list argument, get next string */
16302 if (li == NULL)
16303 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016305 li = li->li_next;
16306 }
16307
16308 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016310 break;
16311 if (lnum <= curbuf->b_ml.ml_line_count)
16312 {
16313 /* existing line, replace it */
16314 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16315 {
16316 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016317 if (lnum == curwin->w_cursor.lnum)
16318 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016319 rettv->vval.v_number = 0; /* OK */
16320 }
16321 }
16322 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16323 {
16324 /* lnum is one past the last line, append the line */
16325 ++added;
16326 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16327 rettv->vval.v_number = 0; /* OK */
16328 }
16329
16330 if (l == NULL) /* only one string argument */
16331 break;
16332 ++lnum;
16333 }
16334
16335 if (added > 0)
16336 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337}
16338
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016339static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16340
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016342 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016343 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016344 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016345set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016346 win_T *wp UNUSED;
16347 typval_T *list_arg UNUSED;
16348 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016349 typval_T *rettv;
16350{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016351#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016352 char_u *act;
16353 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016354#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016355
Bram Moolenaar2641f772005-03-25 21:58:17 +000016356 rettv->vval.v_number = -1;
16357
16358#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016359 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016360 EMSG(_(e_listreq));
16361 else
16362 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016363 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016364
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016365 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016366 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016367 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 if (act == NULL)
16369 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016370 if (*act == 'a' || *act == 'r')
16371 action = *act;
16372 }
16373
Bram Moolenaar81484f42012-12-05 15:16:47 +010016374 if (l != NULL && set_errorlist(wp, l, action,
16375 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016376 rettv->vval.v_number = 0;
16377 }
16378#endif
16379}
16380
16381/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016382 * "setloclist()" function
16383 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016384 static void
16385f_setloclist(argvars, rettv)
16386 typval_T *argvars;
16387 typval_T *rettv;
16388{
16389 win_T *win;
16390
16391 rettv->vval.v_number = -1;
16392
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016393 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016394 if (win != NULL)
16395 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16396}
16397
16398/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016399 * "setmatches()" function
16400 */
16401 static void
16402f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016403 typval_T *argvars UNUSED;
16404 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016405{
16406#ifdef FEAT_SEARCH_EXTRA
16407 list_T *l;
16408 listitem_T *li;
16409 dict_T *d;
16410
16411 rettv->vval.v_number = -1;
16412 if (argvars[0].v_type != VAR_LIST)
16413 {
16414 EMSG(_(e_listreq));
16415 return;
16416 }
16417 if ((l = argvars[0].vval.v_list) != NULL)
16418 {
16419
16420 /* To some extent make sure that we are dealing with a list from
16421 * "getmatches()". */
16422 li = l->lv_first;
16423 while (li != NULL)
16424 {
16425 if (li->li_tv.v_type != VAR_DICT
16426 || (d = li->li_tv.vval.v_dict) == NULL)
16427 {
16428 EMSG(_(e_invarg));
16429 return;
16430 }
16431 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16432 && dict_find(d, (char_u *)"pattern", -1) != NULL
16433 && dict_find(d, (char_u *)"priority", -1) != NULL
16434 && dict_find(d, (char_u *)"id", -1) != NULL))
16435 {
16436 EMSG(_(e_invarg));
16437 return;
16438 }
16439 li = li->li_next;
16440 }
16441
16442 clear_matches(curwin);
16443 li = l->lv_first;
16444 while (li != NULL)
16445 {
16446 d = li->li_tv.vval.v_dict;
16447 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16448 get_dict_string(d, (char_u *)"pattern", FALSE),
16449 (int)get_dict_number(d, (char_u *)"priority"),
16450 (int)get_dict_number(d, (char_u *)"id"));
16451 li = li->li_next;
16452 }
16453 rettv->vval.v_number = 0;
16454 }
16455#endif
16456}
16457
16458/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016459 * "setpos()" function
16460 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016461 static void
16462f_setpos(argvars, rettv)
16463 typval_T *argvars;
16464 typval_T *rettv;
16465{
16466 pos_T pos;
16467 int fnum;
16468 char_u *name;
16469
Bram Moolenaar08250432008-02-13 11:42:46 +000016470 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016471 name = get_tv_string_chk(argvars);
16472 if (name != NULL)
16473 {
16474 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16475 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016476 if (--pos.col < 0)
16477 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016478 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016479 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016480 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016481 if (fnum == curbuf->b_fnum)
16482 {
16483 curwin->w_cursor = pos;
16484 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016485 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016486 }
16487 else
16488 EMSG(_(e_invarg));
16489 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016490 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16491 {
16492 /* set mark */
16493 if (setmark_pos(name[1], &pos, fnum) == OK)
16494 rettv->vval.v_number = 0;
16495 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016496 else
16497 EMSG(_(e_invarg));
16498 }
16499 }
16500}
16501
16502/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016503 * "setqflist()" function
16504 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016505 static void
16506f_setqflist(argvars, rettv)
16507 typval_T *argvars;
16508 typval_T *rettv;
16509{
16510 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16511}
16512
16513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514 * "setreg()" function
16515 */
16516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016517f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 typval_T *argvars;
16519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520{
16521 int regname;
16522 char_u *strregname;
16523 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016524 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 int append;
16526 char_u yank_type;
16527 long block_len;
16528
16529 block_len = -1;
16530 yank_type = MAUTO;
16531 append = FALSE;
16532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016534 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 if (strregname == NULL)
16537 return; /* type error; errmsg already given */
16538 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 if (regname == 0 || regname == '@')
16540 regname = '"';
16541 else if (regname == '=')
16542 return;
16543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016546 stropt = get_tv_string_chk(&argvars[2]);
16547 if (stropt == NULL)
16548 return; /* type error */
16549 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 switch (*stropt)
16551 {
16552 case 'a': case 'A': /* append */
16553 append = TRUE;
16554 break;
16555 case 'v': case 'c': /* character-wise selection */
16556 yank_type = MCHAR;
16557 break;
16558 case 'V': case 'l': /* line-wise selection */
16559 yank_type = MLINE;
16560 break;
16561#ifdef FEAT_VISUAL
16562 case 'b': case Ctrl_V: /* block-wise selection */
16563 yank_type = MBLOCK;
16564 if (VIM_ISDIGIT(stropt[1]))
16565 {
16566 ++stropt;
16567 block_len = getdigits(&stropt) - 1;
16568 --stropt;
16569 }
16570 break;
16571#endif
16572 }
16573 }
16574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 strval = get_tv_string_chk(&argvars[1]);
16576 if (strval != NULL)
16577 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016579 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016580}
16581
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016582/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016583 * "settabvar()" function
16584 */
16585 static void
16586f_settabvar(argvars, rettv)
16587 typval_T *argvars;
16588 typval_T *rettv;
16589{
16590 tabpage_T *save_curtab;
16591 char_u *varname, *tabvarname;
16592 typval_T *varp;
16593 tabpage_T *tp;
16594
16595 rettv->vval.v_number = 0;
16596
16597 if (check_restricted() || check_secure())
16598 return;
16599
16600 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16601 varname = get_tv_string_chk(&argvars[1]);
16602 varp = &argvars[2];
16603
16604 if (tp != NULL && varname != NULL && varp != NULL)
16605 {
16606 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016607 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016608
16609 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16610 if (tabvarname != NULL)
16611 {
16612 STRCPY(tabvarname, "t:");
16613 STRCPY(tabvarname + 2, varname);
16614 set_var(tabvarname, varp, TRUE);
16615 vim_free(tabvarname);
16616 }
16617
16618 /* Restore current tabpage */
16619 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016620 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016621 }
16622}
16623
16624/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016625 * "settabwinvar()" function
16626 */
16627 static void
16628f_settabwinvar(argvars, rettv)
16629 typval_T *argvars;
16630 typval_T *rettv;
16631{
16632 setwinvar(argvars, rettv, 1);
16633}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634
16635/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016636 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016639f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016640 typval_T *argvars;
16641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016643 setwinvar(argvars, rettv, 0);
16644}
16645
16646/*
16647 * "setwinvar()" and "settabwinvar()" functions
16648 */
16649 static void
16650setwinvar(argvars, rettv, off)
16651 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016652 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016653 int off;
16654{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655 win_T *win;
16656#ifdef FEAT_WINDOWS
16657 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016658 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659#endif
16660 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016661 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016663 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664
16665 if (check_restricted() || check_secure())
16666 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016667
16668#ifdef FEAT_WINDOWS
16669 if (off == 1)
16670 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16671 else
16672 tp = curtab;
16673#endif
16674 win = find_win_by_nr(&argvars[off], tp);
16675 varname = get_tv_string_chk(&argvars[off + 1]);
16676 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677
16678 if (win != NULL && varname != NULL && varp != NULL)
16679 {
16680#ifdef FEAT_WINDOWS
16681 /* set curwin to be our win, temporarily */
16682 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016683 save_curtab = curtab;
Bram Moolenaara8596c42012-06-13 14:28:20 +020016684 goto_tabpage_tp(tp, TRUE);
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016685 if (!win_valid(win))
16686 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 curwin = win;
16688 curbuf = curwin->w_buffer;
16689#endif
16690
16691 if (*varname == '&')
16692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016693 long numval;
16694 char_u *strval;
16695 int error = FALSE;
16696
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016698 numval = get_tv_number_chk(varp, &error);
16699 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 if (!error && strval != NULL)
16701 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016702 }
16703 else
16704 {
16705 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16706 if (winvarname != NULL)
16707 {
16708 STRCPY(winvarname, "w:");
16709 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016710 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711 vim_free(winvarname);
16712 }
16713 }
16714
16715#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016716 /* Restore current tabpage and window, if still valid (autocomands can
16717 * make them invalid). */
16718 if (valid_tabpage(save_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +020016719 goto_tabpage_tp(save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 if (win_valid(save_curwin))
16721 {
16722 curwin = save_curwin;
16723 curbuf = curwin->w_buffer;
16724 }
16725#endif
16726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727}
16728
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016729#ifdef FEAT_CRYPT
16730/*
16731 * "sha256({string})" function
16732 */
16733 static void
16734f_sha256(argvars, rettv)
16735 typval_T *argvars;
16736 typval_T *rettv;
16737{
16738 char_u *p;
16739
16740 p = get_tv_string(&argvars[0]);
16741 rettv->vval.v_string = vim_strsave(
16742 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16743 rettv->v_type = VAR_STRING;
16744}
16745#endif /* FEAT_CRYPT */
16746
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016748 * "shellescape({string})" function
16749 */
16750 static void
16751f_shellescape(argvars, rettv)
16752 typval_T *argvars;
16753 typval_T *rettv;
16754{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016755 rettv->vval.v_string = vim_strsave_shellescape(
16756 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016757 rettv->v_type = VAR_STRING;
16758}
16759
16760/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016761 * shiftwidth() function
16762 */
16763 static void
16764f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016765 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016766 typval_T *rettv;
16767{
16768 rettv->vval.v_number = get_sw_value();
16769}
16770
16771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016772 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 */
16774 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016776 typval_T *argvars;
16777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016779 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 p = get_tv_string(&argvars[0]);
16782 rettv->vval.v_string = vim_strsave(p);
16783 simplify_filename(rettv->vval.v_string); /* simplify in place */
16784 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785}
16786
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016787#ifdef FEAT_FLOAT
16788/*
16789 * "sin()" function
16790 */
16791 static void
16792f_sin(argvars, rettv)
16793 typval_T *argvars;
16794 typval_T *rettv;
16795{
16796 float_T f;
16797
16798 rettv->v_type = VAR_FLOAT;
16799 if (get_float_arg(argvars, &f) == OK)
16800 rettv->vval.v_float = sin(f);
16801 else
16802 rettv->vval.v_float = 0.0;
16803}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016804
16805/*
16806 * "sinh()" function
16807 */
16808 static void
16809f_sinh(argvars, rettv)
16810 typval_T *argvars;
16811 typval_T *rettv;
16812{
16813 float_T f;
16814
16815 rettv->v_type = VAR_FLOAT;
16816 if (get_float_arg(argvars, &f) == OK)
16817 rettv->vval.v_float = sinh(f);
16818 else
16819 rettv->vval.v_float = 0.0;
16820}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016821#endif
16822
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823static int
16824#ifdef __BORLANDC__
16825 _RTLENTRYF
16826#endif
16827 item_compare __ARGS((const void *s1, const void *s2));
16828static int
16829#ifdef __BORLANDC__
16830 _RTLENTRYF
16831#endif
16832 item_compare2 __ARGS((const void *s1, const void *s2));
16833
16834static int item_compare_ic;
16835static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016836static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016837static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016838#define ITEM_COMPARE_FAIL 999
16839
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016841 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016843 static int
16844#ifdef __BORLANDC__
16845_RTLENTRYF
16846#endif
16847item_compare(s1, s2)
16848 const void *s1;
16849 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016851 char_u *p1, *p2;
16852 char_u *tofree1, *tofree2;
16853 int res;
16854 char_u numbuf1[NUMBUFLEN];
16855 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016857 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16858 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016859 if (p1 == NULL)
16860 p1 = (char_u *)"";
16861 if (p2 == NULL)
16862 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016863 if (item_compare_ic)
16864 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866 res = STRCMP(p1, p2);
16867 vim_free(tofree1);
16868 vim_free(tofree2);
16869 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870}
16871
16872 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873#ifdef __BORLANDC__
16874_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016875#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016876item_compare2(s1, s2)
16877 const void *s1;
16878 const void *s2;
16879{
16880 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016881 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016882 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016885 /* shortcut after failure in previous call; compare all items equal */
16886 if (item_compare_func_err)
16887 return 0;
16888
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016889 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16890 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016891 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16892 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893
16894 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016895 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016896 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16897 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898 clear_tv(&argv[0]);
16899 clear_tv(&argv[1]);
16900
16901 if (res == FAIL)
16902 res = ITEM_COMPARE_FAIL;
16903 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016904 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16905 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016906 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 clear_tv(&rettv);
16908 return res;
16909}
16910
16911/*
16912 * "sort({list})" function
16913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016916 typval_T *argvars;
16917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918{
Bram Moolenaar33570922005-01-25 22:26:29 +000016919 list_T *l;
16920 listitem_T *li;
16921 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 long len;
16923 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 if (argvars[0].v_type != VAR_LIST)
16926 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 else
16928 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016930 if (l == NULL || tv_check_lock(l->lv_lock,
16931 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932 return;
16933 rettv->vval.v_list = l;
16934 rettv->v_type = VAR_LIST;
16935 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937 len = list_len(l);
16938 if (len <= 1)
16939 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 item_compare_ic = FALSE;
16942 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016943 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944 if (argvars[1].v_type != VAR_UNKNOWN)
16945 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016946 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016948 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 else
16950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016951 int error = FALSE;
16952
16953 i = get_tv_number_chk(&argvars[1], &error);
16954 if (error)
16955 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 if (i == 1)
16957 item_compare_ic = TRUE;
16958 else
16959 item_compare_func = get_tv_string(&argvars[1]);
16960 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016961
16962 if (argvars[2].v_type != VAR_UNKNOWN)
16963 {
16964 /* optional third argument: {dict} */
16965 if (argvars[2].v_type != VAR_DICT)
16966 {
16967 EMSG(_(e_dictreq));
16968 return;
16969 }
16970 item_compare_selfdict = argvars[2].vval.v_dict;
16971 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016975 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 if (ptrs == NULL)
16977 return;
16978 i = 0;
16979 for (li = l->lv_first; li != NULL; li = li->li_next)
16980 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016982 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 /* test the compare function */
16984 if (item_compare_func != NULL
16985 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16986 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016987 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 {
16990 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 item_compare_func == NULL ? item_compare : item_compare2);
16993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016994 if (!item_compare_func_err)
16995 {
16996 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016997 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 l->lv_len = 0;
16999 for (i = 0; i < len; ++i)
17000 list_append(l, ptrs[i]);
17001 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 }
17003
17004 vim_free(ptrs);
17005 }
17006}
17007
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017008/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017009 * "soundfold({word})" function
17010 */
17011 static void
17012f_soundfold(argvars, rettv)
17013 typval_T *argvars;
17014 typval_T *rettv;
17015{
17016 char_u *s;
17017
17018 rettv->v_type = VAR_STRING;
17019 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017020#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017021 rettv->vval.v_string = eval_soundfold(s);
17022#else
17023 rettv->vval.v_string = vim_strsave(s);
17024#endif
17025}
17026
17027/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017028 * "spellbadword()" function
17029 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017030 static void
17031f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017032 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017033 typval_T *rettv;
17034{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017035 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017036 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017037 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017038
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017039 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017040 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017041
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017042#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017043 if (argvars[0].v_type == VAR_UNKNOWN)
17044 {
17045 /* Find the start and length of the badly spelled word. */
17046 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17047 if (len != 0)
17048 word = ml_get_cursor();
17049 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017050 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017051 {
17052 char_u *str = get_tv_string_chk(&argvars[0]);
17053 int capcol = -1;
17054
17055 if (str != NULL)
17056 {
17057 /* Check the argument for spelling. */
17058 while (*str != NUL)
17059 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017060 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017061 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017062 {
17063 word = str;
17064 break;
17065 }
17066 str += len;
17067 }
17068 }
17069 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017070#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017071
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017072 list_append_string(rettv->vval.v_list, word, len);
17073 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017074 attr == HLF_SPB ? "bad" :
17075 attr == HLF_SPR ? "rare" :
17076 attr == HLF_SPL ? "local" :
17077 attr == HLF_SPC ? "caps" :
17078 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017079}
17080
17081/*
17082 * "spellsuggest()" function
17083 */
17084 static void
17085f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017086 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017087 typval_T *rettv;
17088{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017089#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017090 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017091 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017092 int maxcount;
17093 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017094 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017095 listitem_T *li;
17096 int need_capital = FALSE;
17097#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017098
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017099 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017100 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017101
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017102#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017103 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017104 {
17105 str = get_tv_string(&argvars[0]);
17106 if (argvars[1].v_type != VAR_UNKNOWN)
17107 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017108 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017109 if (maxcount <= 0)
17110 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017111 if (argvars[2].v_type != VAR_UNKNOWN)
17112 {
17113 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17114 if (typeerr)
17115 return;
17116 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017117 }
17118 else
17119 maxcount = 25;
17120
Bram Moolenaar4770d092006-01-12 23:22:24 +000017121 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017122
17123 for (i = 0; i < ga.ga_len; ++i)
17124 {
17125 str = ((char_u **)ga.ga_data)[i];
17126
17127 li = listitem_alloc();
17128 if (li == NULL)
17129 vim_free(str);
17130 else
17131 {
17132 li->li_tv.v_type = VAR_STRING;
17133 li->li_tv.v_lock = 0;
17134 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017135 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017136 }
17137 }
17138 ga_clear(&ga);
17139 }
17140#endif
17141}
17142
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017144f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017145 typval_T *argvars;
17146 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017147{
17148 char_u *str;
17149 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017150 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151 regmatch_T regmatch;
17152 char_u patbuf[NUMBUFLEN];
17153 char_u *save_cpo;
17154 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017155 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017156 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017157 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017158
17159 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17160 save_cpo = p_cpo;
17161 p_cpo = (char_u *)"";
17162
17163 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017164 if (argvars[1].v_type != VAR_UNKNOWN)
17165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17167 if (pat == NULL)
17168 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017169 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017170 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017171 }
17172 if (pat == NULL || *pat == NUL)
17173 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017175 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 if (typeerr)
17178 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17181 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017184 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017186 if (*str == NUL)
17187 match = FALSE; /* empty item at the end */
17188 else
17189 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 if (match)
17191 end = regmatch.startp[0];
17192 else
17193 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017194 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17195 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017197 if (list_append_string(rettv->vval.v_list, str,
17198 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017199 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200 }
17201 if (!match)
17202 break;
17203 /* Advance to just after the match. */
17204 if (regmatch.endp[0] > str)
17205 col = 0;
17206 else
17207 {
17208 /* Don't get stuck at the same match. */
17209#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017210 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017211#else
17212 col = 1;
17213#endif
17214 }
17215 str = regmatch.endp[0];
17216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017217
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222}
17223
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017224#ifdef FEAT_FLOAT
17225/*
17226 * "sqrt()" function
17227 */
17228 static void
17229f_sqrt(argvars, rettv)
17230 typval_T *argvars;
17231 typval_T *rettv;
17232{
17233 float_T f;
17234
17235 rettv->v_type = VAR_FLOAT;
17236 if (get_float_arg(argvars, &f) == OK)
17237 rettv->vval.v_float = sqrt(f);
17238 else
17239 rettv->vval.v_float = 0.0;
17240}
17241
17242/*
17243 * "str2float()" function
17244 */
17245 static void
17246f_str2float(argvars, rettv)
17247 typval_T *argvars;
17248 typval_T *rettv;
17249{
17250 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17251
17252 if (*p == '+')
17253 p = skipwhite(p + 1);
17254 (void)string2float(p, &rettv->vval.v_float);
17255 rettv->v_type = VAR_FLOAT;
17256}
17257#endif
17258
Bram Moolenaar2c932302006-03-18 21:42:09 +000017259/*
17260 * "str2nr()" function
17261 */
17262 static void
17263f_str2nr(argvars, rettv)
17264 typval_T *argvars;
17265 typval_T *rettv;
17266{
17267 int base = 10;
17268 char_u *p;
17269 long n;
17270
17271 if (argvars[1].v_type != VAR_UNKNOWN)
17272 {
17273 base = get_tv_number(&argvars[1]);
17274 if (base != 8 && base != 10 && base != 16)
17275 {
17276 EMSG(_(e_invarg));
17277 return;
17278 }
17279 }
17280
17281 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017282 if (*p == '+')
17283 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017284 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17285 rettv->vval.v_number = n;
17286}
17287
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288#ifdef HAVE_STRFTIME
17289/*
17290 * "strftime({format}[, {time}])" function
17291 */
17292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017294 typval_T *argvars;
17295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296{
17297 char_u result_buf[256];
17298 struct tm *curtime;
17299 time_t seconds;
17300 char_u *p;
17301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017304 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017305 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306 seconds = time(NULL);
17307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017308 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 curtime = localtime(&seconds);
17310 /* MSVC returns NULL for an invalid value of seconds. */
17311 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 else
17314 {
17315# ifdef FEAT_MBYTE
17316 vimconv_T conv;
17317 char_u *enc;
17318
17319 conv.vc_type = CONV_NONE;
17320 enc = enc_locale();
17321 convert_setup(&conv, p_enc, enc);
17322 if (conv.vc_type != CONV_NONE)
17323 p = string_convert(&conv, p, NULL);
17324# endif
17325 if (p != NULL)
17326 (void)strftime((char *)result_buf, sizeof(result_buf),
17327 (char *)p, curtime);
17328 else
17329 result_buf[0] = NUL;
17330
17331# ifdef FEAT_MBYTE
17332 if (conv.vc_type != CONV_NONE)
17333 vim_free(p);
17334 convert_setup(&conv, enc, p_enc);
17335 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 else
17338# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017339 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
17341# ifdef FEAT_MBYTE
17342 /* Release conversion descriptors */
17343 convert_setup(&conv, NULL, NULL);
17344 vim_free(enc);
17345# endif
17346 }
17347}
17348#endif
17349
17350/*
17351 * "stridx()" function
17352 */
17353 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017354f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017355 typval_T *argvars;
17356 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357{
17358 char_u buf[NUMBUFLEN];
17359 char_u *needle;
17360 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017361 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017363 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 needle = get_tv_string_chk(&argvars[1]);
17366 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368 if (needle == NULL || haystack == NULL)
17369 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370
Bram Moolenaar33570922005-01-25 22:26:29 +000017371 if (argvars[2].v_type != VAR_UNKNOWN)
17372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 int error = FALSE;
17374
17375 start_idx = get_tv_number_chk(&argvars[2], &error);
17376 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017377 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017378 if (start_idx >= 0)
17379 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017380 }
17381
17382 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17383 if (pos != NULL)
17384 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385}
17386
17387/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017388 * "string()" function
17389 */
17390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017391f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017392 typval_T *argvars;
17393 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017394{
17395 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017396 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017398 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017399 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017400 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017401 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017402 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403}
17404
17405/*
17406 * "strlen()" function
17407 */
17408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 typval_T *argvars;
17411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413 rettv->vval.v_number = (varnumber_T)(STRLEN(
17414 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415}
17416
17417/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017418 * "strchars()" function
17419 */
17420 static void
17421f_strchars(argvars, rettv)
17422 typval_T *argvars;
17423 typval_T *rettv;
17424{
17425 char_u *s = get_tv_string(&argvars[0]);
17426#ifdef FEAT_MBYTE
17427 varnumber_T len = 0;
17428
17429 while (*s != NUL)
17430 {
17431 mb_cptr2char_adv(&s);
17432 ++len;
17433 }
17434 rettv->vval.v_number = len;
17435#else
17436 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17437#endif
17438}
17439
17440/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017441 * "strdisplaywidth()" function
17442 */
17443 static void
17444f_strdisplaywidth(argvars, rettv)
17445 typval_T *argvars;
17446 typval_T *rettv;
17447{
17448 char_u *s = get_tv_string(&argvars[0]);
17449 int col = 0;
17450
17451 if (argvars[1].v_type != VAR_UNKNOWN)
17452 col = get_tv_number(&argvars[1]);
17453
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017454 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017455}
17456
17457/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017458 * "strwidth()" function
17459 */
17460 static void
17461f_strwidth(argvars, rettv)
17462 typval_T *argvars;
17463 typval_T *rettv;
17464{
17465 char_u *s = get_tv_string(&argvars[0]);
17466
17467 rettv->vval.v_number = (varnumber_T)(
17468#ifdef FEAT_MBYTE
17469 mb_string2cells(s, -1)
17470#else
17471 STRLEN(s)
17472#endif
17473 );
17474}
17475
17476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 * "strpart()" function
17478 */
17479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017480f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017481 typval_T *argvars;
17482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483{
17484 char_u *p;
17485 int n;
17486 int len;
17487 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017488 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 slen = (int)STRLEN(p);
17492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017493 n = get_tv_number_chk(&argvars[1], &error);
17494 if (error)
17495 len = 0;
17496 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 else
17499 len = slen - n; /* default len: all bytes that are available. */
17500
17501 /*
17502 * Only return the overlap between the specified part and the actual
17503 * string.
17504 */
17505 if (n < 0)
17506 {
17507 len += n;
17508 n = 0;
17509 }
17510 else if (n > slen)
17511 n = slen;
17512 if (len < 0)
17513 len = 0;
17514 else if (n + len > slen)
17515 len = slen - n;
17516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017517 rettv->v_type = VAR_STRING;
17518 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519}
17520
17521/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017522 * "strridx()" function
17523 */
17524 static void
17525f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 typval_T *argvars;
17527 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017528{
17529 char_u buf[NUMBUFLEN];
17530 char_u *needle;
17531 char_u *haystack;
17532 char_u *rest;
17533 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017534 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017536 needle = get_tv_string_chk(&argvars[1]);
17537 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017538
17539 rettv->vval.v_number = -1;
17540 if (needle == NULL || haystack == NULL)
17541 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017542
17543 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017544 if (argvars[2].v_type != VAR_UNKNOWN)
17545 {
17546 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017547 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017548 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017549 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017550 }
17551 else
17552 end_idx = haystack_len;
17553
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017555 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017557 lastmatch = haystack + end_idx;
17558 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017560 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017561 for (rest = haystack; *rest != '\0'; ++rest)
17562 {
17563 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017564 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017565 break;
17566 lastmatch = rest;
17567 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017568 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569
17570 if (lastmatch == NULL)
17571 rettv->vval.v_number = -1;
17572 else
17573 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17574}
17575
17576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 * "strtrans()" function
17578 */
17579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017580f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017581 typval_T *argvars;
17582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017584 rettv->v_type = VAR_STRING;
17585 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586}
17587
17588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017589 * "submatch()" function
17590 */
17591 static void
17592f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017593 typval_T *argvars;
17594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595{
17596 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017597 rettv->vval.v_string =
17598 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017599}
17600
17601/*
17602 * "substitute()" function
17603 */
17604 static void
17605f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017606 typval_T *argvars;
17607 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017608{
17609 char_u patbuf[NUMBUFLEN];
17610 char_u subbuf[NUMBUFLEN];
17611 char_u flagsbuf[NUMBUFLEN];
17612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017613 char_u *str = get_tv_string_chk(&argvars[0]);
17614 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17615 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17616 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17617
Bram Moolenaar0d660222005-01-07 21:51:51 +000017618 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017619 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17620 rettv->vval.v_string = NULL;
17621 else
17622 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017623}
17624
17625/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017626 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017629f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017630 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632{
17633 int id = 0;
17634#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017635 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 long col;
17637 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017638 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017640 lnum = get_tv_lnum(argvars); /* -1 on type error */
17641 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17642 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017644 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017645 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017646 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647#endif
17648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017649 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650}
17651
17652/*
17653 * "synIDattr(id, what [, mode])" function
17654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017656f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017657 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659{
17660 char_u *p = NULL;
17661#ifdef FEAT_SYN_HL
17662 int id;
17663 char_u *what;
17664 char_u *mode;
17665 char_u modebuf[NUMBUFLEN];
17666 int modec;
17667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 id = get_tv_number(&argvars[0]);
17669 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017670 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017672 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017674 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 modec = 0; /* replace invalid with current */
17676 }
17677 else
17678 {
17679#ifdef FEAT_GUI
17680 if (gui.in_use)
17681 modec = 'g';
17682 else
17683#endif
17684 if (t_colors > 1)
17685 modec = 'c';
17686 else
17687 modec = 't';
17688 }
17689
17690
17691 switch (TOLOWER_ASC(what[0]))
17692 {
17693 case 'b':
17694 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17695 p = highlight_color(id, what, modec);
17696 else /* bold */
17697 p = highlight_has_attr(id, HL_BOLD, modec);
17698 break;
17699
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017700 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 p = highlight_color(id, what, modec);
17702 break;
17703
17704 case 'i':
17705 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17706 p = highlight_has_attr(id, HL_INVERSE, modec);
17707 else /* italic */
17708 p = highlight_has_attr(id, HL_ITALIC, modec);
17709 break;
17710
17711 case 'n': /* name */
17712 p = get_highlight_name(NULL, id - 1);
17713 break;
17714
17715 case 'r': /* reverse */
17716 p = highlight_has_attr(id, HL_INVERSE, modec);
17717 break;
17718
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017719 case 's':
17720 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17721 p = highlight_color(id, what, modec);
17722 else /* standout */
17723 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 break;
17725
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017726 case 'u':
17727 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17728 /* underline */
17729 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17730 else
17731 /* undercurl */
17732 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 break;
17734 }
17735
17736 if (p != NULL)
17737 p = vim_strsave(p);
17738#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017739 rettv->v_type = VAR_STRING;
17740 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741}
17742
17743/*
17744 * "synIDtrans(id)" function
17745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017747f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750{
17751 int id;
17752
17753#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755
17756 if (id > 0)
17757 id = syn_get_final_id(id);
17758 else
17759#endif
17760 id = 0;
17761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763}
17764
17765/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017766 * "synconcealed(lnum, col)" function
17767 */
17768 static void
17769f_synconcealed(argvars, rettv)
17770 typval_T *argvars UNUSED;
17771 typval_T *rettv;
17772{
17773#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17774 long lnum;
17775 long col;
17776 int syntax_flags = 0;
17777 int cchar;
17778 int matchid = 0;
17779 char_u str[NUMBUFLEN];
17780#endif
17781
17782 rettv->v_type = VAR_LIST;
17783 rettv->vval.v_list = NULL;
17784
17785#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17786 lnum = get_tv_lnum(argvars); /* -1 on type error */
17787 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17788
17789 vim_memset(str, NUL, sizeof(str));
17790
17791 if (rettv_list_alloc(rettv) != FAIL)
17792 {
17793 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17794 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17795 && curwin->w_p_cole > 0)
17796 {
17797 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17798 syntax_flags = get_syntax_info(&matchid);
17799
17800 /* get the conceal character */
17801 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17802 {
17803 cchar = syn_get_sub_char();
17804 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17805 cchar = lcs_conceal;
17806 if (cchar != NUL)
17807 {
17808# ifdef FEAT_MBYTE
17809 if (has_mbyte)
17810 (*mb_char2bytes)(cchar, str);
17811 else
17812# endif
17813 str[0] = cchar;
17814 }
17815 }
17816 }
17817
17818 list_append_number(rettv->vval.v_list,
17819 (syntax_flags & HL_CONCEAL) != 0);
17820 /* -1 to auto-determine strlen */
17821 list_append_string(rettv->vval.v_list, str, -1);
17822 list_append_number(rettv->vval.v_list, matchid);
17823 }
17824#endif
17825}
17826
17827/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017828 * "synstack(lnum, col)" function
17829 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017830 static void
17831f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017832 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017833 typval_T *rettv;
17834{
17835#ifdef FEAT_SYN_HL
17836 long lnum;
17837 long col;
17838 int i;
17839 int id;
17840#endif
17841
17842 rettv->v_type = VAR_LIST;
17843 rettv->vval.v_list = NULL;
17844
17845#ifdef FEAT_SYN_HL
17846 lnum = get_tv_lnum(argvars); /* -1 on type error */
17847 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17848
17849 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017850 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017851 && rettv_list_alloc(rettv) != FAIL)
17852 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017853 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017854 for (i = 0; ; ++i)
17855 {
17856 id = syn_get_stack_item(i);
17857 if (id < 0)
17858 break;
17859 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17860 break;
17861 }
17862 }
17863#endif
17864}
17865
17866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 * "system()" function
17868 */
17869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017870f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017871 typval_T *argvars;
17872 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017874 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017876 char_u *infile = NULL;
17877 char_u buf[NUMBUFLEN];
17878 int err = FALSE;
17879 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017881 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017882 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017883
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017884 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017885 {
17886 /*
17887 * Write the string to a temp file, to be used for input of the shell
17888 * command.
17889 */
17890 if ((infile = vim_tempname('i')) == NULL)
17891 {
17892 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017893 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017894 }
17895
17896 fd = mch_fopen((char *)infile, WRITEBIN);
17897 if (fd == NULL)
17898 {
17899 EMSG2(_(e_notopen), infile);
17900 goto done;
17901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017902 p = get_tv_string_buf_chk(&argvars[1], buf);
17903 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017904 {
17905 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017906 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017907 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017908 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17909 err = TRUE;
17910 if (fclose(fd) != 0)
17911 err = TRUE;
17912 if (err)
17913 {
17914 EMSG(_("E677: Error writing temp file"));
17915 goto done;
17916 }
17917 }
17918
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017919 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17920 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017921
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922#ifdef USE_CR
17923 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017924 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 {
17926 char_u *s;
17927
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017928 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 {
17930 if (*s == CAR)
17931 *s = NL;
17932 }
17933 }
17934#else
17935# ifdef USE_CRNL
17936 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017937 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 {
17939 char_u *s, *d;
17940
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017941 d = res;
17942 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 {
17944 if (s[0] == CAR && s[1] == NL)
17945 ++s;
17946 *d++ = *s;
17947 }
17948 *d = NUL;
17949 }
17950# endif
17951#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017952
17953done:
17954 if (infile != NULL)
17955 {
17956 mch_remove(infile);
17957 vim_free(infile);
17958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017959 rettv->v_type = VAR_STRING;
17960 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961}
17962
17963/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017964 * "tabpagebuflist()" function
17965 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017966 static void
17967f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017968 typval_T *argvars UNUSED;
17969 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017970{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017971#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017972 tabpage_T *tp;
17973 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017974
17975 if (argvars[0].v_type == VAR_UNKNOWN)
17976 wp = firstwin;
17977 else
17978 {
17979 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17980 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017981 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017982 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017983 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017984 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017985 for (; wp != NULL; wp = wp->w_next)
17986 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017987 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017988 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017989 }
17990#endif
17991}
17992
17993
17994/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017995 * "tabpagenr()" function
17996 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017997 static void
17998f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017999 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018000 typval_T *rettv;
18001{
18002 int nr = 1;
18003#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018004 char_u *arg;
18005
18006 if (argvars[0].v_type != VAR_UNKNOWN)
18007 {
18008 arg = get_tv_string_chk(&argvars[0]);
18009 nr = 0;
18010 if (arg != NULL)
18011 {
18012 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018013 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018014 else
18015 EMSG2(_(e_invexpr2), arg);
18016 }
18017 }
18018 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018019 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018020#endif
18021 rettv->vval.v_number = nr;
18022}
18023
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018024
18025#ifdef FEAT_WINDOWS
18026static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18027
18028/*
18029 * Common code for tabpagewinnr() and winnr().
18030 */
18031 static int
18032get_winnr(tp, argvar)
18033 tabpage_T *tp;
18034 typval_T *argvar;
18035{
18036 win_T *twin;
18037 int nr = 1;
18038 win_T *wp;
18039 char_u *arg;
18040
18041 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18042 if (argvar->v_type != VAR_UNKNOWN)
18043 {
18044 arg = get_tv_string_chk(argvar);
18045 if (arg == NULL)
18046 nr = 0; /* type error; errmsg already given */
18047 else if (STRCMP(arg, "$") == 0)
18048 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18049 else if (STRCMP(arg, "#") == 0)
18050 {
18051 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18052 if (twin == NULL)
18053 nr = 0;
18054 }
18055 else
18056 {
18057 EMSG2(_(e_invexpr2), arg);
18058 nr = 0;
18059 }
18060 }
18061
18062 if (nr > 0)
18063 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18064 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018065 {
18066 if (wp == NULL)
18067 {
18068 /* didn't find it in this tabpage */
18069 nr = 0;
18070 break;
18071 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018072 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018073 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018074 return nr;
18075}
18076#endif
18077
18078/*
18079 * "tabpagewinnr()" function
18080 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018081 static void
18082f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018083 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018084 typval_T *rettv;
18085{
18086 int nr = 1;
18087#ifdef FEAT_WINDOWS
18088 tabpage_T *tp;
18089
18090 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18091 if (tp == NULL)
18092 nr = 0;
18093 else
18094 nr = get_winnr(tp, &argvars[1]);
18095#endif
18096 rettv->vval.v_number = nr;
18097}
18098
18099
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018100/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018101 * "tagfiles()" function
18102 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018103 static void
18104f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018105 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018106 typval_T *rettv;
18107{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018108 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018109 tagname_T tn;
18110 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018111
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018112 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018113 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018114 fname = alloc(MAXPATHL);
18115 if (fname == NULL)
18116 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018118 for (first = TRUE; ; first = FALSE)
18119 if (get_tagfname(&tn, first, fname) == FAIL
18120 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018121 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018122 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018123 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018124}
18125
18126/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018127 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018128 */
18129 static void
18130f_taglist(argvars, rettv)
18131 typval_T *argvars;
18132 typval_T *rettv;
18133{
18134 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018135
18136 tag_pattern = get_tv_string(&argvars[0]);
18137
18138 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018139 if (*tag_pattern == NUL)
18140 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018141
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018142 if (rettv_list_alloc(rettv) == OK)
18143 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018144}
18145
18146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 * "tempname()" function
18148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018150f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018151 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153{
18154 static int x = 'A';
18155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018156 rettv->v_type = VAR_STRING;
18157 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158
18159 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18160 * names. Skip 'I' and 'O', they are used for shell redirection. */
18161 do
18162 {
18163 if (x == 'Z')
18164 x = '0';
18165 else if (x == '9')
18166 x = 'A';
18167 else
18168 {
18169#ifdef EBCDIC
18170 if (x == 'I')
18171 x = 'J';
18172 else if (x == 'R')
18173 x = 'S';
18174 else
18175#endif
18176 ++x;
18177 }
18178 } while (x == 'I' || x == 'O');
18179}
18180
18181/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018182 * "test(list)" function: Just checking the walls...
18183 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018184 static void
18185f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018186 typval_T *argvars UNUSED;
18187 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018188{
18189 /* Used for unit testing. Change the code below to your liking. */
18190#if 0
18191 listitem_T *li;
18192 list_T *l;
18193 char_u *bad, *good;
18194
18195 if (argvars[0].v_type != VAR_LIST)
18196 return;
18197 l = argvars[0].vval.v_list;
18198 if (l == NULL)
18199 return;
18200 li = l->lv_first;
18201 if (li == NULL)
18202 return;
18203 bad = get_tv_string(&li->li_tv);
18204 li = li->li_next;
18205 if (li == NULL)
18206 return;
18207 good = get_tv_string(&li->li_tv);
18208 rettv->vval.v_number = test_edit_score(bad, good);
18209#endif
18210}
18211
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018212#ifdef FEAT_FLOAT
18213/*
18214 * "tan()" function
18215 */
18216 static void
18217f_tan(argvars, rettv)
18218 typval_T *argvars;
18219 typval_T *rettv;
18220{
18221 float_T f;
18222
18223 rettv->v_type = VAR_FLOAT;
18224 if (get_float_arg(argvars, &f) == OK)
18225 rettv->vval.v_float = tan(f);
18226 else
18227 rettv->vval.v_float = 0.0;
18228}
18229
18230/*
18231 * "tanh()" function
18232 */
18233 static void
18234f_tanh(argvars, rettv)
18235 typval_T *argvars;
18236 typval_T *rettv;
18237{
18238 float_T f;
18239
18240 rettv->v_type = VAR_FLOAT;
18241 if (get_float_arg(argvars, &f) == OK)
18242 rettv->vval.v_float = tanh(f);
18243 else
18244 rettv->vval.v_float = 0.0;
18245}
18246#endif
18247
Bram Moolenaard52d9742005-08-21 22:20:28 +000018248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 * "tolower(string)" function
18250 */
18251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018252f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018253 typval_T *argvars;
18254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255{
18256 char_u *p;
18257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018258 p = vim_strsave(get_tv_string(&argvars[0]));
18259 rettv->v_type = VAR_STRING;
18260 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261
18262 if (p != NULL)
18263 while (*p != NUL)
18264 {
18265#ifdef FEAT_MBYTE
18266 int l;
18267
18268 if (enc_utf8)
18269 {
18270 int c, lc;
18271
18272 c = utf_ptr2char(p);
18273 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018274 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 /* TODO: reallocate string when byte count changes. */
18276 if (utf_char2len(lc) == l)
18277 utf_char2bytes(lc, p);
18278 p += l;
18279 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018280 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 p += l; /* skip multi-byte character */
18282 else
18283#endif
18284 {
18285 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18286 ++p;
18287 }
18288 }
18289}
18290
18291/*
18292 * "toupper(string)" function
18293 */
18294 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018295f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018296 typval_T *argvars;
18297 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018299 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018300 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301}
18302
18303/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018304 * "tr(string, fromstr, tostr)" function
18305 */
18306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018308 typval_T *argvars;
18309 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018310{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018311 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018312 char_u *fromstr;
18313 char_u *tostr;
18314 char_u *p;
18315#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018316 int inlen;
18317 int fromlen;
18318 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018319 int idx;
18320 char_u *cpstr;
18321 int cplen;
18322 int first = TRUE;
18323#endif
18324 char_u buf[NUMBUFLEN];
18325 char_u buf2[NUMBUFLEN];
18326 garray_T ga;
18327
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018328 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018329 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18330 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018331
18332 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018333 rettv->v_type = VAR_STRING;
18334 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018335 if (fromstr == NULL || tostr == NULL)
18336 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018337 ga_init2(&ga, (int)sizeof(char), 80);
18338
18339#ifdef FEAT_MBYTE
18340 if (!has_mbyte)
18341#endif
18342 /* not multi-byte: fromstr and tostr must be the same length */
18343 if (STRLEN(fromstr) != STRLEN(tostr))
18344 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018345#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018346error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018347#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018348 EMSG2(_(e_invarg2), fromstr);
18349 ga_clear(&ga);
18350 return;
18351 }
18352
18353 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018354 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018355 {
18356#ifdef FEAT_MBYTE
18357 if (has_mbyte)
18358 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018359 inlen = (*mb_ptr2len)(in_str);
18360 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018361 cplen = inlen;
18362 idx = 0;
18363 for (p = fromstr; *p != NUL; p += fromlen)
18364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018365 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018366 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018367 {
18368 for (p = tostr; *p != NUL; p += tolen)
18369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018370 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018371 if (idx-- == 0)
18372 {
18373 cplen = tolen;
18374 cpstr = p;
18375 break;
18376 }
18377 }
18378 if (*p == NUL) /* tostr is shorter than fromstr */
18379 goto error;
18380 break;
18381 }
18382 ++idx;
18383 }
18384
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018385 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018386 {
18387 /* Check that fromstr and tostr have the same number of
18388 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018389 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018390 first = FALSE;
18391 for (p = tostr; *p != NUL; p += tolen)
18392 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018393 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018394 --idx;
18395 }
18396 if (idx != 0)
18397 goto error;
18398 }
18399
18400 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018401 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018402 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018403
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018404 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018405 }
18406 else
18407#endif
18408 {
18409 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018410 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018411 if (p != NULL)
18412 ga_append(&ga, tostr[p - fromstr]);
18413 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018414 ga_append(&ga, *in_str);
18415 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018416 }
18417 }
18418
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018419 /* add a terminating NUL */
18420 ga_grow(&ga, 1);
18421 ga_append(&ga, NUL);
18422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018424}
18425
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018426#ifdef FEAT_FLOAT
18427/*
18428 * "trunc({float})" function
18429 */
18430 static void
18431f_trunc(argvars, rettv)
18432 typval_T *argvars;
18433 typval_T *rettv;
18434{
18435 float_T f;
18436
18437 rettv->v_type = VAR_FLOAT;
18438 if (get_float_arg(argvars, &f) == OK)
18439 /* trunc() is not in C90, use floor() or ceil() instead. */
18440 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18441 else
18442 rettv->vval.v_float = 0.0;
18443}
18444#endif
18445
Bram Moolenaar8299df92004-07-10 09:47:34 +000018446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 * "type(expr)" function
18448 */
18449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018450f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018451 typval_T *argvars;
18452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018454 int n;
18455
18456 switch (argvars[0].v_type)
18457 {
18458 case VAR_NUMBER: n = 0; break;
18459 case VAR_STRING: n = 1; break;
18460 case VAR_FUNC: n = 2; break;
18461 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018462 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018463#ifdef FEAT_FLOAT
18464 case VAR_FLOAT: n = 5; break;
18465#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018466 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18467 }
18468 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469}
18470
18471/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018472 * "undofile(name)" function
18473 */
18474 static void
18475f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018476 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018477 typval_T *rettv;
18478{
18479 rettv->v_type = VAR_STRING;
18480#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018481 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018482 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018483
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018484 if (*fname == NUL)
18485 {
18486 /* If there is no file name there will be no undo file. */
18487 rettv->vval.v_string = NULL;
18488 }
18489 else
18490 {
18491 char_u *ffname = FullName_save(fname, FALSE);
18492
18493 if (ffname != NULL)
18494 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18495 vim_free(ffname);
18496 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018497 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018498#else
18499 rettv->vval.v_string = NULL;
18500#endif
18501}
18502
18503/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018504 * "undotree()" function
18505 */
18506 static void
18507f_undotree(argvars, rettv)
18508 typval_T *argvars UNUSED;
18509 typval_T *rettv;
18510{
18511 if (rettv_dict_alloc(rettv) == OK)
18512 {
18513 dict_T *dict = rettv->vval.v_dict;
18514 list_T *list;
18515
Bram Moolenaar730cde92010-06-27 05:18:54 +020018516 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018517 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018518 dict_add_nr_str(dict, "save_last",
18519 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018520 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18521 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018522 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018523
18524 list = list_alloc();
18525 if (list != NULL)
18526 {
18527 u_eval_tree(curbuf->b_u_oldhead, list);
18528 dict_add_list(dict, "entries", list);
18529 }
18530 }
18531}
18532
18533/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018534 * "values(dict)" function
18535 */
18536 static void
18537f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018538 typval_T *argvars;
18539 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018540{
18541 dict_list(argvars, rettv, 1);
18542}
18543
18544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 * "virtcol(string)" function
18546 */
18547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 typval_T *argvars;
18550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551{
18552 colnr_T vcol = 0;
18553 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018554 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018556 fp = var2fpos(&argvars[0], FALSE, &fnum);
18557 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18558 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 {
18560 getvvcol(curwin, fp, NULL, NULL, &vcol);
18561 ++vcol;
18562 }
18563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018564 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565}
18566
18567/*
18568 * "visualmode()" function
18569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018572 typval_T *argvars UNUSED;
18573 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574{
18575#ifdef FEAT_VISUAL
18576 char_u str[2];
18577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018578 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 str[0] = curbuf->b_visual_mode_eval;
18580 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582
18583 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018584 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586#endif
18587}
18588
18589/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018590 * "wildmenumode()" function
18591 */
18592 static void
18593f_wildmenumode(argvars, rettv)
18594 typval_T *argvars UNUSED;
18595 typval_T *rettv UNUSED;
18596{
18597#ifdef FEAT_WILDMENU
18598 if (wild_menu_showing)
18599 rettv->vval.v_number = 1;
18600#endif
18601}
18602
18603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604 * "winbufnr(nr)" function
18605 */
18606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018607f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018608 typval_T *argvars;
18609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610{
18611 win_T *wp;
18612
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018613 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618}
18619
18620/*
18621 * "wincol()" function
18622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018625 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627{
18628 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018629 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630}
18631
18632/*
18633 * "winheight(nr)" function
18634 */
18635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018636f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *argvars;
18638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639{
18640 win_T *wp;
18641
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018642 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018645 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647}
18648
18649/*
18650 * "winline()" function
18651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018653f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018654 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656{
18657 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659}
18660
18661/*
18662 * "winnr()" function
18663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018665f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018666 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668{
18669 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018670
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018672 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018674 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675}
18676
18677/*
18678 * "winrestcmd()" function
18679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018682 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684{
18685#ifdef FEAT_WINDOWS
18686 win_T *wp;
18687 int winnr = 1;
18688 garray_T ga;
18689 char_u buf[50];
18690
18691 ga_init2(&ga, (int)sizeof(char), 70);
18692 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18693 {
18694 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18695 ga_concat(&ga, buf);
18696# ifdef FEAT_VERTSPLIT
18697 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18698 ga_concat(&ga, buf);
18699# endif
18700 ++winnr;
18701 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018702 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018708 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709}
18710
18711/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018712 * "winrestview()" function
18713 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018714 static void
18715f_winrestview(argvars, rettv)
18716 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018717 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018718{
18719 dict_T *dict;
18720
18721 if (argvars[0].v_type != VAR_DICT
18722 || (dict = argvars[0].vval.v_dict) == NULL)
18723 EMSG(_(e_invarg));
18724 else
18725 {
18726 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18727 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18728#ifdef FEAT_VIRTUALEDIT
18729 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18730#endif
18731 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018732 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018733
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018734 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018735#ifdef FEAT_DIFF
18736 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18737#endif
18738 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18739 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18740
18741 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018742 win_new_height(curwin, curwin->w_height);
18743# ifdef FEAT_VERTSPLIT
18744 win_new_width(curwin, W_WIDTH(curwin));
18745# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018746 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018747
18748 if (curwin->w_topline == 0)
18749 curwin->w_topline = 1;
18750 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18751 curwin->w_topline = curbuf->b_ml.ml_line_count;
18752#ifdef FEAT_DIFF
18753 check_topfill(curwin, TRUE);
18754#endif
18755 }
18756}
18757
18758/*
18759 * "winsaveview()" function
18760 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018761 static void
18762f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018763 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018764 typval_T *rettv;
18765{
18766 dict_T *dict;
18767
Bram Moolenaara800b422010-06-27 01:15:55 +020018768 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018769 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018770 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018771
18772 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18773 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18774#ifdef FEAT_VIRTUALEDIT
18775 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18776#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018777 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018778 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18779
18780 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18781#ifdef FEAT_DIFF
18782 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18783#endif
18784 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18785 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18786}
18787
18788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 * "winwidth(nr)" function
18790 */
18791 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018792f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018793 typval_T *argvars;
18794 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795{
18796 win_T *wp;
18797
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018798 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801 else
18802#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806#endif
18807}
18808
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018810 * "writefile()" function
18811 */
18812 static void
18813f_writefile(argvars, rettv)
18814 typval_T *argvars;
18815 typval_T *rettv;
18816{
18817 int binary = FALSE;
18818 char_u *fname;
18819 FILE *fd;
18820 listitem_T *li;
18821 char_u *s;
18822 int ret = 0;
18823 int c;
18824
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018825 if (check_restricted() || check_secure())
18826 return;
18827
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018828 if (argvars[0].v_type != VAR_LIST)
18829 {
18830 EMSG2(_(e_listarg), "writefile()");
18831 return;
18832 }
18833 if (argvars[0].vval.v_list == NULL)
18834 return;
18835
18836 if (argvars[2].v_type != VAR_UNKNOWN
18837 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18838 binary = TRUE;
18839
18840 /* Always open the file in binary mode, library functions have a mind of
18841 * their own about CR-LF conversion. */
18842 fname = get_tv_string(&argvars[1]);
18843 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18844 {
18845 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18846 ret = -1;
18847 }
18848 else
18849 {
18850 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18851 li = li->li_next)
18852 {
18853 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18854 {
18855 if (*s == '\n')
18856 c = putc(NUL, fd);
18857 else
18858 c = putc(*s, fd);
18859 if (c == EOF)
18860 {
18861 ret = -1;
18862 break;
18863 }
18864 }
18865 if (!binary || li->li_next != NULL)
18866 if (putc('\n', fd) == EOF)
18867 {
18868 ret = -1;
18869 break;
18870 }
18871 if (ret < 0)
18872 {
18873 EMSG(_(e_write));
18874 break;
18875 }
18876 }
18877 fclose(fd);
18878 }
18879
18880 rettv->vval.v_number = ret;
18881}
18882
18883/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018884 * "xor(expr, expr)" function
18885 */
18886 static void
18887f_xor(argvars, rettv)
18888 typval_T *argvars;
18889 typval_T *rettv;
18890{
18891 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18892 ^ get_tv_number_chk(&argvars[1], NULL);
18893}
18894
18895
18896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018898 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 */
18900 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018901var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018902 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018903 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018904 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018906 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018908 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909
Bram Moolenaara5525202006-03-02 22:52:09 +000018910 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018911 if (varp->v_type == VAR_LIST)
18912 {
18913 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018914 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018915 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018916 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018917
18918 l = varp->vval.v_list;
18919 if (l == NULL)
18920 return NULL;
18921
18922 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018923 pos.lnum = list_find_nr(l, 0L, &error);
18924 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018925 return NULL; /* invalid line number */
18926
18927 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018928 pos.col = list_find_nr(l, 1L, &error);
18929 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018930 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018931 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018932
18933 /* We accept "$" for the column number: last column. */
18934 li = list_find(l, 1L);
18935 if (li != NULL && li->li_tv.v_type == VAR_STRING
18936 && li->li_tv.vval.v_string != NULL
18937 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18938 pos.col = len + 1;
18939
Bram Moolenaara5525202006-03-02 22:52:09 +000018940 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018941 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018942 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018943 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018944
Bram Moolenaara5525202006-03-02 22:52:09 +000018945#ifdef FEAT_VIRTUALEDIT
18946 /* Get the virtual offset. Defaults to zero. */
18947 pos.coladd = list_find_nr(l, 2L, &error);
18948 if (error)
18949 pos.coladd = 0;
18950#endif
18951
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018952 return &pos;
18953 }
18954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018955 name = get_tv_string_chk(varp);
18956 if (name == NULL)
18957 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018958 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018960#ifdef FEAT_VISUAL
18961 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18962 {
18963 if (VIsual_active)
18964 return &VIsual;
18965 return &curwin->w_cursor;
18966 }
18967#endif
18968 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018970 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18972 return NULL;
18973 return pp;
18974 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018975
18976#ifdef FEAT_VIRTUALEDIT
18977 pos.coladd = 0;
18978#endif
18979
Bram Moolenaar477933c2007-07-17 14:32:23 +000018980 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018981 {
18982 pos.col = 0;
18983 if (name[1] == '0') /* "w0": first visible line */
18984 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018985 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018986 pos.lnum = curwin->w_topline;
18987 return &pos;
18988 }
18989 else if (name[1] == '$') /* "w$": last visible line */
18990 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018991 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018992 pos.lnum = curwin->w_botline - 1;
18993 return &pos;
18994 }
18995 }
18996 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018998 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 {
19000 pos.lnum = curbuf->b_ml.ml_line_count;
19001 pos.col = 0;
19002 }
19003 else
19004 {
19005 pos.lnum = curwin->w_cursor.lnum;
19006 pos.col = (colnr_T)STRLEN(ml_get_curline());
19007 }
19008 return &pos;
19009 }
19010 return NULL;
19011}
19012
19013/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019014 * Convert list in "arg" into a position and optional file number.
19015 * When "fnump" is NULL there is no file number, only 3 items.
19016 * Note that the column is passed on as-is, the caller may want to decrement
19017 * it to use 1 for the first column.
19018 * Return FAIL when conversion is not possible, doesn't check the position for
19019 * validity.
19020 */
19021 static int
19022list2fpos(arg, posp, fnump)
19023 typval_T *arg;
19024 pos_T *posp;
19025 int *fnump;
19026{
19027 list_T *l = arg->vval.v_list;
19028 long i = 0;
19029 long n;
19030
Bram Moolenaarbde35262006-07-23 20:12:24 +000019031 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19032 * when "fnump" isn't NULL and "coladd" is optional. */
19033 if (arg->v_type != VAR_LIST
19034 || l == NULL
19035 || l->lv_len < (fnump == NULL ? 2 : 3)
19036 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019037 return FAIL;
19038
19039 if (fnump != NULL)
19040 {
19041 n = list_find_nr(l, i++, NULL); /* fnum */
19042 if (n < 0)
19043 return FAIL;
19044 if (n == 0)
19045 n = curbuf->b_fnum; /* current buffer */
19046 *fnump = n;
19047 }
19048
19049 n = list_find_nr(l, i++, NULL); /* lnum */
19050 if (n < 0)
19051 return FAIL;
19052 posp->lnum = n;
19053
19054 n = list_find_nr(l, i++, NULL); /* col */
19055 if (n < 0)
19056 return FAIL;
19057 posp->col = n;
19058
19059#ifdef FEAT_VIRTUALEDIT
19060 n = list_find_nr(l, i, NULL);
19061 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019062 posp->coladd = 0;
19063 else
19064 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019065#endif
19066
19067 return OK;
19068}
19069
19070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 * Get the length of an environment variable name.
19072 * Advance "arg" to the first character after the name.
19073 * Return 0 for error.
19074 */
19075 static int
19076get_env_len(arg)
19077 char_u **arg;
19078{
19079 char_u *p;
19080 int len;
19081
19082 for (p = *arg; vim_isIDc(*p); ++p)
19083 ;
19084 if (p == *arg) /* no name found */
19085 return 0;
19086
19087 len = (int)(p - *arg);
19088 *arg = p;
19089 return len;
19090}
19091
19092/*
19093 * Get the length of the name of a function or internal variable.
19094 * "arg" is advanced to the first non-white character after the name.
19095 * Return 0 if something is wrong.
19096 */
19097 static int
19098get_id_len(arg)
19099 char_u **arg;
19100{
19101 char_u *p;
19102 int len;
19103
19104 /* Find the end of the name. */
19105 for (p = *arg; eval_isnamec(*p); ++p)
19106 ;
19107 if (p == *arg) /* no name found */
19108 return 0;
19109
19110 len = (int)(p - *arg);
19111 *arg = skipwhite(p);
19112
19113 return len;
19114}
19115
19116/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019117 * Get the length of the name of a variable or function.
19118 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019120 * Return -1 if curly braces expansion failed.
19121 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 * If the name contains 'magic' {}'s, expand them and return the
19123 * expanded name in an allocated string via 'alias' - caller must free.
19124 */
19125 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019126get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019127 char_u **arg;
19128 char_u **alias;
19129 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019130 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131{
19132 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 char_u *p;
19134 char_u *expr_start;
19135 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136
19137 *alias = NULL; /* default to no alias */
19138
19139 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19140 && (*arg)[2] == (int)KE_SNR)
19141 {
19142 /* hard coded <SNR>, already translated */
19143 *arg += 3;
19144 return get_id_len(arg) + 3;
19145 }
19146 len = eval_fname_script(*arg);
19147 if (len > 0)
19148 {
19149 /* literal "<SID>", "s:" or "<SNR>" */
19150 *arg += len;
19151 }
19152
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019154 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019156 p = find_name_end(*arg, &expr_start, &expr_end,
19157 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 if (expr_start != NULL)
19159 {
19160 char_u *temp_string;
19161
19162 if (!evaluate)
19163 {
19164 len += (int)(p - *arg);
19165 *arg = skipwhite(p);
19166 return len;
19167 }
19168
19169 /*
19170 * Include any <SID> etc in the expanded string:
19171 * Thus the -len here.
19172 */
19173 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19174 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019175 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 *alias = temp_string;
19177 *arg = skipwhite(p);
19178 return (int)STRLEN(temp_string);
19179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180
19181 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019182 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 EMSG2(_(e_invexpr2), *arg);
19184
19185 return len;
19186}
19187
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019188/*
19189 * Find the end of a variable or function name, taking care of magic braces.
19190 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19191 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019192 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019193 * Return a pointer to just after the name. Equal to "arg" if there is no
19194 * valid name.
19195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019197find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 char_u *arg;
19199 char_u **expr_start;
19200 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019201 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 int mb_nest = 0;
19204 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 char_u *p;
19206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019207 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019209 *expr_start = NULL;
19210 *expr_end = NULL;
19211 }
19212
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019213 /* Quick check for valid starting character. */
19214 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19215 return arg;
19216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 for (p = arg; *p != NUL
19218 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019219 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019220 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019221 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019222 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019224 if (*p == '\'')
19225 {
19226 /* skip over 'string' to avoid counting [ and ] inside it. */
19227 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19228 ;
19229 if (*p == NUL)
19230 break;
19231 }
19232 else if (*p == '"')
19233 {
19234 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19235 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19236 if (*p == '\\' && p[1] != NUL)
19237 ++p;
19238 if (*p == NUL)
19239 break;
19240 }
19241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019242 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 if (*p == '[')
19245 ++br_nest;
19246 else if (*p == ']')
19247 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019250 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 if (*p == '{')
19253 {
19254 mb_nest++;
19255 if (expr_start != NULL && *expr_start == NULL)
19256 *expr_start = p;
19257 }
19258 else if (*p == '}')
19259 {
19260 mb_nest--;
19261 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19262 *expr_end = p;
19263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 }
19266
19267 return p;
19268}
19269
19270/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019271 * Expands out the 'magic' {}'s in a variable/function name.
19272 * Note that this can call itself recursively, to deal with
19273 * constructs like foo{bar}{baz}{bam}
19274 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19275 * "in_start" ^
19276 * "expr_start" ^
19277 * "expr_end" ^
19278 * "in_end" ^
19279 *
19280 * Returns a new allocated string, which the caller must free.
19281 * Returns NULL for failure.
19282 */
19283 static char_u *
19284make_expanded_name(in_start, expr_start, expr_end, in_end)
19285 char_u *in_start;
19286 char_u *expr_start;
19287 char_u *expr_end;
19288 char_u *in_end;
19289{
19290 char_u c1;
19291 char_u *retval = NULL;
19292 char_u *temp_result;
19293 char_u *nextcmd = NULL;
19294
19295 if (expr_end == NULL || in_end == NULL)
19296 return NULL;
19297 *expr_start = NUL;
19298 *expr_end = NUL;
19299 c1 = *in_end;
19300 *in_end = NUL;
19301
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019302 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019303 if (temp_result != NULL && nextcmd == NULL)
19304 {
19305 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19306 + (in_end - expr_end) + 1));
19307 if (retval != NULL)
19308 {
19309 STRCPY(retval, in_start);
19310 STRCAT(retval, temp_result);
19311 STRCAT(retval, expr_end + 1);
19312 }
19313 }
19314 vim_free(temp_result);
19315
19316 *in_end = c1; /* put char back for error messages */
19317 *expr_start = '{';
19318 *expr_end = '}';
19319
19320 if (retval != NULL)
19321 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019322 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019323 if (expr_start != NULL)
19324 {
19325 /* Further expansion! */
19326 temp_result = make_expanded_name(retval, expr_start,
19327 expr_end, temp_result);
19328 vim_free(retval);
19329 retval = temp_result;
19330 }
19331 }
19332
19333 return retval;
19334}
19335
19336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019338 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 */
19340 static int
19341eval_isnamec(c)
19342 int c;
19343{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019344 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19345}
19346
19347/*
19348 * Return TRUE if character "c" can be used as the first character in a
19349 * variable or function name (excluding '{' and '}').
19350 */
19351 static int
19352eval_isnamec1(c)
19353 int c;
19354{
19355 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019356}
19357
19358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 * Set number v: variable to "val".
19360 */
19361 void
19362set_vim_var_nr(idx, val)
19363 int idx;
19364 long val;
19365{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019366 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367}
19368
19369/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019370 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371 */
19372 long
19373get_vim_var_nr(idx)
19374 int idx;
19375{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019376 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377}
19378
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019379/*
19380 * Get string v: variable value. Uses a static buffer, can only be used once.
19381 */
19382 char_u *
19383get_vim_var_str(idx)
19384 int idx;
19385{
19386 return get_tv_string(&vimvars[idx].vv_tv);
19387}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019388
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019390 * Get List v: variable value. Caller must take care of reference count when
19391 * needed.
19392 */
19393 list_T *
19394get_vim_var_list(idx)
19395 int idx;
19396{
19397 return vimvars[idx].vv_list;
19398}
19399
19400/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019401 * Set v:char to character "c".
19402 */
19403 void
19404set_vim_var_char(c)
19405 int c;
19406{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019407 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019408
19409#ifdef FEAT_MBYTE
19410 if (has_mbyte)
19411 buf[(*mb_char2bytes)(c, buf)] = NUL;
19412 else
19413#endif
19414 {
19415 buf[0] = c;
19416 buf[1] = NUL;
19417 }
19418 set_vim_var_string(VV_CHAR, buf, -1);
19419}
19420
19421/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019422 * Set v:count to "count" and v:count1 to "count1".
19423 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424 */
19425 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019426set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427 long count;
19428 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019429 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019431 if (set_prevcount)
19432 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019433 vimvars[VV_COUNT].vv_nr = count;
19434 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435}
19436
19437/*
19438 * Set string v: variable to a copy of "val".
19439 */
19440 void
19441set_vim_var_string(idx, val, len)
19442 int idx;
19443 char_u *val;
19444 int len; /* length of "val" to use or -1 (whole string) */
19445{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019446 /* Need to do this (at least) once, since we can't initialize a union.
19447 * Will always be invoked when "v:progname" is set. */
19448 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19449
Bram Moolenaare9a41262005-01-15 22:18:47 +000019450 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019452 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019454 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019456 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457}
19458
19459/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019460 * Set List v: variable to "val".
19461 */
19462 void
19463set_vim_var_list(idx, val)
19464 int idx;
19465 list_T *val;
19466{
19467 list_unref(vimvars[idx].vv_list);
19468 vimvars[idx].vv_list = val;
19469 if (val != NULL)
19470 ++val->lv_refcount;
19471}
19472
19473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 * Set v:register if needed.
19475 */
19476 void
19477set_reg_var(c)
19478 int c;
19479{
19480 char_u regname;
19481
19482 if (c == 0 || c == ' ')
19483 regname = '"';
19484 else
19485 regname = c;
19486 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019487 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 set_vim_var_string(VV_REG, &regname, 1);
19489}
19490
19491/*
19492 * Get or set v:exception. If "oldval" == NULL, return the current value.
19493 * Otherwise, restore the value to "oldval" and return NULL.
19494 * Must always be called in pairs to save and restore v:exception! Does not
19495 * take care of memory allocations.
19496 */
19497 char_u *
19498v_exception(oldval)
19499 char_u *oldval;
19500{
19501 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019502 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503
Bram Moolenaare9a41262005-01-15 22:18:47 +000019504 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 return NULL;
19506}
19507
19508/*
19509 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19510 * Otherwise, restore the value to "oldval" and return NULL.
19511 * Must always be called in pairs to save and restore v:throwpoint! Does not
19512 * take care of memory allocations.
19513 */
19514 char_u *
19515v_throwpoint(oldval)
19516 char_u *oldval;
19517{
19518 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019519 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520
Bram Moolenaare9a41262005-01-15 22:18:47 +000019521 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 return NULL;
19523}
19524
19525#if defined(FEAT_AUTOCMD) || defined(PROTO)
19526/*
19527 * Set v:cmdarg.
19528 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19529 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19530 * Must always be called in pairs!
19531 */
19532 char_u *
19533set_cmdarg(eap, oldarg)
19534 exarg_T *eap;
19535 char_u *oldarg;
19536{
19537 char_u *oldval;
19538 char_u *newval;
19539 unsigned len;
19540
Bram Moolenaare9a41262005-01-15 22:18:47 +000019541 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019542 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019544 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019545 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019546 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 }
19548
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019549 if (eap->force_bin == FORCE_BIN)
19550 len = 6;
19551 else if (eap->force_bin == FORCE_NOBIN)
19552 len = 8;
19553 else
19554 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019555
19556 if (eap->read_edit)
19557 len += 7;
19558
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019559 if (eap->force_ff != 0)
19560 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19561# ifdef FEAT_MBYTE
19562 if (eap->force_enc != 0)
19563 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019564 if (eap->bad_char != 0)
19565 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019566# endif
19567
19568 newval = alloc(len + 1);
19569 if (newval == NULL)
19570 return NULL;
19571
19572 if (eap->force_bin == FORCE_BIN)
19573 sprintf((char *)newval, " ++bin");
19574 else if (eap->force_bin == FORCE_NOBIN)
19575 sprintf((char *)newval, " ++nobin");
19576 else
19577 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019578
19579 if (eap->read_edit)
19580 STRCAT(newval, " ++edit");
19581
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019582 if (eap->force_ff != 0)
19583 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19584 eap->cmd + eap->force_ff);
19585# ifdef FEAT_MBYTE
19586 if (eap->force_enc != 0)
19587 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19588 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019589 if (eap->bad_char == BAD_KEEP)
19590 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19591 else if (eap->bad_char == BAD_DROP)
19592 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19593 else if (eap->bad_char != 0)
19594 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019595# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019596 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019597 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598}
19599#endif
19600
19601/*
19602 * Get the value of internal variable "name".
19603 * Return OK or FAIL.
19604 */
19605 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019606get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 char_u *name;
19608 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019609 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019610 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611{
19612 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 typval_T *tv = NULL;
19614 typval_T atv;
19615 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617
19618 /* truncate the name, so that we can use strcmp() */
19619 cc = name[len];
19620 name[len] = NUL;
19621
19622 /*
19623 * Check for "b:changedtick".
19624 */
19625 if (STRCMP(name, "b:changedtick") == 0)
19626 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019627 atv.v_type = VAR_NUMBER;
19628 atv.vval.v_number = curbuf->b_changedtick;
19629 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 }
19631
19632 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 * Check for user-defined variables.
19634 */
19635 else
19636 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019637 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 }
19641
Bram Moolenaare9a41262005-01-15 22:18:47 +000019642 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019644 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 ret = FAIL;
19647 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019649 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650
19651 name[len] = cc;
19652
19653 return ret;
19654}
19655
19656/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019657 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19658 * Also handle function call with Funcref variable: func(expr)
19659 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19660 */
19661 static int
19662handle_subscript(arg, rettv, evaluate, verbose)
19663 char_u **arg;
19664 typval_T *rettv;
19665 int evaluate; /* do more than finding the end */
19666 int verbose; /* give error messages */
19667{
19668 int ret = OK;
19669 dict_T *selfdict = NULL;
19670 char_u *s;
19671 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019672 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019673
19674 while (ret == OK
19675 && (**arg == '['
19676 || (**arg == '.' && rettv->v_type == VAR_DICT)
19677 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19678 && !vim_iswhite(*(*arg - 1)))
19679 {
19680 if (**arg == '(')
19681 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019682 /* need to copy the funcref so that we can clear rettv */
19683 functv = *rettv;
19684 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019685
19686 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019687 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019688 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019689 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19690 &len, evaluate, selfdict);
19691
19692 /* Clear the funcref afterwards, so that deleting it while
19693 * evaluating the arguments is possible (see test55). */
19694 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019695
19696 /* Stop the expression evaluation when immediately aborting on
19697 * error, or when an interrupt occurred or an exception was thrown
19698 * but not caught. */
19699 if (aborting())
19700 {
19701 if (ret == OK)
19702 clear_tv(rettv);
19703 ret = FAIL;
19704 }
19705 dict_unref(selfdict);
19706 selfdict = NULL;
19707 }
19708 else /* **arg == '[' || **arg == '.' */
19709 {
19710 dict_unref(selfdict);
19711 if (rettv->v_type == VAR_DICT)
19712 {
19713 selfdict = rettv->vval.v_dict;
19714 if (selfdict != NULL)
19715 ++selfdict->dv_refcount;
19716 }
19717 else
19718 selfdict = NULL;
19719 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19720 {
19721 clear_tv(rettv);
19722 ret = FAIL;
19723 }
19724 }
19725 }
19726 dict_unref(selfdict);
19727 return ret;
19728}
19729
19730/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019731 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019732 * value).
19733 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019736{
Bram Moolenaar33570922005-01-25 22:26:29 +000019737 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019738}
19739
19740/*
19741 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742 * The string "s" must have been allocated, it is consumed.
19743 * Return NULL for out of memory, the variable otherwise.
19744 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019745 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 char_u *s;
19748{
Bram Moolenaar33570922005-01-25 22:26:29 +000019749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019751 rettv = alloc_tv();
19752 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 rettv->v_type = VAR_STRING;
19755 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 }
19757 else
19758 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760}
19761
19762/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019763 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019765 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019766free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768{
19769 if (varp != NULL)
19770 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019771 switch (varp->v_type)
19772 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019773 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019774 func_unref(varp->vval.v_string);
19775 /*FALLTHROUGH*/
19776 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019777 vim_free(varp->vval.v_string);
19778 break;
19779 case VAR_LIST:
19780 list_unref(varp->vval.v_list);
19781 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019782 case VAR_DICT:
19783 dict_unref(varp->vval.v_dict);
19784 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019785 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019786#ifdef FEAT_FLOAT
19787 case VAR_FLOAT:
19788#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019789 case VAR_UNKNOWN:
19790 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019791 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019792 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019793 break;
19794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 vim_free(varp);
19796 }
19797}
19798
19799/*
19800 * Free the memory for a variable value and set the value to NULL or 0.
19801 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019802 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019804 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805{
19806 if (varp != NULL)
19807 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019810 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019811 func_unref(varp->vval.v_string);
19812 /*FALLTHROUGH*/
19813 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019814 vim_free(varp->vval.v_string);
19815 varp->vval.v_string = NULL;
19816 break;
19817 case VAR_LIST:
19818 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019819 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019820 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019821 case VAR_DICT:
19822 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019823 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019824 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019826 varp->vval.v_number = 0;
19827 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019828#ifdef FEAT_FLOAT
19829 case VAR_FLOAT:
19830 varp->vval.v_float = 0.0;
19831 break;
19832#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 case VAR_UNKNOWN:
19834 break;
19835 default:
19836 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019838 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 }
19840}
19841
19842/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 * Set the value of a variable to NULL without freeing items.
19844 */
19845 static void
19846init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019847 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848{
19849 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019850 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851}
19852
19853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 * Get the number value of a variable.
19855 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019856 * For incompatible types, return 0.
19857 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19858 * caller of incompatible types: it sets *denote to TRUE if "denote"
19859 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 */
19861 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019863 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019865 int error = FALSE;
19866
19867 return get_tv_number_chk(varp, &error); /* return 0L on error */
19868}
19869
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019870 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019871get_tv_number_chk(varp, denote)
19872 typval_T *varp;
19873 int *denote;
19874{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019877 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019879 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019881#ifdef FEAT_FLOAT
19882 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019883 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019884 break;
19885#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019886 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019887 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019888 break;
19889 case VAR_STRING:
19890 if (varp->vval.v_string != NULL)
19891 vim_str2nr(varp->vval.v_string, NULL, NULL,
19892 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019893 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019894 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019895 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019896 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019897 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019898 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019899 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019900 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019901 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019902 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019904 if (denote == NULL) /* useful for values that must be unsigned */
19905 n = -1;
19906 else
19907 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019908 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909}
19910
19911/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019912 * Get the lnum from the first argument.
19913 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019914 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 */
19916 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019917get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919{
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 linenr_T lnum;
19922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019923 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924 if (lnum == 0) /* no valid number, try using line() */
19925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926 rettv.v_type = VAR_NUMBER;
19927 f_line(argvars, &rettv);
19928 lnum = rettv.vval.v_number;
19929 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 }
19931 return lnum;
19932}
19933
19934/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019935 * Get the lnum from the first argument.
19936 * Also accepts "$", then "buf" is used.
19937 * Returns 0 on error.
19938 */
19939 static linenr_T
19940get_tv_lnum_buf(argvars, buf)
19941 typval_T *argvars;
19942 buf_T *buf;
19943{
19944 if (argvars[0].v_type == VAR_STRING
19945 && argvars[0].vval.v_string != NULL
19946 && argvars[0].vval.v_string[0] == '$'
19947 && buf != NULL)
19948 return buf->b_ml.ml_line_count;
19949 return get_tv_number_chk(&argvars[0], NULL);
19950}
19951
19952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 * Get the string value of a variable.
19954 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019955 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19956 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 * If the String variable has never been set, return an empty string.
19958 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019959 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19960 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961 */
19962 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019964 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965{
19966 static char_u mybuf[NUMBUFLEN];
19967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019968 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019969}
19970
19971 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019973 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019974 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019976 char_u *res = get_tv_string_buf_chk(varp, buf);
19977
19978 return res != NULL ? res : (char_u *)"";
19979}
19980
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019981 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019982get_tv_string_chk(varp)
19983 typval_T *varp;
19984{
19985 static char_u mybuf[NUMBUFLEN];
19986
19987 return get_tv_string_buf_chk(varp, mybuf);
19988}
19989
19990 static char_u *
19991get_tv_string_buf_chk(varp, buf)
19992 typval_T *varp;
19993 char_u *buf;
19994{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019997 case VAR_NUMBER:
19998 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19999 return buf;
20000 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020001 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020002 break;
20003 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020004 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020005 break;
20006 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020007 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020009#ifdef FEAT_FLOAT
20010 case VAR_FLOAT:
20011 EMSG(_("E806: using Float as a String"));
20012 break;
20013#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020014 case VAR_STRING:
20015 if (varp->vval.v_string != NULL)
20016 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020017 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020019 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020020 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020022 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023}
20024
20025/*
20026 * Find variable "name" in the list of variables.
20027 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020028 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020029 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020030 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020033find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020035 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020038 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039
Bram Moolenaara7043832005-01-21 11:56:39 +000020040 ht = find_var_ht(name, &varname);
20041 if (htp != NULL)
20042 *htp = ht;
20043 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020045 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046}
20047
20048/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020049 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020050 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020053find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020055 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020056 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020057 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020058{
Bram Moolenaar33570922005-01-25 22:26:29 +000020059 hashitem_T *hi;
20060
20061 if (*varname == NUL)
20062 {
20063 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020064 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020066 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 case 'g': return &globvars_var;
20068 case 'v': return &vimvars_var;
20069 case 'b': return &curbuf->b_bufvar;
20070 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020071#ifdef FEAT_WINDOWS
20072 case 't': return &curtab->tp_winvar;
20073#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020074 case 'l': return current_funccal == NULL
20075 ? NULL : &current_funccal->l_vars_var;
20076 case 'a': return current_funccal == NULL
20077 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020078 }
20079 return NULL;
20080 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020081
20082 hi = hash_find(ht, varname);
20083 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020084 {
20085 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020086 * worked find the variable again. Don't auto-load a script if it was
20087 * loaded already, otherwise it would be loaded every time when
20088 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020089 if (ht == &globvarht && !writing)
20090 {
20091 /* Note: script_autoload() may make "hi" invalid. It must either
20092 * be obtained again or not used. */
20093 if (!script_autoload(varname, FALSE) || aborting())
20094 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020095 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020096 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020097 if (HASHITEM_EMPTY(hi))
20098 return NULL;
20099 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020101}
20102
20103/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020105 * Set "varname" to the start of name without ':'.
20106 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020108find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 char_u *name;
20110 char_u **varname;
20111{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020112 hashitem_T *hi;
20113
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 if (name[1] != ':')
20115 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020116 /* The name must not start with a colon or #. */
20117 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 return NULL;
20119 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020120
20121 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020122 hi = hash_find(&compat_hashtab, name);
20123 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020124 return &compat_hashtab;
20125
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 return &globvarht; /* global variable */
20128 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 }
20130 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020131 if (*name == 'g') /* global variable */
20132 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020133 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20134 */
20135 if (vim_strchr(name + 2, ':') != NULL
20136 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020137 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020139 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020141 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020142#ifdef FEAT_WINDOWS
20143 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020144 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020145#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020146 if (*name == 'v') /* v: variable */
20147 return &vimvarht;
20148 if (*name == 'a' && current_funccal != NULL) /* function argument */
20149 return &current_funccal->l_avars.dv_hashtab;
20150 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20151 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 if (*name == 's' /* script variable */
20153 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20154 return &SCRIPT_VARS(current_SID);
20155 return NULL;
20156}
20157
20158/*
20159 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020160 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 * Returns NULL when it doesn't exist.
20162 */
20163 char_u *
20164get_var_value(name)
20165 char_u *name;
20166{
Bram Moolenaar33570922005-01-25 22:26:29 +000020167 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168
Bram Moolenaara7043832005-01-21 11:56:39 +000020169 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 if (v == NULL)
20171 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173}
20174
20175/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020176 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 * sourcing this script and when executing functions defined in the script.
20178 */
20179 void
20180new_script_vars(id)
20181 scid_T id;
20182{
Bram Moolenaara7043832005-01-21 11:56:39 +000020183 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 hashtab_T *ht;
20185 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020186
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20188 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020189 /* Re-allocating ga_data means that an ht_array pointing to
20190 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020191 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020192 for (i = 1; i <= ga_scripts.ga_len; ++i)
20193 {
20194 ht = &SCRIPT_VARS(i);
20195 if (ht->ht_mask == HT_INIT_SIZE - 1)
20196 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020197 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020199 }
20200
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 while (ga_scripts.ga_len < id)
20202 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020203 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020204 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020205 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 }
20208 }
20209}
20210
20211/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20213 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 */
20215 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020216init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020217 dict_T *dict;
20218 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020219 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220{
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020222 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020223 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020224 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020225 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 dict_var->di_tv.vval.v_dict = dict;
20227 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020228 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20230 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231}
20232
20233/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020234 * Unreference a dictionary initialized by init_var_dict().
20235 */
20236 void
20237unref_var_dict(dict)
20238 dict_T *dict;
20239{
20240 /* Now the dict needs to be freed if no one else is using it, go back to
20241 * normal reference counting. */
20242 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20243 dict_unref(dict);
20244}
20245
20246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020248 * Frees all allocated variables and the value they contain.
20249 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 */
20251 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020252vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020253 hashtab_T *ht;
20254{
20255 vars_clear_ext(ht, TRUE);
20256}
20257
20258/*
20259 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20260 */
20261 static void
20262vars_clear_ext(ht, free_val)
20263 hashtab_T *ht;
20264 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265{
Bram Moolenaara7043832005-01-21 11:56:39 +000020266 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020267 hashitem_T *hi;
20268 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020269
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020271 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020272 for (hi = ht->ht_array; todo > 0; ++hi)
20273 {
20274 if (!HASHITEM_EMPTY(hi))
20275 {
20276 --todo;
20277
Bram Moolenaar33570922005-01-25 22:26:29 +000020278 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020279 * ht_array might change then. hash_clear() takes care of it
20280 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020281 v = HI2DI(hi);
20282 if (free_val)
20283 clear_tv(&v->di_tv);
20284 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20285 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020286 }
20287 }
20288 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020289 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290}
20291
Bram Moolenaara7043832005-01-21 11:56:39 +000020292/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 * Delete a variable from hashtab "ht" at item "hi".
20294 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020297delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020298 hashtab_T *ht;
20299 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300{
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020302
20303 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020304 clear_tv(&di->di_tv);
20305 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
20308/*
20309 * List the value of one internal variable.
20310 */
20311 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020312list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020313 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020315 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020317 char_u *tofree;
20318 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020319 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020320
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020321 current_copyID += COPYID_INC;
20322 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020324 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020325 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020326}
20327
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020329list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 char_u *prefix;
20331 char_u *name;
20332 int type;
20333 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020334 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335{
Bram Moolenaar31859182007-08-14 20:41:13 +000020336 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20337 msg_start();
20338 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 if (name != NULL) /* "a:" vars don't have a name stored */
20340 msg_puts(name);
20341 msg_putchar(' ');
20342 msg_advance(22);
20343 if (type == VAR_NUMBER)
20344 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020345 else if (type == VAR_FUNC)
20346 msg_putchar('*');
20347 else if (type == VAR_LIST)
20348 {
20349 msg_putchar('[');
20350 if (*string == '[')
20351 ++string;
20352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020353 else if (type == VAR_DICT)
20354 {
20355 msg_putchar('{');
20356 if (*string == '{')
20357 ++string;
20358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 else
20360 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020361
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020363
20364 if (type == VAR_FUNC)
20365 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020366 if (*first)
20367 {
20368 msg_clr_eos();
20369 *first = FALSE;
20370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371}
20372
20373/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020374 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 * If the variable already exists, the value is updated.
20376 * Otherwise the variable is created.
20377 */
20378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383{
Bram Moolenaar33570922005-01-25 22:26:29 +000020384 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020388 ht = find_var_ht(name, &varname);
20389 if (ht == NULL || *varname == NUL)
20390 {
20391 EMSG2(_(e_illvar), name);
20392 return;
20393 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020394 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020395
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020396 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20397 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020398
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020401 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020402 if (var_check_ro(v->di_flags, name)
20403 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 return;
20405 if (v->di_tv.v_type != tv->v_type
20406 && !((v->di_tv.v_type == VAR_STRING
20407 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020408 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020409 || tv->v_type == VAR_NUMBER))
20410#ifdef FEAT_FLOAT
20411 && !((v->di_tv.v_type == VAR_NUMBER
20412 || v->di_tv.v_type == VAR_FLOAT)
20413 && (tv->v_type == VAR_NUMBER
20414 || tv->v_type == VAR_FLOAT))
20415#endif
20416 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020417 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020418 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020419 return;
20420 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020421
20422 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020423 * Handle setting internal v: variables separately: we don't change
20424 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020425 */
20426 if (ht == &vimvarht)
20427 {
20428 if (v->di_tv.v_type == VAR_STRING)
20429 {
20430 vim_free(v->di_tv.vval.v_string);
20431 if (copy || tv->v_type != VAR_STRING)
20432 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20433 else
20434 {
20435 /* Take over the string to avoid an extra alloc/free. */
20436 v->di_tv.vval.v_string = tv->vval.v_string;
20437 tv->vval.v_string = NULL;
20438 }
20439 }
20440 else if (v->di_tv.v_type != VAR_NUMBER)
20441 EMSG2(_(e_intern2), "set_var()");
20442 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020443 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020444 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020445 if (STRCMP(varname, "searchforward") == 0)
20446 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20447 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020448 return;
20449 }
20450
20451 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 }
20453 else /* add a new variable */
20454 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020455 /* Can't add "v:" variable. */
20456 if (ht == &vimvarht)
20457 {
20458 EMSG2(_(e_illvar), name);
20459 return;
20460 }
20461
Bram Moolenaar92124a32005-06-17 22:03:40 +000020462 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020463 if (!valid_varname(varname))
20464 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020465
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020466 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20467 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020468 if (v == NULL)
20469 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020473 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 return;
20475 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020476 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020478
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020479 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020480 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020481 else
20482 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020483 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020484 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020485 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487}
20488
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020489/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020490 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020491 * Also give an error message.
20492 */
20493 static int
20494var_check_ro(flags, name)
20495 int flags;
20496 char_u *name;
20497{
20498 if (flags & DI_FLAGS_RO)
20499 {
20500 EMSG2(_(e_readonlyvar), name);
20501 return TRUE;
20502 }
20503 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20504 {
20505 EMSG2(_(e_readonlysbx), name);
20506 return TRUE;
20507 }
20508 return FALSE;
20509}
20510
20511/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020512 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20513 * Also give an error message.
20514 */
20515 static int
20516var_check_fixed(flags, name)
20517 int flags;
20518 char_u *name;
20519{
20520 if (flags & DI_FLAGS_FIX)
20521 {
20522 EMSG2(_("E795: Cannot delete variable %s"), name);
20523 return TRUE;
20524 }
20525 return FALSE;
20526}
20527
20528/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020529 * Check if a funcref is assigned to a valid variable name.
20530 * Return TRUE and give an error if not.
20531 */
20532 static int
20533var_check_func_name(name, new_var)
20534 char_u *name; /* points to start of variable name */
20535 int new_var; /* TRUE when creating the variable */
20536{
20537 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20538 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20539 ? name[2] : name[0]))
20540 {
20541 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20542 name);
20543 return TRUE;
20544 }
20545 /* Don't allow hiding a function. When "v" is not NULL we might be
20546 * assigning another function to the same var, the type is checked
20547 * below. */
20548 if (new_var && function_exists(name))
20549 {
20550 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20551 name);
20552 return TRUE;
20553 }
20554 return FALSE;
20555}
20556
20557/*
20558 * Check if a variable name is valid.
20559 * Return FALSE and give an error if not.
20560 */
20561 static int
20562valid_varname(varname)
20563 char_u *varname;
20564{
20565 char_u *p;
20566
20567 for (p = varname; *p != NUL; ++p)
20568 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20569 && *p != AUTOLOAD_CHAR)
20570 {
20571 EMSG2(_(e_illvar), varname);
20572 return FALSE;
20573 }
20574 return TRUE;
20575}
20576
20577/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020578 * Return TRUE if typeval "tv" is set to be locked (immutable).
20579 * Also give an error message, using "name".
20580 */
20581 static int
20582tv_check_lock(lock, name)
20583 int lock;
20584 char_u *name;
20585{
20586 if (lock & VAR_LOCKED)
20587 {
20588 EMSG2(_("E741: Value is locked: %s"),
20589 name == NULL ? (char_u *)_("Unknown") : name);
20590 return TRUE;
20591 }
20592 if (lock & VAR_FIXED)
20593 {
20594 EMSG2(_("E742: Cannot change value of %s"),
20595 name == NULL ? (char_u *)_("Unknown") : name);
20596 return TRUE;
20597 }
20598 return FALSE;
20599}
20600
20601/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020602 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020603 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020604 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020605 * It is OK for "from" and "to" to point to the same item. This is used to
20606 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020608 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020609copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020610 typval_T *from;
20611 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020613 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020614 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020615 switch (from->v_type)
20616 {
20617 case VAR_NUMBER:
20618 to->vval.v_number = from->vval.v_number;
20619 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020620#ifdef FEAT_FLOAT
20621 case VAR_FLOAT:
20622 to->vval.v_float = from->vval.v_float;
20623 break;
20624#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020625 case VAR_STRING:
20626 case VAR_FUNC:
20627 if (from->vval.v_string == NULL)
20628 to->vval.v_string = NULL;
20629 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020630 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020631 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020632 if (from->v_type == VAR_FUNC)
20633 func_ref(to->vval.v_string);
20634 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020635 break;
20636 case VAR_LIST:
20637 if (from->vval.v_list == NULL)
20638 to->vval.v_list = NULL;
20639 else
20640 {
20641 to->vval.v_list = from->vval.v_list;
20642 ++to->vval.v_list->lv_refcount;
20643 }
20644 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020645 case VAR_DICT:
20646 if (from->vval.v_dict == NULL)
20647 to->vval.v_dict = NULL;
20648 else
20649 {
20650 to->vval.v_dict = from->vval.v_dict;
20651 ++to->vval.v_dict->dv_refcount;
20652 }
20653 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020654 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020655 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020656 break;
20657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658}
20659
20660/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020661 * Make a copy of an item.
20662 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020663 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20664 * reference to an already copied list/dict can be used.
20665 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020666 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020667 static int
20668item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 typval_T *from;
20670 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020671 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020672 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020673{
20674 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020675 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020676
Bram Moolenaar33570922005-01-25 22:26:29 +000020677 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020678 {
20679 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020680 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020681 }
20682 ++recurse;
20683
20684 switch (from->v_type)
20685 {
20686 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020687#ifdef FEAT_FLOAT
20688 case VAR_FLOAT:
20689#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020690 case VAR_STRING:
20691 case VAR_FUNC:
20692 copy_tv(from, to);
20693 break;
20694 case VAR_LIST:
20695 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020696 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020697 if (from->vval.v_list == NULL)
20698 to->vval.v_list = NULL;
20699 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20700 {
20701 /* use the copy made earlier */
20702 to->vval.v_list = from->vval.v_list->lv_copylist;
20703 ++to->vval.v_list->lv_refcount;
20704 }
20705 else
20706 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20707 if (to->vval.v_list == NULL)
20708 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020709 break;
20710 case VAR_DICT:
20711 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020712 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020713 if (from->vval.v_dict == NULL)
20714 to->vval.v_dict = NULL;
20715 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20716 {
20717 /* use the copy made earlier */
20718 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20719 ++to->vval.v_dict->dv_refcount;
20720 }
20721 else
20722 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20723 if (to->vval.v_dict == NULL)
20724 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020725 break;
20726 default:
20727 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020728 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020729 }
20730 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020731 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020732}
20733
20734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 * ":echo expr1 ..." print each argument separated with a space, add a
20736 * newline at the end.
20737 * ":echon expr1 ..." print each argument plain.
20738 */
20739 void
20740ex_echo(eap)
20741 exarg_T *eap;
20742{
20743 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020744 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020745 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 char_u *p;
20747 int needclr = TRUE;
20748 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020749 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750
20751 if (eap->skip)
20752 ++emsg_skip;
20753 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020755 /* If eval1() causes an error message the text from the command may
20756 * still need to be cleared. E.g., "echo 22,44". */
20757 need_clr_eos = needclr;
20758
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020760 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 {
20762 /*
20763 * Report the invalid expression unless the expression evaluation
20764 * has been cancelled due to an aborting error, an interrupt, or an
20765 * exception.
20766 */
20767 if (!aborting())
20768 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020769 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 break;
20771 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020772 need_clr_eos = FALSE;
20773
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 if (!eap->skip)
20775 {
20776 if (atstart)
20777 {
20778 atstart = FALSE;
20779 /* Call msg_start() after eval1(), evaluating the expression
20780 * may cause a message to appear. */
20781 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020782 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020783 /* Mark the saved text as finishing the line, so that what
20784 * follows is displayed on a new line when scrolling back
20785 * at the more prompt. */
20786 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 }
20790 else if (eap->cmdidx == CMD_echo)
20791 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020792 current_copyID += COPYID_INC;
20793 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020794 if (p != NULL)
20795 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020797 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020799 if (*p != TAB && needclr)
20800 {
20801 /* remove any text still there from the command */
20802 msg_clr_eos();
20803 needclr = FALSE;
20804 }
20805 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 }
20807 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020808 {
20809#ifdef FEAT_MBYTE
20810 if (has_mbyte)
20811 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020812 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020813
20814 (void)msg_outtrans_len_attr(p, i, echo_attr);
20815 p += i - 1;
20816 }
20817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020819 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020822 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020824 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 arg = skipwhite(arg);
20826 }
20827 eap->nextcmd = check_nextcmd(arg);
20828
20829 if (eap->skip)
20830 --emsg_skip;
20831 else
20832 {
20833 /* remove text that may still be there from the command */
20834 if (needclr)
20835 msg_clr_eos();
20836 if (eap->cmdidx == CMD_echo)
20837 msg_end();
20838 }
20839}
20840
20841/*
20842 * ":echohl {name}".
20843 */
20844 void
20845ex_echohl(eap)
20846 exarg_T *eap;
20847{
20848 int id;
20849
20850 id = syn_name2id(eap->arg);
20851 if (id == 0)
20852 echo_attr = 0;
20853 else
20854 echo_attr = syn_id2attr(id);
20855}
20856
20857/*
20858 * ":execute expr1 ..." execute the result of an expression.
20859 * ":echomsg expr1 ..." Print a message
20860 * ":echoerr expr1 ..." Print an error
20861 * Each gets spaces around each argument and a newline at the end for
20862 * echo commands
20863 */
20864 void
20865ex_execute(eap)
20866 exarg_T *eap;
20867{
20868 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020869 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 int ret = OK;
20871 char_u *p;
20872 garray_T ga;
20873 int len;
20874 int save_did_emsg;
20875
20876 ga_init2(&ga, 1, 80);
20877
20878 if (eap->skip)
20879 ++emsg_skip;
20880 while (*arg != NUL && *arg != '|' && *arg != '\n')
20881 {
20882 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884 {
20885 /*
20886 * Report the invalid expression unless the expression evaluation
20887 * has been cancelled due to an aborting error, an interrupt, or an
20888 * exception.
20889 */
20890 if (!aborting())
20891 EMSG2(_(e_invexpr2), p);
20892 ret = FAIL;
20893 break;
20894 }
20895
20896 if (!eap->skip)
20897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020898 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 len = (int)STRLEN(p);
20900 if (ga_grow(&ga, len + 2) == FAIL)
20901 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020902 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 ret = FAIL;
20904 break;
20905 }
20906 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 ga.ga_len += len;
20910 }
20911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 arg = skipwhite(arg);
20914 }
20915
20916 if (ret != FAIL && ga.ga_data != NULL)
20917 {
20918 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020921 out_flush();
20922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 else if (eap->cmdidx == CMD_echoerr)
20924 {
20925 /* We don't want to abort following commands, restore did_emsg. */
20926 save_did_emsg = did_emsg;
20927 EMSG((char_u *)ga.ga_data);
20928 if (!force_abort)
20929 did_emsg = save_did_emsg;
20930 }
20931 else if (eap->cmdidx == CMD_execute)
20932 do_cmdline((char_u *)ga.ga_data,
20933 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20934 }
20935
20936 ga_clear(&ga);
20937
20938 if (eap->skip)
20939 --emsg_skip;
20940
20941 eap->nextcmd = check_nextcmd(arg);
20942}
20943
20944/*
20945 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20946 * "arg" points to the "&" or '+' when called, to "option" when returning.
20947 * Returns NULL when no option name found. Otherwise pointer to the char
20948 * after the option name.
20949 */
20950 static char_u *
20951find_option_end(arg, opt_flags)
20952 char_u **arg;
20953 int *opt_flags;
20954{
20955 char_u *p = *arg;
20956
20957 ++p;
20958 if (*p == 'g' && p[1] == ':')
20959 {
20960 *opt_flags = OPT_GLOBAL;
20961 p += 2;
20962 }
20963 else if (*p == 'l' && p[1] == ':')
20964 {
20965 *opt_flags = OPT_LOCAL;
20966 p += 2;
20967 }
20968 else
20969 *opt_flags = 0;
20970
20971 if (!ASCII_ISALPHA(*p))
20972 return NULL;
20973 *arg = p;
20974
20975 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20976 p += 4; /* termcap option */
20977 else
20978 while (ASCII_ISALPHA(*p))
20979 ++p;
20980 return p;
20981}
20982
20983/*
20984 * ":function"
20985 */
20986 void
20987ex_function(eap)
20988 exarg_T *eap;
20989{
20990 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020991 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 int j;
20993 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995 char_u *name = NULL;
20996 char_u *p;
20997 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020998 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 garray_T newargs;
21000 garray_T newlines;
21001 int varargs = FALSE;
21002 int mustend = FALSE;
21003 int flags = 0;
21004 ufunc_T *fp;
21005 int indent;
21006 int nesting;
21007 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021008 dictitem_T *v;
21009 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021010 static int func_nr = 0; /* number for nameless function */
21011 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021012 hashtab_T *ht;
21013 int todo;
21014 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021015 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016
21017 /*
21018 * ":function" without argument: list functions.
21019 */
21020 if (ends_excmd(*eap->arg))
21021 {
21022 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021023 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021024 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021025 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021026 {
21027 if (!HASHITEM_EMPTY(hi))
21028 {
21029 --todo;
21030 fp = HI2UF(hi);
21031 if (!isdigit(*fp->uf_name))
21032 list_func_head(fp, FALSE);
21033 }
21034 }
21035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 eap->nextcmd = check_nextcmd(eap->arg);
21037 return;
21038 }
21039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021040 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021041 * ":function /pat": list functions matching pattern.
21042 */
21043 if (*eap->arg == '/')
21044 {
21045 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21046 if (!eap->skip)
21047 {
21048 regmatch_T regmatch;
21049
21050 c = *p;
21051 *p = NUL;
21052 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21053 *p = c;
21054 if (regmatch.regprog != NULL)
21055 {
21056 regmatch.rm_ic = p_ic;
21057
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021058 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021059 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21060 {
21061 if (!HASHITEM_EMPTY(hi))
21062 {
21063 --todo;
21064 fp = HI2UF(hi);
21065 if (!isdigit(*fp->uf_name)
21066 && vim_regexec(&regmatch, fp->uf_name, 0))
21067 list_func_head(fp, FALSE);
21068 }
21069 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021070 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021071 }
21072 }
21073 if (*p == '/')
21074 ++p;
21075 eap->nextcmd = check_nextcmd(p);
21076 return;
21077 }
21078
21079 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021080 * Get the function name. There are these situations:
21081 * func normal function name
21082 * "name" == func, "fudi.fd_dict" == NULL
21083 * dict.func new dictionary entry
21084 * "name" == NULL, "fudi.fd_dict" set,
21085 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21086 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021087 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021088 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21089 * dict.func existing dict entry that's not a Funcref
21090 * "name" == NULL, "fudi.fd_dict" set,
21091 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021094 name = trans_function_name(&p, eap->skip, 0, &fudi);
21095 paren = (vim_strchr(p, '(') != NULL);
21096 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 {
21098 /*
21099 * Return on an invalid expression in braces, unless the expression
21100 * evaluation has been cancelled due to an aborting error, an
21101 * interrupt, or an exception.
21102 */
21103 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021105 if (!eap->skip && fudi.fd_newkey != NULL)
21106 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021107 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 else
21111 eap->skip = TRUE;
21112 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021113
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 /* An error in a function call during evaluation of an expression in magic
21115 * braces should not cause the function not to be defined. */
21116 saved_did_emsg = did_emsg;
21117 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118
21119 /*
21120 * ":function func" with only function name: list function.
21121 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021122 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 {
21124 if (!ends_excmd(*skipwhite(p)))
21125 {
21126 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021127 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 }
21129 eap->nextcmd = check_nextcmd(p);
21130 if (eap->nextcmd != NULL)
21131 *p = NUL;
21132 if (!eap->skip && !got_int)
21133 {
21134 fp = find_func(name);
21135 if (fp != NULL)
21136 {
21137 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021138 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021140 if (FUNCLINE(fp, j) == NULL)
21141 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 msg_putchar('\n');
21143 msg_outnum((long)(j + 1));
21144 if (j < 9)
21145 msg_putchar(' ');
21146 if (j < 99)
21147 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021148 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 out_flush(); /* show a line at a time */
21150 ui_breakcheck();
21151 }
21152 if (!got_int)
21153 {
21154 msg_putchar('\n');
21155 msg_puts((char_u *)" endfunction");
21156 }
21157 }
21158 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021159 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021161 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 }
21163
21164 /*
21165 * ":function name(arg1, arg2)" Define function.
21166 */
21167 p = skipwhite(p);
21168 if (*p != '(')
21169 {
21170 if (!eap->skip)
21171 {
21172 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021173 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 }
21175 /* attempt to continue by skipping some text */
21176 if (vim_strchr(p, '(') != NULL)
21177 p = vim_strchr(p, '(');
21178 }
21179 p = skipwhite(p + 1);
21180
21181 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21182 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21183
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021184 if (!eap->skip)
21185 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021186 /* Check the name of the function. Unless it's a dictionary function
21187 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021188 if (name != NULL)
21189 arg = name;
21190 else
21191 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021192 if (arg != NULL && (fudi.fd_di == NULL
21193 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021194 {
21195 if (*arg == K_SPECIAL)
21196 j = 3;
21197 else
21198 j = 0;
21199 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21200 : eval_isnamec(arg[j])))
21201 ++j;
21202 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021203 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021204 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021205 /* Disallow using the g: dict. */
21206 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21207 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021208 }
21209
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 /*
21211 * Isolate the arguments: "arg1, arg2, ...)"
21212 */
21213 while (*p != ')')
21214 {
21215 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21216 {
21217 varargs = TRUE;
21218 p += 3;
21219 mustend = TRUE;
21220 }
21221 else
21222 {
21223 arg = p;
21224 while (ASCII_ISALNUM(*p) || *p == '_')
21225 ++p;
21226 if (arg == p || isdigit(*arg)
21227 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21228 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21229 {
21230 if (!eap->skip)
21231 EMSG2(_("E125: Illegal argument: %s"), arg);
21232 break;
21233 }
21234 if (ga_grow(&newargs, 1) == FAIL)
21235 goto erret;
21236 c = *p;
21237 *p = NUL;
21238 arg = vim_strsave(arg);
21239 if (arg == NULL)
21240 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021241
21242 /* Check for duplicate argument name. */
21243 for (i = 0; i < newargs.ga_len; ++i)
21244 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21245 {
21246 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21247 goto erret;
21248 }
21249
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21251 *p = c;
21252 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 if (*p == ',')
21254 ++p;
21255 else
21256 mustend = TRUE;
21257 }
21258 p = skipwhite(p);
21259 if (mustend && *p != ')')
21260 {
21261 if (!eap->skip)
21262 EMSG2(_(e_invarg2), eap->arg);
21263 break;
21264 }
21265 }
21266 ++p; /* skip the ')' */
21267
Bram Moolenaare9a41262005-01-15 22:18:47 +000021268 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 for (;;)
21270 {
21271 p = skipwhite(p);
21272 if (STRNCMP(p, "range", 5) == 0)
21273 {
21274 flags |= FC_RANGE;
21275 p += 5;
21276 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021277 else if (STRNCMP(p, "dict", 4) == 0)
21278 {
21279 flags |= FC_DICT;
21280 p += 4;
21281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 else if (STRNCMP(p, "abort", 5) == 0)
21283 {
21284 flags |= FC_ABORT;
21285 p += 5;
21286 }
21287 else
21288 break;
21289 }
21290
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021291 /* When there is a line break use what follows for the function body.
21292 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21293 if (*p == '\n')
21294 line_arg = p + 1;
21295 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 EMSG(_(e_trailing));
21297
21298 /*
21299 * Read the body of the function, until ":endfunction" is found.
21300 */
21301 if (KeyTyped)
21302 {
21303 /* Check if the function already exists, don't let the user type the
21304 * whole function before telling him it doesn't work! For a script we
21305 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 if (!eap->skip && !eap->forceit)
21307 {
21308 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21309 EMSG(_(e_funcdict));
21310 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021311 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021314 if (!eap->skip && did_emsg)
21315 goto erret;
21316
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 msg_putchar('\n'); /* don't overwrite the function name */
21318 cmdline_row = msg_row;
21319 }
21320
21321 indent = 2;
21322 nesting = 0;
21323 for (;;)
21324 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021325 if (KeyTyped)
21326 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021328 sourcing_lnum_off = sourcing_lnum;
21329
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021330 if (line_arg != NULL)
21331 {
21332 /* Use eap->arg, split up in parts by line breaks. */
21333 theline = line_arg;
21334 p = vim_strchr(theline, '\n');
21335 if (p == NULL)
21336 line_arg += STRLEN(line_arg);
21337 else
21338 {
21339 *p = NUL;
21340 line_arg = p + 1;
21341 }
21342 }
21343 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 theline = getcmdline(':', 0L, indent);
21345 else
21346 theline = eap->getline(':', eap->cookie, indent);
21347 if (KeyTyped)
21348 lines_left = Rows - 1;
21349 if (theline == NULL)
21350 {
21351 EMSG(_("E126: Missing :endfunction"));
21352 goto erret;
21353 }
21354
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021355 /* Detect line continuation: sourcing_lnum increased more than one. */
21356 if (sourcing_lnum > sourcing_lnum_off + 1)
21357 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21358 else
21359 sourcing_lnum_off = 0;
21360
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 if (skip_until != NULL)
21362 {
21363 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21364 * don't check for ":endfunc". */
21365 if (STRCMP(theline, skip_until) == 0)
21366 {
21367 vim_free(skip_until);
21368 skip_until = NULL;
21369 }
21370 }
21371 else
21372 {
21373 /* skip ':' and blanks*/
21374 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21375 ;
21376
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021377 /* Check for "endfunction". */
21378 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021380 if (line_arg == NULL)
21381 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 break;
21383 }
21384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021385 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 * at "end". */
21387 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21388 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021389 else if (STRNCMP(p, "if", 2) == 0
21390 || STRNCMP(p, "wh", 2) == 0
21391 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 || STRNCMP(p, "try", 3) == 0)
21393 indent += 2;
21394
21395 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021396 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021398 if (*p == '!')
21399 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 p += eval_fname_script(p);
21401 if (ASCII_ISALPHA(*p))
21402 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021403 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 if (*skipwhite(p) == '(')
21405 {
21406 ++nesting;
21407 indent += 2;
21408 }
21409 }
21410 }
21411
21412 /* Check for ":append" or ":insert". */
21413 p = skip_range(p, NULL);
21414 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21415 || (p[0] == 'i'
21416 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21417 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21418 skip_until = vim_strsave((char_u *)".");
21419
21420 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21421 arg = skipwhite(skiptowhite(p));
21422 if (arg[0] == '<' && arg[1] =='<'
21423 && ((p[0] == 'p' && p[1] == 'y'
21424 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21425 || (p[0] == 'p' && p[1] == 'e'
21426 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21427 || (p[0] == 't' && p[1] == 'c'
21428 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021429 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21430 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21432 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021433 || (p[0] == 'm' && p[1] == 'z'
21434 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 ))
21436 {
21437 /* ":python <<" continues until a dot, like ":append" */
21438 p = skipwhite(arg + 2);
21439 if (*p == NUL)
21440 skip_until = vim_strsave((char_u *)".");
21441 else
21442 skip_until = vim_strsave(p);
21443 }
21444 }
21445
21446 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021447 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021448 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021449 if (line_arg == NULL)
21450 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021452 }
21453
21454 /* Copy the line to newly allocated memory. get_one_sourceline()
21455 * allocates 250 bytes per line, this saves 80% on average. The cost
21456 * is an extra alloc/free. */
21457 p = vim_strsave(theline);
21458 if (p != NULL)
21459 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021460 if (line_arg == NULL)
21461 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021462 theline = p;
21463 }
21464
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021465 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21466
21467 /* Add NULL lines for continuation lines, so that the line count is
21468 * equal to the index in the growarray. */
21469 while (sourcing_lnum_off-- > 0)
21470 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021471
21472 /* Check for end of eap->arg. */
21473 if (line_arg != NULL && *line_arg == NUL)
21474 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 }
21476
21477 /* Don't define the function when skipping commands or when an error was
21478 * detected. */
21479 if (eap->skip || did_emsg)
21480 goto erret;
21481
21482 /*
21483 * If there are no errors, add the function
21484 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021485 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021486 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021487 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021488 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021489 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021490 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021491 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021492 goto erret;
21493 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021494
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021495 fp = find_func(name);
21496 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021498 if (!eap->forceit)
21499 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021500 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021501 goto erret;
21502 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021503 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021504 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021505 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021506 name);
21507 goto erret;
21508 }
21509 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021510 ga_clear_strings(&(fp->uf_args));
21511 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 vim_free(name);
21513 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 }
21516 else
21517 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021518 char numbuf[20];
21519
21520 fp = NULL;
21521 if (fudi.fd_newkey == NULL && !eap->forceit)
21522 {
21523 EMSG(_(e_funcdict));
21524 goto erret;
21525 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021526 if (fudi.fd_di == NULL)
21527 {
21528 /* Can't add a function to a locked dictionary */
21529 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21530 goto erret;
21531 }
21532 /* Can't change an existing function if it is locked */
21533 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21534 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021535
21536 /* Give the function a sequential number. Can only be used with a
21537 * Funcref! */
21538 vim_free(name);
21539 sprintf(numbuf, "%d", ++func_nr);
21540 name = vim_strsave((char_u *)numbuf);
21541 if (name == NULL)
21542 goto erret;
21543 }
21544
21545 if (fp == NULL)
21546 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021547 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021548 {
21549 int slen, plen;
21550 char_u *scriptname;
21551
21552 /* Check that the autoload name matches the script name. */
21553 j = FAIL;
21554 if (sourcing_name != NULL)
21555 {
21556 scriptname = autoload_name(name);
21557 if (scriptname != NULL)
21558 {
21559 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021560 plen = (int)STRLEN(p);
21561 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021562 if (slen > plen && fnamecmp(p,
21563 sourcing_name + slen - plen) == 0)
21564 j = OK;
21565 vim_free(scriptname);
21566 }
21567 }
21568 if (j == FAIL)
21569 {
21570 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21571 goto erret;
21572 }
21573 }
21574
21575 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 if (fp == NULL)
21577 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578
21579 if (fudi.fd_dict != NULL)
21580 {
21581 if (fudi.fd_di == NULL)
21582 {
21583 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021584 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021585 if (fudi.fd_di == NULL)
21586 {
21587 vim_free(fp);
21588 goto erret;
21589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021590 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21591 {
21592 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021593 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021594 goto erret;
21595 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 }
21597 else
21598 /* overwrite existing dict entry */
21599 clear_tv(&fudi.fd_di->di_tv);
21600 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021601 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021602 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021603 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021604
21605 /* behave like "dict" was used */
21606 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021607 }
21608
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021610 STRCPY(fp->uf_name, name);
21611 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021613 fp->uf_args = newargs;
21614 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021615#ifdef FEAT_PROFILE
21616 fp->uf_tml_count = NULL;
21617 fp->uf_tml_total = NULL;
21618 fp->uf_tml_self = NULL;
21619 fp->uf_profiling = FALSE;
21620 if (prof_def_func())
21621 func_do_profile(fp);
21622#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021623 fp->uf_varargs = varargs;
21624 fp->uf_flags = flags;
21625 fp->uf_calls = 0;
21626 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021627 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628
21629erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 ga_clear_strings(&newargs);
21631 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632ret_free:
21633 vim_free(skip_until);
21634 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637}
21638
21639/*
21640 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021641 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021643 * flags:
21644 * TFN_INT: internal function name OK
21645 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 * Advances "pp" to just after the function name (if no error).
21647 */
21648 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 char_u **pp;
21651 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021652 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021653 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021655 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 char_u *start;
21657 char_u *end;
21658 int lead;
21659 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021662
21663 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021664 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021666
21667 /* Check for hard coded <SNR>: already translated function ID (from a user
21668 * command). */
21669 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21670 && (*pp)[2] == (int)KE_SNR)
21671 {
21672 *pp += 3;
21673 len = get_id_len(pp) + 3;
21674 return vim_strnsave(start, len);
21675 }
21676
21677 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21678 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021680 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021682
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021683 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21684 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021685 if (end == start)
21686 {
21687 if (!skip)
21688 EMSG(_("E129: Function name required"));
21689 goto theend;
21690 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021691 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021692 {
21693 /*
21694 * Report an invalid expression in braces, unless the expression
21695 * evaluation has been cancelled due to an aborting error, an
21696 * interrupt, or an exception.
21697 */
21698 if (!aborting())
21699 {
21700 if (end != NULL)
21701 EMSG2(_(e_invarg2), start);
21702 }
21703 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021704 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021705 goto theend;
21706 }
21707
21708 if (lv.ll_tv != NULL)
21709 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021710 if (fdp != NULL)
21711 {
21712 fdp->fd_dict = lv.ll_dict;
21713 fdp->fd_newkey = lv.ll_newkey;
21714 lv.ll_newkey = NULL;
21715 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021716 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021717 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21718 {
21719 name = vim_strsave(lv.ll_tv->vval.v_string);
21720 *pp = end;
21721 }
21722 else
21723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021724 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21725 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021726 EMSG(_(e_funcref));
21727 else
21728 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021729 name = NULL;
21730 }
21731 goto theend;
21732 }
21733
21734 if (lv.ll_name == NULL)
21735 {
21736 /* Error found, but continue after the function name. */
21737 *pp = end;
21738 goto theend;
21739 }
21740
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021741 /* Check if the name is a Funcref. If so, use the value. */
21742 if (lv.ll_exp_name != NULL)
21743 {
21744 len = (int)STRLEN(lv.ll_exp_name);
21745 name = deref_func_name(lv.ll_exp_name, &len);
21746 if (name == lv.ll_exp_name)
21747 name = NULL;
21748 }
21749 else
21750 {
21751 len = (int)(end - *pp);
21752 name = deref_func_name(*pp, &len);
21753 if (name == *pp)
21754 name = NULL;
21755 }
21756 if (name != NULL)
21757 {
21758 name = vim_strsave(name);
21759 *pp = end;
21760 goto theend;
21761 }
21762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021763 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021764 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021765 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021766 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21767 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21768 {
21769 /* When there was "s:" already or the name expanded to get a
21770 * leading "s:" then remove it. */
21771 lv.ll_name += 2;
21772 len -= 2;
21773 lead = 2;
21774 }
21775 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021776 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021777 {
21778 if (lead == 2) /* skip over "s:" */
21779 lv.ll_name += 2;
21780 len = (int)(end - lv.ll_name);
21781 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021782
21783 /*
21784 * Copy the function name to allocated memory.
21785 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21786 * Accept <SNR>123_name() outside a script.
21787 */
21788 if (skip)
21789 lead = 0; /* do nothing */
21790 else if (lead > 0)
21791 {
21792 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021793 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21794 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021795 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021796 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021797 if (current_SID <= 0)
21798 {
21799 EMSG(_(e_usingsid));
21800 goto theend;
21801 }
21802 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21803 lead += (int)STRLEN(sid_buf);
21804 }
21805 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021806 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021807 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021808 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021809 goto theend;
21810 }
21811 name = alloc((unsigned)(len + lead + 1));
21812 if (name != NULL)
21813 {
21814 if (lead > 0)
21815 {
21816 name[0] = K_SPECIAL;
21817 name[1] = KS_EXTRA;
21818 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021819 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021820 STRCPY(name + 3, sid_buf);
21821 }
21822 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21823 name[len + lead] = NUL;
21824 }
21825 *pp = end;
21826
21827theend:
21828 clear_lval(&lv);
21829 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830}
21831
21832/*
21833 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21834 * Return 2 if "p" starts with "s:".
21835 * Return 0 otherwise.
21836 */
21837 static int
21838eval_fname_script(p)
21839 char_u *p;
21840{
21841 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21842 || STRNICMP(p + 1, "SNR>", 4) == 0))
21843 return 5;
21844 if (p[0] == 's' && p[1] == ':')
21845 return 2;
21846 return 0;
21847}
21848
21849/*
21850 * Return TRUE if "p" starts with "<SID>" or "s:".
21851 * Only works if eval_fname_script() returned non-zero for "p"!
21852 */
21853 static int
21854eval_fname_sid(p)
21855 char_u *p;
21856{
21857 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21858}
21859
21860/*
21861 * List the head of the function: "name(arg1, arg2)".
21862 */
21863 static void
21864list_func_head(fp, indent)
21865 ufunc_T *fp;
21866 int indent;
21867{
21868 int j;
21869
21870 msg_start();
21871 if (indent)
21872 MSG_PUTS(" ");
21873 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021874 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 {
21876 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021877 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 }
21879 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021880 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021882 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 {
21884 if (j)
21885 MSG_PUTS(", ");
21886 msg_puts(FUNCARG(fp, j));
21887 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021888 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889 {
21890 if (j)
21891 MSG_PUTS(", ");
21892 MSG_PUTS("...");
21893 }
21894 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021895 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021896 if (p_verbose > 0)
21897 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898}
21899
21900/*
21901 * Find a function by name, return pointer to it in ufuncs.
21902 * Return NULL for unknown function.
21903 */
21904 static ufunc_T *
21905find_func(name)
21906 char_u *name;
21907{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021908 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021910 hi = hash_find(&func_hashtab, name);
21911 if (!HASHITEM_EMPTY(hi))
21912 return HI2UF(hi);
21913 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914}
21915
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021916#if defined(EXITFREE) || defined(PROTO)
21917 void
21918free_all_functions()
21919{
21920 hashitem_T *hi;
21921
21922 /* Need to start all over every time, because func_free() may change the
21923 * hash table. */
21924 while (func_hashtab.ht_used > 0)
21925 for (hi = func_hashtab.ht_array; ; ++hi)
21926 if (!HASHITEM_EMPTY(hi))
21927 {
21928 func_free(HI2UF(hi));
21929 break;
21930 }
21931}
21932#endif
21933
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021934/*
21935 * Return TRUE if a function "name" exists.
21936 */
21937 static int
21938function_exists(name)
21939 char_u *name;
21940{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021941 char_u *nm = name;
21942 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 int n = FALSE;
21944
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021945 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021946 nm = skipwhite(nm);
21947
21948 /* Only accept "funcname", "funcname ", "funcname (..." and
21949 * "funcname(...", not "funcname!...". */
21950 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021951 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021952 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021953 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021954 else
21955 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021956 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021957 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021958 return n;
21959}
21960
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021961/*
21962 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021963 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021964 */
21965 static int
21966builtin_function(name)
21967 char_u *name;
21968{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021969 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21970 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021971}
21972
Bram Moolenaar05159a02005-02-26 23:04:13 +000021973#if defined(FEAT_PROFILE) || defined(PROTO)
21974/*
21975 * Start profiling function "fp".
21976 */
21977 static void
21978func_do_profile(fp)
21979 ufunc_T *fp;
21980{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021981 int len = fp->uf_lines.ga_len;
21982
21983 if (len == 0)
21984 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021985 fp->uf_tm_count = 0;
21986 profile_zero(&fp->uf_tm_self);
21987 profile_zero(&fp->uf_tm_total);
21988 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021989 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021990 if (fp->uf_tml_total == NULL)
21991 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021992 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021993 if (fp->uf_tml_self == NULL)
21994 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021995 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996 fp->uf_tml_idx = -1;
21997 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21998 || fp->uf_tml_self == NULL)
21999 return; /* out of memory */
22000
22001 fp->uf_profiling = TRUE;
22002}
22003
22004/*
22005 * Dump the profiling results for all functions in file "fd".
22006 */
22007 void
22008func_dump_profile(fd)
22009 FILE *fd;
22010{
22011 hashitem_T *hi;
22012 int todo;
22013 ufunc_T *fp;
22014 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022015 ufunc_T **sorttab;
22016 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022017
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022018 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022019 if (todo == 0)
22020 return; /* nothing to dump */
22021
Bram Moolenaar73830342005-02-28 22:48:19 +000022022 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22023
Bram Moolenaar05159a02005-02-26 23:04:13 +000022024 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22025 {
22026 if (!HASHITEM_EMPTY(hi))
22027 {
22028 --todo;
22029 fp = HI2UF(hi);
22030 if (fp->uf_profiling)
22031 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022032 if (sorttab != NULL)
22033 sorttab[st_len++] = fp;
22034
Bram Moolenaar05159a02005-02-26 23:04:13 +000022035 if (fp->uf_name[0] == K_SPECIAL)
22036 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22037 else
22038 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22039 if (fp->uf_tm_count == 1)
22040 fprintf(fd, "Called 1 time\n");
22041 else
22042 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22043 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22044 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22045 fprintf(fd, "\n");
22046 fprintf(fd, "count total (s) self (s)\n");
22047
22048 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22049 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022050 if (FUNCLINE(fp, i) == NULL)
22051 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022052 prof_func_line(fd, fp->uf_tml_count[i],
22053 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022054 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22055 }
22056 fprintf(fd, "\n");
22057 }
22058 }
22059 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022060
22061 if (sorttab != NULL && st_len > 0)
22062 {
22063 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22064 prof_total_cmp);
22065 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22066 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22067 prof_self_cmp);
22068 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22069 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022070
22071 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022072}
Bram Moolenaar73830342005-02-28 22:48:19 +000022073
22074 static void
22075prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22076 FILE *fd;
22077 ufunc_T **sorttab;
22078 int st_len;
22079 char *title;
22080 int prefer_self; /* when equal print only self time */
22081{
22082 int i;
22083 ufunc_T *fp;
22084
22085 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22086 fprintf(fd, "count total (s) self (s) function\n");
22087 for (i = 0; i < 20 && i < st_len; ++i)
22088 {
22089 fp = sorttab[i];
22090 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22091 prefer_self);
22092 if (fp->uf_name[0] == K_SPECIAL)
22093 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22094 else
22095 fprintf(fd, " %s()\n", fp->uf_name);
22096 }
22097 fprintf(fd, "\n");
22098}
22099
22100/*
22101 * Print the count and times for one function or function line.
22102 */
22103 static void
22104prof_func_line(fd, count, total, self, prefer_self)
22105 FILE *fd;
22106 int count;
22107 proftime_T *total;
22108 proftime_T *self;
22109 int prefer_self; /* when equal print only self time */
22110{
22111 if (count > 0)
22112 {
22113 fprintf(fd, "%5d ", count);
22114 if (prefer_self && profile_equal(total, self))
22115 fprintf(fd, " ");
22116 else
22117 fprintf(fd, "%s ", profile_msg(total));
22118 if (!prefer_self && profile_equal(total, self))
22119 fprintf(fd, " ");
22120 else
22121 fprintf(fd, "%s ", profile_msg(self));
22122 }
22123 else
22124 fprintf(fd, " ");
22125}
22126
22127/*
22128 * Compare function for total time sorting.
22129 */
22130 static int
22131#ifdef __BORLANDC__
22132_RTLENTRYF
22133#endif
22134prof_total_cmp(s1, s2)
22135 const void *s1;
22136 const void *s2;
22137{
22138 ufunc_T *p1, *p2;
22139
22140 p1 = *(ufunc_T **)s1;
22141 p2 = *(ufunc_T **)s2;
22142 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22143}
22144
22145/*
22146 * Compare function for self time sorting.
22147 */
22148 static int
22149#ifdef __BORLANDC__
22150_RTLENTRYF
22151#endif
22152prof_self_cmp(s1, s2)
22153 const void *s1;
22154 const void *s2;
22155{
22156 ufunc_T *p1, *p2;
22157
22158 p1 = *(ufunc_T **)s1;
22159 p2 = *(ufunc_T **)s2;
22160 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22161}
22162
Bram Moolenaar05159a02005-02-26 23:04:13 +000022163#endif
22164
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022165/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022166 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022167 * Return TRUE if a package was loaded.
22168 */
22169 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022170script_autoload(name, reload)
22171 char_u *name;
22172 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022173{
22174 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022175 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022176 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022177 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022178
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022179 /* Return quickly when autoload disabled. */
22180 if (no_autoload)
22181 return FALSE;
22182
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022183 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022184 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022185 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022186 return FALSE;
22187
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022188 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022189
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022190 /* Find the name in the list of previously loaded package names. Skip
22191 * "autoload/", it's always the same. */
22192 for (i = 0; i < ga_loaded.ga_len; ++i)
22193 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22194 break;
22195 if (!reload && i < ga_loaded.ga_len)
22196 ret = FALSE; /* was loaded already */
22197 else
22198 {
22199 /* Remember the name if it wasn't loaded already. */
22200 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22201 {
22202 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22203 tofree = NULL;
22204 }
22205
22206 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022207 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022208 ret = TRUE;
22209 }
22210
22211 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022212 return ret;
22213}
22214
22215/*
22216 * Return the autoload script name for a function or variable name.
22217 * Returns NULL when out of memory.
22218 */
22219 static char_u *
22220autoload_name(name)
22221 char_u *name;
22222{
22223 char_u *p;
22224 char_u *scriptname;
22225
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022226 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022227 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22228 if (scriptname == NULL)
22229 return FALSE;
22230 STRCPY(scriptname, "autoload/");
22231 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022232 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022233 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022234 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022235 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022236 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022237}
22238
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22240
22241/*
22242 * Function given to ExpandGeneric() to obtain the list of user defined
22243 * function names.
22244 */
22245 char_u *
22246get_user_func_name(xp, idx)
22247 expand_T *xp;
22248 int idx;
22249{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 static long_u done;
22251 static hashitem_T *hi;
22252 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253
22254 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022256 done = 0;
22257 hi = func_hashtab.ht_array;
22258 }
22259 if (done < func_hashtab.ht_used)
22260 {
22261 if (done++ > 0)
22262 ++hi;
22263 while (HASHITEM_EMPTY(hi))
22264 ++hi;
22265 fp = HI2UF(hi);
22266
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022267 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022268 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022269
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022270 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22271 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272
22273 cat_func_name(IObuff, fp);
22274 if (xp->xp_context != EXPAND_USER_FUNC)
22275 {
22276 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022277 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 STRCAT(IObuff, ")");
22279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 return IObuff;
22281 }
22282 return NULL;
22283}
22284
22285#endif /* FEAT_CMDL_COMPL */
22286
22287/*
22288 * Copy the function name of "fp" to buffer "buf".
22289 * "buf" must be able to hold the function name plus three bytes.
22290 * Takes care of script-local function names.
22291 */
22292 static void
22293cat_func_name(buf, fp)
22294 char_u *buf;
22295 ufunc_T *fp;
22296{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022297 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 {
22299 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022300 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 }
22302 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022303 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304}
22305
22306/*
22307 * ":delfunction {name}"
22308 */
22309 void
22310ex_delfunction(eap)
22311 exarg_T *eap;
22312{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022313 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 char_u *p;
22315 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022316 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317
22318 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022319 name = trans_function_name(&p, eap->skip, 0, &fudi);
22320 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022322 {
22323 if (fudi.fd_dict != NULL && !eap->skip)
22324 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 if (!ends_excmd(*skipwhite(p)))
22328 {
22329 vim_free(name);
22330 EMSG(_(e_trailing));
22331 return;
22332 }
22333 eap->nextcmd = check_nextcmd(p);
22334 if (eap->nextcmd != NULL)
22335 *p = NUL;
22336
22337 if (!eap->skip)
22338 fp = find_func(name);
22339 vim_free(name);
22340
22341 if (!eap->skip)
22342 {
22343 if (fp == NULL)
22344 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022345 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 return;
22347 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022348 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 {
22350 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22351 return;
22352 }
22353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022354 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022356 /* Delete the dict item that refers to the function, it will
22357 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022358 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022360 else
22361 func_free(fp);
22362 }
22363}
22364
22365/*
22366 * Free a function and remove it from the list of functions.
22367 */
22368 static void
22369func_free(fp)
22370 ufunc_T *fp;
22371{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022372 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022373
22374 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022375 ga_clear_strings(&(fp->uf_args));
22376 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022377#ifdef FEAT_PROFILE
22378 vim_free(fp->uf_tml_count);
22379 vim_free(fp->uf_tml_total);
22380 vim_free(fp->uf_tml_self);
22381#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022382
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022383 /* remove the function from the function hashtable */
22384 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22385 if (HASHITEM_EMPTY(hi))
22386 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022387 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022388 hash_remove(&func_hashtab, hi);
22389
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022390 vim_free(fp);
22391}
22392
22393/*
22394 * Unreference a Function: decrement the reference count and free it when it
22395 * becomes zero. Only for numbered functions.
22396 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022397 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022398func_unref(name)
22399 char_u *name;
22400{
22401 ufunc_T *fp;
22402
22403 if (name != NULL && isdigit(*name))
22404 {
22405 fp = find_func(name);
22406 if (fp == NULL)
22407 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022408 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022409 {
22410 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022411 * when "uf_calls" becomes zero. */
22412 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022413 func_free(fp);
22414 }
22415 }
22416}
22417
22418/*
22419 * Count a reference to a Function.
22420 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022421 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022422func_ref(name)
22423 char_u *name;
22424{
22425 ufunc_T *fp;
22426
22427 if (name != NULL && isdigit(*name))
22428 {
22429 fp = find_func(name);
22430 if (fp == NULL)
22431 EMSG2(_(e_intern2), "func_ref()");
22432 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022433 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 }
22435}
22436
22437/*
22438 * Call a user function.
22439 */
22440 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022441call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 ufunc_T *fp; /* pointer to function */
22443 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 typval_T *argvars; /* arguments */
22445 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 linenr_T firstline; /* first line of range */
22447 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449{
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 char_u *save_sourcing_name;
22451 linenr_T save_sourcing_lnum;
22452 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022453 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022454 int save_did_emsg;
22455 static int depth = 0;
22456 dictitem_T *v;
22457 int fixvar_idx = 0; /* index in fixvar[] */
22458 int i;
22459 int ai;
22460 char_u numbuf[NUMBUFLEN];
22461 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022462#ifdef FEAT_PROFILE
22463 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022464 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466
22467 /* If depth of calling is getting too high, don't execute the function */
22468 if (depth >= p_mfd)
22469 {
22470 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022471 rettv->v_type = VAR_NUMBER;
22472 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 return;
22474 }
22475 ++depth;
22476
22477 line_breakcheck(); /* check for CTRL-C hit */
22478
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022479 fc = (funccall_T *)alloc(sizeof(funccall_T));
22480 fc->caller = current_funccal;
22481 current_funccal = fc;
22482 fc->func = fp;
22483 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022484 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022485 fc->linenr = 0;
22486 fc->returned = FALSE;
22487 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022489 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22490 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022493 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22495 * each argument variable and saves a lot of time.
22496 */
22497 /*
22498 * Init l: variables.
22499 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022500 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022501 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022502 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022503 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22504 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022505 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022506 name = v->di_key;
22507 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022508 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022509 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022511 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022512 v->di_tv.vval.v_dict = selfdict;
22513 ++selfdict->dv_refcount;
22514 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022515
Bram Moolenaar33570922005-01-25 22:26:29 +000022516 /*
22517 * Init a: variables.
22518 * Set a:0 to "argcount".
22519 * Set a:000 to a list with room for the "..." arguments.
22520 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022521 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022522 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022523 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022524 /* Use "name" to avoid a warning from some compiler that checks the
22525 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022526 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022527 name = v->di_key;
22528 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022529 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022530 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022531 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022532 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022533 v->di_tv.vval.v_list = &fc->l_varlist;
22534 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22535 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22536 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022537
22538 /*
22539 * Set a:firstline to "firstline" and a:lastline to "lastline".
22540 * Set a:name to named arguments.
22541 * Set a:N to the "..." arguments.
22542 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022543 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022545 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 (varnumber_T)lastline);
22547 for (i = 0; i < argcount; ++i)
22548 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022549 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 if (ai < 0)
22551 /* named argument a:name */
22552 name = FUNCARG(fp, i);
22553 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022554 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 /* "..." argument a:1, a:2, etc. */
22556 sprintf((char *)numbuf, "%d", ai + 1);
22557 name = numbuf;
22558 }
22559 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22560 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022561 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022562 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22563 }
22564 else
22565 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022566 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22567 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022568 if (v == NULL)
22569 break;
22570 v->di_flags = DI_FLAGS_RO;
22571 }
22572 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022573 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022574
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022575 /* Note: the values are copied directly to avoid alloc/free.
22576 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022578 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579
22580 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22581 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022582 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22583 fc->l_listitems[ai].li_tv = argvars[i];
22584 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022585 }
22586 }
22587
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 /* Don't redraw while executing the function. */
22589 ++RedrawingDisabled;
22590 save_sourcing_name = sourcing_name;
22591 save_sourcing_lnum = sourcing_lnum;
22592 sourcing_lnum = 1;
22593 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022594 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 if (sourcing_name != NULL)
22596 {
22597 if (save_sourcing_name != NULL
22598 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22599 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22600 else
22601 STRCPY(sourcing_name, "function ");
22602 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22603
22604 if (p_verbose >= 12)
22605 {
22606 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022607 verbose_enter_scroll();
22608
Bram Moolenaar555b2802005-05-19 21:08:39 +000022609 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 if (p_verbose >= 14)
22611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022613 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022614 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022615 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616
22617 msg_puts((char_u *)"(");
22618 for (i = 0; i < argcount; ++i)
22619 {
22620 if (i > 0)
22621 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022622 if (argvars[i].v_type == VAR_NUMBER)
22623 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 else
22625 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022626 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22627 if (s != NULL)
22628 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022629 if (vim_strsize(s) > MSG_BUF_CLEN)
22630 {
22631 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22632 s = buf;
22633 }
22634 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022635 vim_free(tofree);
22636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 }
22638 }
22639 msg_puts((char_u *)")");
22640 }
22641 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022642
22643 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 --no_wait_return;
22645 }
22646 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022648 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022649 {
22650 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22651 func_do_profile(fp);
22652 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022653 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022654 {
22655 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022656 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022657 profile_zero(&fp->uf_tm_children);
22658 }
22659 script_prof_save(&wait_start);
22660 }
22661#endif
22662
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022664 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 save_did_emsg = did_emsg;
22666 did_emsg = FALSE;
22667
22668 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022669 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22671
22672 --RedrawingDisabled;
22673
22674 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022675 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022677 clear_tv(rettv);
22678 rettv->v_type = VAR_NUMBER;
22679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 }
22681
Bram Moolenaar05159a02005-02-26 23:04:13 +000022682#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022683 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022684 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022685 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022686 profile_end(&call_start);
22687 profile_sub_wait(&wait_start, &call_start);
22688 profile_add(&fp->uf_tm_total, &call_start);
22689 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022690 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022691 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022692 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22693 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022694 }
22695 }
22696#endif
22697
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 /* when being verbose, mention the return value */
22699 if (p_verbose >= 12)
22700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022702 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022705 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022706 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022707 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022708 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022711 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022712 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022713 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022714 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022715
Bram Moolenaar555b2802005-05-19 21:08:39 +000022716 /* The value may be very long. Skip the middle part, so that we
22717 * have some idea how it starts and ends. smsg() would always
22718 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022719 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022720 if (s != NULL)
22721 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022722 if (vim_strsize(s) > MSG_BUF_CLEN)
22723 {
22724 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22725 s = buf;
22726 }
22727 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022728 vim_free(tofree);
22729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 }
22731 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022732
22733 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 --no_wait_return;
22735 }
22736
22737 vim_free(sourcing_name);
22738 sourcing_name = save_sourcing_name;
22739 sourcing_lnum = save_sourcing_lnum;
22740 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022741#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022742 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022743 script_prof_restore(&wait_start);
22744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745
22746 if (p_verbose >= 12 && sourcing_name != NULL)
22747 {
22748 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022749 verbose_enter_scroll();
22750
Bram Moolenaar555b2802005-05-19 21:08:39 +000022751 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022753
22754 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 --no_wait_return;
22756 }
22757
22758 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022759 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022761
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022762 /* If the a:000 list and the l: and a: dicts are not referenced we can
22763 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022764 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22765 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22766 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22767 {
22768 free_funccal(fc, FALSE);
22769 }
22770 else
22771 {
22772 hashitem_T *hi;
22773 listitem_T *li;
22774 int todo;
22775
22776 /* "fc" is still in use. This can happen when returning "a:000" or
22777 * assigning "l:" to a global variable.
22778 * Link "fc" in the list for garbage collection later. */
22779 fc->caller = previous_funccal;
22780 previous_funccal = fc;
22781
22782 /* Make a copy of the a: variables, since we didn't do that above. */
22783 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22784 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22785 {
22786 if (!HASHITEM_EMPTY(hi))
22787 {
22788 --todo;
22789 v = HI2DI(hi);
22790 copy_tv(&v->di_tv, &v->di_tv);
22791 }
22792 }
22793
22794 /* Make a copy of the a:000 items, since we didn't do that above. */
22795 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22796 copy_tv(&li->li_tv, &li->li_tv);
22797 }
22798}
22799
22800/*
22801 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022802 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022803 */
22804 static int
22805can_free_funccal(fc, copyID)
22806 funccall_T *fc;
22807 int copyID;
22808{
22809 return (fc->l_varlist.lv_copyID != copyID
22810 && fc->l_vars.dv_copyID != copyID
22811 && fc->l_avars.dv_copyID != copyID);
22812}
22813
22814/*
22815 * Free "fc" and what it contains.
22816 */
22817 static void
22818free_funccal(fc, free_val)
22819 funccall_T *fc;
22820 int free_val; /* a: vars were allocated */
22821{
22822 listitem_T *li;
22823
22824 /* The a: variables typevals may not have been allocated, only free the
22825 * allocated variables. */
22826 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22827
22828 /* free all l: variables */
22829 vars_clear(&fc->l_vars.dv_hashtab);
22830
22831 /* Free the a:000 variables if they were allocated. */
22832 if (free_val)
22833 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22834 clear_tv(&li->li_tv);
22835
22836 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837}
22838
22839/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 * Add a number variable "name" to dict "dp" with value "nr".
22841 */
22842 static void
22843add_nr_var(dp, v, name, nr)
22844 dict_T *dp;
22845 dictitem_T *v;
22846 char *name;
22847 varnumber_T nr;
22848{
22849 STRCPY(v->di_key, name);
22850 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22851 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22852 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022853 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 v->di_tv.vval.v_number = nr;
22855}
22856
22857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 * ":return [expr]"
22859 */
22860 void
22861ex_return(eap)
22862 exarg_T *eap;
22863{
22864 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022865 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 int returning = FALSE;
22867
22868 if (current_funccal == NULL)
22869 {
22870 EMSG(_("E133: :return not inside a function"));
22871 return;
22872 }
22873
22874 if (eap->skip)
22875 ++emsg_skip;
22876
22877 eap->nextcmd = NULL;
22878 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022879 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 {
22881 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022882 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022884 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 }
22886 /* It's safer to return also on error. */
22887 else if (!eap->skip)
22888 {
22889 /*
22890 * Return unless the expression evaluation has been cancelled due to an
22891 * aborting error, an interrupt, or an exception.
22892 */
22893 if (!aborting())
22894 returning = do_return(eap, FALSE, TRUE, NULL);
22895 }
22896
22897 /* When skipping or the return gets pending, advance to the next command
22898 * in this line (!returning). Otherwise, ignore the rest of the line.
22899 * Following lines will be ignored by get_func_line(). */
22900 if (returning)
22901 eap->nextcmd = NULL;
22902 else if (eap->nextcmd == NULL) /* no argument */
22903 eap->nextcmd = check_nextcmd(arg);
22904
22905 if (eap->skip)
22906 --emsg_skip;
22907}
22908
22909/*
22910 * Return from a function. Possibly makes the return pending. Also called
22911 * for a pending return at the ":endtry" or after returning from an extra
22912 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022913 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022914 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 * FALSE when the return gets pending.
22916 */
22917 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022918do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 exarg_T *eap;
22920 int reanimate;
22921 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022922 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923{
22924 int idx;
22925 struct condstack *cstack = eap->cstack;
22926
22927 if (reanimate)
22928 /* Undo the return. */
22929 current_funccal->returned = FALSE;
22930
22931 /*
22932 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22933 * not in its finally clause (which then is to be executed next) is found.
22934 * In this case, make the ":return" pending for execution at the ":endtry".
22935 * Otherwise, return normally.
22936 */
22937 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22938 if (idx >= 0)
22939 {
22940 cstack->cs_pending[idx] = CSTP_RETURN;
22941
22942 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 /* A pending return again gets pending. "rettv" points to an
22944 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022946 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 else
22948 {
22949 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022950 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022952 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022954 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 {
22956 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022957 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 else
22960 EMSG(_(e_outofmem));
22961 }
22962 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022963 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964
22965 if (reanimate)
22966 {
22967 /* The pending return value could be overwritten by a ":return"
22968 * without argument in a finally clause; reset the default
22969 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022970 current_funccal->rettv->v_type = VAR_NUMBER;
22971 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 }
22973 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022974 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 }
22976 else
22977 {
22978 current_funccal->returned = TRUE;
22979
22980 /* If the return is carried out now, store the return value. For
22981 * a return immediately after reanimation, the value is already
22982 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022983 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022985 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022986 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022988 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 }
22990 }
22991
22992 return idx < 0;
22993}
22994
22995/*
22996 * Free the variable with a pending return value.
22997 */
22998 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022999discard_pending_return(rettv)
23000 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001{
Bram Moolenaar33570922005-01-25 22:26:29 +000023002 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003}
23004
23005/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023006 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 * is an allocated string. Used by report_pending() for verbose messages.
23008 */
23009 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023010get_return_cmd(rettv)
23011 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023013 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023014 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023015 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023017 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023018 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023019 if (s == NULL)
23020 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023021
23022 STRCPY(IObuff, ":return ");
23023 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23024 if (STRLEN(s) + 8 >= IOSIZE)
23025 STRCPY(IObuff + IOSIZE - 4, "...");
23026 vim_free(tofree);
23027 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028}
23029
23030/*
23031 * Get next function line.
23032 * Called by do_cmdline() to get the next line.
23033 * Returns allocated string, or NULL for end of function.
23034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 char_u *
23036get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023037 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023039 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040{
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023042 ufunc_T *fp = fcp->func;
23043 char_u *retval;
23044 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045
23046 /* If breakpoints have been added/deleted need to check for it. */
23047 if (fcp->dbg_tick != debug_tick)
23048 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023049 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 sourcing_lnum);
23051 fcp->dbg_tick = debug_tick;
23052 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023053#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023054 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023055 func_line_end(cookie);
23056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057
Bram Moolenaar05159a02005-02-26 23:04:13 +000023058 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023059 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23060 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 retval = NULL;
23062 else
23063 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023064 /* Skip NULL lines (continuation lines). */
23065 while (fcp->linenr < gap->ga_len
23066 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23067 ++fcp->linenr;
23068 if (fcp->linenr >= gap->ga_len)
23069 retval = NULL;
23070 else
23071 {
23072 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23073 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023074#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023075 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023076 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023077#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 }
23080
23081 /* Did we encounter a breakpoint? */
23082 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23083 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023084 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023086 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 sourcing_lnum);
23088 fcp->dbg_tick = debug_tick;
23089 }
23090
23091 return retval;
23092}
23093
Bram Moolenaar05159a02005-02-26 23:04:13 +000023094#if defined(FEAT_PROFILE) || defined(PROTO)
23095/*
23096 * Called when starting to read a function line.
23097 * "sourcing_lnum" must be correct!
23098 * When skipping lines it may not actually be executed, but we won't find out
23099 * until later and we need to store the time now.
23100 */
23101 void
23102func_line_start(cookie)
23103 void *cookie;
23104{
23105 funccall_T *fcp = (funccall_T *)cookie;
23106 ufunc_T *fp = fcp->func;
23107
23108 if (fp->uf_profiling && sourcing_lnum >= 1
23109 && sourcing_lnum <= fp->uf_lines.ga_len)
23110 {
23111 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023112 /* Skip continuation lines. */
23113 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23114 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023115 fp->uf_tml_execed = FALSE;
23116 profile_start(&fp->uf_tml_start);
23117 profile_zero(&fp->uf_tml_children);
23118 profile_get_wait(&fp->uf_tml_wait);
23119 }
23120}
23121
23122/*
23123 * Called when actually executing a function line.
23124 */
23125 void
23126func_line_exec(cookie)
23127 void *cookie;
23128{
23129 funccall_T *fcp = (funccall_T *)cookie;
23130 ufunc_T *fp = fcp->func;
23131
23132 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23133 fp->uf_tml_execed = TRUE;
23134}
23135
23136/*
23137 * Called when done with a function line.
23138 */
23139 void
23140func_line_end(cookie)
23141 void *cookie;
23142{
23143 funccall_T *fcp = (funccall_T *)cookie;
23144 ufunc_T *fp = fcp->func;
23145
23146 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23147 {
23148 if (fp->uf_tml_execed)
23149 {
23150 ++fp->uf_tml_count[fp->uf_tml_idx];
23151 profile_end(&fp->uf_tml_start);
23152 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023153 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023154 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23155 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023156 }
23157 fp->uf_tml_idx = -1;
23158 }
23159}
23160#endif
23161
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162/*
23163 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023164 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 */
23166 int
23167func_has_ended(cookie)
23168 void *cookie;
23169{
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171
23172 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23173 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023174 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 || fcp->returned);
23176}
23177
23178/*
23179 * return TRUE if cookie indicates a function which "abort"s on errors.
23180 */
23181 int
23182func_has_abort(cookie)
23183 void *cookie;
23184{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023185 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186}
23187
23188#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23189typedef enum
23190{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023191 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23192 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23193 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194} var_flavour_T;
23195
23196static var_flavour_T var_flavour __ARGS((char_u *varname));
23197
23198 static var_flavour_T
23199var_flavour(varname)
23200 char_u *varname;
23201{
23202 char_u *p = varname;
23203
23204 if (ASCII_ISUPPER(*p))
23205 {
23206 while (*(++p))
23207 if (ASCII_ISLOWER(*p))
23208 return VAR_FLAVOUR_SESSION;
23209 return VAR_FLAVOUR_VIMINFO;
23210 }
23211 else
23212 return VAR_FLAVOUR_DEFAULT;
23213}
23214#endif
23215
23216#if defined(FEAT_VIMINFO) || defined(PROTO)
23217/*
23218 * Restore global vars that start with a capital from the viminfo file
23219 */
23220 int
23221read_viminfo_varlist(virp, writing)
23222 vir_T *virp;
23223 int writing;
23224{
23225 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023226 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228
23229 if (!writing && (find_viminfo_parameter('!') != NULL))
23230 {
23231 tab = vim_strchr(virp->vir_line + 1, '\t');
23232 if (tab != NULL)
23233 {
23234 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023235 switch (*tab)
23236 {
23237 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023238#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023239 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023240#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023241 case 'D': type = VAR_DICT; break;
23242 case 'L': type = VAR_LIST; break;
23243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244
23245 tab = vim_strchr(tab, '\t');
23246 if (tab != NULL)
23247 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023248 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023249 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023250 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023252#ifdef FEAT_FLOAT
23253 else if (type == VAR_FLOAT)
23254 (void)string2float(tab + 1, &tv.vval.v_float);
23255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023257 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023258 if (type == VAR_DICT || type == VAR_LIST)
23259 {
23260 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23261
23262 if (etv == NULL)
23263 /* Failed to parse back the dict or list, use it as a
23264 * string. */
23265 tv.v_type = VAR_STRING;
23266 else
23267 {
23268 vim_free(tv.vval.v_string);
23269 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023270 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023271 }
23272 }
23273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023274 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023275
23276 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023277 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023278 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23279 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 }
23281 }
23282 }
23283
23284 return viminfo_readline(virp);
23285}
23286
23287/*
23288 * Write global vars that start with a capital to the viminfo file
23289 */
23290 void
23291write_viminfo_varlist(fp)
23292 FILE *fp;
23293{
Bram Moolenaar33570922005-01-25 22:26:29 +000023294 hashitem_T *hi;
23295 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023296 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023297 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023298 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023299 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023300 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301
23302 if (find_viminfo_parameter('!') == NULL)
23303 return;
23304
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023305 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023306
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023307 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023310 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023312 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023313 this_var = HI2DI(hi);
23314 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023315 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023316 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023317 {
23318 case VAR_STRING: s = "STR"; break;
23319 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023320#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023321 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023322#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023323 case VAR_DICT: s = "DIC"; break;
23324 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023325 default: continue;
23326 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023327 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023328 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023329 if (p != NULL)
23330 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023331 vim_free(tofree);
23332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 }
23334 }
23335}
23336#endif
23337
23338#if defined(FEAT_SESSION) || defined(PROTO)
23339 int
23340store_session_globals(fd)
23341 FILE *fd;
23342{
Bram Moolenaar33570922005-01-25 22:26:29 +000023343 hashitem_T *hi;
23344 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023345 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 char_u *p, *t;
23347
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023348 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023349 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023351 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023353 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 this_var = HI2DI(hi);
23355 if ((this_var->di_tv.v_type == VAR_NUMBER
23356 || this_var->di_tv.v_type == VAR_STRING)
23357 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023359 /* Escape special characters with a backslash. Turn a LF and
23360 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023361 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023362 (char_u *)"\\\"\n\r");
23363 if (p == NULL) /* out of memory */
23364 break;
23365 for (t = p; *t != NUL; ++t)
23366 if (*t == '\n')
23367 *t = 'n';
23368 else if (*t == '\r')
23369 *t = 'r';
23370 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023371 this_var->di_key,
23372 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23373 : ' ',
23374 p,
23375 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23376 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023377 || put_eol(fd) == FAIL)
23378 {
23379 vim_free(p);
23380 return FAIL;
23381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 vim_free(p);
23383 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023384#ifdef FEAT_FLOAT
23385 else if (this_var->di_tv.v_type == VAR_FLOAT
23386 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23387 {
23388 float_T f = this_var->di_tv.vval.v_float;
23389 int sign = ' ';
23390
23391 if (f < 0)
23392 {
23393 f = -f;
23394 sign = '-';
23395 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023396 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023397 this_var->di_key, sign, f) < 0)
23398 || put_eol(fd) == FAIL)
23399 return FAIL;
23400 }
23401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 }
23403 }
23404 return OK;
23405}
23406#endif
23407
Bram Moolenaar661b1822005-07-28 22:36:45 +000023408/*
23409 * Display script name where an item was last set.
23410 * Should only be invoked when 'verbose' is non-zero.
23411 */
23412 void
23413last_set_msg(scriptID)
23414 scid_T scriptID;
23415{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023416 char_u *p;
23417
Bram Moolenaar661b1822005-07-28 22:36:45 +000023418 if (scriptID != 0)
23419 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023420 p = home_replace_save(NULL, get_scriptname(scriptID));
23421 if (p != NULL)
23422 {
23423 verbose_enter();
23424 MSG_PUTS(_("\n\tLast set from "));
23425 MSG_PUTS(p);
23426 vim_free(p);
23427 verbose_leave();
23428 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023429 }
23430}
23431
Bram Moolenaard812df62008-11-09 12:46:09 +000023432/*
23433 * List v:oldfiles in a nice way.
23434 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023435 void
23436ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023437 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023438{
23439 list_T *l = vimvars[VV_OLDFILES].vv_list;
23440 listitem_T *li;
23441 int nr = 0;
23442
23443 if (l == NULL)
23444 msg((char_u *)_("No old files"));
23445 else
23446 {
23447 msg_start();
23448 msg_scroll = TRUE;
23449 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23450 {
23451 msg_outnum((long)++nr);
23452 MSG_PUTS(": ");
23453 msg_outtrans(get_tv_string(&li->li_tv));
23454 msg_putchar('\n');
23455 out_flush(); /* output one line at a time */
23456 ui_breakcheck();
23457 }
23458 /* Assume "got_int" was set to truncate the listing. */
23459 got_int = FALSE;
23460
23461#ifdef FEAT_BROWSE_CMD
23462 if (cmdmod.browse)
23463 {
23464 quit_more = FALSE;
23465 nr = prompt_for_number(FALSE);
23466 msg_starthere();
23467 if (nr > 0)
23468 {
23469 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23470 (long)nr);
23471
23472 if (p != NULL)
23473 {
23474 p = expand_env_save(p);
23475 eap->arg = p;
23476 eap->cmdidx = CMD_edit;
23477 cmdmod.browse = FALSE;
23478 do_exedit(eap, NULL);
23479 vim_free(p);
23480 }
23481 }
23482 }
23483#endif
23484 }
23485}
23486
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487#endif /* FEAT_EVAL */
23488
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023490#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491
23492#ifdef WIN3264
23493/*
23494 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23495 */
23496static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23497static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23498static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23499
23500/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023501 * Get the short path (8.3) for the filename in "fnamep".
23502 * Only works for a valid file name.
23503 * When the path gets longer "fnamep" is changed and the allocated buffer
23504 * is put in "bufp".
23505 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23506 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 */
23508 static int
23509get_short_pathname(fnamep, bufp, fnamelen)
23510 char_u **fnamep;
23511 char_u **bufp;
23512 int *fnamelen;
23513{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023514 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 char_u *newbuf;
23516
23517 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 l = GetShortPathName(*fnamep, *fnamep, len);
23519 if (l > len - 1)
23520 {
23521 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023522 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 newbuf = vim_strnsave(*fnamep, l);
23524 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023525 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526
23527 vim_free(*bufp);
23528 *fnamep = *bufp = newbuf;
23529
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023530 /* Really should always succeed, as the buffer is big enough. */
23531 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 }
23533
23534 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023535 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536}
23537
23538/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023539 * Get the short path (8.3) for the filename in "fname". The converted
23540 * path is returned in "bufp".
23541 *
23542 * Some of the directories specified in "fname" may not exist. This function
23543 * will shorten the existing directories at the beginning of the path and then
23544 * append the remaining non-existing path.
23545 *
23546 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023547 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023548 * bufp - Pointer to an allocated buffer for the filename.
23549 * fnamelen - Length of the filename pointed to by fname
23550 *
23551 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 */
23553 static int
23554shortpath_for_invalid_fname(fname, bufp, fnamelen)
23555 char_u **fname;
23556 char_u **bufp;
23557 int *fnamelen;
23558{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023559 char_u *short_fname, *save_fname, *pbuf_unused;
23560 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023562 int old_len, len;
23563 int new_len, sfx_len;
23564 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565
23566 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023567 old_len = *fnamelen;
23568 save_fname = vim_strnsave(*fname, old_len);
23569 pbuf_unused = NULL;
23570 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023572 endp = save_fname + old_len - 1; /* Find the end of the copy */
23573 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023575 /*
23576 * Try shortening the supplied path till it succeeds by removing one
23577 * directory at a time from the tail of the path.
23578 */
23579 len = 0;
23580 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023582 /* go back one path-separator */
23583 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23584 --endp;
23585 if (endp <= save_fname)
23586 break; /* processed the complete path */
23587
23588 /*
23589 * Replace the path separator with a NUL and try to shorten the
23590 * resulting path.
23591 */
23592 ch = *endp;
23593 *endp = 0;
23594 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023595 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023596 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23597 {
23598 retval = FAIL;
23599 goto theend;
23600 }
23601 *endp = ch; /* preserve the string */
23602
23603 if (len > 0)
23604 break; /* successfully shortened the path */
23605
23606 /* failed to shorten the path. Skip the path separator */
23607 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 }
23609
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023610 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023612 /*
23613 * Succeeded in shortening the path. Now concatenate the shortened
23614 * path with the remaining path at the tail.
23615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023617 /* Compute the length of the new path. */
23618 sfx_len = (int)(save_endp - endp) + 1;
23619 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023621 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023623 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023625 /* There is not enough space in the currently allocated string,
23626 * copy it to a buffer big enough. */
23627 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023629 {
23630 retval = FAIL;
23631 goto theend;
23632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 }
23634 else
23635 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023636 /* Transfer short_fname to the main buffer (it's big enough),
23637 * unless get_short_pathname() did its work in-place. */
23638 *fname = *bufp = save_fname;
23639 if (short_fname != save_fname)
23640 vim_strncpy(save_fname, short_fname, len);
23641 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023643
23644 /* concat the not-shortened part of the path */
23645 vim_strncpy(*fname + len, endp, sfx_len);
23646 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023648
23649theend:
23650 vim_free(pbuf_unused);
23651 vim_free(save_fname);
23652
23653 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654}
23655
23656/*
23657 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023658 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 */
23660 static int
23661shortpath_for_partial(fnamep, bufp, fnamelen)
23662 char_u **fnamep;
23663 char_u **bufp;
23664 int *fnamelen;
23665{
23666 int sepcount, len, tflen;
23667 char_u *p;
23668 char_u *pbuf, *tfname;
23669 int hasTilde;
23670
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023671 /* Count up the path separators from the RHS.. so we know which part
23672 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023674 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 if (vim_ispathsep(*p))
23676 ++sepcount;
23677
23678 /* Need full path first (use expand_env() to remove a "~/") */
23679 hasTilde = (**fnamep == '~');
23680 if (hasTilde)
23681 pbuf = tfname = expand_env_save(*fnamep);
23682 else
23683 pbuf = tfname = FullName_save(*fnamep, FALSE);
23684
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023685 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023687 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23688 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689
23690 if (len == 0)
23691 {
23692 /* Don't have a valid filename, so shorten the rest of the
23693 * path if we can. This CAN give us invalid 8.3 filenames, but
23694 * there's not a lot of point in guessing what it might be.
23695 */
23696 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023697 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23698 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 }
23700
23701 /* Count the paths backward to find the beginning of the desired string. */
23702 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023703 {
23704#ifdef FEAT_MBYTE
23705 if (has_mbyte)
23706 p -= mb_head_off(tfname, p);
23707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 if (vim_ispathsep(*p))
23709 {
23710 if (sepcount == 0 || (hasTilde && sepcount == 1))
23711 break;
23712 else
23713 sepcount --;
23714 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716 if (hasTilde)
23717 {
23718 --p;
23719 if (p >= tfname)
23720 *p = '~';
23721 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023722 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 }
23724 else
23725 ++p;
23726
23727 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23728 vim_free(*bufp);
23729 *fnamelen = (int)STRLEN(p);
23730 *bufp = pbuf;
23731 *fnamep = p;
23732
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023733 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734}
23735#endif /* WIN3264 */
23736
23737/*
23738 * Adjust a filename, according to a string of modifiers.
23739 * *fnamep must be NUL terminated when called. When returning, the length is
23740 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023741 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 * When there is an error, *fnamep is set to NULL.
23743 */
23744 int
23745modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23746 char_u *src; /* string with modifiers */
23747 int *usedlen; /* characters after src that are used */
23748 char_u **fnamep; /* file name so far */
23749 char_u **bufp; /* buffer for allocated file name or NULL */
23750 int *fnamelen; /* length of fnamep */
23751{
23752 int valid = 0;
23753 char_u *tail;
23754 char_u *s, *p, *pbuf;
23755 char_u dirname[MAXPATHL];
23756 int c;
23757 int has_fullname = 0;
23758#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023759 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 int has_shortname = 0;
23761#endif
23762
23763repeat:
23764 /* ":p" - full path/file_name */
23765 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23766 {
23767 has_fullname = 1;
23768
23769 valid |= VALID_PATH;
23770 *usedlen += 2;
23771
23772 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23773 if ((*fnamep)[0] == '~'
23774#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23775 && ((*fnamep)[1] == '/'
23776# ifdef BACKSLASH_IN_FILENAME
23777 || (*fnamep)[1] == '\\'
23778# endif
23779 || (*fnamep)[1] == NUL)
23780
23781#endif
23782 )
23783 {
23784 *fnamep = expand_env_save(*fnamep);
23785 vim_free(*bufp); /* free any allocated file name */
23786 *bufp = *fnamep;
23787 if (*fnamep == NULL)
23788 return -1;
23789 }
23790
23791 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023792 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 {
23794 if (vim_ispathsep(*p)
23795 && p[1] == '.'
23796 && (p[2] == NUL
23797 || vim_ispathsep(p[2])
23798 || (p[2] == '.'
23799 && (p[3] == NUL || vim_ispathsep(p[3])))))
23800 break;
23801 }
23802
23803 /* FullName_save() is slow, don't use it when not needed. */
23804 if (*p != NUL || !vim_isAbsName(*fnamep))
23805 {
23806 *fnamep = FullName_save(*fnamep, *p != NUL);
23807 vim_free(*bufp); /* free any allocated file name */
23808 *bufp = *fnamep;
23809 if (*fnamep == NULL)
23810 return -1;
23811 }
23812
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023813#ifdef WIN3264
23814# if _WIN32_WINNT >= 0x0500
23815 if (vim_strchr(*fnamep, '~') != NULL)
23816 {
23817 /* Expand 8.3 filename to full path. Needed to make sure the same
23818 * file does not have two different names.
23819 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23820 p = alloc(_MAX_PATH + 1);
23821 if (p != NULL)
23822 {
23823 if (GetLongPathName(*fnamep, p, MAXPATHL))
23824 {
23825 vim_free(*bufp);
23826 *bufp = *fnamep = p;
23827 }
23828 else
23829 vim_free(p);
23830 }
23831 }
23832# endif
23833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 /* Append a path separator to a directory. */
23835 if (mch_isdir(*fnamep))
23836 {
23837 /* Make room for one or two extra characters. */
23838 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23839 vim_free(*bufp); /* free any allocated file name */
23840 *bufp = *fnamep;
23841 if (*fnamep == NULL)
23842 return -1;
23843 add_pathsep(*fnamep);
23844 }
23845 }
23846
23847 /* ":." - path relative to the current directory */
23848 /* ":~" - path relative to the home directory */
23849 /* ":8" - shortname path - postponed till after */
23850 while (src[*usedlen] == ':'
23851 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23852 {
23853 *usedlen += 2;
23854 if (c == '8')
23855 {
23856#ifdef WIN3264
23857 has_shortname = 1; /* Postpone this. */
23858#endif
23859 continue;
23860 }
23861 pbuf = NULL;
23862 /* Need full path first (use expand_env() to remove a "~/") */
23863 if (!has_fullname)
23864 {
23865 if (c == '.' && **fnamep == '~')
23866 p = pbuf = expand_env_save(*fnamep);
23867 else
23868 p = pbuf = FullName_save(*fnamep, FALSE);
23869 }
23870 else
23871 p = *fnamep;
23872
23873 has_fullname = 0;
23874
23875 if (p != NULL)
23876 {
23877 if (c == '.')
23878 {
23879 mch_dirname(dirname, MAXPATHL);
23880 s = shorten_fname(p, dirname);
23881 if (s != NULL)
23882 {
23883 *fnamep = s;
23884 if (pbuf != NULL)
23885 {
23886 vim_free(*bufp); /* free any allocated file name */
23887 *bufp = pbuf;
23888 pbuf = NULL;
23889 }
23890 }
23891 }
23892 else
23893 {
23894 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23895 /* Only replace it when it starts with '~' */
23896 if (*dirname == '~')
23897 {
23898 s = vim_strsave(dirname);
23899 if (s != NULL)
23900 {
23901 *fnamep = s;
23902 vim_free(*bufp);
23903 *bufp = s;
23904 }
23905 }
23906 }
23907 vim_free(pbuf);
23908 }
23909 }
23910
23911 tail = gettail(*fnamep);
23912 *fnamelen = (int)STRLEN(*fnamep);
23913
23914 /* ":h" - head, remove "/file_name", can be repeated */
23915 /* Don't remove the first "/" or "c:\" */
23916 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23917 {
23918 valid |= VALID_HEAD;
23919 *usedlen += 2;
23920 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023921 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023922 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 *fnamelen = (int)(tail - *fnamep);
23924#ifdef VMS
23925 if (*fnamelen > 0)
23926 *fnamelen += 1; /* the path separator is part of the path */
23927#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023928 if (*fnamelen == 0)
23929 {
23930 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23931 p = vim_strsave((char_u *)".");
23932 if (p == NULL)
23933 return -1;
23934 vim_free(*bufp);
23935 *bufp = *fnamep = tail = p;
23936 *fnamelen = 1;
23937 }
23938 else
23939 {
23940 while (tail > s && !after_pathsep(s, tail))
23941 mb_ptr_back(*fnamep, tail);
23942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 }
23944
23945 /* ":8" - shortname */
23946 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23947 {
23948 *usedlen += 2;
23949#ifdef WIN3264
23950 has_shortname = 1;
23951#endif
23952 }
23953
23954#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023955 /*
23956 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 */
23958 if (has_shortname)
23959 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023960 /* Copy the string if it is shortened by :h and when it wasn't copied
23961 * yet, because we are going to change it in place. Avoids changing
23962 * the buffer name for "%:8". */
23963 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 {
23965 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023966 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967 return -1;
23968 vim_free(*bufp);
23969 *bufp = *fnamep = p;
23970 }
23971
23972 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023973 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 if (!has_fullname && !vim_isAbsName(*fnamep))
23975 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023976 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 return -1;
23978 }
23979 else
23980 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023981 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982
Bram Moolenaardc935552011-08-17 15:23:23 +020023983 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023985 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 return -1;
23987
23988 if (l == 0)
23989 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023990 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023992 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023993 return -1;
23994 }
23995 *fnamelen = l;
23996 }
23997 }
23998#endif /* WIN3264 */
23999
24000 /* ":t" - tail, just the basename */
24001 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24002 {
24003 *usedlen += 2;
24004 *fnamelen -= (int)(tail - *fnamep);
24005 *fnamep = tail;
24006 }
24007
24008 /* ":e" - extension, can be repeated */
24009 /* ":r" - root, without extension, can be repeated */
24010 while (src[*usedlen] == ':'
24011 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24012 {
24013 /* find a '.' in the tail:
24014 * - for second :e: before the current fname
24015 * - otherwise: The last '.'
24016 */
24017 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24018 s = *fnamep - 2;
24019 else
24020 s = *fnamep + *fnamelen - 1;
24021 for ( ; s > tail; --s)
24022 if (s[0] == '.')
24023 break;
24024 if (src[*usedlen + 1] == 'e') /* :e */
24025 {
24026 if (s > tail)
24027 {
24028 *fnamelen += (int)(*fnamep - (s + 1));
24029 *fnamep = s + 1;
24030#ifdef VMS
24031 /* cut version from the extension */
24032 s = *fnamep + *fnamelen - 1;
24033 for ( ; s > *fnamep; --s)
24034 if (s[0] == ';')
24035 break;
24036 if (s > *fnamep)
24037 *fnamelen = s - *fnamep;
24038#endif
24039 }
24040 else if (*fnamep <= tail)
24041 *fnamelen = 0;
24042 }
24043 else /* :r */
24044 {
24045 if (s > tail) /* remove one extension */
24046 *fnamelen = (int)(s - *fnamep);
24047 }
24048 *usedlen += 2;
24049 }
24050
24051 /* ":s?pat?foo?" - substitute */
24052 /* ":gs?pat?foo?" - global substitute */
24053 if (src[*usedlen] == ':'
24054 && (src[*usedlen + 1] == 's'
24055 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24056 {
24057 char_u *str;
24058 char_u *pat;
24059 char_u *sub;
24060 int sep;
24061 char_u *flags;
24062 int didit = FALSE;
24063
24064 flags = (char_u *)"";
24065 s = src + *usedlen + 2;
24066 if (src[*usedlen + 1] == 'g')
24067 {
24068 flags = (char_u *)"g";
24069 ++s;
24070 }
24071
24072 sep = *s++;
24073 if (sep)
24074 {
24075 /* find end of pattern */
24076 p = vim_strchr(s, sep);
24077 if (p != NULL)
24078 {
24079 pat = vim_strnsave(s, (int)(p - s));
24080 if (pat != NULL)
24081 {
24082 s = p + 1;
24083 /* find end of substitution */
24084 p = vim_strchr(s, sep);
24085 if (p != NULL)
24086 {
24087 sub = vim_strnsave(s, (int)(p - s));
24088 str = vim_strnsave(*fnamep, *fnamelen);
24089 if (sub != NULL && str != NULL)
24090 {
24091 *usedlen = (int)(p + 1 - src);
24092 s = do_string_sub(str, pat, sub, flags);
24093 if (s != NULL)
24094 {
24095 *fnamep = s;
24096 *fnamelen = (int)STRLEN(s);
24097 vim_free(*bufp);
24098 *bufp = s;
24099 didit = TRUE;
24100 }
24101 }
24102 vim_free(sub);
24103 vim_free(str);
24104 }
24105 vim_free(pat);
24106 }
24107 }
24108 /* after using ":s", repeat all the modifiers */
24109 if (didit)
24110 goto repeat;
24111 }
24112 }
24113
24114 return valid;
24115}
24116
24117/*
24118 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24119 * "flags" can be "g" to do a global substitute.
24120 * Returns an allocated string, NULL for error.
24121 */
24122 char_u *
24123do_string_sub(str, pat, sub, flags)
24124 char_u *str;
24125 char_u *pat;
24126 char_u *sub;
24127 char_u *flags;
24128{
24129 int sublen;
24130 regmatch_T regmatch;
24131 int i;
24132 int do_all;
24133 char_u *tail;
24134 garray_T ga;
24135 char_u *ret;
24136 char_u *save_cpo;
24137
24138 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24139 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024140 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141
24142 ga_init2(&ga, 1, 200);
24143
24144 do_all = (flags[0] == 'g');
24145
24146 regmatch.rm_ic = p_ic;
24147 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24148 if (regmatch.regprog != NULL)
24149 {
24150 tail = str;
24151 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24152 {
24153 /*
24154 * Get some space for a temporary buffer to do the substitution
24155 * into. It will contain:
24156 * - The text up to where the match is.
24157 * - The substituted text.
24158 * - The text after the match.
24159 */
24160 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24161 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24162 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24163 {
24164 ga_clear(&ga);
24165 break;
24166 }
24167
24168 /* copy the text up to where the match is */
24169 i = (int)(regmatch.startp[0] - tail);
24170 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24171 /* add the substituted text */
24172 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24173 + ga.ga_len + i, TRUE, TRUE, FALSE);
24174 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 /* avoid getting stuck on a match with an empty string */
24176 if (tail == regmatch.endp[0])
24177 {
24178 if (*tail == NUL)
24179 break;
24180 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24181 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 }
24183 else
24184 {
24185 tail = regmatch.endp[0];
24186 if (*tail == NUL)
24187 break;
24188 }
24189 if (!do_all)
24190 break;
24191 }
24192
24193 if (ga.ga_data != NULL)
24194 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24195
24196 vim_free(regmatch.regprog);
24197 }
24198
24199 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24200 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024201 if (p_cpo == empty_option)
24202 p_cpo = save_cpo;
24203 else
24204 /* Darn, evaluating {sub} expression changed the value. */
24205 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206
24207 return ret;
24208}
24209
24210#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */