blob: db2dc616e8cfbdacfb6cfefdd83baa5fff8be649 [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));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
394static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
395static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
396static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
397static void item_lock __ARGS((typval_T *tv, int deep, int lock));
398static int tv_islocked __ARGS((typval_T *tv));
399
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
401static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
402static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
403static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
404static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000406static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
407static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000408
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000409static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000414static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000415static void listitem_free __ARGS((listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100417static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
418static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
419static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000420static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000422static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
424static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000425static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000426static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100427static 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 +0000428static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000429static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200430static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000431static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
433static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000437static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000438static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
439static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000441#ifdef FEAT_FLOAT
442static int string2float __ARGS((char_u *text, float_T *value));
443#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
445static int find_internal_func __ARGS((char_u *name));
446static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
447static 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 +0200448static 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 +0000449static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000450static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000451
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000452#ifdef FEAT_FLOAT
453static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200454static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100457static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200463static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200465static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
479static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
480#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000481static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000484static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000486#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000487static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000488static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
490#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000491static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#ifdef FEAT_FLOAT
494static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200495static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
500static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200511#ifdef FEAT_FLOAT
512static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
513#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000514static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000516static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000522#ifdef FEAT_FLOAT
523static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200525static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000527static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000536static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000538static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000544static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000552static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000553static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000554static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000555static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000556static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200558static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000559static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000567static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000581static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100586static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000588static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000589static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000600#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200601static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000602static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
603#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200604#ifdef FEAT_LUA
605static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
606#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000611static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000612static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000613static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000615static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000616static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000619#ifdef vim_mkdir
620static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
621#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100623#ifdef FEAT_MZSCHEME
624static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100628static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000629static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000630#ifdef FEAT_FLOAT
631static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
632#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000633static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000634static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000635static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200636#ifdef FEAT_PYTHON3
637static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
639#ifdef FEAT_PYTHON
640static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000642static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000643static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000644static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000646static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#ifdef FEAT_FLOAT
657static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
658#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100659static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000662static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000663static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000664static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000671static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000672static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000673static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000674static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200676static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000677static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100679#ifdef FEAT_CRYPT
680static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
681#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000682static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200683static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000685#ifdef FEAT_FLOAT
686static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200687static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000690static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000691static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
692static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
696static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
697#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000698static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200699static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000700#ifdef HAVE_STRFTIME
701static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
703static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200709static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200710static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000711static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000716static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200717static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000718static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000720static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000721static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000722static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000723static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000724static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000725static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200726#ifdef FEAT_FLOAT
727static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
728static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
729#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000730static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
734static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
735#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000736static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200737static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200738static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100742static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000743static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000749static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000752static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100753static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000755static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000756static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757static int get_env_len __ARGS((char_u **arg));
758static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000759static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000760static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
761#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
762#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
763 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000764static 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 +0000765static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
768static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static typval_T *alloc_tv __ARGS((void));
770static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static void init_tv __ARGS((typval_T *varp));
772static long get_tv_number __ARGS((typval_T *varp));
773static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000774static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static char_u *get_tv_string __ARGS((typval_T *varp));
776static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000777static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200779static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
781static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
782static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000783static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
784static 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 +0000785static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
786static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000787static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200788static int var_check_func_name __ARGS((char_u *name, int new_var));
789static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000790static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000791static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
793static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
794static int eval_fname_script __ARGS((char_u *p));
795static int eval_fname_sid __ARGS((char_u *p));
796static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static ufunc_T *find_func __ARGS((char_u *name));
798static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000799static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800#ifdef FEAT_PROFILE
801static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000802static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
803static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
804static int
805# ifdef __BORLANDC__
806 _RTLENTRYF
807# endif
808 prof_total_cmp __ARGS((const void *s1, const void *s2));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000815static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000816static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000817static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000818static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000819static 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 +0000820static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
821static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000823static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
824static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000825static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000826static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000827static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200829
830#ifdef EBCDIC
831static int compare_func_name __ARGS((const void *s1, const void *s2));
832static void sortFunctions __ARGS(());
833#endif
834
835
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000836/* Character used as separated in autoload function/variable names. */
837#define AUTOLOAD_CHAR '#'
838
Bram Moolenaar33570922005-01-25 22:26:29 +0000839/*
840 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000841 */
842 void
843eval_init()
844{
Bram Moolenaar33570922005-01-25 22:26:29 +0000845 int i;
846 struct vimvar *p;
847
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200848 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
849 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200850 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000851 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000852 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000853
854 for (i = 0; i < VV_LEN; ++i)
855 {
856 p = &vimvars[i];
857 STRCPY(p->vv_di.di_key, p->vv_name);
858 if (p->vv_flags & VV_RO)
859 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
860 else if (p->vv_flags & VV_RO_SBX)
861 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
862 else
863 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000864
865 /* add to v: scope dict, unless the value is not always available */
866 if (p->vv_type != VAR_UNKNOWN)
867 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000869 /* add to compat scope dict */
870 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000871 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000872 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200873 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200874
875#ifdef EBCDIC
876 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100877 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878 */
879 sortFunctions();
880#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000881}
882
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000883#if defined(EXITFREE) || defined(PROTO)
884 void
885eval_clear()
886{
887 int i;
888 struct vimvar *p;
889
890 for (i = 0; i < VV_LEN; ++i)
891 {
892 p = &vimvars[i];
893 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000894 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000895 vim_free(p->vv_str);
896 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000897 }
898 else if (p->vv_di.di_tv.v_type == VAR_LIST)
899 {
900 list_unref(p->vv_list);
901 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000903 }
904 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000905 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906 hash_clear(&compat_hashtab);
907
Bram Moolenaard9fba312005-06-26 22:34:35 +0000908 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100909# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200910 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100911# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000912
913 /* global variables */
914 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000915
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000916 /* autoloaded script names */
917 ga_clear_strings(&ga_loaded);
918
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200919 /* script-local variables */
920 for (i = 1; i <= ga_scripts.ga_len; ++i)
921 {
922 vars_clear(&SCRIPT_VARS(i));
923 vim_free(SCRIPT_SV(i));
924 }
925 ga_clear(&ga_scripts);
926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000927 /* unreferenced lists and dicts */
928 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000929
930 /* functions */
931 free_all_functions();
932 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933}
934#endif
935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 * Return the name of the executed function.
938 */
939 char_u *
940func_name(cookie)
941 void *cookie;
942{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000943 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944}
945
946/*
947 * Return the address holding the next breakpoint line for a funccall cookie.
948 */
949 linenr_T *
950func_breakpoint(cookie)
951 void *cookie;
952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
956/*
957 * Return the address holding the debug tick for a funccall cookie.
958 */
959 int *
960func_dbg_tick(cookie)
961 void *cookie;
962{
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/*
967 * Return the nesting level for a funccall cookie.
968 */
969 int
970func_level(cookie)
971 void *cookie;
972{
Bram Moolenaar33570922005-01-25 22:26:29 +0000973 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000977funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000979/* pointer to list of previously used funccal, still around because some
980 * item in it is still being used. */
981funccall_T *previous_funccal = NULL;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Return TRUE when a function was ended by a ":return" command.
985 */
986 int
987current_func_returned()
988{
989 return current_funccal->returned;
990}
991
992
993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Set an internal variable to a string value. Creates the variable if it does
995 * not already exist.
996 */
997 void
998set_internal_string_var(name, value)
999 char_u *name;
1000 char_u *value;
1001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001002 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 val = vim_strsave(value);
1006 if (val != NULL)
1007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001008 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001009 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014 }
1015}
1016
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001018static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static char_u *redir_endp = NULL;
1020static char_u *redir_varname = NULL;
1021
1022/*
1023 * Start recording command output to a variable
1024 * Returns OK if successfully completed the setup. FAIL otherwise.
1025 */
1026 int
1027var_redir_start(name, append)
1028 char_u *name;
1029 int append; /* append to an existing variable */
1030{
1031 int save_emsg;
1032 int err;
1033 typval_T tv;
1034
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001035 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001036 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 {
1038 EMSG(_(e_invarg));
1039 return FAIL;
1040 }
1041
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001042 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 redir_varname = vim_strsave(name);
1044 if (redir_varname == NULL)
1045 return FAIL;
1046
1047 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1048 if (redir_lval == NULL)
1049 {
1050 var_redir_stop();
1051 return FAIL;
1052 }
1053
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001054 /* The output is stored in growarray "redir_ga" until redirection ends. */
1055 ga_init2(&redir_ga, (int)sizeof(char), 500);
1056
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001058 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1059 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1061 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001062 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp != NULL && *redir_endp != NUL)
1064 /* Trailing characters are present after the variable name */
1065 EMSG(_(e_trailing));
1066 else
1067 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001068 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 var_redir_stop();
1070 return FAIL;
1071 }
1072
1073 /* check if we can write to the variable: set it to or append an empty
1074 * string */
1075 save_emsg = did_emsg;
1076 did_emsg = FALSE;
1077 tv.v_type = VAR_STRING;
1078 tv.vval.v_string = (char_u *)"";
1079 if (append)
1080 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1081 else
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001083 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001085 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 if (err)
1087 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001088 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 var_redir_stop();
1090 return FAIL;
1091 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097 * Append "value[value_len]" to the variable set by var_redir_start().
1098 * The actual appending is postponed until redirection ends, because the value
1099 * appended may in fact be the string we write to, changing it may cause freed
1100 * memory to be used:
1101 * :redir => foo
1102 * :let foo
1103 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 */
1105 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 if (redir_lval == NULL)
1113 return;
1114
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 {
1122 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 }
1125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127}
1128
1129/*
1130 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001131 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 */
1133 void
1134var_redir_stop()
1135{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 typval_T tv;
1137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_lval != NULL)
1139 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 /* If there was no error: assign the text to the variable. */
1141 if (redir_endp != NULL)
1142 {
1143 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1144 tv.v_type = VAR_STRING;
1145 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001146 /* Call get_lval() again, if it's inside a Dict or List it may
1147 * have changed. */
1148 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1149 FALSE, FALSE, FALSE, FNE_CHECK_START);
1150 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1151 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1152 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 /* free the collected output */
1156 vim_free(redir_ga.ga_data);
1157 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 vim_free(redir_lval);
1160 redir_lval = NULL;
1161 }
1162 vim_free(redir_varname);
1163 redir_varname = NULL;
1164}
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# if defined(FEAT_MBYTE) || defined(PROTO)
1167 int
1168eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1169 char_u *enc_from;
1170 char_u *enc_to;
1171 char_u *fname_from;
1172 char_u *fname_to;
1173{
1174 int err = FALSE;
1175
1176 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1177 set_vim_var_string(VV_CC_TO, enc_to, -1);
1178 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1179 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1180 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1181 err = TRUE;
1182 set_vim_var_string(VV_CC_FROM, NULL, -1);
1183 set_vim_var_string(VV_CC_TO, NULL, -1);
1184 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1185 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1186
1187 if (err)
1188 return FAIL;
1189 return OK;
1190}
1191# endif
1192
1193# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1194 int
1195eval_printexpr(fname, args)
1196 char_u *fname;
1197 char_u *args;
1198{
1199 int err = FALSE;
1200
1201 set_vim_var_string(VV_FNAME_IN, fname, -1);
1202 set_vim_var_string(VV_CMDARG, args, -1);
1203 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1204 err = TRUE;
1205 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1206 set_vim_var_string(VV_CMDARG, NULL, -1);
1207
1208 if (err)
1209 {
1210 mch_remove(fname);
1211 return FAIL;
1212 }
1213 return OK;
1214}
1215# endif
1216
1217# if defined(FEAT_DIFF) || defined(PROTO)
1218 void
1219eval_diff(origfile, newfile, outfile)
1220 char_u *origfile;
1221 char_u *newfile;
1222 char_u *outfile;
1223{
1224 int err = FALSE;
1225
1226 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1227 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1228 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1229 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1230 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1231 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1232 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1233}
1234
1235 void
1236eval_patch(origfile, difffile, outfile)
1237 char_u *origfile;
1238 char_u *difffile;
1239 char_u *outfile;
1240{
1241 int err;
1242
1243 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1244 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1245 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1246 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1247 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1248 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1249 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1250}
1251# endif
1252
1253/*
1254 * Top level evaluation function, returning a boolean.
1255 * Sets "error" to TRUE if there was an error.
1256 * Return TRUE or FALSE.
1257 */
1258 int
1259eval_to_bool(arg, error, nextcmd, skip)
1260 char_u *arg;
1261 int *error;
1262 char_u **nextcmd;
1263 int skip; /* only parse, don't execute */
1264{
Bram Moolenaar33570922005-01-25 22:26:29 +00001265 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 int retval = FALSE;
1267
1268 if (skip)
1269 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 else
1273 {
1274 *error = FALSE;
1275 if (!skip)
1276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001277 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280 }
1281 if (skip)
1282 --emsg_skip;
1283
1284 return retval;
1285}
1286
1287/*
1288 * Top level evaluation function, returning a string. If "skip" is TRUE,
1289 * only parsing to "nextcmd" is done, without reporting errors. Return
1290 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1291 */
1292 char_u *
1293eval_to_string_skip(arg, nextcmd, skip)
1294 char_u *arg;
1295 char_u **nextcmd;
1296 int skip; /* only parse, don't execute */
1297{
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 char_u *retval;
1300
1301 if (skip)
1302 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 retval = NULL;
1305 else
1306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 retval = vim_strsave(get_tv_string(&tv));
1308 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 if (skip)
1311 --emsg_skip;
1312
1313 return retval;
1314}
1315
1316/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001317 * Skip over an expression at "*pp".
1318 * Return FAIL for an error, OK otherwise.
1319 */
1320 int
1321skip_expr(pp)
1322 char_u **pp;
1323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325
1326 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328}
1329
1330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332 * When "convert" is TRUE convert a List into a sequence of lines and convert
1333 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Return pointer to allocated memory, or NULL for failure.
1335 */
1336 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 char_u *arg;
1339 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001344 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001345#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001354 {
1355 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001356 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001357 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001359 if (tv.vval.v_list->lv_len > 0)
1360 ga_append(&ga, NL);
1361 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 ga_append(&ga, NUL);
1363 retval = (char_u *)ga.ga_data;
1364 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365#ifdef FEAT_FLOAT
1366 else if (convert && tv.v_type == VAR_FLOAT)
1367 {
1368 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1369 retval = vim_strsave(numbuf);
1370 }
1371#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 else
1373 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376
1377 return retval;
1378}
1379
1380/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 * Call eval_to_string() without using current local variables and using
1382 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 */
1384 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 char_u *arg;
1387 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 char_u *retval;
1391 void *save_funccalp;
1392
1393 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 if (use_sandbox)
1395 ++sandbox;
1396 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (use_sandbox)
1399 --sandbox;
1400 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 restore_funccal(save_funccalp);
1402 return retval;
1403}
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405/*
1406 * Top level evaluation function, returning a number.
1407 * Evaluates "expr" silently.
1408 * Returns -1 for an error.
1409 */
1410 int
1411eval_to_number(expr)
1412 char_u *expr;
1413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001416 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
1418 ++emsg_off;
1419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 retval = -1;
1422 else
1423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001424 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 --emsg_off;
1428
1429 return retval;
1430}
1431
Bram Moolenaara40058a2005-07-11 22:42:07 +00001432/*
1433 * Prepare v: variable "idx" to be used.
1434 * Save the current typeval in "save_tv".
1435 * When not used yet add the variable to the v: hashtable.
1436 */
1437 static void
1438prepare_vimvar(idx, save_tv)
1439 int idx;
1440 typval_T *save_tv;
1441{
1442 *save_tv = vimvars[idx].vv_tv;
1443 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1444 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1445}
1446
1447/*
1448 * Restore v: variable "idx" to typeval "save_tv".
1449 * When no longer defined, remove the variable from the v: hashtable.
1450 */
1451 static void
1452restore_vimvar(idx, save_tv)
1453 int idx;
1454 typval_T *save_tv;
1455{
1456 hashitem_T *hi;
1457
Bram Moolenaara40058a2005-07-11 22:42:07 +00001458 vimvars[idx].vv_tv = *save_tv;
1459 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1460 {
1461 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1462 if (HASHITEM_EMPTY(hi))
1463 EMSG2(_(e_intern2), "restore_vimvar()");
1464 else
1465 hash_remove(&vimvarht, hi);
1466 }
1467}
1468
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001469#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001470/*
1471 * Evaluate an expression to a list with suggestions.
1472 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001473 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001474 */
1475 list_T *
1476eval_spell_expr(badword, expr)
1477 char_u *badword;
1478 char_u *expr;
1479{
1480 typval_T save_val;
1481 typval_T rettv;
1482 list_T *list = NULL;
1483 char_u *p = skipwhite(expr);
1484
1485 /* Set "v:val" to the bad word. */
1486 prepare_vimvar(VV_VAL, &save_val);
1487 vimvars[VV_VAL].vv_type = VAR_STRING;
1488 vimvars[VV_VAL].vv_str = badword;
1489 if (p_verbose == 0)
1490 ++emsg_off;
1491
1492 if (eval1(&p, &rettv, TRUE) == OK)
1493 {
1494 if (rettv.v_type != VAR_LIST)
1495 clear_tv(&rettv);
1496 else
1497 list = rettv.vval.v_list;
1498 }
1499
1500 if (p_verbose == 0)
1501 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001502 restore_vimvar(VV_VAL, &save_val);
1503
1504 return list;
1505}
1506
1507/*
1508 * "list" is supposed to contain two items: a word and a number. Return the
1509 * word in "pp" and the number as the return value.
1510 * Return -1 if anything isn't right.
1511 * Used to get the good word and score from the eval_spell_expr() result.
1512 */
1513 int
1514get_spellword(list, pp)
1515 list_T *list;
1516 char_u **pp;
1517{
1518 listitem_T *li;
1519
1520 li = list->lv_first;
1521 if (li == NULL)
1522 return -1;
1523 *pp = get_tv_string(&li->li_tv);
1524
1525 li = li->li_next;
1526 if (li == NULL)
1527 return -1;
1528 return get_tv_number(&li->li_tv);
1529}
1530#endif
1531
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001532/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001533 * Top level evaluation function.
1534 * Returns an allocated typval_T with the result.
1535 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536 */
1537 typval_T *
1538eval_expr(arg, nextcmd)
1539 char_u *arg;
1540 char_u **nextcmd;
1541{
1542 typval_T *tv;
1543
1544 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 {
1547 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 }
1550
1551 return tv;
1552}
1553
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001561 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001562call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001567 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 long n;
1572 int len;
1573 int i;
1574 int doesrange;
1575 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001578 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 for (i = 0; i < argc; i++)
1583 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001584 /* Pass a NULL or empty argument as an empty string */
1585 if (argv[i] == NULL || *argv[i] == NUL)
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 continue;
1590 }
1591
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001592 if (str_arg_only)
1593 len = 0;
1594 else
1595 /* Recognize a number argument, the others must be strings. */
1596 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (len != 0 && len == (int)STRLEN(argv[i]))
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_NUMBER;
1600 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 else
1603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001604 argvars[i].v_type = VAR_STRING;
1605 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608
1609 if (safe)
1610 {
1611 save_funccalp = save_funccal();
1612 ++sandbox;
1613 }
1614
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1616 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (safe)
1620 {
1621 --sandbox;
1622 restore_funccal(save_funccalp);
1623 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 vim_free(argvars);
1625
1626 if (ret == FAIL)
1627 clear_tv(rettv);
1628
1629 return ret;
1630}
1631
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001632/*
1633 * Call vimL function "func" and return the result as a number.
1634 * Returns -1 when calling the function fails.
1635 * Uses argv[argc] for the function arguments.
1636 */
1637 long
1638call_func_retnr(func, argc, argv, safe)
1639 char_u *func;
1640 int argc;
1641 char_u **argv;
1642 int safe; /* use the sandbox */
1643{
1644 typval_T rettv;
1645 long retval;
1646
1647 /* All arguments are passed as strings, no conversion to number. */
1648 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1649 return -1;
1650
1651 retval = get_tv_number_chk(&rettv, NULL);
1652 clear_tv(&rettv);
1653 return retval;
1654}
1655
1656#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1657 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1658
Bram Moolenaar4f688582007-07-24 12:34:30 +00001659# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001661 * Call vimL function "func" and return the result as a string.
1662 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 * Uses argv[argc] for the function arguments.
1664 */
1665 void *
1666call_func_retstr(func, argc, argv, safe)
1667 char_u *func;
1668 int argc;
1669 char_u **argv;
1670 int safe; /* use the sandbox */
1671{
1672 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001675 /* All arguments are passed as strings, no conversion to number. */
1676 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 return NULL;
1678
1679 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 return retval;
1682}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001683# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001688 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 */
1690 void *
1691call_func_retlist(func, argc, argv, safe)
1692 char_u *func;
1693 int argc;
1694 char_u **argv;
1695 int safe; /* use the sandbox */
1696{
1697 typval_T rettv;
1698
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001699 /* All arguments are passed as strings, no conversion to number. */
1700 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 return NULL;
1702
1703 if (rettv.v_type != VAR_LIST)
1704 {
1705 clear_tv(&rettv);
1706 return NULL;
1707 }
1708
1709 return rettv.vval.v_list;
1710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#endif
1712
1713/*
1714 * Save the current function call pointer, and set it to NULL.
1715 * Used when executing autocommands and for ":source".
1716 */
1717 void *
1718save_funccal()
1719{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 current_funccal = NULL;
1723 return (void *)fc;
1724}
1725
1726 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727restore_funccal(vfc)
1728 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001730 funccall_T *fc = (funccall_T *)vfc;
1731
1732 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733}
1734
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735#if defined(FEAT_PROFILE) || defined(PROTO)
1736/*
1737 * Prepare profiling for entering a child or something else that is not
1738 * counted for the script/function itself.
1739 * Should always be called in pair with prof_child_exit().
1740 */
1741 void
1742prof_child_enter(tm)
1743 proftime_T *tm; /* place to store waittime */
1744{
1745 funccall_T *fc = current_funccal;
1746
1747 if (fc != NULL && fc->func->uf_profiling)
1748 profile_start(&fc->prof_child);
1749 script_prof_save(tm);
1750}
1751
1752/*
1753 * Take care of time spent in a child.
1754 * Should always be called after prof_child_enter().
1755 */
1756 void
1757prof_child_exit(tm)
1758 proftime_T *tm; /* where waittime was stored */
1759{
1760 funccall_T *fc = current_funccal;
1761
1762 if (fc != NULL && fc->func->uf_profiling)
1763 {
1764 profile_end(&fc->prof_child);
1765 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1766 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1767 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1768 }
1769 script_prof_restore(tm);
1770}
1771#endif
1772
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_FOLDING
1775/*
1776 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1777 * it in "*cp". Doesn't give error messages.
1778 */
1779 int
1780eval_foldexpr(arg, cp)
1781 char_u *arg;
1782 int *cp;
1783{
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 int retval;
1786 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001787 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1788 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001791 if (use_sandbox)
1792 ++sandbox;
1793 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 if (tv.v_type == VAR_NUMBER)
1801 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001802 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 retval = 0;
1804 else
1805 {
1806 /* If the result is a string, check if there is a non-digit before
1807 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (!VIM_ISDIGIT(*s) && *s != '-')
1810 *cp = *s++;
1811 retval = atol((char *)s);
1812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001816 if (use_sandbox)
1817 --sandbox;
1818 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820 return retval;
1821}
1822#endif
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let" list all variable values
1826 * ":let var1 var2" list variable values
1827 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 * ":let var += expr" assignment command.
1829 * ":let var -= expr" assignment command.
1830 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 */
1833 void
1834ex_let(eap)
1835 exarg_T *eap;
1836{
1837 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 int var_count = 0;
1842 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001843 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaardb552d602006-03-23 22:59:57 +00001847 argend = skip_var_list(arg, &var_count, &semicolon);
1848 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001850 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1851 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001855 /*
1856 * ":let" without "=": list variables
1857 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 if (*arg == '[')
1859 EMSG(_(e_invarg));
1860 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_glob_vars(&first);
1867 list_buf_vars(&first);
1868 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001871#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_script_vars(&first);
1873 list_func_vars(&first);
1874 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 eap->nextcmd = check_nextcmd(arg);
1877 }
1878 else
1879 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 op[0] = '=';
1881 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 {
1884 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1885 op[0] = expr[-1]; /* +=, -= or .= */
1886 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (eap->skip)
1890 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (eap->skip)
1893 {
1894 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 --emsg_skip;
1897 }
1898 else if (i != FAIL)
1899 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 }
1905}
1906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907/*
1908 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1909 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 * When "nextchars" is not NULL it points to a string with characters that
1911 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1912 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 * Returns OK or FAIL;
1914 */
1915 static int
1916ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1917 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int copy; /* copy values from "tv", don't move */
1920 int semicolon; /* from skip_var_list() */
1921 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923{
1924 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 listitem_T *item;
1928 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929
1930 if (*arg != '[')
1931 {
1932 /*
1933 * ":let var = expr" or ":for var in list"
1934 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 return FAIL;
1937 return OK;
1938 }
1939
1940 /*
1941 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1942 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001943 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 {
1945 EMSG(_(e_listreq));
1946 return FAIL;
1947 }
1948
1949 i = list_len(l);
1950 if (semicolon == 0 && var_count < i)
1951 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001952 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 return FAIL;
1954 }
1955 if (var_count - semicolon > i)
1956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001957 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 }
1960
1961 item = l->lv_first;
1962 while (*arg != ']')
1963 {
1964 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 item = item->li_next;
1967 if (arg == NULL)
1968 return FAIL;
1969
1970 arg = skipwhite(arg);
1971 if (*arg == ';')
1972 {
1973 /* Put the rest of the list (may be empty) in the var after ';'.
1974 * Create a new list for this. */
1975 l = list_alloc();
1976 if (l == NULL)
1977 return FAIL;
1978 while (item != NULL)
1979 {
1980 list_append_tv(l, &item->li_tv);
1981 item = item->li_next;
1982 }
1983
1984 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001985 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 ltv.vval.v_list = l;
1987 l->lv_refcount = 1;
1988
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1990 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 clear_tv(&ltv);
1992 if (arg == NULL)
1993 return FAIL;
1994 break;
1995 }
1996 else if (*arg != ',' && *arg != ']')
1997 {
1998 EMSG2(_(e_intern2), "ex_let_vars()");
1999 return FAIL;
2000 }
2001 }
2002
2003 return OK;
2004}
2005
2006/*
2007 * Skip over assignable variable "var" or list of variables "[var, var]".
2008 * Used for ":let varvar = expr" and ":for varvar in expr".
2009 * For "[var, var]" increment "*var_count" for each variable.
2010 * for "[var, var; var]" set "semicolon".
2011 * Return NULL for an error.
2012 */
2013 static char_u *
2014skip_var_list(arg, var_count, semicolon)
2015 char_u *arg;
2016 int *var_count;
2017 int *semicolon;
2018{
2019 char_u *p, *s;
2020
2021 if (*arg == '[')
2022 {
2023 /* "[var, var]": find the matching ']'. */
2024 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002025 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 {
2027 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2028 s = skip_var_one(p);
2029 if (s == p)
2030 {
2031 EMSG2(_(e_invarg2), p);
2032 return NULL;
2033 }
2034 ++*var_count;
2035
2036 p = skipwhite(s);
2037 if (*p == ']')
2038 break;
2039 else if (*p == ';')
2040 {
2041 if (*semicolon == 1)
2042 {
2043 EMSG(_("Double ; in list of variables"));
2044 return NULL;
2045 }
2046 *semicolon = 1;
2047 }
2048 else if (*p != ',')
2049 {
2050 EMSG2(_(e_invarg2), p);
2051 return NULL;
2052 }
2053 }
2054 return p + 1;
2055 }
2056 else
2057 return skip_var_one(arg);
2058}
2059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002060/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002061 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 static char_u *
2065skip_var_one(arg)
2066 char_u *arg;
2067{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 if (*arg == '@' && arg[1] != NUL)
2069 return arg + 2;
2070 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2071 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072}
2073
Bram Moolenaara7043832005-01-21 11:56:39 +00002074/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 * List variables for hashtab "ht" with prefix "prefix".
2076 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002079list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084{
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 hashitem_T *hi;
2086 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 int todo;
2088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2091 {
2092 if (!HASHITEM_EMPTY(hi))
2093 {
2094 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 di = HI2DI(hi);
2096 if (empty || di->di_tv.v_type != VAR_STRING
2097 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 }
2100 }
2101}
2102
2103/*
2104 * List global variables.
2105 */
2106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_glob_vars(first)
2108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111}
2112
2113/*
2114 * List buffer variables.
2115 */
2116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_buf_vars(first)
2118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 char_u numbuf[NUMBUFLEN];
2121
Bram Moolenaar429fa852013-04-15 12:27:36 +02002122 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124
2125 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2127 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002128}
2129
2130/*
2131 * List window variables.
2132 */
2133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134list_win_vars(first)
2135 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141#ifdef FEAT_WINDOWS
2142/*
2143 * List tab page variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_tab_vars(first)
2147 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002151}
2152#endif
2153
Bram Moolenaara7043832005-01-21 11:56:39 +00002154/*
2155 * List Vim variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_vim_vars(first)
2159 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162}
2163
2164/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165 * List script-local variables, if there is a script.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_script_vars(first)
2169 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170{
2171 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2173 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174}
2175
2176/*
2177 * List function variables, if there is a function.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_func_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_funccal != NULL)
2184 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * List variables in "arg".
2190 */
2191 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 exarg_T *eap;
2194 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196{
2197 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 char_u *name_start;
2201 char_u *arg_subsc;
2202 char_u *tofree;
2203 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204
2205 while (!ends_excmd(*arg) && !got_int)
2206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002209 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2211 {
2212 emsg_severe = TRUE;
2213 EMSG(_(e_trailing));
2214 break;
2215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 /* get_name_len() takes care of expanding curly braces */
2220 name_start = name = arg;
2221 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2222 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224 /* This is mainly to keep test 49 working: when expanding
2225 * curly braces fails overrule the exception error message. */
2226 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 emsg_severe = TRUE;
2229 EMSG2(_(e_invarg2), arg);
2230 break;
2231 }
2232 error = TRUE;
2233 }
2234 else
2235 {
2236 if (tofree != NULL)
2237 name = tofree;
2238 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 /* handle d.key, l[idx], f(expr) */
2243 arg_subsc = arg;
2244 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'g': list_glob_vars(first); break;
2253 case 'b': list_buf_vars(first); break;
2254 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'v': list_vim_vars(first); break;
2259 case 's': list_script_vars(first); break;
2260 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 default:
2262 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 }
2265 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 {
2267 char_u numbuf[NUMBUFLEN];
2268 char_u *tf;
2269 int c;
2270 char_u *s;
2271
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002272 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 c = *arg;
2274 *arg = NUL;
2275 list_one_var_a((char_u *)"",
2276 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 tv.v_type,
2278 s == NULL ? (char_u *)"" : s,
2279 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 *arg = c;
2281 vim_free(tf);
2282 }
2283 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287
2288 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290
2291 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 }
2293
2294 return arg;
2295}
2296
2297/*
2298 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2299 * Returns a pointer to the char just after the var name.
2300 * Returns NULL if there is an error.
2301 */
2302 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002307 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309{
2310 int c1;
2311 char_u *name;
2312 char_u *p;
2313 char_u *arg_end = NULL;
2314 int len;
2315 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317
2318 /*
2319 * ":let $VAR = expr": Set environment variable.
2320 */
2321 if (*arg == '$')
2322 {
2323 /* Find the end of the name. */
2324 ++arg;
2325 name = arg;
2326 len = get_env_len(&arg);
2327 if (len == 0)
2328 EMSG2(_(e_invarg2), name - 1);
2329 else
2330 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 if (op != NULL && (*op == '+' || *op == '-'))
2332 EMSG2(_(e_letwrong), op);
2333 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002334 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002336 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
2338 c1 = name[len];
2339 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340 p = get_tv_string_chk(tv);
2341 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 {
2343 int mustfree = FALSE;
2344 char_u *s = vim_getenv(name, &mustfree);
2345
2346 if (s != NULL)
2347 {
2348 p = tofree = concat_str(s, p);
2349 if (mustfree)
2350 vim_free(s);
2351 }
2352 }
2353 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (STRICMP(name, "HOME") == 0)
2357 init_homedir();
2358 else if (didset_vim && STRICMP(name, "VIM") == 0)
2359 didset_vim = FALSE;
2360 else if (didset_vimruntime
2361 && STRICMP(name, "VIMRUNTIME") == 0)
2362 didset_vimruntime = FALSE;
2363 arg_end = arg;
2364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
2368 }
2369 }
2370
2371 /*
2372 * ":let &option = expr": Set option value.
2373 * ":let &l:option = expr": Set local option value.
2374 * ":let &g:option = expr": Set global option value.
2375 */
2376 else if (*arg == '&')
2377 {
2378 /* Find the end of the name. */
2379 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 if (p == NULL || (endchars != NULL
2381 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 EMSG(_(e_letunexp));
2383 else
2384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 long n;
2386 int opt_type;
2387 long numval;
2388 char_u *stringval = NULL;
2389 char_u *s;
2390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 c1 = *p;
2392 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393
2394 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 s = get_tv_string_chk(tv); /* != NULL if number or string */
2396 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 opt_type = get_option_value(arg, &numval,
2399 &stringval, opt_flags);
2400 if ((opt_type == 1 && *op == '.')
2401 || (opt_type == 0 && *op != '.'))
2402 EMSG2(_(e_letwrong), op);
2403 else
2404 {
2405 if (opt_type == 1) /* number */
2406 {
2407 if (*op == '+')
2408 n = numval + n;
2409 else
2410 n = numval - n;
2411 }
2412 else if (opt_type == 0 && stringval != NULL) /* string */
2413 {
2414 s = concat_str(stringval, s);
2415 vim_free(stringval);
2416 stringval = s;
2417 }
2418 }
2419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 if (s != NULL)
2421 {
2422 set_option_value(arg, n, s, opt_flags);
2423 arg_end = p;
2424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 }
2428 }
2429
2430 /*
2431 * ":let @r = expr": Set register contents.
2432 */
2433 else if (*arg == '@')
2434 {
2435 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 if (op != NULL && (*op == '+' || *op == '-'))
2437 EMSG2(_(e_letwrong), op);
2438 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 EMSG(_(e_letunexp));
2441 else
2442 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002443 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 char_u *s;
2445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 p = get_tv_string_chk(tv);
2447 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002449 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 if (s != NULL)
2451 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002452 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 vim_free(s);
2454 }
2455 }
2456 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 arg_end = arg + 1;
2460 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002461 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002469 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002473 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2477 EMSG(_(e_letunexp));
2478 else
2479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 arg_end = p;
2482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
2486
2487 else
2488 EMSG2(_(e_invarg2), arg);
2489
2490 return arg_end;
2491}
2492
2493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2495 */
2496 static int
2497check_changedtick(arg)
2498 char_u *arg;
2499{
2500 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2501 {
2502 EMSG2(_(e_readonlyvar), arg);
2503 return TRUE;
2504 }
2505 return FALSE;
2506}
2507
2508/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * Get an lval: variable, Dict item or List item that can be assigned a value
2510 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2511 * "name.key", "name.key[expr]" etc.
2512 * Indexing only works if "name" is an existing List or Dictionary.
2513 * "name" points to the start of the name.
2514 * If "rettv" is not NULL it points to the value to be assigned.
2515 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2516 * wrong; must end in space or cmd separator.
2517 *
2518 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002519 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 */
2522 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 typval_T *rettv;
2526 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 int unlet;
2528 int skip;
2529 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002530 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 char_u *expr_start, *expr_end;
2534 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 dictitem_T *v;
2536 typval_T var1;
2537 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546
2547 if (skip)
2548 {
2549 /* When skipping just find the end of the name. */
2550 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002551 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 }
2553
2554 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (expr_start != NULL)
2557 {
2558 /* Don't expand the name when we already know there is an error. */
2559 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2560 && *p != '[' && *p != '.')
2561 {
2562 EMSG(_(e_trailing));
2563 return NULL;
2564 }
2565
2566 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2567 if (lp->ll_exp_name == NULL)
2568 {
2569 /* Report an invalid expression in braces, unless the
2570 * expression evaluation has been cancelled due to an
2571 * aborting error, an interrupt, or an exception. */
2572 if (!aborting() && !quiet)
2573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002574 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 EMSG2(_(e_invarg2), name);
2576 return NULL;
2577 }
2578 }
2579 lp->ll_name = lp->ll_exp_name;
2580 }
2581 else
2582 lp->ll_name = name;
2583
2584 /* Without [idx] or .key we are done. */
2585 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2586 return p;
2587
2588 cc = *p;
2589 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002590 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (v == NULL && !quiet)
2592 EMSG2(_(e_undefvar), lp->ll_name);
2593 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 if (v == NULL)
2595 return NULL;
2596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /*
2598 * Loop until no more [idx] or .key is following.
2599 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2604 && !(lp->ll_tv->v_type == VAR_DICT
2605 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E689: Can only index a List or Dictionary"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSG(_("E708: [:] must come last"));
2615 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 len = -1;
2619 if (*p == '.')
2620 {
2621 key = p + 1;
2622 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2623 ;
2624 if (len == 0)
2625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (!quiet)
2627 EMSG(_(e_emptykey));
2628 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 }
2630 p = key + len;
2631 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 if (*p == ':')
2637 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 else
2639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 empty1 = FALSE;
2641 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002643 if (get_tv_string_chk(&var1) == NULL)
2644 {
2645 /* not a number or string */
2646 clear_tv(&var1);
2647 return NULL;
2648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650
2651 /* Optionally get the second index [ :expr]. */
2652 if (*p == ':')
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002657 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 if (!empty1)
2659 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (rettv != NULL && (rettv->v_type != VAR_LIST
2663 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (!empty1)
2668 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671 p = skipwhite(p + 1);
2672 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 else
2675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 if (get_tv_string_chk(&var2) == NULL)
2684 {
2685 /* not a number or string */
2686 if (!empty1)
2687 clear_tv(&var1);
2688 clear_tv(&var2);
2689 return NULL;
2690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707
2708 /* Skip to past ']'. */
2709 ++p;
2710 }
2711
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
2714 if (len == -1)
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (*key == NUL)
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (!quiet)
2721 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 lp->ll_list = NULL;
2727 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002728 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002729
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002730 /* When assigning to a scope dictionary check that a function and
2731 * variable name is valid (only variable name unless it is l: or
2732 * g: dictionary). Disallow overwriting a builtin function. */
2733 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002734 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002735 int prevval;
2736 int wrong;
2737
2738 if (len != -1)
2739 {
2740 prevval = key[len];
2741 key[len] = NUL;
2742 }
2743 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2744 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 || !valid_varname(key);
2747 if (len != -1)
2748 key[len] = prevval;
2749 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002750 return NULL;
2751 }
2752
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002755 /* Can't add "v:" variable. */
2756 if (lp->ll_dict == &vimvardict)
2757 {
2758 EMSG2(_(e_illvar), name);
2759 return NULL;
2760 }
2761
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002762 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002766 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (len == -1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
2771 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (len == -1)
2776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 p = NULL;
2779 break;
2780 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002781 /* existing variable, need to check if it can be changed */
2782 else if (var_check_ro(lp->ll_di->di_flags, name))
2783 return NULL;
2784
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (len == -1)
2786 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 }
2789 else
2790 {
2791 /*
2792 * Get the number and item for the only or first index of the List.
2793 */
2794 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 else
2797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002798 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 clear_tv(&var1);
2800 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002801 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_list = lp->ll_tv->vval.v_list;
2803 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2804 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002806 if (lp->ll_n1 < 0)
2807 {
2808 lp->ll_n1 = 0;
2809 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2810 }
2811 }
2812 if (lp->ll_li == NULL)
2813 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002816 if (!quiet)
2817 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 }
2820
2821 /*
2822 * May need to find the item or absolute index for the second
2823 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 * When no index given: "lp->ll_empty2" is TRUE.
2825 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002829 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002835 {
2836 if (!quiet)
2837 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002839 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2844 if (lp->ll_n1 < 0)
2845 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2846 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002847 {
2848 if (!quiet)
2849 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002855 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002856 }
2857
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return p;
2859}
2860
2861/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 */
2864 static void
2865clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867{
2868 vim_free(lp->ll_exp_name);
2869 vim_free(lp->ll_newkey);
2870}
2871
2872/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002873 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002875 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 */
2877 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002878set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002883 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884{
2885 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002886 listitem_T *ri;
2887 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888
2889 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 cc = *endp;
2894 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 if (op != NULL && *op != '=')
2896 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002897 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002898
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002899 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002900 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002901 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902 {
2903 if (tv_op(&tv, rettv, op) == OK)
2904 set_var(lp->ll_name, &tv, FALSE);
2905 clear_tv(&tv);
2906 }
2907 }
2908 else
2909 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002913 else if (tv_check_lock(lp->ll_newkey == NULL
2914 ? lp->ll_tv->v_lock
2915 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2916 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 else if (lp->ll_range)
2918 {
2919 /*
2920 * Assign the List values to the list items.
2921 */
2922 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2926 else
2927 {
2928 clear_tv(&lp->ll_li->li_tv);
2929 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2930 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 ri = ri->li_next;
2932 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2933 break;
2934 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002937 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002938 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 ri = NULL;
2940 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_li = lp->ll_li->li_next;
2944 ++lp->ll_n1;
2945 }
2946 if (ri != NULL)
2947 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 else if (lp->ll_empty2
2949 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 : lp->ll_n1 != lp->ll_n2)
2951 EMSG(_("E711: List value has not enough items"));
2952 }
2953 else
2954 {
2955 /*
2956 * Assign to a List or Dictionary item.
2957 */
2958 if (lp->ll_newkey != NULL)
2959 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 if (op != NULL && *op != '=')
2961 {
2962 EMSG2(_(e_letwrong), op);
2963 return;
2964 }
2965
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002967 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 if (di == NULL)
2969 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002970 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2971 {
2972 vim_free(di);
2973 return;
2974 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002976 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 else if (op != NULL && *op != '=')
2978 {
2979 tv_op(lp->ll_tv, rettv, op);
2980 return;
2981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002984
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 /*
2986 * Assign the value to the variable or list item.
2987 */
2988 if (copy)
2989 copy_tv(rettv, lp->ll_tv);
2990 else
2991 {
2992 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002993 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 }
2996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997}
2998
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002999/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3001 * Returns OK or FAIL.
3002 */
3003 static int
3004tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003005 typval_T *tv1;
3006 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 char_u *op;
3008{
3009 long n;
3010 char_u numbuf[NUMBUFLEN];
3011 char_u *s;
3012
3013 /* Can't do anything with a Funcref or a Dict on the right. */
3014 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3015 {
3016 switch (tv1->v_type)
3017 {
3018 case VAR_DICT:
3019 case VAR_FUNC:
3020 break;
3021
3022 case VAR_LIST:
3023 if (*op != '+' || tv2->v_type != VAR_LIST)
3024 break;
3025 /* List += List */
3026 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3027 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3028 return OK;
3029
3030 case VAR_NUMBER:
3031 case VAR_STRING:
3032 if (tv2->v_type == VAR_LIST)
3033 break;
3034 if (*op == '+' || *op == '-')
3035 {
3036 /* nr += nr or nr -= nr*/
3037 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003038#ifdef FEAT_FLOAT
3039 if (tv2->v_type == VAR_FLOAT)
3040 {
3041 float_T f = n;
3042
3043 if (*op == '+')
3044 f += tv2->vval.v_float;
3045 else
3046 f -= tv2->vval.v_float;
3047 clear_tv(tv1);
3048 tv1->v_type = VAR_FLOAT;
3049 tv1->vval.v_float = f;
3050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003052#endif
3053 {
3054 if (*op == '+')
3055 n += get_tv_number(tv2);
3056 else
3057 n -= get_tv_number(tv2);
3058 clear_tv(tv1);
3059 tv1->v_type = VAR_NUMBER;
3060 tv1->vval.v_number = n;
3061 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 }
3063 else
3064 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065 if (tv2->v_type == VAR_FLOAT)
3066 break;
3067
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 /* str .= str */
3069 s = get_tv_string(tv1);
3070 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3071 clear_tv(tv1);
3072 tv1->v_type = VAR_STRING;
3073 tv1->vval.v_string = s;
3074 }
3075 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003076
3077#ifdef FEAT_FLOAT
3078 case VAR_FLOAT:
3079 {
3080 float_T f;
3081
3082 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3083 && tv2->v_type != VAR_NUMBER
3084 && tv2->v_type != VAR_STRING))
3085 break;
3086 if (tv2->v_type == VAR_FLOAT)
3087 f = tv2->vval.v_float;
3088 else
3089 f = get_tv_number(tv2);
3090 if (*op == '+')
3091 tv1->vval.v_float += f;
3092 else
3093 tv1->vval.v_float -= f;
3094 }
3095 return OK;
3096#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 }
3098 }
3099
3100 EMSG2(_(e_letwrong), op);
3101 return FAIL;
3102}
3103
3104/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105 * Add a watcher to a list.
3106 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003107 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 list_T *l;
3110 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111{
3112 lw->lw_next = l->lv_watch;
3113 l->lv_watch = lw;
3114}
3115
3116/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003117 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 * No warning when it isn't found...
3119 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 list_T *l;
3123 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124{
Bram Moolenaar33570922005-01-25 22:26:29 +00003125 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126
3127 lwp = &l->lv_watch;
3128 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3129 {
3130 if (lw == lwrem)
3131 {
3132 *lwp = lw->lw_next;
3133 break;
3134 }
3135 lwp = &lw->lw_next;
3136 }
3137}
3138
3139/*
3140 * Just before removing an item from a list: advance watchers to the next
3141 * item.
3142 */
3143 static void
3144list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003145 list_T *l;
3146 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147{
Bram Moolenaar33570922005-01-25 22:26:29 +00003148 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149
3150 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3151 if (lw->lw_item == item)
3152 lw->lw_item = item->li_next;
3153}
3154
3155/*
3156 * Evaluate the expression used in a ":for var in expr" command.
3157 * "arg" points to "var".
3158 * Set "*errp" to TRUE for an error, FALSE otherwise;
3159 * Return a pointer that holds the info. Null when there is an error.
3160 */
3161 void *
3162eval_for_line(arg, errp, nextcmdp, skip)
3163 char_u *arg;
3164 int *errp;
3165 char_u **nextcmdp;
3166 int skip;
3167{
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003169 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 typval_T tv;
3171 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172
3173 *errp = TRUE; /* default: there is an error */
3174
Bram Moolenaar33570922005-01-25 22:26:29 +00003175 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176 if (fi == NULL)
3177 return NULL;
3178
3179 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3180 if (expr == NULL)
3181 return fi;
3182
3183 expr = skipwhite(expr);
3184 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3185 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003186 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 return fi;
3188 }
3189
3190 if (skip)
3191 ++emsg_skip;
3192 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3193 {
3194 *errp = FALSE;
3195 if (!skip)
3196 {
3197 l = tv.vval.v_list;
3198 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 clear_tv(&tv);
3202 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203 else
3204 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003205 /* No need to increment the refcount, it's already set for the
3206 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 fi->fi_list = l;
3208 list_add_watch(l, &fi->fi_lw);
3209 fi->fi_lw.lw_item = l->lv_first;
3210 }
3211 }
3212 }
3213 if (skip)
3214 --emsg_skip;
3215
3216 return fi;
3217}
3218
3219/*
3220 * Use the first item in a ":for" list. Advance to the next.
3221 * Assign the values to the variable (list). "arg" points to the first one.
3222 * Return TRUE when a valid item was found, FALSE when at end of list or
3223 * something wrong.
3224 */
3225 int
3226next_for_item(fi_void, arg)
3227 void *fi_void;
3228 char_u *arg;
3229{
Bram Moolenaar33570922005-01-25 22:26:29 +00003230 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233
3234 item = fi->fi_lw.lw_item;
3235 if (item == NULL)
3236 result = FALSE;
3237 else
3238 {
3239 fi->fi_lw.lw_item = item->li_next;
3240 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3241 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3242 }
3243 return result;
3244}
3245
3246/*
3247 * Free the structure used to store info used by ":for".
3248 */
3249 void
3250free_for_info(fi_void)
3251 void *fi_void;
3252{
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003255 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 list_unref(fi->fi_list);
3259 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 vim_free(fi);
3261}
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3264
3265 void
3266set_context_for_expression(xp, arg, cmdidx)
3267 expand_T *xp;
3268 char_u *arg;
3269 cmdidx_T cmdidx;
3270{
3271 int got_eq = FALSE;
3272 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 if (cmdidx == CMD_let)
3276 {
3277 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003278 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 {
3280 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003281 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 {
3283 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 if (vim_iswhite(*p))
3286 break;
3287 }
3288 return;
3289 }
3290 }
3291 else
3292 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3293 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 while ((xp->xp_pattern = vim_strpbrk(arg,
3295 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3296 {
3297 c = *xp->xp_pattern;
3298 if (c == '&')
3299 {
3300 c = xp->xp_pattern[1];
3301 if (c == '&')
3302 {
3303 ++xp->xp_pattern;
3304 xp->xp_context = cmdidx != CMD_let || got_eq
3305 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3306 }
3307 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003310 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3311 xp->xp_pattern += 2;
3312
3313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 else if (c == '$')
3316 {
3317 /* environment variable */
3318 xp->xp_context = EXPAND_ENV_VARS;
3319 }
3320 else if (c == '=')
3321 {
3322 got_eq = TRUE;
3323 xp->xp_context = EXPAND_EXPRESSION;
3324 }
3325 else if (c == '<'
3326 && xp->xp_context == EXPAND_FUNCTIONS
3327 && vim_strchr(xp->xp_pattern, '(') == NULL)
3328 {
3329 /* Function name can start with "<SNR>" */
3330 break;
3331 }
3332 else if (cmdidx != CMD_let || got_eq)
3333 {
3334 if (c == '"') /* string */
3335 {
3336 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3337 if (c == '\\' && xp->xp_pattern[1] != NUL)
3338 ++xp->xp_pattern;
3339 xp->xp_context = EXPAND_NOTHING;
3340 }
3341 else if (c == '\'') /* literal string */
3342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003343 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3345 /* skip */ ;
3346 xp->xp_context = EXPAND_NOTHING;
3347 }
3348 else if (c == '|')
3349 {
3350 if (xp->xp_pattern[1] == '|')
3351 {
3352 ++xp->xp_pattern;
3353 xp->xp_context = EXPAND_EXPRESSION;
3354 }
3355 else
3356 xp->xp_context = EXPAND_COMMANDS;
3357 }
3358 else
3359 xp->xp_context = EXPAND_EXPRESSION;
3360 }
3361 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 /* Doesn't look like something valid, expand as an expression
3363 * anyway. */
3364 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 arg = xp->xp_pattern;
3366 if (*arg != NUL)
3367 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3368 /* skip */ ;
3369 }
3370 xp->xp_pattern = arg;
3371}
3372
3373#endif /* FEAT_CMDL_COMPL */
3374
3375/*
3376 * ":1,25call func(arg1, arg2)" function call.
3377 */
3378 void
3379ex_call(eap)
3380 exarg_T *eap;
3381{
3382 char_u *arg = eap->arg;
3383 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003385 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003387 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 linenr_T lnum;
3389 int doesrange;
3390 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003391 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003393 if (eap->skip)
3394 {
3395 /* trans_function_name() doesn't work well when skipping, use eval0()
3396 * instead to skip to any following command, e.g. for:
3397 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003398 ++emsg_skip;
3399 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3400 clear_tv(&rettv);
3401 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003402 return;
3403 }
3404
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003405 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003406 if (fudi.fd_newkey != NULL)
3407 {
3408 /* Still need to give an error message for missing key. */
3409 EMSG2(_(e_dictkey), fudi.fd_newkey);
3410 vim_free(fudi.fd_newkey);
3411 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003412 if (tofree == NULL)
3413 return;
3414
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003415 /* Increase refcount on dictionary, it could get deleted when evaluating
3416 * the arguments. */
3417 if (fudi.fd_dict != NULL)
3418 ++fudi.fd_dict->dv_refcount;
3419
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003420 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003421 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003422 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar532c7802005-01-27 14:44:31 +00003424 /* Skip white space to allow ":call func ()". Not good, but required for
3425 * backward compatibility. */
3426 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003427 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429 if (*startarg != '(')
3430 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003431 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 goto end;
3433 }
3434
3435 /*
3436 * When skipping, evaluate the function once, to find the end of the
3437 * arguments.
3438 * When the function takes a range, this is discovered after the first
3439 * call, and the loop is broken.
3440 */
3441 if (eap->skip)
3442 {
3443 ++emsg_skip;
3444 lnum = eap->line2; /* do it once, also with an invalid range */
3445 }
3446 else
3447 lnum = eap->line1;
3448 for ( ; lnum <= eap->line2; ++lnum)
3449 {
3450 if (!eap->skip && eap->addr_count > 0)
3451 {
3452 curwin->w_cursor.lnum = lnum;
3453 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003454#ifdef FEAT_VIRTUALEDIT
3455 curwin->w_cursor.coladd = 0;
3456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003459 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003460 eap->line1, eap->line2, &doesrange,
3461 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 failed = TRUE;
3464 break;
3465 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003466
3467 /* Handle a function returning a Funcref, Dictionary or List. */
3468 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3469 {
3470 failed = TRUE;
3471 break;
3472 }
3473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (doesrange || eap->skip)
3476 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003479 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (aborting())
3483 break;
3484 }
3485 if (eap->skip)
3486 --emsg_skip;
3487
3488 if (!failed)
3489 {
3490 /* Check for trailing illegal characters and a following command. */
3491 if (!ends_excmd(*arg))
3492 {
3493 emsg_severe = TRUE;
3494 EMSG(_(e_trailing));
3495 }
3496 else
3497 eap->nextcmd = check_nextcmd(arg);
3498 }
3499
3500end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003501 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003502 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503}
3504
3505/*
3506 * ":unlet[!] var1 ... " command.
3507 */
3508 void
3509ex_unlet(eap)
3510 exarg_T *eap;
3511{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003512 ex_unletlock(eap, eap->arg, 0);
3513}
3514
3515/*
3516 * ":lockvar" and ":unlockvar" commands
3517 */
3518 void
3519ex_lockvar(eap)
3520 exarg_T *eap;
3521{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003523 int deep = 2;
3524
3525 if (eap->forceit)
3526 deep = -1;
3527 else if (vim_isdigit(*arg))
3528 {
3529 deep = getdigits(&arg);
3530 arg = skipwhite(arg);
3531 }
3532
3533 ex_unletlock(eap, arg, deep);
3534}
3535
3536/*
3537 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3538 */
3539 static void
3540ex_unletlock(eap, argstart, deep)
3541 exarg_T *eap;
3542 char_u *argstart;
3543 int deep;
3544{
3545 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003548 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 do
3551 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003553 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3554 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003555 if (lv.ll_name == NULL)
3556 error = TRUE; /* error but continue parsing */
3557 if (name_end == NULL || (!vim_iswhite(*name_end)
3558 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003560 if (name_end != NULL)
3561 {
3562 emsg_severe = TRUE;
3563 EMSG(_(e_trailing));
3564 }
3565 if (!(eap->skip || error))
3566 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 break;
3568 }
3569
3570 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003571 {
3572 if (eap->cmdidx == CMD_unlet)
3573 {
3574 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3575 error = TRUE;
3576 }
3577 else
3578 {
3579 if (do_lock_var(&lv, name_end, deep,
3580 eap->cmdidx == CMD_lockvar) == FAIL)
3581 error = TRUE;
3582 }
3583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003585 if (!eap->skip)
3586 clear_lval(&lv);
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 arg = skipwhite(name_end);
3589 } while (!ends_excmd(*arg));
3590
3591 eap->nextcmd = check_nextcmd(arg);
3592}
3593
Bram Moolenaar8c711452005-01-14 21:53:12 +00003594 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003595do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003596 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 int forceit;
3599{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003600 int ret = OK;
3601 int cc;
3602
3603 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003605 cc = *name_end;
3606 *name_end = NUL;
3607
3608 /* Normal name or expanded name. */
3609 if (check_changedtick(lp->ll_name))
3610 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003612 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003614 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3616 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 else if (lp->ll_range)
3618 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003619 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003620
3621 /* Delete a range of List items. */
3622 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3623 {
3624 li = lp->ll_li->li_next;
3625 listitem_remove(lp->ll_list, lp->ll_li);
3626 lp->ll_li = li;
3627 ++lp->ll_n1;
3628 }
3629 }
3630 else
3631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 /* unlet a List item. */
3634 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003637 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 }
3639
3640 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003641}
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643/*
3644 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
3647 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
Bram Moolenaar33570922005-01-25 22:26:29 +00003652 hashtab_T *ht;
3653 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003654 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003655 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar33570922005-01-25 22:26:29 +00003657 ht = find_var_ht(name, &varname);
3658 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003660 hi = hash_find(ht, varname);
3661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003662 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003663 di = HI2DI(hi);
3664 if (var_check_fixed(di->di_flags, name)
3665 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003666 return FAIL;
3667 delete_var(ht, hi);
3668 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671 if (forceit)
3672 return OK;
3673 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 return FAIL;
3675}
3676
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677/*
3678 * Lock or unlock variable indicated by "lp".
3679 * "deep" is the levels to go (-1 for unlimited);
3680 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3681 */
3682 static int
3683do_lock_var(lp, name_end, deep, lock)
3684 lval_T *lp;
3685 char_u *name_end;
3686 int deep;
3687 int lock;
3688{
3689 int ret = OK;
3690 int cc;
3691 dictitem_T *di;
3692
3693 if (deep == 0) /* nothing to do */
3694 return OK;
3695
3696 if (lp->ll_tv == NULL)
3697 {
3698 cc = *name_end;
3699 *name_end = NUL;
3700
3701 /* Normal name or expanded name. */
3702 if (check_changedtick(lp->ll_name))
3703 ret = FAIL;
3704 else
3705 {
3706 di = find_var(lp->ll_name, NULL);
3707 if (di == NULL)
3708 ret = FAIL;
3709 else
3710 {
3711 if (lock)
3712 di->di_flags |= DI_FLAGS_LOCK;
3713 else
3714 di->di_flags &= ~DI_FLAGS_LOCK;
3715 item_lock(&di->di_tv, deep, lock);
3716 }
3717 }
3718 *name_end = cc;
3719 }
3720 else if (lp->ll_range)
3721 {
3722 listitem_T *li = lp->ll_li;
3723
3724 /* (un)lock a range of List items. */
3725 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3726 {
3727 item_lock(&li->li_tv, deep, lock);
3728 li = li->li_next;
3729 ++lp->ll_n1;
3730 }
3731 }
3732 else if (lp->ll_list != NULL)
3733 /* (un)lock a List item. */
3734 item_lock(&lp->ll_li->li_tv, deep, lock);
3735 else
3736 /* un(lock) a Dictionary item. */
3737 item_lock(&lp->ll_di->di_tv, deep, lock);
3738
3739 return ret;
3740}
3741
3742/*
3743 * Lock or unlock an item. "deep" is nr of levels to go.
3744 */
3745 static void
3746item_lock(tv, deep, lock)
3747 typval_T *tv;
3748 int deep;
3749 int lock;
3750{
3751 static int recurse = 0;
3752 list_T *l;
3753 listitem_T *li;
3754 dict_T *d;
3755 hashitem_T *hi;
3756 int todo;
3757
3758 if (recurse >= DICT_MAXNEST)
3759 {
3760 EMSG(_("E743: variable nested too deep for (un)lock"));
3761 return;
3762 }
3763 if (deep == 0)
3764 return;
3765 ++recurse;
3766
3767 /* lock/unlock the item itself */
3768 if (lock)
3769 tv->v_lock |= VAR_LOCKED;
3770 else
3771 tv->v_lock &= ~VAR_LOCKED;
3772
3773 switch (tv->v_type)
3774 {
3775 case VAR_LIST:
3776 if ((l = tv->vval.v_list) != NULL)
3777 {
3778 if (lock)
3779 l->lv_lock |= VAR_LOCKED;
3780 else
3781 l->lv_lock &= ~VAR_LOCKED;
3782 if (deep < 0 || deep > 1)
3783 /* recursive: lock/unlock the items the List contains */
3784 for (li = l->lv_first; li != NULL; li = li->li_next)
3785 item_lock(&li->li_tv, deep - 1, lock);
3786 }
3787 break;
3788 case VAR_DICT:
3789 if ((d = tv->vval.v_dict) != NULL)
3790 {
3791 if (lock)
3792 d->dv_lock |= VAR_LOCKED;
3793 else
3794 d->dv_lock &= ~VAR_LOCKED;
3795 if (deep < 0 || deep > 1)
3796 {
3797 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003798 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003799 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3800 {
3801 if (!HASHITEM_EMPTY(hi))
3802 {
3803 --todo;
3804 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3805 }
3806 }
3807 }
3808 }
3809 }
3810 --recurse;
3811}
3812
Bram Moolenaara40058a2005-07-11 22:42:07 +00003813/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003814 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3815 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003816 */
3817 static int
3818tv_islocked(tv)
3819 typval_T *tv;
3820{
3821 return (tv->v_lock & VAR_LOCKED)
3822 || (tv->v_type == VAR_LIST
3823 && tv->vval.v_list != NULL
3824 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3825 || (tv->v_type == VAR_DICT
3826 && tv->vval.v_dict != NULL
3827 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3828}
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3831/*
3832 * Delete all "menutrans_" variables.
3833 */
3834 void
3835del_menutrans_vars()
3836{
Bram Moolenaar33570922005-01-25 22:26:29 +00003837 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003838 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839
Bram Moolenaar33570922005-01-25 22:26:29 +00003840 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003841 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003843 {
3844 if (!HASHITEM_EMPTY(hi))
3845 {
3846 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003847 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3848 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003849 }
3850 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003851 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852}
3853#endif
3854
3855#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3856
3857/*
3858 * Local string buffer for the next two functions to store a variable name
3859 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3860 * get_user_var_name().
3861 */
3862
3863static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3864
3865static char_u *varnamebuf = NULL;
3866static int varnamebuflen = 0;
3867
3868/*
3869 * Function to concatenate a prefix and a variable name.
3870 */
3871 static char_u *
3872cat_prefix_varname(prefix, name)
3873 int prefix;
3874 char_u *name;
3875{
3876 int len;
3877
3878 len = (int)STRLEN(name) + 3;
3879 if (len > varnamebuflen)
3880 {
3881 vim_free(varnamebuf);
3882 len += 10; /* some additional space */
3883 varnamebuf = alloc(len);
3884 if (varnamebuf == NULL)
3885 {
3886 varnamebuflen = 0;
3887 return NULL;
3888 }
3889 varnamebuflen = len;
3890 }
3891 *varnamebuf = prefix;
3892 varnamebuf[1] = ':';
3893 STRCPY(varnamebuf + 2, name);
3894 return varnamebuf;
3895}
3896
3897/*
3898 * Function given to ExpandGeneric() to obtain the list of user defined
3899 * (global/buffer/window/built-in) variable names.
3900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 char_u *
3902get_user_var_name(xp, idx)
3903 expand_T *xp;
3904 int idx;
3905{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003906 static long_u gdone;
3907 static long_u bdone;
3908 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003909#ifdef FEAT_WINDOWS
3910 static long_u tdone;
3911#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003912 static int vidx;
3913 static hashitem_T *hi;
3914 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915
3916 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003918 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003919#ifdef FEAT_WINDOWS
3920 tdone = 0;
3921#endif
3922 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003923
3924 /* Global variables */
3925 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003927 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003928 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003929 else
3930 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 while (HASHITEM_EMPTY(hi))
3932 ++hi;
3933 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3934 return cat_prefix_varname('g', hi->hi_key);
3935 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003937
3938 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003939 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003940 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003944 else
3945 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 while (HASHITEM_EMPTY(hi))
3947 ++hi;
3948 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003952 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 return (char_u *)"b:changedtick";
3954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955
3956 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003957 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003960 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003962 else
3963 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003964 while (HASHITEM_EMPTY(hi))
3965 ++hi;
3966 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003969#ifdef FEAT_WINDOWS
3970 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003971 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003972 if (tdone < ht->ht_used)
3973 {
3974 if (tdone++ == 0)
3975 hi = ht->ht_array;
3976 else
3977 ++hi;
3978 while (HASHITEM_EMPTY(hi))
3979 ++hi;
3980 return cat_prefix_varname('t', hi->hi_key);
3981 }
3982#endif
3983
Bram Moolenaar33570922005-01-25 22:26:29 +00003984 /* v: variables */
3985 if (vidx < VV_LEN)
3986 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 vim_free(varnamebuf);
3989 varnamebuf = NULL;
3990 varnamebuflen = 0;
3991 return NULL;
3992}
3993
3994#endif /* FEAT_CMDL_COMPL */
3995
3996/*
3997 * types for expressions.
3998 */
3999typedef enum
4000{
4001 TYPE_UNKNOWN = 0
4002 , TYPE_EQUAL /* == */
4003 , TYPE_NEQUAL /* != */
4004 , TYPE_GREATER /* > */
4005 , TYPE_GEQUAL /* >= */
4006 , TYPE_SMALLER /* < */
4007 , TYPE_SEQUAL /* <= */
4008 , TYPE_MATCH /* =~ */
4009 , TYPE_NOMATCH /* !~ */
4010} exptype_T;
4011
4012/*
4013 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004014 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4016 */
4017
4018/*
4019 * Handle zero level expression.
4020 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004021 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004022 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 * Return OK or FAIL.
4024 */
4025 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004026eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u **nextcmd;
4030 int evaluate;
4031{
4032 int ret;
4033 char_u *p;
4034
4035 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004036 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 if (ret == FAIL || !ends_excmd(*p))
4038 {
4039 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 /*
4042 * Report the invalid expression unless the expression evaluation has
4043 * been cancelled due to an aborting error, an interrupt, or an
4044 * exception.
4045 */
4046 if (!aborting())
4047 EMSG2(_(e_invexpr2), arg);
4048 ret = FAIL;
4049 }
4050 if (nextcmd != NULL)
4051 *nextcmd = check_nextcmd(p);
4052
4053 return ret;
4054}
4055
4056/*
4057 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004058 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 *
4060 * "arg" must point to the first non-white of the expression.
4061 * "arg" is advanced to the next non-white after the recognized expression.
4062 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004063 * Note: "rettv.v_lock" is not set.
4064 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 * Return OK or FAIL.
4066 */
4067 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004068eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 int evaluate;
4072{
4073 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075
4076 /*
4077 * Get the first variable.
4078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004079 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 return FAIL;
4081
4082 if ((*arg)[0] == '?')
4083 {
4084 result = FALSE;
4085 if (evaluate)
4086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004087 int error = FALSE;
4088
4089 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004091 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004092 if (error)
4093 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095
4096 /*
4097 * Get the second variable.
4098 */
4099 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 return FAIL;
4102
4103 /*
4104 * Check for the ":".
4105 */
4106 if ((*arg)[0] != ':')
4107 {
4108 EMSG(_("E109: Missing ':' after '?'"));
4109 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004110 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 return FAIL;
4112 }
4113
4114 /*
4115 * Get the third variable.
4116 */
4117 *arg = skipwhite(*arg + 1);
4118 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4119 {
4120 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return FAIL;
4123 }
4124 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127
4128 return OK;
4129}
4130
4131/*
4132 * Handle first level expression:
4133 * expr2 || expr2 || expr2 logical OR
4134 *
4135 * "arg" must point to the first non-white of the expression.
4136 * "arg" is advanced to the next non-white after the recognized expression.
4137 *
4138 * Return OK or FAIL.
4139 */
4140 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int evaluate;
4145{
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 long result;
4148 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
4151 /*
4152 * Get the first variable.
4153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 return FAIL;
4156
4157 /*
4158 * Repeat until there is no following "||".
4159 */
4160 first = TRUE;
4161 result = FALSE;
4162 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4163 {
4164 if (evaluate && first)
4165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004168 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (error)
4170 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 first = FALSE;
4172 }
4173
4174 /*
4175 * Get the second variable.
4176 */
4177 *arg = skipwhite(*arg + 2);
4178 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4179 return FAIL;
4180
4181 /*
4182 * Compute the result.
4183 */
4184 if (evaluate && !result)
4185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192 if (evaluate)
4193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 rettv->v_type = VAR_NUMBER;
4195 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 }
4198
4199 return OK;
4200}
4201
4202/*
4203 * Handle second level expression:
4204 * expr3 && expr3 && expr3 logical AND
4205 *
4206 * "arg" must point to the first non-white of the expression.
4207 * "arg" is advanced to the next non-white after the recognized expression.
4208 *
4209 * Return OK or FAIL.
4210 */
4211 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 int evaluate;
4216{
Bram Moolenaar33570922005-01-25 22:26:29 +00004217 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 long result;
4219 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004220 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
4222 /*
4223 * Get the first variable.
4224 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return FAIL;
4227
4228 /*
4229 * Repeat until there is no following "&&".
4230 */
4231 first = TRUE;
4232 result = TRUE;
4233 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4234 {
4235 if (evaluate && first)
4236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004239 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 if (error)
4241 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 first = FALSE;
4243 }
4244
4245 /*
4246 * Get the second variable.
4247 */
4248 *arg = skipwhite(*arg + 2);
4249 if (eval4(arg, &var2, evaluate && result) == FAIL)
4250 return FAIL;
4251
4252 /*
4253 * Compute the result.
4254 */
4255 if (evaluate && result)
4256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (error)
4261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 if (evaluate)
4264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 rettv->v_type = VAR_NUMBER;
4266 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268 }
4269
4270 return OK;
4271}
4272
4273/*
4274 * Handle third level expression:
4275 * var1 == var2
4276 * var1 =~ var2
4277 * var1 != var2
4278 * var1 !~ var2
4279 * var1 > var2
4280 * var1 >= var2
4281 * var1 < var2
4282 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004283 * var1 is var2
4284 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 *
4286 * "arg" must point to the first non-white of the expression.
4287 * "arg" is advanced to the next non-white after the recognized expression.
4288 *
4289 * Return OK or FAIL.
4290 */
4291 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 int evaluate;
4296{
Bram Moolenaar33570922005-01-25 22:26:29 +00004297 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 char_u *p;
4299 int i;
4300 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004301 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 int len = 2;
4303 long n1, n2;
4304 char_u *s1, *s2;
4305 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4306 regmatch_T regmatch;
4307 int ic;
4308 char_u *save_cpo;
4309
4310 /*
4311 * Get the first variable.
4312 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315
4316 p = *arg;
4317 switch (p[0])
4318 {
4319 case '=': if (p[1] == '=')
4320 type = TYPE_EQUAL;
4321 else if (p[1] == '~')
4322 type = TYPE_MATCH;
4323 break;
4324 case '!': if (p[1] == '=')
4325 type = TYPE_NEQUAL;
4326 else if (p[1] == '~')
4327 type = TYPE_NOMATCH;
4328 break;
4329 case '>': if (p[1] != '=')
4330 {
4331 type = TYPE_GREATER;
4332 len = 1;
4333 }
4334 else
4335 type = TYPE_GEQUAL;
4336 break;
4337 case '<': if (p[1] != '=')
4338 {
4339 type = TYPE_SMALLER;
4340 len = 1;
4341 }
4342 else
4343 type = TYPE_SEQUAL;
4344 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004345 case 'i': if (p[1] == 's')
4346 {
4347 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4348 len = 5;
4349 if (!vim_isIDc(p[len]))
4350 {
4351 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4352 type_is = TRUE;
4353 }
4354 }
4355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357
4358 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004359 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 */
4361 if (type != TYPE_UNKNOWN)
4362 {
4363 /* extra question mark appended: ignore case */
4364 if (p[len] == '?')
4365 {
4366 ic = TRUE;
4367 ++len;
4368 }
4369 /* extra '#' appended: match case */
4370 else if (p[len] == '#')
4371 {
4372 ic = FALSE;
4373 ++len;
4374 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004375 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 else
4377 ic = p_ic;
4378
4379 /*
4380 * Get the second variable.
4381 */
4382 *arg = skipwhite(p + len);
4383 if (eval5(arg, &var2, evaluate) == FAIL)
4384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 return FAIL;
4387 }
4388
4389 if (evaluate)
4390 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004391 if (type_is && rettv->v_type != var2.v_type)
4392 {
4393 /* For "is" a different type always means FALSE, for "notis"
4394 * it means TRUE. */
4395 n1 = (type == TYPE_NEQUAL);
4396 }
4397 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4398 {
4399 if (type_is)
4400 {
4401 n1 = (rettv->v_type == var2.v_type
4402 && rettv->vval.v_list == var2.vval.v_list);
4403 if (type == TYPE_NEQUAL)
4404 n1 = !n1;
4405 }
4406 else if (rettv->v_type != var2.v_type
4407 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4408 {
4409 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004410 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004411 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004412 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 clear_tv(rettv);
4414 clear_tv(&var2);
4415 return FAIL;
4416 }
4417 else
4418 {
4419 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004420 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4421 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004422 if (type == TYPE_NEQUAL)
4423 n1 = !n1;
4424 }
4425 }
4426
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004427 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4428 {
4429 if (type_is)
4430 {
4431 n1 = (rettv->v_type == var2.v_type
4432 && rettv->vval.v_dict == var2.vval.v_dict);
4433 if (type == TYPE_NEQUAL)
4434 n1 = !n1;
4435 }
4436 else if (rettv->v_type != var2.v_type
4437 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4438 {
4439 if (rettv->v_type != var2.v_type)
4440 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4441 else
4442 EMSG(_("E736: Invalid operation for Dictionary"));
4443 clear_tv(rettv);
4444 clear_tv(&var2);
4445 return FAIL;
4446 }
4447 else
4448 {
4449 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004450 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4451 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004452 if (type == TYPE_NEQUAL)
4453 n1 = !n1;
4454 }
4455 }
4456
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004457 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4458 {
4459 if (rettv->v_type != var2.v_type
4460 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4461 {
4462 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004463 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004465 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 clear_tv(rettv);
4467 clear_tv(&var2);
4468 return FAIL;
4469 }
4470 else
4471 {
4472 /* Compare two Funcrefs for being equal or unequal. */
4473 if (rettv->vval.v_string == NULL
4474 || var2.vval.v_string == NULL)
4475 n1 = FALSE;
4476 else
4477 n1 = STRCMP(rettv->vval.v_string,
4478 var2.vval.v_string) == 0;
4479 if (type == TYPE_NEQUAL)
4480 n1 = !n1;
4481 }
4482 }
4483
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004484#ifdef FEAT_FLOAT
4485 /*
4486 * If one of the two variables is a float, compare as a float.
4487 * When using "=~" or "!~", always compare as string.
4488 */
4489 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4490 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4491 {
4492 float_T f1, f2;
4493
4494 if (rettv->v_type == VAR_FLOAT)
4495 f1 = rettv->vval.v_float;
4496 else
4497 f1 = get_tv_number(rettv);
4498 if (var2.v_type == VAR_FLOAT)
4499 f2 = var2.vval.v_float;
4500 else
4501 f2 = get_tv_number(&var2);
4502 n1 = FALSE;
4503 switch (type)
4504 {
4505 case TYPE_EQUAL: n1 = (f1 == f2); break;
4506 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4507 case TYPE_GREATER: n1 = (f1 > f2); break;
4508 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4509 case TYPE_SMALLER: n1 = (f1 < f2); break;
4510 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4511 case TYPE_UNKNOWN:
4512 case TYPE_MATCH:
4513 case TYPE_NOMATCH: break; /* avoid gcc warning */
4514 }
4515 }
4516#endif
4517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 /*
4519 * If one of the two variables is a number, compare as a number.
4520 * When using "=~" or "!~", always compare as string.
4521 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004522 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004525 n1 = get_tv_number(rettv);
4526 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 switch (type)
4528 {
4529 case TYPE_EQUAL: n1 = (n1 == n2); break;
4530 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4531 case TYPE_GREATER: n1 = (n1 > n2); break;
4532 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4533 case TYPE_SMALLER: n1 = (n1 < n2); break;
4534 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4535 case TYPE_UNKNOWN:
4536 case TYPE_MATCH:
4537 case TYPE_NOMATCH: break; /* avoid gcc warning */
4538 }
4539 }
4540 else
4541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004542 s1 = get_tv_string_buf(rettv, buf1);
4543 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4545 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4546 else
4547 i = 0;
4548 n1 = FALSE;
4549 switch (type)
4550 {
4551 case TYPE_EQUAL: n1 = (i == 0); break;
4552 case TYPE_NEQUAL: n1 = (i != 0); break;
4553 case TYPE_GREATER: n1 = (i > 0); break;
4554 case TYPE_GEQUAL: n1 = (i >= 0); break;
4555 case TYPE_SMALLER: n1 = (i < 0); break;
4556 case TYPE_SEQUAL: n1 = (i <= 0); break;
4557
4558 case TYPE_MATCH:
4559 case TYPE_NOMATCH:
4560 /* avoid 'l' flag in 'cpoptions' */
4561 save_cpo = p_cpo;
4562 p_cpo = (char_u *)"";
4563 regmatch.regprog = vim_regcomp(s2,
4564 RE_MAGIC + RE_STRING);
4565 regmatch.rm_ic = ic;
4566 if (regmatch.regprog != NULL)
4567 {
4568 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4569 vim_free(regmatch.regprog);
4570 if (type == TYPE_NOMATCH)
4571 n1 = !n1;
4572 }
4573 p_cpo = save_cpo;
4574 break;
4575
4576 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4577 }
4578 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 clear_tv(rettv);
4580 clear_tv(&var2);
4581 rettv->v_type = VAR_NUMBER;
4582 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584 }
4585
4586 return OK;
4587}
4588
4589/*
4590 * Handle fourth level expression:
4591 * + number addition
4592 * - number subtraction
4593 * . string concatenation
4594 *
4595 * "arg" must point to the first non-white of the expression.
4596 * "arg" is advanced to the next non-white after the recognized expression.
4597 *
4598 * Return OK or FAIL.
4599 */
4600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004601eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 int evaluate;
4605{
Bram Moolenaar33570922005-01-25 22:26:29 +00004606 typval_T var2;
4607 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 int op;
4609 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004610#ifdef FEAT_FLOAT
4611 float_T f1 = 0, f2 = 0;
4612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 char_u *s1, *s2;
4614 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4615 char_u *p;
4616
4617 /*
4618 * Get the first variable.
4619 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004620 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 return FAIL;
4622
4623 /*
4624 * Repeat computing, until no '+', '-' or '.' is following.
4625 */
4626 for (;;)
4627 {
4628 op = **arg;
4629 if (op != '+' && op != '-' && op != '.')
4630 break;
4631
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004632 if ((op != '+' || rettv->v_type != VAR_LIST)
4633#ifdef FEAT_FLOAT
4634 && (op == '.' || rettv->v_type != VAR_FLOAT)
4635#endif
4636 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004637 {
4638 /* For "list + ...", an illegal use of the first operand as
4639 * a number cannot be determined before evaluating the 2nd
4640 * operand: if this is also a list, all is ok.
4641 * For "something . ...", "something - ..." or "non-list + ...",
4642 * we know that the first operand needs to be a string or number
4643 * without evaluating the 2nd operand. So check before to avoid
4644 * side effects after an error. */
4645 if (evaluate && get_tv_string_chk(rettv) == NULL)
4646 {
4647 clear_tv(rettv);
4648 return FAIL;
4649 }
4650 }
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 /*
4653 * Get the second variable.
4654 */
4655 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004656 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 return FAIL;
4660 }
4661
4662 if (evaluate)
4663 {
4664 /*
4665 * Compute the result.
4666 */
4667 if (op == '.')
4668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4670 s2 = get_tv_string_buf_chk(&var2, buf2);
4671 if (s2 == NULL) /* type error ? */
4672 {
4673 clear_tv(rettv);
4674 clear_tv(&var2);
4675 return FAIL;
4676 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004677 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 clear_tv(rettv);
4679 rettv->v_type = VAR_STRING;
4680 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004682 else if (op == '+' && rettv->v_type == VAR_LIST
4683 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004684 {
4685 /* concatenate Lists */
4686 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4687 &var3) == FAIL)
4688 {
4689 clear_tv(rettv);
4690 clear_tv(&var2);
4691 return FAIL;
4692 }
4693 clear_tv(rettv);
4694 *rettv = var3;
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 else
4697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 int error = FALSE;
4699
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004700#ifdef FEAT_FLOAT
4701 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004703 f1 = rettv->vval.v_float;
4704 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004705 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004706 else
4707#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004708 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004709 n1 = get_tv_number_chk(rettv, &error);
4710 if (error)
4711 {
4712 /* This can only happen for "list + non-list". For
4713 * "non-list + ..." or "something - ...", we returned
4714 * before evaluating the 2nd operand. */
4715 clear_tv(rettv);
4716 return FAIL;
4717 }
4718#ifdef FEAT_FLOAT
4719 if (var2.v_type == VAR_FLOAT)
4720 f1 = n1;
4721#endif
4722 }
4723#ifdef FEAT_FLOAT
4724 if (var2.v_type == VAR_FLOAT)
4725 {
4726 f2 = var2.vval.v_float;
4727 n2 = 0;
4728 }
4729 else
4730#endif
4731 {
4732 n2 = get_tv_number_chk(&var2, &error);
4733 if (error)
4734 {
4735 clear_tv(rettv);
4736 clear_tv(&var2);
4737 return FAIL;
4738 }
4739#ifdef FEAT_FLOAT
4740 if (rettv->v_type == VAR_FLOAT)
4741 f2 = n2;
4742#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745
4746#ifdef FEAT_FLOAT
4747 /* If there is a float on either side the result is a float. */
4748 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4749 {
4750 if (op == '+')
4751 f1 = f1 + f2;
4752 else
4753 f1 = f1 - f2;
4754 rettv->v_type = VAR_FLOAT;
4755 rettv->vval.v_float = f1;
4756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758#endif
4759 {
4760 if (op == '+')
4761 n1 = n1 + n2;
4762 else
4763 n1 = n1 - n2;
4764 rettv->v_type = VAR_NUMBER;
4765 rettv->vval.v_number = n1;
4766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 }
4771 return OK;
4772}
4773
4774/*
4775 * Handle fifth level expression:
4776 * * number multiplication
4777 * / number division
4778 * % number modulo
4779 *
4780 * "arg" must point to the first non-white of the expression.
4781 * "arg" is advanced to the next non-white after the recognized expression.
4782 *
4783 * Return OK or FAIL.
4784 */
4785 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004786eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
Bram Moolenaar33570922005-01-25 22:26:29 +00004792 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 int op;
4794 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795#ifdef FEAT_FLOAT
4796 int use_float = FALSE;
4797 float_T f1 = 0, f2;
4798#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 /*
4802 * Get the first variable.
4803 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004804 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return FAIL;
4806
4807 /*
4808 * Repeat computing, until no '*', '/' or '%' is following.
4809 */
4810 for (;;)
4811 {
4812 op = **arg;
4813 if (op != '*' && op != '/' && op != '%')
4814 break;
4815
4816 if (evaluate)
4817 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004818#ifdef FEAT_FLOAT
4819 if (rettv->v_type == VAR_FLOAT)
4820 {
4821 f1 = rettv->vval.v_float;
4822 use_float = TRUE;
4823 n1 = 0;
4824 }
4825 else
4826#endif
4827 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 if (error)
4830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 }
4832 else
4833 n1 = 0;
4834
4835 /*
4836 * Get the second variable.
4837 */
4838 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004839 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return FAIL;
4841
4842 if (evaluate)
4843 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#ifdef FEAT_FLOAT
4845 if (var2.v_type == VAR_FLOAT)
4846 {
4847 if (!use_float)
4848 {
4849 f1 = n1;
4850 use_float = TRUE;
4851 }
4852 f2 = var2.vval.v_float;
4853 n2 = 0;
4854 }
4855 else
4856#endif
4857 {
4858 n2 = get_tv_number_chk(&var2, &error);
4859 clear_tv(&var2);
4860 if (error)
4861 return FAIL;
4862#ifdef FEAT_FLOAT
4863 if (use_float)
4864 f2 = n2;
4865#endif
4866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868 /*
4869 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872#ifdef FEAT_FLOAT
4873 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875 if (op == '*')
4876 f1 = f1 * f2;
4877 else if (op == '/')
4878 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004879# ifdef VMS
4880 /* VMS crashes on divide by zero, work around it */
4881 if (f2 == 0.0)
4882 {
4883 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004884 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004885 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004888 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004889 }
4890 else
4891 f1 = f1 / f2;
4892# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893 /* We rely on the floating point library to handle divide
4894 * by zero to result in "inf" and not a crash. */
4895 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004896# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004900 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 return FAIL;
4902 }
4903 rettv->v_type = VAR_FLOAT;
4904 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909 if (op == '*')
4910 n1 = n1 * n2;
4911 else if (op == '/')
4912 {
4913 if (n2 == 0) /* give an error message? */
4914 {
4915 if (n1 == 0)
4916 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4917 else if (n1 < 0)
4918 n1 = -0x7fffffffL;
4919 else
4920 n1 = 0x7fffffffL;
4921 }
4922 else
4923 n1 = n1 / n2;
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926 {
4927 if (n2 == 0) /* give an error message? */
4928 n1 = 0;
4929 else
4930 n1 = n1 % n2;
4931 }
4932 rettv->v_type = VAR_NUMBER;
4933 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936 }
4937
4938 return OK;
4939}
4940
4941/*
4942 * Handle sixth level expression:
4943 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004944 * "string" string constant
4945 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 * &option-name option value
4947 * @r register contents
4948 * identifier variable value
4949 * function() function call
4950 * $VAR environment variable
4951 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004952 * [expr, expr] List
4953 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 *
4955 * Also handle:
4956 * ! in front logical NOT
4957 * - in front unary minus
4958 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 * trailing [] subscript in String or List
4960 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 *
4962 * "arg" must point to the first non-white of the expression.
4963 * "arg" is advanced to the next non-white after the recognized expression.
4964 *
4965 * Return OK or FAIL.
4966 */
4967 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004968eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004972 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 long n;
4975 int len;
4976 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 char_u *start_leader, *end_leader;
4978 int ret = OK;
4979 char_u *alias;
4980
4981 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004982 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004985 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986
4987 /*
4988 * Skip '!' and '-' characters. They are handled later.
4989 */
4990 start_leader = *arg;
4991 while (**arg == '!' || **arg == '-' || **arg == '+')
4992 *arg = skipwhite(*arg + 1);
4993 end_leader = *arg;
4994
4995 switch (**arg)
4996 {
4997 /*
4998 * Number constant.
4999 */
5000 case '0':
5001 case '1':
5002 case '2':
5003 case '3':
5004 case '4':
5005 case '5':
5006 case '6':
5007 case '7':
5008 case '8':
5009 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 {
5011#ifdef FEAT_FLOAT
5012 char_u *p = skipdigits(*arg + 1);
5013 int get_float = FALSE;
5014
5015 /* We accept a float when the format matches
5016 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005017 * strict to avoid backwards compatibility problems.
5018 * Don't look for a float after the "." operator, so that
5019 * ":let vers = 1.2.3" doesn't fail. */
5020 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 get_float = TRUE;
5023 p = skipdigits(p + 2);
5024 if (*p == 'e' || *p == 'E')
5025 {
5026 ++p;
5027 if (*p == '-' || *p == '+')
5028 ++p;
5029 if (!vim_isdigit(*p))
5030 get_float = FALSE;
5031 else
5032 p = skipdigits(p + 1);
5033 }
5034 if (ASCII_ISALPHA(*p) || *p == '.')
5035 get_float = FALSE;
5036 }
5037 if (get_float)
5038 {
5039 float_T f;
5040
5041 *arg += string2float(*arg, &f);
5042 if (evaluate)
5043 {
5044 rettv->v_type = VAR_FLOAT;
5045 rettv->vval.v_float = f;
5046 }
5047 }
5048 else
5049#endif
5050 {
5051 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5052 *arg += len;
5053 if (evaluate)
5054 {
5055 rettv->v_type = VAR_NUMBER;
5056 rettv->vval.v_number = n;
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
5062 /*
5063 * String constant: "string".
5064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 break;
5067
5068 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005072 break;
5073
5074 /*
5075 * List: [expr, expr]
5076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079
5080 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * Dictionary: {key: val, key: val}
5082 */
5083 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5084 break;
5085
5086 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005087 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005089 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /*
5093 * Environment variable: $VAR.
5094 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097
5098 /*
5099 * Register contents: @r.
5100 */
5101 case '@': ++*arg;
5102 if (evaluate)
5103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005105 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107 if (**arg != NUL)
5108 ++*arg;
5109 break;
5110
5111 /*
5112 * nested expression: (expression).
5113 */
5114 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (**arg == ')')
5117 ++*arg;
5118 else if (ret == OK)
5119 {
5120 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 ret = FAIL;
5123 }
5124 break;
5125
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
5128 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129
5130 if (ret == NOTDONE)
5131 {
5132 /*
5133 * Must be a variable or function name.
5134 * Can also be a curly-braces kind of name: {expr}.
5135 */
5136 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005137 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 if (alias != NULL)
5139 s = alias;
5140
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005141 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 ret = FAIL;
5143 else
5144 {
5145 if (**arg == '(') /* recursive! */
5146 {
5147 /* If "s" is the name of a variable of type VAR_FUNC
5148 * use its contents. */
5149 s = deref_func_name(s, &len);
5150
5151 /* Invoke the function. */
5152 ret = get_func_tv(s, len, rettv, arg,
5153 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005154 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005155
5156 /* If evaluate is FALSE rettv->v_type was not set in
5157 * get_func_tv, but it's needed in handle_subscript() to parse
5158 * what follows. So set it here. */
5159 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5160 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005161 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005162 rettv->v_type = VAR_FUNC;
5163 }
5164
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 /* Stop the expression evaluation when immediately
5166 * aborting on error, or when an interrupt occurred or
5167 * an exception was thrown but not caught. */
5168 if (aborting())
5169 {
5170 if (ret == OK)
5171 clear_tv(rettv);
5172 ret = FAIL;
5173 }
5174 }
5175 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005176 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005177 else
5178 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005180 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 }
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 *arg = skipwhite(*arg);
5184
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005185 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5186 * expr(expr). */
5187 if (ret == OK)
5188 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 /*
5191 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5192 */
5193 if (ret == OK && evaluate && end_leader > start_leader)
5194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005195 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 int val = 0;
5197#ifdef FEAT_FLOAT
5198 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005199
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005200 if (rettv->v_type == VAR_FLOAT)
5201 f = rettv->vval.v_float;
5202 else
5203#endif
5204 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 clear_tv(rettv);
5208 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 else
5211 {
5212 while (end_leader > start_leader)
5213 {
5214 --end_leader;
5215 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005216 {
5217#ifdef FEAT_FLOAT
5218 if (rettv->v_type == VAR_FLOAT)
5219 f = !f;
5220 else
5221#endif
5222 val = !val;
5223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005225 {
5226#ifdef FEAT_FLOAT
5227 if (rettv->v_type == VAR_FLOAT)
5228 f = -f;
5229 else
5230#endif
5231 val = -val;
5232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005234#ifdef FEAT_FLOAT
5235 if (rettv->v_type == VAR_FLOAT)
5236 {
5237 clear_tv(rettv);
5238 rettv->vval.v_float = f;
5239 }
5240 else
5241#endif
5242 {
5243 clear_tv(rettv);
5244 rettv->v_type = VAR_NUMBER;
5245 rettv->vval.v_number = val;
5246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249
5250 return ret;
5251}
5252
5253/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005254 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5255 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5257 */
5258 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264{
5265 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 long len = -1;
5269 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005270 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005271 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005273 if (rettv->v_type == VAR_FUNC
5274#ifdef FEAT_FLOAT
5275 || rettv->v_type == VAR_FLOAT
5276#endif
5277 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279 if (verbose)
5280 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 return FAIL;
5282 }
5283
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /*
5287 * dict.name
5288 */
5289 key = *arg + 1;
5290 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5291 ;
5292 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 }
5296 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 /*
5299 * something[idx]
5300 *
5301 * Get the (first) variable from inside the [].
5302 */
5303 *arg = skipwhite(*arg + 1);
5304 if (**arg == ':')
5305 empty1 = TRUE;
5306 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5307 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5309 {
5310 /* not a number or string */
5311 clear_tv(&var1);
5312 return FAIL;
5313 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005314
5315 /*
5316 * Get the second variable from inside the [:].
5317 */
5318 if (**arg == ':')
5319 {
5320 range = TRUE;
5321 *arg = skipwhite(*arg + 1);
5322 if (**arg == ']')
5323 empty2 = TRUE;
5324 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 if (!empty1)
5327 clear_tv(&var1);
5328 return FAIL;
5329 }
5330 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5331 {
5332 /* not a number or string */
5333 if (!empty1)
5334 clear_tv(&var1);
5335 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336 return FAIL;
5337 }
5338 }
5339
5340 /* Check for the ']'. */
5341 if (**arg != ']')
5342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005343 if (verbose)
5344 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005345 clear_tv(&var1);
5346 if (range)
5347 clear_tv(&var2);
5348 return FAIL;
5349 }
5350 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 }
5352
5353 if (evaluate)
5354 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 n1 = 0;
5356 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005358 n1 = get_tv_number(&var1);
5359 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 }
5361 if (range)
5362 {
5363 if (empty2)
5364 n2 = -1;
5365 else
5366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 n2 = get_tv_number(&var2);
5368 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 }
5370 }
5371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 {
5374 case VAR_NUMBER:
5375 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005376 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 len = (long)STRLEN(s);
5378 if (range)
5379 {
5380 /* The resulting variable is a substring. If the indexes
5381 * are out of range the result is empty. */
5382 if (n1 < 0)
5383 {
5384 n1 = len + n1;
5385 if (n1 < 0)
5386 n1 = 0;
5387 }
5388 if (n2 < 0)
5389 n2 = len + n2;
5390 else if (n2 >= len)
5391 n2 = len;
5392 if (n1 >= len || n2 < 0 || n1 > n2)
5393 s = NULL;
5394 else
5395 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5396 }
5397 else
5398 {
5399 /* The resulting variable is a string of a single
5400 * character. If the index is too big or negative the
5401 * result is empty. */
5402 if (n1 >= len || n1 < 0)
5403 s = NULL;
5404 else
5405 s = vim_strnsave(s + n1, 1);
5406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 clear_tv(rettv);
5408 rettv->v_type = VAR_STRING;
5409 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 break;
5411
5412 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 if (n1 < 0)
5415 n1 = len + n1;
5416 if (!empty1 && (n1 < 0 || n1 >= len))
5417 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005418 /* For a range we allow invalid values and return an empty
5419 * list. A list index out of range is an error. */
5420 if (!range)
5421 {
5422 if (verbose)
5423 EMSGN(_(e_listidx), n1);
5424 return FAIL;
5425 }
5426 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 }
5428 if (range)
5429 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005430 list_T *l;
5431 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432
5433 if (n2 < 0)
5434 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005435 else if (n2 >= len)
5436 n2 = len - 1;
5437 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005438 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 l = list_alloc();
5440 if (l == NULL)
5441 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005442 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443 n1 <= n2; ++n1)
5444 {
5445 if (list_append_tv(l, &item->li_tv) == FAIL)
5446 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005447 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 return FAIL;
5449 }
5450 item = item->li_next;
5451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005452 clear_tv(rettv);
5453 rettv->v_type = VAR_LIST;
5454 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005455 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 else
5458 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005459 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 clear_tv(rettv);
5461 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464
5465 case VAR_DICT:
5466 if (range)
5467 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005468 if (verbose)
5469 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 if (len == -1)
5471 clear_tv(&var1);
5472 return FAIL;
5473 }
5474 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005475 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476
5477 if (len == -1)
5478 {
5479 key = get_tv_string(&var1);
5480 if (*key == NUL)
5481 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005482 if (verbose)
5483 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484 clear_tv(&var1);
5485 return FAIL;
5486 }
5487 }
5488
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005489 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005491 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005492 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 if (len == -1)
5494 clear_tv(&var1);
5495 if (item == NULL)
5496 return FAIL;
5497
5498 copy_tv(&item->di_tv, &var1);
5499 clear_tv(rettv);
5500 *rettv = var1;
5501 }
5502 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 }
5505
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 return OK;
5507}
5508
5509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 * Get an option value.
5511 * "arg" points to the '&' or '+' before the option name.
5512 * "arg" is advanced to character after the option name.
5513 * Return OK or FAIL.
5514 */
5515 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 int evaluate;
5520{
5521 char_u *option_end;
5522 long numval;
5523 char_u *stringval;
5524 int opt_type;
5525 int c;
5526 int working = (**arg == '+'); /* has("+option") */
5527 int ret = OK;
5528 int opt_flags;
5529
5530 /*
5531 * Isolate the option name and find its value.
5532 */
5533 option_end = find_option_end(arg, &opt_flags);
5534 if (option_end == NULL)
5535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005536 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 EMSG2(_("E112: Option name missing: %s"), *arg);
5538 return FAIL;
5539 }
5540
5541 if (!evaluate)
5542 {
5543 *arg = option_end;
5544 return OK;
5545 }
5546
5547 c = *option_end;
5548 *option_end = NUL;
5549 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 if (opt_type == -3) /* invalid name */
5553 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 EMSG2(_("E113: Unknown option: %s"), *arg);
5556 ret = FAIL;
5557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
5560 if (opt_type == -2) /* hidden string option */
5561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 rettv->v_type = VAR_STRING;
5563 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565 else if (opt_type == -1) /* hidden number option */
5566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_NUMBER;
5568 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else if (opt_type == 1) /* number option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_NUMBER;
5573 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else /* string option */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->v_type = VAR_STRING;
5578 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 }
5581 else if (working && (opt_type == -2 || opt_type == -1))
5582 ret = FAIL;
5583
5584 *option_end = c; /* put back for error messages */
5585 *arg = option_end;
5586
5587 return ret;
5588}
5589
5590/*
5591 * Allocate a variable for a string constant.
5592 * Return OK or FAIL.
5593 */
5594 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005595get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 int evaluate;
5599{
5600 char_u *p;
5601 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 int extra = 0;
5603
5604 /*
5605 * Find the end of the string, skipping backslashed characters.
5606 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005607 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 if (*p == '\\' && p[1] != NUL)
5610 {
5611 ++p;
5612 /* A "\<x>" form occupies at least 4 characters, and produces up
5613 * to 6 characters: reserve space for 2 extra */
5614 if (*p == '<')
5615 extra += 2;
5616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618
5619 if (*p != '"')
5620 {
5621 EMSG2(_("E114: Missing quote: %s"), *arg);
5622 return FAIL;
5623 }
5624
5625 /* If only parsing, set *arg and return here */
5626 if (!evaluate)
5627 {
5628 *arg = p + 1;
5629 return OK;
5630 }
5631
5632 /*
5633 * Copy the string into allocated memory, handling backslashed
5634 * characters.
5635 */
5636 name = alloc((unsigned)(p - *arg + extra));
5637 if (name == NULL)
5638 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
5644 if (*p == '\\')
5645 {
5646 switch (*++p)
5647 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 case 'b': *name++ = BS; ++p; break;
5649 case 'e': *name++ = ESC; ++p; break;
5650 case 'f': *name++ = FF; ++p; break;
5651 case 'n': *name++ = NL; ++p; break;
5652 case 'r': *name++ = CAR; ++p; break;
5653 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 case 'X': /* hex: "\x1", "\x12" */
5656 case 'x':
5657 case 'u': /* Unicode: "\u0023" */
5658 case 'U':
5659 if (vim_isxdigit(p[1]))
5660 {
5661 int n, nr;
5662 int c = toupper(*p);
5663
5664 if (c == 'X')
5665 n = 2;
5666 else
5667 n = 4;
5668 nr = 0;
5669 while (--n >= 0 && vim_isxdigit(p[1]))
5670 {
5671 ++p;
5672 nr = (nr << 4) + hex2nr(*p);
5673 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#ifdef FEAT_MBYTE
5676 /* For "\u" store the number according to
5677 * 'encoding'. */
5678 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
5681#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 break;
5685
5686 /* octal: "\1", "\12", "\123" */
5687 case '0':
5688 case '1':
5689 case '2':
5690 case '3':
5691 case '4':
5692 case '5':
5693 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 case '7': *name = *p++ - '0';
5695 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 *name = (*name << 3) + *p++ - '0';
5698 if (*p >= '0' && *p <= '7')
5699 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 break;
5703
5704 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (extra != 0)
5707 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 break;
5710 }
5711 /* FALLTHROUGH */
5712
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715 }
5716 }
5717 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 *arg = p + 1;
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 return OK;
5725}
5726
5727/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005728 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 * Return OK or FAIL.
5730 */
5731 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 int evaluate;
5736{
5737 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005738 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 int reduce = 0;
5740
5741 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 */
5744 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005746 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 break;
5750 ++reduce;
5751 ++p;
5752 }
5753 }
5754
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 return FAIL;
5759 }
5760
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 if (!evaluate)
5763 {
5764 *arg = p + 1;
5765 return OK;
5766 }
5767
5768 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 */
5771 str = alloc((unsigned)((p - *arg) - reduce));
5772 if (str == NULL)
5773 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 rettv->v_type = VAR_STRING;
5775 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 break;
5783 ++p;
5784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 *arg = p + 1;
5789
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 return OK;
5791}
5792
5793/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005794 * Allocate a variable for a List and fill it from "*arg".
5795 * Return OK or FAIL.
5796 */
5797 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005798get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005800 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 int evaluate;
5802{
Bram Moolenaar33570922005-01-25 22:26:29 +00005803 list_T *l = NULL;
5804 typval_T tv;
5805 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806
5807 if (evaluate)
5808 {
5809 l = list_alloc();
5810 if (l == NULL)
5811 return FAIL;
5812 }
5813
5814 *arg = skipwhite(*arg + 1);
5815 while (**arg != ']' && **arg != NUL)
5816 {
5817 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5818 goto failret;
5819 if (evaluate)
5820 {
5821 item = listitem_alloc();
5822 if (item != NULL)
5823 {
5824 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005825 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 list_append(l, item);
5827 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005828 else
5829 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 }
5831
5832 if (**arg == ']')
5833 break;
5834 if (**arg != ',')
5835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 goto failret;
5838 }
5839 *arg = skipwhite(*arg + 1);
5840 }
5841
5842 if (**arg != ']')
5843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845failret:
5846 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005847 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848 return FAIL;
5849 }
5850
5851 *arg = skipwhite(*arg + 1);
5852 if (evaluate)
5853 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005854 rettv->v_type = VAR_LIST;
5855 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 ++l->lv_refcount;
5857 }
5858
5859 return OK;
5860}
5861
5862/*
5863 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005864 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005866 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867list_alloc()
5868{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005869 list_T *l;
5870
5871 l = (list_T *)alloc_clear(sizeof(list_T));
5872 if (l != NULL)
5873 {
5874 /* Prepend the list to the list of lists for garbage collection. */
5875 if (first_list != NULL)
5876 first_list->lv_used_prev = l;
5877 l->lv_used_prev = NULL;
5878 l->lv_used_next = first_list;
5879 first_list = l;
5880 }
5881 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882}
5883
5884/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005885 * Allocate an empty list for a return value.
5886 * Returns OK or FAIL.
5887 */
5888 static int
5889rettv_list_alloc(rettv)
5890 typval_T *rettv;
5891{
5892 list_T *l = list_alloc();
5893
5894 if (l == NULL)
5895 return FAIL;
5896
5897 rettv->vval.v_list = l;
5898 rettv->v_type = VAR_LIST;
5899 ++l->lv_refcount;
5900 return OK;
5901}
5902
5903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 * Unreference a list: decrement the reference count and free it when it
5905 * becomes zero.
5906 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005907 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005911 if (l != NULL && --l->lv_refcount <= 0)
5912 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913}
5914
5915/*
5916 * Free a list, including all items it points to.
5917 * Ignores the reference count.
5918 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005919 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005920list_free(l, recurse)
5921 list_T *l;
5922 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005926 /* Remove the list from the list of lists for garbage collection. */
5927 if (l->lv_used_prev == NULL)
5928 first_list = l->lv_used_next;
5929 else
5930 l->lv_used_prev->lv_used_next = l->lv_used_next;
5931 if (l->lv_used_next != NULL)
5932 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5933
Bram Moolenaard9fba312005-06-26 22:34:35 +00005934 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005936 /* Remove the item before deleting it. */
5937 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005938 if (recurse || (item->li_tv.v_type != VAR_LIST
5939 && item->li_tv.v_type != VAR_DICT))
5940 clear_tv(&item->li_tv);
5941 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942 }
5943 vim_free(l);
5944}
5945
5946/*
5947 * Allocate a list item.
5948 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005949 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950listitem_alloc()
5951{
Bram Moolenaar33570922005-01-25 22:26:29 +00005952 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953}
5954
5955/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005956 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 */
5958 static void
5959listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 vim_free(item);
5964}
5965
5966/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005967 * Remove a list item from a List and free it. Also clears the value.
5968 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005969 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005970listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l;
5972 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005973{
5974 list_remove(l, item, item);
5975 listitem_free(item);
5976}
5977
5978/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 * Get the number of items in a list.
5980 */
5981 static long
5982list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005983 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 if (l == NULL)
5986 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005987 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988}
5989
5990/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005991 * Return TRUE when two lists have exactly the same values.
5992 */
5993 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005994list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 list_T *l1;
5996 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005998 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999{
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006001
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006002 if (l1 == NULL || l2 == NULL)
6003 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006004 if (l1 == l2)
6005 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006006 if (list_len(l1) != list_len(l2))
6007 return FALSE;
6008
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009 for (item1 = l1->lv_first, item2 = l2->lv_first;
6010 item1 != NULL && item2 != NULL;
6011 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006012 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006013 return FALSE;
6014 return item1 == NULL && item2 == NULL;
6015}
6016
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006017#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6018 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006019/*
6020 * Return the dictitem that an entry in a hashtable points to.
6021 */
6022 dictitem_T *
6023dict_lookup(hi)
6024 hashitem_T *hi;
6025{
6026 return HI2DI(hi);
6027}
6028#endif
6029
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006031 * Return TRUE when two dictionaries have exactly the same key/values.
6032 */
6033 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006034dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 dict_T *d1;
6036 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006038 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039{
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 hashitem_T *hi;
6041 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006042 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006044 if (d1 == NULL || d2 == NULL)
6045 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006046 if (d1 == d2)
6047 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 if (dict_len(d1) != dict_len(d2))
6049 return FALSE;
6050
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006051 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 if (!HASHITEM_EMPTY(hi))
6055 {
6056 item2 = dict_find(d2, hi->hi_key, -1);
6057 if (item2 == NULL)
6058 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006059 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 return FALSE;
6061 --todo;
6062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006063 }
6064 return TRUE;
6065}
6066
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067static int tv_equal_recurse_limit;
6068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006069/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006070 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006071 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006072 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006073 */
6074 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006075tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006076 typval_T *tv1;
6077 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006078 int ic; /* ignore case */
6079 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006080{
6081 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006082 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006084 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006086 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006087 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006089 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006090 * recursiveness to a limit. We guess they are equal then.
6091 * A fixed limit has the problem of still taking an awful long time.
6092 * Reduce the limit every time running into it. That should work fine for
6093 * deeply linked structures that are not recursively linked and catch
6094 * recursiveness quickly. */
6095 if (!recursive)
6096 tv_equal_recurse_limit = 1000;
6097 if (recursive_cnt >= tv_equal_recurse_limit)
6098 {
6099 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006100 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006102
6103 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006105 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 ++recursive_cnt;
6107 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6108 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006109 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110
6111 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 ++recursive_cnt;
6113 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6114 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006115 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006116
6117 case VAR_FUNC:
6118 return (tv1->vval.v_string != NULL
6119 && tv2->vval.v_string != NULL
6120 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6121
6122 case VAR_NUMBER:
6123 return tv1->vval.v_number == tv2->vval.v_number;
6124
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006125#ifdef FEAT_FLOAT
6126 case VAR_FLOAT:
6127 return tv1->vval.v_float == tv2->vval.v_float;
6128#endif
6129
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006130 case VAR_STRING:
6131 s1 = get_tv_string_buf(tv1, buf1);
6132 s2 = get_tv_string_buf(tv2, buf2);
6133 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006134 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135
6136 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006137 return TRUE;
6138}
6139
6140/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141 * Locate item with index "n" in list "l" and return it.
6142 * A negative index is counted from the end; -1 is the last item.
6143 * Returns NULL when "n" is out of range.
6144 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006145 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148 long n;
6149{
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 long idx;
6152
6153 if (l == NULL)
6154 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006155
6156 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006158 n = l->lv_len + n;
6159
6160 /* Check for index out of range. */
6161 if (n < 0 || n >= l->lv_len)
6162 return NULL;
6163
6164 /* When there is a cached index may start search from there. */
6165 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006167 if (n < l->lv_idx / 2)
6168 {
6169 /* closest to the start of the list */
6170 item = l->lv_first;
6171 idx = 0;
6172 }
6173 else if (n > (l->lv_idx + l->lv_len) / 2)
6174 {
6175 /* closest to the end of the list */
6176 item = l->lv_last;
6177 idx = l->lv_len - 1;
6178 }
6179 else
6180 {
6181 /* closest to the cached index */
6182 item = l->lv_idx_item;
6183 idx = l->lv_idx;
6184 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185 }
6186 else
6187 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006188 if (n < l->lv_len / 2)
6189 {
6190 /* closest to the start of the list */
6191 item = l->lv_first;
6192 idx = 0;
6193 }
6194 else
6195 {
6196 /* closest to the end of the list */
6197 item = l->lv_last;
6198 idx = l->lv_len - 1;
6199 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006201
6202 while (n > idx)
6203 {
6204 /* search forward */
6205 item = item->li_next;
6206 ++idx;
6207 }
6208 while (n < idx)
6209 {
6210 /* search backward */
6211 item = item->li_prev;
6212 --idx;
6213 }
6214
6215 /* cache the used index */
6216 l->lv_idx = idx;
6217 l->lv_idx_item = item;
6218
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 return item;
6220}
6221
6222/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006223 * Get list item "l[idx]" as a number.
6224 */
6225 static long
6226list_find_nr(l, idx, errorp)
6227 list_T *l;
6228 long idx;
6229 int *errorp; /* set to TRUE when something wrong */
6230{
6231 listitem_T *li;
6232
6233 li = list_find(l, idx);
6234 if (li == NULL)
6235 {
6236 if (errorp != NULL)
6237 *errorp = TRUE;
6238 return -1L;
6239 }
6240 return get_tv_number_chk(&li->li_tv, errorp);
6241}
6242
6243/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006244 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6245 */
6246 char_u *
6247list_find_str(l, idx)
6248 list_T *l;
6249 long idx;
6250{
6251 listitem_T *li;
6252
6253 li = list_find(l, idx - 1);
6254 if (li == NULL)
6255 {
6256 EMSGN(_(e_listidx), idx);
6257 return NULL;
6258 }
6259 return get_tv_string(&li->li_tv);
6260}
6261
6262/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006263 * Locate "item" list "l" and return its index.
6264 * Returns -1 when "item" is not in the list.
6265 */
6266 static long
6267list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 list_T *l;
6269 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006270{
6271 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006273
6274 if (l == NULL)
6275 return -1;
6276 idx = 0;
6277 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6278 ++idx;
6279 if (li == NULL)
6280 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006281 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006282}
6283
6284/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 * Append item "item" to the end of list "l".
6286 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006287 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 list_T *l;
6290 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291{
6292 if (l->lv_last == NULL)
6293 {
6294 /* empty list */
6295 l->lv_first = item;
6296 l->lv_last = item;
6297 item->li_prev = NULL;
6298 }
6299 else
6300 {
6301 l->lv_last->li_next = item;
6302 item->li_prev = l->lv_last;
6303 l->lv_last = item;
6304 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006305 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 item->li_next = NULL;
6307}
6308
6309/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006311 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006313 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 list_T *l;
6316 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006318 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319
Bram Moolenaar05159a02005-02-26 23:04:13 +00006320 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006322 copy_tv(tv, &li->li_tv);
6323 list_append(l, li);
6324 return OK;
6325}
6326
6327/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006328 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006329 * Return FAIL when out of memory.
6330 */
6331 int
6332list_append_dict(list, dict)
6333 list_T *list;
6334 dict_T *dict;
6335{
6336 listitem_T *li = listitem_alloc();
6337
6338 if (li == NULL)
6339 return FAIL;
6340 li->li_tv.v_type = VAR_DICT;
6341 li->li_tv.v_lock = 0;
6342 li->li_tv.vval.v_dict = dict;
6343 list_append(list, li);
6344 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 return OK;
6346}
6347
6348/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006349 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006350 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006351 * Returns FAIL when out of memory.
6352 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006353 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006354list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006355 list_T *l;
6356 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006357 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006358{
6359 listitem_T *li = listitem_alloc();
6360
6361 if (li == NULL)
6362 return FAIL;
6363 list_append(l, li);
6364 li->li_tv.v_type = VAR_STRING;
6365 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006366 if (str == NULL)
6367 li->li_tv.vval.v_string = NULL;
6368 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006369 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006370 return FAIL;
6371 return OK;
6372}
6373
6374/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006375 * Append "n" to list "l".
6376 * Returns FAIL when out of memory.
6377 */
6378 static int
6379list_append_number(l, n)
6380 list_T *l;
6381 varnumber_T n;
6382{
6383 listitem_T *li;
6384
6385 li = listitem_alloc();
6386 if (li == NULL)
6387 return FAIL;
6388 li->li_tv.v_type = VAR_NUMBER;
6389 li->li_tv.v_lock = 0;
6390 li->li_tv.vval.v_number = n;
6391 list_append(l, li);
6392 return OK;
6393}
6394
6395/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397 * If "item" is NULL append at the end.
6398 * Return FAIL when out of memory.
6399 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006400 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 list_T *l;
6403 typval_T *tv;
6404 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405{
Bram Moolenaar33570922005-01-25 22:26:29 +00006406 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407
6408 if (ni == NULL)
6409 return FAIL;
6410 copy_tv(tv, &ni->li_tv);
6411 if (item == NULL)
6412 /* Append new item at end of list. */
6413 list_append(l, ni);
6414 else
6415 {
6416 /* Insert new item before existing item. */
6417 ni->li_prev = item->li_prev;
6418 ni->li_next = item;
6419 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006420 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006422 ++l->lv_idx;
6423 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 l->lv_idx_item = NULL;
6428 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 }
6432 return OK;
6433}
6434
6435/*
6436 * Extend "l1" with "l2".
6437 * If "bef" is NULL append at the end, otherwise insert before this item.
6438 * Returns FAIL when out of memory.
6439 */
6440 static int
6441list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006442 list_T *l1;
6443 list_T *l2;
6444 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006445{
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006447 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006449 /* We also quit the loop when we have inserted the original item count of
6450 * the list, avoid a hang when we extend a list with itself. */
6451 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6453 return FAIL;
6454 return OK;
6455}
6456
6457/*
6458 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6459 * Return FAIL when out of memory.
6460 */
6461 static int
6462list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006463 list_T *l1;
6464 list_T *l2;
6465 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466{
Bram Moolenaar33570922005-01-25 22:26:29 +00006467 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006469 if (l1 == NULL || l2 == NULL)
6470 return FAIL;
6471
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006473 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474 if (l == NULL)
6475 return FAIL;
6476 tv->v_type = VAR_LIST;
6477 tv->vval.v_list = l;
6478
6479 /* append all items from the second list */
6480 return list_extend(l, l2, NULL);
6481}
6482
6483/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006484 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006486 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 * Returns NULL when out of memory.
6488 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006490list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006493 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494{
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 list_T *copy;
6496 listitem_T *item;
6497 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498
6499 if (orig == NULL)
6500 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501
6502 copy = list_alloc();
6503 if (copy != NULL)
6504 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006505 if (copyID != 0)
6506 {
6507 /* Do this before adding the items, because one of the items may
6508 * refer back to this list. */
6509 orig->lv_copyID = copyID;
6510 orig->lv_copylist = copy;
6511 }
6512 for (item = orig->lv_first; item != NULL && !got_int;
6513 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 {
6515 ni = listitem_alloc();
6516 if (ni == NULL)
6517 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 {
6520 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6521 {
6522 vim_free(ni);
6523 break;
6524 }
6525 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006527 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006528 list_append(copy, ni);
6529 }
6530 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (item != NULL)
6532 {
6533 list_unref(copy);
6534 copy = NULL;
6535 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 }
6537
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 return copy;
6539}
6540
6541/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006543 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006545 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006546list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006547 list_T *l;
6548 listitem_T *item;
6549 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550{
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553 /* notify watchers */
6554 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006555 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006556 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 list_fix_watch(l, ip);
6558 if (ip == item2)
6559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561
6562 if (item2->li_next == NULL)
6563 l->lv_last = item->li_prev;
6564 else
6565 item2->li_next->li_prev = item->li_prev;
6566 if (item->li_prev == NULL)
6567 l->lv_first = item2->li_next;
6568 else
6569 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006570 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571}
6572
6573/*
6574 * Return an allocated string with the string representation of a list.
6575 * May return NULL.
6576 */
6577 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006578list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581{
6582 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583
6584 if (tv->vval.v_list == NULL)
6585 return NULL;
6586 ga_init2(&ga, (int)sizeof(char), 80);
6587 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006588 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006589 {
6590 vim_free(ga.ga_data);
6591 return NULL;
6592 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 ga_append(&ga, ']');
6594 ga_append(&ga, NUL);
6595 return (char_u *)ga.ga_data;
6596}
6597
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006598typedef struct join_S {
6599 char_u *s;
6600 char_u *tofree;
6601} join_T;
6602
6603 static int
6604list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6605 garray_T *gap; /* to store the result in */
6606 list_T *l;
6607 char_u *sep;
6608 int echo_style;
6609 int copyID;
6610 garray_T *join_gap; /* to keep each list item string */
6611{
6612 int i;
6613 join_T *p;
6614 int len;
6615 int sumlen = 0;
6616 int first = TRUE;
6617 char_u *tofree;
6618 char_u numbuf[NUMBUFLEN];
6619 listitem_T *item;
6620 char_u *s;
6621
6622 /* Stringify each item in the list. */
6623 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6624 {
6625 if (echo_style)
6626 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6627 else
6628 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6629 if (s == NULL)
6630 return FAIL;
6631
6632 len = (int)STRLEN(s);
6633 sumlen += len;
6634
6635 ga_grow(join_gap, 1);
6636 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6637 if (tofree != NULL || s != numbuf)
6638 {
6639 p->s = s;
6640 p->tofree = tofree;
6641 }
6642 else
6643 {
6644 p->s = vim_strnsave(s, len);
6645 p->tofree = p->s;
6646 }
6647
6648 line_breakcheck();
6649 }
6650
6651 /* Allocate result buffer with its total size, avoid re-allocation and
6652 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6653 if (join_gap->ga_len >= 2)
6654 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6655 if (ga_grow(gap, sumlen + 2) == FAIL)
6656 return FAIL;
6657
6658 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6659 {
6660 if (first)
6661 first = FALSE;
6662 else
6663 ga_concat(gap, sep);
6664 p = ((join_T *)join_gap->ga_data) + i;
6665
6666 if (p->s != NULL)
6667 ga_concat(gap, p->s);
6668 line_breakcheck();
6669 }
6670
6671 return OK;
6672}
6673
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006675 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006676 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006678 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006680list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006684 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006685 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687 garray_T join_ga;
6688 int retval;
6689 join_T *p;
6690 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6693 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6694
6695 /* Dispose each item in join_ga. */
6696 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006697 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006698 p = (join_T *)join_ga.ga_data;
6699 for (i = 0; i < join_ga.ga_len; ++i)
6700 {
6701 vim_free(p->tofree);
6702 ++p;
6703 }
6704 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006705 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006706
6707 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006708}
6709
6710/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006711 * Garbage collection for lists and dictionaries.
6712 *
6713 * We use reference counts to be able to free most items right away when they
6714 * are no longer used. But for composite items it's possible that it becomes
6715 * unused while the reference count is > 0: When there is a recursive
6716 * reference. Example:
6717 * :let l = [1, 2, 3]
6718 * :let d = {9: l}
6719 * :let l[1] = d
6720 *
6721 * Since this is quite unusual we handle this with garbage collection: every
6722 * once in a while find out which lists and dicts are not referenced from any
6723 * variable.
6724 *
6725 * Here is a good reference text about garbage collection (refers to Python
6726 * but it applies to all reference-counting mechanisms):
6727 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006728 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006729
6730/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006731 * Do garbage collection for lists and dicts.
6732 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006734 int
6735garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006736{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006737 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006738 buf_T *buf;
6739 win_T *wp;
6740 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006741 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742 int did_free;
6743 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006744#ifdef FEAT_WINDOWS
6745 tabpage_T *tp;
6746#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006747
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006748 /* Only do this once. */
6749 want_garbage_collect = FALSE;
6750 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006751 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006752
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006753 /* We advance by two because we add one for items referenced through
6754 * previous_funccal. */
6755 current_copyID += COPYID_INC;
6756 copyID = current_copyID;
6757
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006758 /*
6759 * 1. Go through all accessible variables and mark all lists and dicts
6760 * with copyID.
6761 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006762
6763 /* Don't free variables in the previous_funccal list unless they are only
6764 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006765 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006766 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6767 {
6768 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6769 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6770 }
6771
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 /* script-local variables */
6773 for (i = 1; i <= ga_scripts.ga_len; ++i)
6774 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6775
6776 /* buffer-local variables */
6777 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006778 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006779
6780 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006782 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006783#ifdef FEAT_AUTOCMD
6784 if (aucmd_win != NULL)
6785 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6786#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006788#ifdef FEAT_WINDOWS
6789 /* tabpage-local variables */
6790 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006791 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006792#endif
6793
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006794 /* global variables */
6795 set_ref_in_ht(&globvarht, copyID);
6796
6797 /* function-local variables */
6798 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6799 {
6800 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6801 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6802 }
6803
Bram Moolenaard812df62008-11-09 12:46:09 +00006804 /* v: vars */
6805 set_ref_in_ht(&vimvarht, copyID);
6806
Bram Moolenaar1dced572012-04-05 16:54:08 +02006807#ifdef FEAT_LUA
6808 set_ref_in_lua(copyID);
6809#endif
6810
Bram Moolenaardb913952012-06-29 12:54:53 +02006811#ifdef FEAT_PYTHON
6812 set_ref_in_python(copyID);
6813#endif
6814
6815#ifdef FEAT_PYTHON3
6816 set_ref_in_python3(copyID);
6817#endif
6818
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006819 /*
6820 * 2. Free lists and dictionaries that are not referenced.
6821 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006822 did_free = free_unref_items(copyID);
6823
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006824 /*
6825 * 3. Check if any funccal can be freed now.
6826 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006827 for (pfc = &previous_funccal; *pfc != NULL; )
6828 {
6829 if (can_free_funccal(*pfc, copyID))
6830 {
6831 fc = *pfc;
6832 *pfc = fc->caller;
6833 free_funccal(fc, TRUE);
6834 did_free = TRUE;
6835 did_free_funccal = TRUE;
6836 }
6837 else
6838 pfc = &(*pfc)->caller;
6839 }
6840 if (did_free_funccal)
6841 /* When a funccal was freed some more items might be garbage
6842 * collected, so run again. */
6843 (void)garbage_collect();
6844
6845 return did_free;
6846}
6847
6848/*
6849 * Free lists and dictionaries that are no longer referenced.
6850 */
6851 static int
6852free_unref_items(copyID)
6853 int copyID;
6854{
6855 dict_T *dd;
6856 list_T *ll;
6857 int did_free = FALSE;
6858
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 */
6862 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006865 /* Free the Dictionary and ordinary items it contains, but don't
6866 * recurse into Lists and Dictionaries, they will be in the list
6867 * of dicts or list of lists. */
6868 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 did_free = TRUE;
6870
6871 /* restart, next dict may also have been freed */
6872 dd = first_dict;
6873 }
6874 else
6875 dd = dd->dv_used_next;
6876
6877 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878 * Go through the list of lists and free items without the copyID.
6879 * But don't free a list that has a watcher (used in a for loop), these
6880 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881 */
6882 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6884 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006886 /* Free the List and ordinary items it contains, but don't recurse
6887 * into Lists and Dictionaries, they will be in the list of dicts
6888 * or list of lists. */
6889 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 did_free = TRUE;
6891
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006892 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 ll = first_list;
6894 }
6895 else
6896 ll = ll->lv_used_next;
6897
6898 return did_free;
6899}
6900
6901/*
6902 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6903 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006904 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905set_ref_in_ht(ht, copyID)
6906 hashtab_T *ht;
6907 int copyID;
6908{
6909 int todo;
6910 hashitem_T *hi;
6911
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006912 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 for (hi = ht->ht_array; todo > 0; ++hi)
6914 if (!HASHITEM_EMPTY(hi))
6915 {
6916 --todo;
6917 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6918 }
6919}
6920
6921/*
6922 * Mark all lists and dicts referenced through list "l" with "copyID".
6923 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006924 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925set_ref_in_list(l, copyID)
6926 list_T *l;
6927 int copyID;
6928{
6929 listitem_T *li;
6930
6931 for (li = l->lv_first; li != NULL; li = li->li_next)
6932 set_ref_in_item(&li->li_tv, copyID);
6933}
6934
6935/*
6936 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6937 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006938 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939set_ref_in_item(tv, copyID)
6940 typval_T *tv;
6941 int copyID;
6942{
6943 dict_T *dd;
6944 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945
6946 switch (tv->v_type)
6947 {
6948 case VAR_DICT:
6949 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006950 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006952 /* Didn't see this dict yet. */
6953 dd->dv_copyID = copyID;
6954 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957
6958 case VAR_LIST:
6959 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006960 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 /* Didn't see this list yet. */
6963 ll->lv_copyID = copyID;
6964 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006969}
6970
6971/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972 * Allocate an empty header for a dictionary.
6973 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006974 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975dict_alloc()
6976{
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006978
Bram Moolenaar33570922005-01-25 22:26:29 +00006979 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006980 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006981 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006982 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 if (first_dict != NULL)
6984 first_dict->dv_used_prev = d;
6985 d->dv_used_next = first_dict;
6986 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006987 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988
Bram Moolenaar33570922005-01-25 22:26:29 +00006989 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006990 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006991 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006992 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006993 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006994 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006995 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006996}
6997
6998/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006999 * Allocate an empty dict for a return value.
7000 * Returns OK or FAIL.
7001 */
7002 static int
7003rettv_dict_alloc(rettv)
7004 typval_T *rettv;
7005{
7006 dict_T *d = dict_alloc();
7007
7008 if (d == NULL)
7009 return FAIL;
7010
7011 rettv->vval.v_dict = d;
7012 rettv->v_type = VAR_DICT;
7013 ++d->dv_refcount;
7014 return OK;
7015}
7016
7017
7018/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019 * Unreference a Dictionary: decrement the reference count and free it when it
7020 * becomes zero.
7021 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007022 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007023dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007024 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007026 if (d != NULL && --d->dv_refcount <= 0)
7027 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028}
7029
7030/*
7031 * Free a Dictionary, including all items it contains.
7032 * Ignores the reference count.
7033 */
7034 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007035dict_free(d, recurse)
7036 dict_T *d;
7037 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007039 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007040 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 /* Remove the dict from the list of dicts for garbage collection. */
7044 if (d->dv_used_prev == NULL)
7045 first_dict = d->dv_used_next;
7046 else
7047 d->dv_used_prev->dv_used_next = d->dv_used_next;
7048 if (d->dv_used_next != NULL)
7049 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7050
7051 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007053 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007054 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 if (!HASHITEM_EMPTY(hi))
7057 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007058 /* Remove the item before deleting it, just in case there is
7059 * something recursive causing trouble. */
7060 di = HI2DI(hi);
7061 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007062 if (recurse || (di->di_tv.v_type != VAR_LIST
7063 && di->di_tv.v_type != VAR_DICT))
7064 clear_tv(&di->di_tv);
7065 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007066 --todo;
7067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007069 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007070 vim_free(d);
7071}
7072
7073/*
7074 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007075 * The "key" is copied to the new item.
7076 * Note that the value of the item "di_tv" still needs to be initialized!
7077 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007079 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080dictitem_alloc(key)
7081 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007082{
Bram Moolenaar33570922005-01-25 22:26:29 +00007083 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007085 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007087 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007089 di->di_flags = 0;
7090 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007092}
7093
7094/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007095 * Make a copy of a Dictionary item.
7096 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007098dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100{
Bram Moolenaar33570922005-01-25 22:26:29 +00007101 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007102
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007103 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7104 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105 if (di != NULL)
7106 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007107 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007108 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007109 copy_tv(&org->di_tv, &di->di_tv);
7110 }
7111 return di;
7112}
7113
7114/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 * Remove item "item" from Dictionary "dict" and free it.
7116 */
7117 static void
7118dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007119 dict_T *dict;
7120 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121{
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007125 if (HASHITEM_EMPTY(hi))
7126 EMSG2(_(e_intern2), "dictitem_remove()");
7127 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129 dictitem_free(item);
7130}
7131
7132/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 * Free a dict item. Also clears the value.
7134 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007135 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007136dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 clear_tv(&item->di_tv);
7140 vim_free(item);
7141}
7142
7143/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007144 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7145 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007146 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007147 * Returns NULL when out of memory.
7148 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007150dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007151 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007153 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007154{
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 dict_T *copy;
7156 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007157 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159
7160 if (orig == NULL)
7161 return NULL;
7162
7163 copy = dict_alloc();
7164 if (copy != NULL)
7165 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 if (copyID != 0)
7167 {
7168 orig->dv_copyID = copyID;
7169 orig->dv_copydict = copy;
7170 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007171 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007174 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007176 --todo;
7177
7178 di = dictitem_alloc(hi->hi_key);
7179 if (di == NULL)
7180 break;
7181 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007182 {
7183 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7184 copyID) == FAIL)
7185 {
7186 vim_free(di);
7187 break;
7188 }
7189 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 else
7191 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7192 if (dict_add(copy, di) == FAIL)
7193 {
7194 dictitem_free(di);
7195 break;
7196 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007201 if (todo > 0)
7202 {
7203 dict_unref(copy);
7204 copy = NULL;
7205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 }
7207
7208 return copy;
7209}
7210
7211/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007213 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007215 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007216dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 dict_T *d;
7218 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219{
Bram Moolenaar33570922005-01-25 22:26:29 +00007220 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221}
7222
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007224 * Add a number or string entry to dictionary "d".
7225 * When "str" is NULL use number "nr", otherwise use "str".
7226 * Returns FAIL when out of memory and when key already exists.
7227 */
7228 int
7229dict_add_nr_str(d, key, nr, str)
7230 dict_T *d;
7231 char *key;
7232 long nr;
7233 char_u *str;
7234{
7235 dictitem_T *item;
7236
7237 item = dictitem_alloc((char_u *)key);
7238 if (item == NULL)
7239 return FAIL;
7240 item->di_tv.v_lock = 0;
7241 if (str == NULL)
7242 {
7243 item->di_tv.v_type = VAR_NUMBER;
7244 item->di_tv.vval.v_number = nr;
7245 }
7246 else
7247 {
7248 item->di_tv.v_type = VAR_STRING;
7249 item->di_tv.vval.v_string = vim_strsave(str);
7250 }
7251 if (dict_add(d, item) == FAIL)
7252 {
7253 dictitem_free(item);
7254 return FAIL;
7255 }
7256 return OK;
7257}
7258
7259/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007260 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007261 * Returns FAIL when out of memory and when key already exists.
7262 */
7263 int
7264dict_add_list(d, key, list)
7265 dict_T *d;
7266 char *key;
7267 list_T *list;
7268{
7269 dictitem_T *item;
7270
7271 item = dictitem_alloc((char_u *)key);
7272 if (item == NULL)
7273 return FAIL;
7274 item->di_tv.v_lock = 0;
7275 item->di_tv.v_type = VAR_LIST;
7276 item->di_tv.vval.v_list = list;
7277 if (dict_add(d, item) == FAIL)
7278 {
7279 dictitem_free(item);
7280 return FAIL;
7281 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007282 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007283 return OK;
7284}
7285
7286/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 * Get the number of items in a Dictionary.
7288 */
7289 static long
7290dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 if (d == NULL)
7294 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007295 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296}
7297
7298/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 * Find item "key[len]" in Dictionary "d".
7300 * If "len" is negative use strlen(key).
7301 * Returns NULL when not found.
7302 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007303 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007305 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 char_u *key;
7307 int len;
7308{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309#define AKEYLEN 200
7310 char_u buf[AKEYLEN];
7311 char_u *akey;
7312 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 if (len < 0)
7316 akey = key;
7317 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 tofree = akey = vim_strnsave(key, len);
7320 if (akey == NULL)
7321 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007322 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 else
7324 {
7325 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007326 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 akey = buf;
7328 }
7329
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 vim_free(tofree);
7332 if (HASHITEM_EMPTY(hi))
7333 return NULL;
7334 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335}
7336
7337/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007338 * Get a string item from a dictionary.
7339 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007340 * Returns NULL if the entry doesn't exist or out of memory.
7341 */
7342 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007344 dict_T *d;
7345 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007347{
7348 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007349 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007350
7351 di = dict_find(d, key, -1);
7352 if (di == NULL)
7353 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 s = get_tv_string(&di->di_tv);
7355 if (save && s != NULL)
7356 s = vim_strsave(s);
7357 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007358}
7359
7360/*
7361 * Get a number item from a dictionary.
7362 * Returns 0 if the entry doesn't exist or out of memory.
7363 */
7364 long
7365get_dict_number(d, key)
7366 dict_T *d;
7367 char_u *key;
7368{
7369 dictitem_T *di;
7370
7371 di = dict_find(d, key, -1);
7372 if (di == NULL)
7373 return 0;
7374 return get_tv_number(&di->di_tv);
7375}
7376
7377/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378 * Return an allocated string with the string representation of a Dictionary.
7379 * May return NULL.
7380 */
7381 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007382dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007384 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385{
7386 garray_T ga;
7387 int first = TRUE;
7388 char_u *tofree;
7389 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 return NULL;
7397 ga_init2(&ga, (int)sizeof(char), 80);
7398 ga_append(&ga, '{');
7399
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007400 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007405 --todo;
7406
7407 if (first)
7408 first = FALSE;
7409 else
7410 ga_concat(&ga, (char_u *)", ");
7411
7412 tofree = string_quote(hi->hi_key, FALSE);
7413 if (tofree != NULL)
7414 {
7415 ga_concat(&ga, tofree);
7416 vim_free(tofree);
7417 }
7418 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007419 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007420 if (s != NULL)
7421 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007423 if (s == NULL)
7424 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 if (todo > 0)
7428 {
7429 vim_free(ga.ga_data);
7430 return NULL;
7431 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432
7433 ga_append(&ga, '}');
7434 ga_append(&ga, NUL);
7435 return (char_u *)ga.ga_data;
7436}
7437
7438/*
7439 * Allocate a variable for a Dictionary and fill it from "*arg".
7440 * Return OK or FAIL. Returns NOTDONE for {expr}.
7441 */
7442 static int
7443get_dict_tv(arg, rettv, evaluate)
7444 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007445 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446 int evaluate;
7447{
Bram Moolenaar33570922005-01-25 22:26:29 +00007448 dict_T *d = NULL;
7449 typval_T tvkey;
7450 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007451 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007452 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007454 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007455
7456 /*
7457 * First check if it's not a curly-braces thing: {expr}.
7458 * Must do this without evaluating, otherwise a function may be called
7459 * twice. Unfortunately this means we need to call eval1() twice for the
7460 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007461 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007462 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463 if (*start != '}')
7464 {
7465 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7466 return FAIL;
7467 if (*start == '}')
7468 return NOTDONE;
7469 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
7471 if (evaluate)
7472 {
7473 d = dict_alloc();
7474 if (d == NULL)
7475 return FAIL;
7476 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477 tvkey.v_type = VAR_UNKNOWN;
7478 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479
7480 *arg = skipwhite(*arg + 1);
7481 while (**arg != '}' && **arg != NUL)
7482 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 goto failret;
7485 if (**arg != ':')
7486 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007487 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 goto failret;
7490 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007491 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007493 key = get_tv_string_buf_chk(&tvkey, buf);
7494 if (key == NULL || *key == NUL)
7495 {
7496 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7497 if (key != NULL)
7498 EMSG(_(e_emptykey));
7499 clear_tv(&tvkey);
7500 goto failret;
7501 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503
7504 *arg = skipwhite(*arg + 1);
7505 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7506 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007507 if (evaluate)
7508 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 goto failret;
7510 }
7511 if (evaluate)
7512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 if (item != NULL)
7515 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007516 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 clear_tv(&tv);
7519 goto failret;
7520 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 item = dictitem_alloc(key);
7522 clear_tv(&tvkey);
7523 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007525 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007526 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 if (dict_add(d, item) == FAIL)
7528 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 }
7530 }
7531
7532 if (**arg == '}')
7533 break;
7534 if (**arg != ',')
7535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007536 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 goto failret;
7538 }
7539 *arg = skipwhite(*arg + 1);
7540 }
7541
7542 if (**arg != '}')
7543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007544 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545failret:
7546 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007547 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548 return FAIL;
7549 }
7550
7551 *arg = skipwhite(*arg + 1);
7552 if (evaluate)
7553 {
7554 rettv->v_type = VAR_DICT;
7555 rettv->vval.v_dict = d;
7556 ++d->dv_refcount;
7557 }
7558
7559 return OK;
7560}
7561
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 * Return a string with the string representation of a variable.
7564 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007565 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007566 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007567 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007568 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 */
7570 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007571echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007573 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007574 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007576{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007577 static int recurse = 0;
7578 char_u *r = NULL;
7579
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007581 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007582 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 *tofree = NULL;
7584 return NULL;
7585 }
7586 ++recurse;
7587
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007588 switch (tv->v_type)
7589 {
7590 case VAR_FUNC:
7591 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592 r = tv->vval.v_string;
7593 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007594
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007596 if (tv->vval.v_list == NULL)
7597 {
7598 *tofree = NULL;
7599 r = NULL;
7600 }
7601 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7602 {
7603 *tofree = NULL;
7604 r = (char_u *)"[...]";
7605 }
7606 else
7607 {
7608 tv->vval.v_list->lv_copyID = copyID;
7609 *tofree = list2string(tv, copyID);
7610 r = *tofree;
7611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007612 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007613
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007615 if (tv->vval.v_dict == NULL)
7616 {
7617 *tofree = NULL;
7618 r = NULL;
7619 }
7620 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7621 {
7622 *tofree = NULL;
7623 r = (char_u *)"{...}";
7624 }
7625 else
7626 {
7627 tv->vval.v_dict->dv_copyID = copyID;
7628 *tofree = dict2string(tv, copyID);
7629 r = *tofree;
7630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007633 case VAR_STRING:
7634 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 *tofree = NULL;
7636 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007637 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007638
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007639#ifdef FEAT_FLOAT
7640 case VAR_FLOAT:
7641 *tofree = NULL;
7642 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7643 r = numbuf;
7644 break;
7645#endif
7646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651
7652 --recurse;
7653 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007654}
7655
7656/*
7657 * Return a string with the string representation of a variable.
7658 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7659 * "numbuf" is used for a number.
7660 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007661 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662 */
7663 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666 char_u **tofree;
7667 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007668 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007669{
7670 switch (tv->v_type)
7671 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672 case VAR_FUNC:
7673 *tofree = string_quote(tv->vval.v_string, TRUE);
7674 return *tofree;
7675 case VAR_STRING:
7676 *tofree = string_quote(tv->vval.v_string, FALSE);
7677 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007678#ifdef FEAT_FLOAT
7679 case VAR_FLOAT:
7680 *tofree = NULL;
7681 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7682 return numbuf;
7683#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007684 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007685 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007689 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007691 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692}
7693
7694/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 * Return string "str" in ' quotes, doubling ' characters.
7696 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 */
7699 static char_u *
7700string_quote(str, function)
7701 char_u *str;
7702 int function;
7703{
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 char_u *p, *r, *s;
7706
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 len = (function ? 13 : 3);
7708 if (str != NULL)
7709 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007710 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 for (p = str; *p != NUL; mb_ptr_adv(p))
7712 if (*p == '\'')
7713 ++len;
7714 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 s = r = alloc(len);
7716 if (r != NULL)
7717 {
7718 if (function)
7719 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 r += 10;
7722 }
7723 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 if (str != NULL)
7726 for (p = str; *p != NUL; )
7727 {
7728 if (*p == '\'')
7729 *r++ = '\'';
7730 MB_COPY_CHAR(p, r);
7731 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007733 if (function)
7734 *r++ = ')';
7735 *r++ = NUL;
7736 }
7737 return s;
7738}
7739
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007740#ifdef FEAT_FLOAT
7741/*
7742 * Convert the string "text" to a floating point number.
7743 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7744 * this always uses a decimal point.
7745 * Returns the length of the text that was consumed.
7746 */
7747 static int
7748string2float(text, value)
7749 char_u *text;
7750 float_T *value; /* result stored here */
7751{
7752 char *s = (char *)text;
7753 float_T f;
7754
7755 f = strtod(s, &s);
7756 *value = f;
7757 return (int)((char_u *)s - text);
7758}
7759#endif
7760
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 * Get the value of an environment variable.
7763 * "arg" is pointing to the '$'. It is advanced to after the name.
7764 * If the environment variable was not set, silently assume it is empty.
7765 * Always return OK.
7766 */
7767 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007768get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 int evaluate;
7772{
7773 char_u *string = NULL;
7774 int len;
7775 int cc;
7776 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007777 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
7779 ++*arg;
7780 name = *arg;
7781 len = get_env_len(arg);
7782 if (evaluate)
7783 {
7784 if (len != 0)
7785 {
7786 cc = name[len];
7787 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007788 /* first try vim_getenv(), fast for normal environment vars */
7789 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007791 {
7792 if (!mustfree)
7793 string = vim_strsave(string);
7794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 else
7796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007797 if (mustfree)
7798 vim_free(string);
7799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 /* next try expanding things like $VIM and ${HOME} */
7801 string = expand_env_save(name - 1);
7802 if (string != NULL && *string == '$')
7803 {
7804 vim_free(string);
7805 string = NULL;
7806 }
7807 }
7808 name[len] = cc;
7809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007810 rettv->v_type = VAR_STRING;
7811 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 }
7813
7814 return OK;
7815}
7816
7817/*
7818 * Array with names and number of arguments of all internal functions
7819 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7820 */
7821static struct fst
7822{
7823 char *f_name; /* function name */
7824 char f_min_argc; /* minimal number of arguments */
7825 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007826 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007827 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828} functions[] =
7829{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007830#ifdef FEAT_FLOAT
7831 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007832 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007833#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007834 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007835 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"append", 2, 2, f_append},
7837 {"argc", 0, 0, f_argc},
7838 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007839 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007840#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007841 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007843 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007846 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {"bufexists", 1, 1, f_bufexists},
7848 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7849 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7850 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7851 {"buflisted", 1, 1, f_buflisted},
7852 {"bufloaded", 1, 1, f_bufloaded},
7853 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007854 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"bufwinnr", 1, 1, f_bufwinnr},
7856 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007857 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007858 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#ifdef FEAT_FLOAT
7860 {"ceil", 1, 1, f_ceil},
7861#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007862 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007863 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007865 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007867#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007868 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007869 {"complete_add", 1, 1, f_complete_add},
7870 {"complete_check", 0, 0, f_complete_check},
7871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007873 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#ifdef FEAT_FLOAT
7875 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007876 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007877#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007878 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007880 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007881 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"delete", 1, 1, f_delete},
7883 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007884 {"diff_filler", 1, 1, f_diff_filler},
7885 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007886 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {"eventhandler", 0, 0, f_eventhandler},
7890 {"executable", 1, 1, f_executable},
7891 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007892#ifdef FEAT_FLOAT
7893 {"exp", 1, 1, f_exp},
7894#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007895 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007896 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007897 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7899 {"filereadable", 1, 1, f_filereadable},
7900 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007901 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007902 {"finddir", 1, 3, f_finddir},
7903 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#ifdef FEAT_FLOAT
7905 {"float2nr", 1, 1, f_float2nr},
7906 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007907 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007909 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"fnamemodify", 2, 2, f_fnamemodify},
7911 {"foldclosed", 1, 1, f_foldclosed},
7912 {"foldclosedend", 1, 1, f_foldclosedend},
7913 {"foldlevel", 1, 1, f_foldlevel},
7914 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007915 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007918 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007919 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007920 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007921 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"getchar", 0, 1, f_getchar},
7923 {"getcharmod", 0, 0, f_getcharmod},
7924 {"getcmdline", 0, 0, f_getcmdline},
7925 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007926 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007928 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007929 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {"getfsize", 1, 1, f_getfsize},
7931 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007932 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007933 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007934 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007935 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007936 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007937 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007938 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007939 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007941 {"gettabvar", 2, 3, f_gettabvar},
7942 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"getwinposx", 0, 0, f_getwinposx},
7944 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007945 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007946 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007947 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007949 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007950 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007951 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7953 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7954 {"histadd", 2, 2, f_histadd},
7955 {"histdel", 1, 2, f_histdel},
7956 {"histget", 1, 2, f_histget},
7957 {"histnr", 1, 1, f_histnr},
7958 {"hlID", 1, 1, f_hlID},
7959 {"hlexists", 1, 1, f_hlexists},
7960 {"hostname", 0, 0, f_hostname},
7961 {"iconv", 3, 3, f_iconv},
7962 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007964 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007966 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"inputrestore", 0, 0, f_inputrestore},
7968 {"inputsave", 0, 0, f_inputsave},
7969 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007970 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007971 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007973 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007975 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007978 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"libcall", 3, 3, f_libcall},
7980 {"libcallnr", 3, 3, f_libcallnr},
7981 {"line", 1, 1, f_line},
7982 {"line2byte", 1, 1, f_line2byte},
7983 {"lispindent", 1, 1, f_lispindent},
7984 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007985#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007986 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007987 {"log10", 1, 1, f_log10},
7988#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007989#ifdef FEAT_LUA
7990 {"luaeval", 1, 2, f_luaeval},
7991#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007992 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007993 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007994 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007995 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007996 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007997 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007998 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007999 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008000 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008001 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008002 {"max", 1, 1, f_max},
8003 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008004#ifdef vim_mkdir
8005 {"mkdir", 1, 3, f_mkdir},
8006#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008007 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008008#ifdef FEAT_MZSCHEME
8009 {"mzeval", 1, 1, f_mzeval},
8010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008012 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008013 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008014 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015#ifdef FEAT_FLOAT
8016 {"pow", 2, 2, f_pow},
8017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008019 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008020 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008021#ifdef FEAT_PYTHON3
8022 {"py3eval", 1, 1, f_py3eval},
8023#endif
8024#ifdef FEAT_PYTHON
8025 {"pyeval", 1, 1, f_pyeval},
8026#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008027 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008028 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008029 {"reltime", 0, 2, f_reltime},
8030 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"remote_expr", 2, 3, f_remote_expr},
8032 {"remote_foreground", 1, 1, f_remote_foreground},
8033 {"remote_peek", 1, 2, f_remote_peek},
8034 {"remote_read", 1, 1, f_remote_read},
8035 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008038 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008040 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008041#ifdef FEAT_FLOAT
8042 {"round", 1, 1, f_round},
8043#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008044 {"screencol", 0, 0, f_screencol},
8045 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008046 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008047 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008048 {"searchpair", 3, 7, f_searchpair},
8049 {"searchpairpos", 3, 7, f_searchpairpos},
8050 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {"server2client", 2, 2, f_server2client},
8052 {"serverlist", 0, 0, f_serverlist},
8053 {"setbufvar", 3, 3, f_setbufvar},
8054 {"setcmdpos", 1, 1, f_setcmdpos},
8055 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008056 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008057 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008058 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008059 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008061 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008062 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008064#ifdef FEAT_CRYPT
8065 {"sha256", 1, 1, f_sha256},
8066#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008067 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008068 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070#ifdef FEAT_FLOAT
8071 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008072 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008074 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008075 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008076 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008077 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008078 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079#ifdef FEAT_FLOAT
8080 {"sqrt", 1, 1, f_sqrt},
8081 {"str2float", 1, 1, f_str2float},
8082#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008083 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008084 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008085 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086#ifdef HAVE_STRFTIME
8087 {"strftime", 1, 2, f_strftime},
8088#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008089 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008090 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 {"strlen", 1, 1, f_strlen},
8092 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008093 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008095 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {"submatch", 1, 1, f_submatch},
8097 {"substitute", 4, 4, f_substitute},
8098 {"synID", 3, 3, f_synID},
8099 {"synIDattr", 2, 3, f_synIDattr},
8100 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008101 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008102 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008103 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008104 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008105 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008106 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008107 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008108 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008109#ifdef FEAT_FLOAT
8110 {"tan", 1, 1, f_tan},
8111 {"tanh", 1, 1, f_tanh},
8112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008114 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"tolower", 1, 1, f_tolower},
8116 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008117 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008118#ifdef FEAT_FLOAT
8119 {"trunc", 1, 1, f_trunc},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008122 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008123 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008124 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"virtcol", 1, 1, f_virtcol},
8126 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008127 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"winbufnr", 1, 1, f_winbufnr},
8129 {"wincol", 0, 0, f_wincol},
8130 {"winheight", 1, 1, f_winheight},
8131 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008132 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008134 {"winrestview", 1, 1, f_winrestview},
8135 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008137 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008138 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139};
8140
8141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8142
8143/*
8144 * Function given to ExpandGeneric() to obtain the list of internal
8145 * or user defined function names.
8146 */
8147 char_u *
8148get_function_name(xp, idx)
8149 expand_T *xp;
8150 int idx;
8151{
8152 static int intidx = -1;
8153 char_u *name;
8154
8155 if (idx == 0)
8156 intidx = -1;
8157 if (intidx < 0)
8158 {
8159 name = get_user_func_name(xp, idx);
8160 if (name != NULL)
8161 return name;
8162 }
8163 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8164 {
8165 STRCPY(IObuff, functions[intidx].f_name);
8166 STRCAT(IObuff, "(");
8167 if (functions[intidx].f_max_argc == 0)
8168 STRCAT(IObuff, ")");
8169 return IObuff;
8170 }
8171
8172 return NULL;
8173}
8174
8175/*
8176 * Function given to ExpandGeneric() to obtain the list of internal or
8177 * user defined variable or function names.
8178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 char_u *
8180get_expr_name(xp, idx)
8181 expand_T *xp;
8182 int idx;
8183{
8184 static int intidx = -1;
8185 char_u *name;
8186
8187 if (idx == 0)
8188 intidx = -1;
8189 if (intidx < 0)
8190 {
8191 name = get_function_name(xp, idx);
8192 if (name != NULL)
8193 return name;
8194 }
8195 return get_user_var_name(xp, ++intidx);
8196}
8197
8198#endif /* FEAT_CMDL_COMPL */
8199
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008200#if defined(EBCDIC) || defined(PROTO)
8201/*
8202 * Compare struct fst by function name.
8203 */
8204 static int
8205compare_func_name(s1, s2)
8206 const void *s1;
8207 const void *s2;
8208{
8209 struct fst *p1 = (struct fst *)s1;
8210 struct fst *p2 = (struct fst *)s2;
8211
8212 return STRCMP(p1->f_name, p2->f_name);
8213}
8214
8215/*
8216 * Sort the function table by function name.
8217 * The sorting of the table above is ASCII dependant.
8218 * On machines using EBCDIC we have to sort it.
8219 */
8220 static void
8221sortFunctions()
8222{
8223 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8224
8225 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8226}
8227#endif
8228
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230/*
8231 * Find internal function in table above.
8232 * Return index, or -1 if not found
8233 */
8234 static int
8235find_internal_func(name)
8236 char_u *name; /* name of the function */
8237{
8238 int first = 0;
8239 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8240 int cmp;
8241 int x;
8242
8243 /*
8244 * Find the function name in the table. Binary search.
8245 */
8246 while (first <= last)
8247 {
8248 x = first + ((unsigned)(last - first) >> 1);
8249 cmp = STRCMP(name, functions[x].f_name);
8250 if (cmp < 0)
8251 last = x - 1;
8252 else if (cmp > 0)
8253 first = x + 1;
8254 else
8255 return x;
8256 }
8257 return -1;
8258}
8259
8260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008261 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8262 * name it contains, otherwise return "name".
8263 */
8264 static char_u *
8265deref_func_name(name, lenp)
8266 char_u *name;
8267 int *lenp;
8268{
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 int cc;
8271
8272 cc = name[*lenp];
8273 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008274 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008278 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008279 {
8280 *lenp = 0;
8281 return (char_u *)""; /* just in case */
8282 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008283 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 }
8286
8287 return name;
8288}
8289
8290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * Allocate a variable for the result of a function.
8292 * Return OK or FAIL.
8293 */
8294 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008295get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8296 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 char_u *name; /* name of the function */
8298 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 char_u **arg; /* argument, pointing to the '(' */
8301 linenr_T firstline; /* first line of range */
8302 linenr_T lastline; /* last line of range */
8303 int *doesrange; /* return: function handled range */
8304 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 char_u *argp;
8308 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008309 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 int argcount = 0; /* number of arguments found */
8311
8312 /*
8313 * Get the arguments.
8314 */
8315 argp = *arg;
8316 while (argcount < MAX_FUNC_ARGS)
8317 {
8318 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8319 if (*argp == ')' || *argp == ',' || *argp == NUL)
8320 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8322 {
8323 ret = FAIL;
8324 break;
8325 }
8326 ++argcount;
8327 if (*argp != ',')
8328 break;
8329 }
8330 if (*argp == ')')
8331 ++argp;
8332 else
8333 ret = FAIL;
8334
8335 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008336 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008337 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 {
8340 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008341 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008342 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008343 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
8346 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 *arg = skipwhite(argp);
8350 return ret;
8351}
8352
8353
8354/*
8355 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008356 * Return OK when the function can't be called, FAIL otherwise.
8357 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 */
8359 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008360call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008361 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008362 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008364 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008366 typval_T *argvars; /* vars for arguments, must have "argcount"
8367 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 linenr_T firstline; /* first line of range */
8369 linenr_T lastline; /* last line of range */
8370 int *doesrange; /* return: function handled range */
8371 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
8374 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375#define ERROR_UNKNOWN 0
8376#define ERROR_TOOMANY 1
8377#define ERROR_TOOFEW 2
8378#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008379#define ERROR_DICT 4
8380#define ERROR_NONE 5
8381#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 int error = ERROR_NONE;
8383 int i;
8384 int llen;
8385 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386#define FLEN_FIXED 40
8387 char_u fname_buf[FLEN_FIXED + 1];
8388 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008389 char_u *name;
8390
8391 /* Make a copy of the name, if it comes from a funcref variable it could
8392 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008393 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008394 if (name == NULL)
8395 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
8397 /*
8398 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8399 * Change <SNR>123_name() to K_SNR 123_name().
8400 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 llen = eval_fname_script(name);
8403 if (llen > 0)
8404 {
8405 fname_buf[0] = K_SPECIAL;
8406 fname_buf[1] = KS_EXTRA;
8407 fname_buf[2] = (int)KE_SNR;
8408 i = 3;
8409 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8410 {
8411 if (current_SID <= 0)
8412 error = ERROR_SCRIPT;
8413 else
8414 {
8415 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8416 i = (int)STRLEN(fname_buf);
8417 }
8418 }
8419 if (i + STRLEN(name + llen) < FLEN_FIXED)
8420 {
8421 STRCPY(fname_buf + i, name + llen);
8422 fname = fname_buf;
8423 }
8424 else
8425 {
8426 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8427 if (fname == NULL)
8428 error = ERROR_OTHER;
8429 else
8430 {
8431 mch_memmove(fname, fname_buf, (size_t)i);
8432 STRCPY(fname + i, name + llen);
8433 }
8434 }
8435 }
8436 else
8437 fname = name;
8438
8439 *doesrange = FALSE;
8440
8441
8442 /* execute the function if no errors detected and executing */
8443 if (evaluate && error == ERROR_NONE)
8444 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008445 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8446 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 error = ERROR_UNKNOWN;
8448
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008449 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {
8451 /*
8452 * User defined function.
8453 */
8454 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 /* Trigger FuncUndefined event, may load the function. */
8458 if (fp == NULL
8459 && apply_autocmds(EVENT_FUNCUNDEFINED,
8460 fname, fname, TRUE, NULL)
8461 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 fp = find_func(fname);
8465 }
8466#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008467 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008468 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008469 {
8470 /* loaded a package, search for the function again */
8471 fp = find_func(fname);
8472 }
8473
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 if (fp != NULL)
8475 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008478 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008480 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008482 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008483 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 else
8485 {
8486 /*
8487 * Call the user function.
8488 * Save and restore search patterns, script variables and
8489 * redo buffer.
8490 */
8491 save_search_patterns();
8492 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008494 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008496 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8497 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8498 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008499 /* Function was unreferenced while being used, free it
8500 * now. */
8501 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 restoreRedobuff();
8503 restore_search_patterns();
8504 error = ERROR_NONE;
8505 }
8506 }
8507 }
8508 else
8509 {
8510 /*
8511 * Find the function name in the table, call its implementation.
8512 */
8513 i = find_internal_func(fname);
8514 if (i >= 0)
8515 {
8516 if (argcount < functions[i].f_min_argc)
8517 error = ERROR_TOOFEW;
8518 else if (argcount > functions[i].f_max_argc)
8519 error = ERROR_TOOMANY;
8520 else
8521 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008523 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 error = ERROR_NONE;
8525 }
8526 }
8527 }
8528 /*
8529 * The function call (or "FuncUndefined" autocommand sequence) might
8530 * have been aborted by an error, an interrupt, or an explicitly thrown
8531 * exception that has not been caught so far. This situation can be
8532 * tested for by calling aborting(). For an error in an internal
8533 * function or for the "E132" error in call_user_func(), however, the
8534 * throw point at which the "force_abort" flag (temporarily reset by
8535 * emsg()) is normally updated has not been reached yet. We need to
8536 * update that flag first to make aborting() reliable.
8537 */
8538 update_force_abort();
8539 }
8540 if (error == ERROR_NONE)
8541 ret = OK;
8542
8543 /*
8544 * Report an error unless the argument evaluation or function call has been
8545 * cancelled due to an aborting error, an interrupt, or an exception.
8546 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008547 if (!aborting())
8548 {
8549 switch (error)
8550 {
8551 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 break;
8554 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008555 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008556 break;
8557 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008558 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 name);
8560 break;
8561 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008562 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 name);
8564 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008566 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567 name);
8568 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008569 }
8570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 if (fname != name && fname != fname_buf)
8573 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008574 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
8576 return ret;
8577}
8578
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008579/*
8580 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008581 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008582 */
8583 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008584emsg_funcname(ermsg, name)
8585 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008586 char_u *name;
8587{
8588 char_u *p;
8589
8590 if (*name == K_SPECIAL)
8591 p = concat_str((char_u *)"<SNR>", name + 3);
8592 else
8593 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008594 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008595 if (p != name)
8596 vim_free(p);
8597}
8598
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008599/*
8600 * Return TRUE for a non-zero Number and a non-empty String.
8601 */
8602 static int
8603non_zero_arg(argvars)
8604 typval_T *argvars;
8605{
8606 return ((argvars[0].v_type == VAR_NUMBER
8607 && argvars[0].vval.v_number != 0)
8608 || (argvars[0].v_type == VAR_STRING
8609 && argvars[0].vval.v_string != NULL
8610 && *argvars[0].vval.v_string != NUL));
8611}
8612
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613/*********************************************
8614 * Implementation of the built-in functions
8615 */
8616
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008617#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008618static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8619
8620/*
8621 * Get the float value of "argvars[0]" into "f".
8622 * Returns FAIL when the argument is not a Number or Float.
8623 */
8624 static int
8625get_float_arg(argvars, f)
8626 typval_T *argvars;
8627 float_T *f;
8628{
8629 if (argvars[0].v_type == VAR_FLOAT)
8630 {
8631 *f = argvars[0].vval.v_float;
8632 return OK;
8633 }
8634 if (argvars[0].v_type == VAR_NUMBER)
8635 {
8636 *f = (float_T)argvars[0].vval.v_number;
8637 return OK;
8638 }
8639 EMSG(_("E808: Number or Float required"));
8640 return FAIL;
8641}
8642
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008643/*
8644 * "abs(expr)" function
8645 */
8646 static void
8647f_abs(argvars, rettv)
8648 typval_T *argvars;
8649 typval_T *rettv;
8650{
8651 if (argvars[0].v_type == VAR_FLOAT)
8652 {
8653 rettv->v_type = VAR_FLOAT;
8654 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8655 }
8656 else
8657 {
8658 varnumber_T n;
8659 int error = FALSE;
8660
8661 n = get_tv_number_chk(&argvars[0], &error);
8662 if (error)
8663 rettv->vval.v_number = -1;
8664 else if (n > 0)
8665 rettv->vval.v_number = n;
8666 else
8667 rettv->vval.v_number = -n;
8668 }
8669}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670
8671/*
8672 * "acos()" function
8673 */
8674 static void
8675f_acos(argvars, rettv)
8676 typval_T *argvars;
8677 typval_T *rettv;
8678{
8679 float_T f;
8680
8681 rettv->v_type = VAR_FLOAT;
8682 if (get_float_arg(argvars, &f) == OK)
8683 rettv->vval.v_float = acos(f);
8684 else
8685 rettv->vval.v_float = 0.0;
8686}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008687#endif
8688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008690 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 */
8692 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008693f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *argvars;
8695 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
Bram Moolenaar33570922005-01-25 22:26:29 +00008697 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008700 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008702 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008703 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008704 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008706 }
8707 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008708 EMSG(_(e_listreq));
8709}
8710
8711/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008712 * "and(expr, expr)" function
8713 */
8714 static void
8715f_and(argvars, rettv)
8716 typval_T *argvars;
8717 typval_T *rettv;
8718{
8719 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8720 & get_tv_number_chk(&argvars[1], NULL);
8721}
8722
8723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008724 * "append(lnum, string/list)" function
8725 */
8726 static void
8727f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 typval_T *argvars;
8729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008730{
8731 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008732 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 list_T *l = NULL;
8734 listitem_T *li = NULL;
8735 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008736 long added = 0;
8737
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738 lnum = get_tv_lnum(argvars);
8739 if (lnum >= 0
8740 && lnum <= curbuf->b_ml.ml_line_count
8741 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008742 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008744 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745 l = argvars[1].vval.v_list;
8746 if (l == NULL)
8747 return;
8748 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750 for (;;)
8751 {
8752 if (l == NULL)
8753 tv = &argvars[1]; /* append a string */
8754 else if (li == NULL)
8755 break; /* end of list */
8756 else
8757 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008758 line = get_tv_string_chk(tv);
8759 if (line == NULL) /* type error */
8760 {
8761 rettv->vval.v_number = 1; /* Failed */
8762 break;
8763 }
8764 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 ++added;
8766 if (l == NULL)
8767 break;
8768 li = li->li_next;
8769 }
8770
8771 appended_lines_mark(lnum, added);
8772 if (curwin->w_cursor.lnum > lnum)
8773 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008775 else
8776 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
8779/*
8780 * "argc()" function
8781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008784 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
8791 * "argidx()" function
8792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008795 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
8801/*
8802 * "argv(nr)" function
8803 */
8804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *argvars;
8807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
8809 int idx;
8810
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008811 if (argvars[0].v_type != VAR_UNKNOWN)
8812 {
8813 idx = get_tv_number_chk(&argvars[0], NULL);
8814 if (idx >= 0 && idx < ARGCOUNT)
8815 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8816 else
8817 rettv->vval.v_string = NULL;
8818 rettv->v_type = VAR_STRING;
8819 }
8820 else if (rettv_list_alloc(rettv) == OK)
8821 for (idx = 0; idx < ARGCOUNT; ++idx)
8822 list_append_string(rettv->vval.v_list,
8823 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008826#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008827/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008828 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008829 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830 static void
8831f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008832 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008833 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835 float_T f;
8836
8837 rettv->v_type = VAR_FLOAT;
8838 if (get_float_arg(argvars, &f) == OK)
8839 rettv->vval.v_float = asin(f);
8840 else
8841 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842}
8843
8844/*
8845 * "atan()" function
8846 */
8847 static void
8848f_atan(argvars, rettv)
8849 typval_T *argvars;
8850 typval_T *rettv;
8851{
8852 float_T f;
8853
8854 rettv->v_type = VAR_FLOAT;
8855 if (get_float_arg(argvars, &f) == OK)
8856 rettv->vval.v_float = atan(f);
8857 else
8858 rettv->vval.v_float = 0.0;
8859}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008860
8861/*
8862 * "atan2()" function
8863 */
8864 static void
8865f_atan2(argvars, rettv)
8866 typval_T *argvars;
8867 typval_T *rettv;
8868{
8869 float_T fx, fy;
8870
8871 rettv->v_type = VAR_FLOAT;
8872 if (get_float_arg(argvars, &fx) == OK
8873 && get_float_arg(&argvars[1], &fy) == OK)
8874 rettv->vval.v_float = atan2(fx, fy);
8875 else
8876 rettv->vval.v_float = 0.0;
8877}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008878#endif
8879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880/*
8881 * "browse(save, title, initdir, default)" function
8882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008884f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008885 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888#ifdef FEAT_BROWSE
8889 int save;
8890 char_u *title;
8891 char_u *initdir;
8892 char_u *defname;
8893 char_u buf[NUMBUFLEN];
8894 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897 save = get_tv_number_chk(&argvars[0], &error);
8898 title = get_tv_string_chk(&argvars[1]);
8899 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8900 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 if (error || title == NULL || initdir == NULL || defname == NULL)
8903 rettv->vval.v_string = NULL;
8904 else
8905 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906 do_browse(save ? BROWSE_SAVE : 0,
8907 title, defname, NULL, initdir, NULL, curbuf);
8908#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008910#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008912}
8913
8914/*
8915 * "browsedir(title, initdir)" function
8916 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008919 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008920 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008921{
8922#ifdef FEAT_BROWSE
8923 char_u *title;
8924 char_u *initdir;
8925 char_u buf[NUMBUFLEN];
8926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927 title = get_tv_string_chk(&argvars[0]);
8928 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008930 if (title == NULL || initdir == NULL)
8931 rettv->vval.v_string = NULL;
8932 else
8933 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008934 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939}
8940
Bram Moolenaar33570922005-01-25 22:26:29 +00008941static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943/*
8944 * Find a buffer by number or exact name.
8945 */
8946 static buf_T *
8947find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008948 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949{
8950 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008952 if (avar->v_type == VAR_NUMBER)
8953 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008954 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008956 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008957 if (buf == NULL)
8958 {
8959 /* No full path name match, try a match with a URL or a "nofile"
8960 * buffer, these don't use the full path. */
8961 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8962 if (buf->b_fname != NULL
8963 && (path_with_url(buf->b_fname)
8964#ifdef FEAT_QUICKFIX
8965 || bt_nofile(buf)
8966#endif
8967 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008968 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008969 break;
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 }
8972 return buf;
8973}
8974
8975/*
8976 * "bufexists(expr)" function
8977 */
8978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008980 typval_T *argvars;
8981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986/*
8987 * "buflisted(expr)" function
8988 */
8989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008991 typval_T *argvars;
8992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
8994 buf_T *buf;
8995
8996 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998}
8999
9000/*
9001 * "bufloaded(expr)" function
9002 */
9003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 buf_T *buf;
9009
9010 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012}
9013
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009014static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009015
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016/*
9017 * Get buffer by number or pattern.
9018 */
9019 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009020get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009022 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009024 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 int save_magic;
9026 char_u *save_cpo;
9027 buf_T *buf;
9028
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009029 if (tv->v_type == VAR_NUMBER)
9030 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009031 if (tv->v_type != VAR_STRING)
9032 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 if (name == NULL || *name == NUL)
9034 return curbuf;
9035 if (name[0] == '$' && name[1] == NUL)
9036 return lastbuf;
9037
9038 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9039 save_magic = p_magic;
9040 p_magic = TRUE;
9041 save_cpo = p_cpo;
9042 p_cpo = (char_u *)"";
9043
9044 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009045 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 p_magic = save_magic;
9048 p_cpo = save_cpo;
9049
9050 /* If not found, try expanding the name, like done for bufexists(). */
9051 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009052 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 return buf;
9055}
9056
9057/*
9058 * "bufname(expr)" function
9059 */
9060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 typval_T *argvars;
9063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 buf_T *buf;
9066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009069 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 --emsg_off;
9076}
9077
9078/*
9079 * "bufnr(expr)" function
9080 */
9081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 typval_T *argvars;
9084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085{
9086 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009087 int error = FALSE;
9088 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009090 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009092 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009093 --emsg_off;
9094
9095 /* If the buffer isn't found and the second argument is not zero create a
9096 * new buffer. */
9097 if (buf == NULL
9098 && argvars[1].v_type != VAR_UNKNOWN
9099 && get_tv_number_chk(&argvars[1], &error) != 0
9100 && !error
9101 && (name = get_tv_string_chk(&argvars[0])) != NULL
9102 && !error)
9103 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9104
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109}
9110
9111/*
9112 * "bufwinnr(nr)" function
9113 */
9114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009116 typval_T *argvars;
9117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119#ifdef FEAT_WINDOWS
9120 win_T *wp;
9121 int winnr = 0;
9122#endif
9123 buf_T *buf;
9124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009127 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#ifdef FEAT_WINDOWS
9129 for (wp = firstwin; wp; wp = wp->w_next)
9130 {
9131 ++winnr;
9132 if (wp->w_buffer == buf)
9133 break;
9134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138#endif
9139 --emsg_off;
9140}
9141
9142/*
9143 * "byte2line(byte)" function
9144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#else
9153 long boff = 0;
9154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009155 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009159 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 (linenr_T)0, &boff);
9161#endif
9162}
9163
9164/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009165 * "byteidx()" function
9166 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009169 typval_T *argvars;
9170 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009171{
9172#ifdef FEAT_MBYTE
9173 char_u *t;
9174#endif
9175 char_u *str;
9176 long idx;
9177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178 str = get_tv_string_chk(&argvars[0]);
9179 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009181 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009182 return;
9183
9184#ifdef FEAT_MBYTE
9185 t = str;
9186 for ( ; idx > 0; idx--)
9187 {
9188 if (*t == NUL) /* EOL reached */
9189 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009190 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009191 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009192 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009193#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009194 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009196#endif
9197}
9198
Bram Moolenaardb913952012-06-29 12:54:53 +02009199 int
9200func_call(name, args, selfdict, rettv)
9201 char_u *name;
9202 typval_T *args;
9203 dict_T *selfdict;
9204 typval_T *rettv;
9205{
9206 listitem_T *item;
9207 typval_T argv[MAX_FUNC_ARGS + 1];
9208 int argc = 0;
9209 int dummy;
9210 int r = 0;
9211
9212 for (item = args->vval.v_list->lv_first; item != NULL;
9213 item = item->li_next)
9214 {
9215 if (argc == MAX_FUNC_ARGS)
9216 {
9217 EMSG(_("E699: Too many arguments"));
9218 break;
9219 }
9220 /* Make a copy of each argument. This is needed to be able to set
9221 * v_lock to VAR_FIXED in the copy without changing the original list.
9222 */
9223 copy_tv(&item->li_tv, &argv[argc++]);
9224 }
9225
9226 if (item == NULL)
9227 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9228 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9229 &dummy, TRUE, selfdict);
9230
9231 /* Free the arguments. */
9232 while (argc > 0)
9233 clear_tv(&argv[--argc]);
9234
9235 return r;
9236}
9237
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009238/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009239 * "call(func, arglist)" function
9240 */
9241 static void
9242f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009243 typval_T *argvars;
9244 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009245{
9246 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009247 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009248
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009249 if (argvars[1].v_type != VAR_LIST)
9250 {
9251 EMSG(_(e_listreq));
9252 return;
9253 }
9254 if (argvars[1].vval.v_list == NULL)
9255 return;
9256
9257 if (argvars[0].v_type == VAR_FUNC)
9258 func = argvars[0].vval.v_string;
9259 else
9260 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009261 if (*func == NUL)
9262 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009263
Bram Moolenaare9a41262005-01-15 22:18:47 +00009264 if (argvars[2].v_type != VAR_UNKNOWN)
9265 {
9266 if (argvars[2].v_type != VAR_DICT)
9267 {
9268 EMSG(_(e_dictreq));
9269 return;
9270 }
9271 selfdict = argvars[2].vval.v_dict;
9272 }
9273
Bram Moolenaardb913952012-06-29 12:54:53 +02009274 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009275}
9276
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009277#ifdef FEAT_FLOAT
9278/*
9279 * "ceil({float})" function
9280 */
9281 static void
9282f_ceil(argvars, rettv)
9283 typval_T *argvars;
9284 typval_T *rettv;
9285{
9286 float_T f;
9287
9288 rettv->v_type = VAR_FLOAT;
9289 if (get_float_arg(argvars, &f) == OK)
9290 rettv->vval.v_float = ceil(f);
9291 else
9292 rettv->vval.v_float = 0.0;
9293}
9294#endif
9295
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009296/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009297 * "changenr()" function
9298 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009299 static void
9300f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009301 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009302 typval_T *rettv;
9303{
9304 rettv->vval.v_number = curbuf->b_u_seq_cur;
9305}
9306
9307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 * "char2nr(string)" function
9309 */
9310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315#ifdef FEAT_MBYTE
9316 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009317 {
9318 int utf8 = 0;
9319
9320 if (argvars[1].v_type != VAR_UNKNOWN)
9321 utf8 = get_tv_number_chk(&argvars[1], NULL);
9322
9323 if (utf8)
9324 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9325 else
9326 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 else
9329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
9332
9333/*
9334 * "cindent(lnum)" function
9335 */
9336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009338 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
9341#ifdef FEAT_CINDENT
9342 pos_T pos;
9343 linenr_T lnum;
9344
9345 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9348 {
9349 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009350 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 curwin->w_cursor = pos;
9352 }
9353 else
9354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009355 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356}
9357
9358/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009359 * "clearmatches()" function
9360 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009361 static void
9362f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009363 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009364 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009365{
9366#ifdef FEAT_SEARCH_EXTRA
9367 clear_matches(curwin);
9368#endif
9369}
9370
9371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 * "col(string)" function
9373 */
9374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009375f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 colnr_T col = 0;
9380 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009381 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009383 fp = var2fpos(&argvars[0], FALSE, &fnum);
9384 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 {
9386 if (fp->col == MAXCOL)
9387 {
9388 /* '> can be MAXCOL, get the length of the line then */
9389 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009390 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 else
9392 col = MAXCOL;
9393 }
9394 else
9395 {
9396 col = fp->col + 1;
9397#ifdef FEAT_VIRTUALEDIT
9398 /* col(".") when the cursor is on the NUL at the end of the line
9399 * because of "coladd" can be seen as an extra column. */
9400 if (virtual_active() && fp == &curwin->w_cursor)
9401 {
9402 char_u *p = ml_get_cursor();
9403
9404 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9405 curwin->w_virtcol - curwin->w_cursor.coladd))
9406 {
9407# ifdef FEAT_MBYTE
9408 int l;
9409
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009410 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 col += l;
9412# else
9413 if (*p != NUL && p[1] == NUL)
9414 ++col;
9415# endif
9416 }
9417 }
9418#endif
9419 }
9420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009421 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422}
9423
Bram Moolenaar572cb562005-08-05 21:35:02 +00009424#if defined(FEAT_INS_EXPAND)
9425/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009426 * "complete()" function
9427 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009428 static void
9429f_complete(argvars, rettv)
9430 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009431 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009432{
9433 int startcol;
9434
9435 if ((State & INSERT) == 0)
9436 {
9437 EMSG(_("E785: complete() can only be used in Insert mode"));
9438 return;
9439 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009440
9441 /* Check for undo allowed here, because if something was already inserted
9442 * the line was already saved for undo and this check isn't done. */
9443 if (!undo_allowed())
9444 return;
9445
Bram Moolenaarade00832006-03-10 21:46:58 +00009446 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9447 {
9448 EMSG(_(e_invarg));
9449 return;
9450 }
9451
9452 startcol = get_tv_number_chk(&argvars[0], NULL);
9453 if (startcol <= 0)
9454 return;
9455
9456 set_completion(startcol - 1, argvars[1].vval.v_list);
9457}
9458
9459/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009460 * "complete_add()" function
9461 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009462 static void
9463f_complete_add(argvars, rettv)
9464 typval_T *argvars;
9465 typval_T *rettv;
9466{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009467 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009468}
9469
9470/*
9471 * "complete_check()" function
9472 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009473 static void
9474f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009475 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009476 typval_T *rettv;
9477{
9478 int saved = RedrawingDisabled;
9479
9480 RedrawingDisabled = 0;
9481 ins_compl_check_keys(0);
9482 rettv->vval.v_number = compl_interrupted;
9483 RedrawingDisabled = saved;
9484}
9485#endif
9486
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487/*
9488 * "confirm(message, buttons[, default [, type]])" function
9489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009491f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009492 typval_T *argvars UNUSED;
9493 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9496 char_u *message;
9497 char_u *buttons = NULL;
9498 char_u buf[NUMBUFLEN];
9499 char_u buf2[NUMBUFLEN];
9500 int def = 1;
9501 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 char_u *typestr;
9503 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009505 message = get_tv_string_chk(&argvars[0]);
9506 if (message == NULL)
9507 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009508 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9511 if (buttons == NULL)
9512 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009516 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9519 if (typestr == NULL)
9520 error = TRUE;
9521 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009523 switch (TOUPPER_ASC(*typestr))
9524 {
9525 case 'E': type = VIM_ERROR; break;
9526 case 'Q': type = VIM_QUESTION; break;
9527 case 'I': type = VIM_INFO; break;
9528 case 'W': type = VIM_WARNING; break;
9529 case 'G': type = VIM_GENERIC; break;
9530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532 }
9533 }
9534 }
9535
9536 if (buttons == NULL || *buttons == NUL)
9537 buttons = (char_u *)_("&Ok");
9538
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009539 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009540 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009541 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542#endif
9543}
9544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009545/*
9546 * "copy()" function
9547 */
9548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 typval_T *argvars;
9551 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009553 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009556#ifdef FEAT_FLOAT
9557/*
9558 * "cos()" function
9559 */
9560 static void
9561f_cos(argvars, rettv)
9562 typval_T *argvars;
9563 typval_T *rettv;
9564{
9565 float_T f;
9566
9567 rettv->v_type = VAR_FLOAT;
9568 if (get_float_arg(argvars, &f) == OK)
9569 rettv->vval.v_float = cos(f);
9570 else
9571 rettv->vval.v_float = 0.0;
9572}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009573
9574/*
9575 * "cosh()" function
9576 */
9577 static void
9578f_cosh(argvars, rettv)
9579 typval_T *argvars;
9580 typval_T *rettv;
9581{
9582 float_T f;
9583
9584 rettv->v_type = VAR_FLOAT;
9585 if (get_float_arg(argvars, &f) == OK)
9586 rettv->vval.v_float = cosh(f);
9587 else
9588 rettv->vval.v_float = 0.0;
9589}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009590#endif
9591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009593 * "count()" function
9594 */
9595 static void
9596f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009597 typval_T *argvars;
9598 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009599{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 long n = 0;
9601 int ic = FALSE;
9602
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009604 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 listitem_T *li;
9606 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009607 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009608
Bram Moolenaare9a41262005-01-15 22:18:47 +00009609 if ((l = argvars[0].vval.v_list) != NULL)
9610 {
9611 li = l->lv_first;
9612 if (argvars[2].v_type != VAR_UNKNOWN)
9613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009614 int error = FALSE;
9615
9616 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009617 if (argvars[3].v_type != VAR_UNKNOWN)
9618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619 idx = get_tv_number_chk(&argvars[3], &error);
9620 if (!error)
9621 {
9622 li = list_find(l, idx);
9623 if (li == NULL)
9624 EMSGN(_(e_listidx), idx);
9625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009627 if (error)
9628 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009629 }
9630
9631 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009632 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 ++n;
9634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 else if (argvars[0].v_type == VAR_DICT)
9637 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009638 int todo;
9639 dict_T *d;
9640 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009642 if ((d = argvars[0].vval.v_dict) != NULL)
9643 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009644 int error = FALSE;
9645
Bram Moolenaare9a41262005-01-15 22:18:47 +00009646 if (argvars[2].v_type != VAR_UNKNOWN)
9647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 if (argvars[3].v_type != VAR_UNKNOWN)
9650 EMSG(_(e_invarg));
9651 }
9652
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009653 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009655 {
9656 if (!HASHITEM_EMPTY(hi))
9657 {
9658 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009659 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009660 ++n;
9661 }
9662 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663 }
9664 }
9665 else
9666 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009667 rettv->vval.v_number = n;
9668}
9669
9670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9672 *
9673 * Checks the existence of a cscope connection.
9674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009677 typval_T *argvars UNUSED;
9678 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
9680#ifdef FEAT_CSCOPE
9681 int num = 0;
9682 char_u *dbpath = NULL;
9683 char_u *prepend = NULL;
9684 char_u buf[NUMBUFLEN];
9685
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 if (argvars[0].v_type != VAR_UNKNOWN
9687 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 num = (int)get_tv_number(&argvars[0]);
9690 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 }
9694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#endif
9697}
9698
9699/*
9700 * "cursor(lnum, col)" function
9701 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009702 * Moves the cursor to the specified line and column.
9703 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 typval_T *argvars;
9708 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009711#ifdef FEAT_VIRTUALEDIT
9712 long coladd = 0;
9713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009715 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009716 if (argvars[1].v_type == VAR_UNKNOWN)
9717 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009718 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009719
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009721 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009722 line = pos.lnum;
9723 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009724#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726#endif
9727 }
9728 else
9729 {
9730 line = get_tv_lnum(argvars);
9731 col = get_tv_number_chk(&argvars[1], NULL);
9732#ifdef FEAT_VIRTUALEDIT
9733 if (argvars[2].v_type != VAR_UNKNOWN)
9734 coladd = get_tv_number_chk(&argvars[2], NULL);
9735#endif
9736 }
9737 if (line < 0 || col < 0
9738#ifdef FEAT_VIRTUALEDIT
9739 || coladd < 0
9740#endif
9741 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (line > 0)
9744 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (col > 0)
9746 curwin->w_cursor.col = col - 1;
9747#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009748 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
9750
9751 /* Make sure the cursor is in a valid position. */
9752 check_cursor();
9753#ifdef FEAT_MBYTE
9754 /* Correct cursor for multi-byte character. */
9755 if (has_mbyte)
9756 mb_adjust_cursor();
9757#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009758
9759 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009760 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761}
9762
9763/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009764 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 */
9766 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009768 typval_T *argvars;
9769 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009771 int noref = 0;
9772
9773 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009774 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009775 if (noref < 0 || noref > 1)
9776 EMSG(_(e_invarg));
9777 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009778 {
9779 current_copyID += COPYID_INC;
9780 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782}
9783
9784/*
9785 * "delete()" function
9786 */
9787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009789 typval_T *argvars;
9790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796}
9797
9798/*
9799 * "did_filetype()" function
9800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009803 typval_T *argvars UNUSED;
9804 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805{
9806#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808#endif
9809}
9810
9811/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009812 * "diff_filler()" function
9813 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009816 typval_T *argvars UNUSED;
9817 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009818{
9819#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009821#endif
9822}
9823
9824/*
9825 * "diff_hlID()" function
9826 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009829 typval_T *argvars UNUSED;
9830 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009831{
9832#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 static linenr_T prev_lnum = 0;
9835 static int changedtick = 0;
9836 static int fnum = 0;
9837 static int change_start = 0;
9838 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009839 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009840 int filler_lines;
9841 int col;
9842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 if (lnum < 0) /* ignore type error in {lnum} arg */
9844 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009845 if (lnum != prev_lnum
9846 || changedtick != curbuf->b_changedtick
9847 || fnum != curbuf->b_fnum)
9848 {
9849 /* New line, buffer, change: need to get the values. */
9850 filler_lines = diff_check(curwin, lnum);
9851 if (filler_lines < 0)
9852 {
9853 if (filler_lines == -1)
9854 {
9855 change_start = MAXCOL;
9856 change_end = -1;
9857 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9858 hlID = HLF_ADD; /* added line */
9859 else
9860 hlID = HLF_CHD; /* changed line */
9861 }
9862 else
9863 hlID = HLF_ADD; /* added line */
9864 }
9865 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009866 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009867 prev_lnum = lnum;
9868 changedtick = curbuf->b_changedtick;
9869 fnum = curbuf->b_fnum;
9870 }
9871
9872 if (hlID == HLF_CHD || hlID == HLF_TXD)
9873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875 if (col >= change_start && col <= change_end)
9876 hlID = HLF_TXD; /* changed text */
9877 else
9878 hlID = HLF_CHD; /* changed line */
9879 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009880 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009881#endif
9882}
9883
9884/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009885 * "empty({expr})" function
9886 */
9887 static void
9888f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009889 typval_T *argvars;
9890 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009891{
9892 int n;
9893
9894 switch (argvars[0].v_type)
9895 {
9896 case VAR_STRING:
9897 case VAR_FUNC:
9898 n = argvars[0].vval.v_string == NULL
9899 || *argvars[0].vval.v_string == NUL;
9900 break;
9901 case VAR_NUMBER:
9902 n = argvars[0].vval.v_number == 0;
9903 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009904#ifdef FEAT_FLOAT
9905 case VAR_FLOAT:
9906 n = argvars[0].vval.v_float == 0.0;
9907 break;
9908#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009909 case VAR_LIST:
9910 n = argvars[0].vval.v_list == NULL
9911 || argvars[0].vval.v_list->lv_first == NULL;
9912 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009913 case VAR_DICT:
9914 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009916 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009917 default:
9918 EMSG2(_(e_intern2), "f_empty()");
9919 n = 0;
9920 }
9921
9922 rettv->vval.v_number = n;
9923}
9924
9925/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 * "escape({string}, {chars})" function
9927 */
9928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 typval_T *argvars;
9931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 char_u buf[NUMBUFLEN];
9934
Bram Moolenaar758711c2005-02-02 23:11:38 +00009935 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9936 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
9940/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009941 * "eval()" function
9942 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009943 static void
9944f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009947{
9948 char_u *s;
9949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950 s = get_tv_string_chk(&argvars[0]);
9951 if (s != NULL)
9952 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9955 {
9956 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009957 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009958 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959 else if (*s != NUL)
9960 EMSG(_(e_trailing));
9961}
9962
9963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 * "eventhandler()" function
9965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009968 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972}
9973
9974/*
9975 * "executable()" function
9976 */
9977 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009979 typval_T *argvars;
9980 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983}
9984
9985/*
9986 * "exists()" function
9987 */
9988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009990 typval_T *argvars;
9991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 char_u *p;
9994 char_u *name;
9995 int n = FALSE;
9996 int len = 0;
9997
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009998 no_autoload = TRUE;
9999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 if (*p == '$') /* environment variable */
10002 {
10003 /* first try "normal" environment variables (fast) */
10004 if (mch_getenv(p + 1) != NULL)
10005 n = TRUE;
10006 else
10007 {
10008 /* try expanding things like $VIM and ${HOME} */
10009 p = expand_env_save(p);
10010 if (p != NULL && *p != '$')
10011 n = TRUE;
10012 vim_free(p);
10013 }
10014 }
10015 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010017 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010018 if (*skipwhite(p) != NUL)
10019 n = FALSE; /* trailing garbage */
10020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 else if (*p == '*') /* internal or user defined function */
10022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010023 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else if (*p == ':')
10026 {
10027 n = cmd_exists(p + 1);
10028 }
10029 else if (*p == '#')
10030 {
10031#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010032 if (p[1] == '#')
10033 n = autocmd_supported(p + 2);
10034 else
10035 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036#endif
10037 }
10038 else /* internal variable */
10039 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010040 char_u *tofree;
10041 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010043 /* get_name_len() takes care of expanding curly braces */
10044 name = p;
10045 len = get_name_len(&p, &tofree, TRUE, FALSE);
10046 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010048 if (tofree != NULL)
10049 name = tofree;
10050 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10051 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010053 /* handle d.key, l[idx], f(expr) */
10054 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10055 if (n)
10056 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010059 if (*p != NUL)
10060 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010062 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 }
10064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010065 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010066
10067 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068}
10069
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010070#ifdef FEAT_FLOAT
10071/*
10072 * "exp()" function
10073 */
10074 static void
10075f_exp(argvars, rettv)
10076 typval_T *argvars;
10077 typval_T *rettv;
10078{
10079 float_T f;
10080
10081 rettv->v_type = VAR_FLOAT;
10082 if (get_float_arg(argvars, &f) == OK)
10083 rettv->vval.v_float = exp(f);
10084 else
10085 rettv->vval.v_float = 0.0;
10086}
10087#endif
10088
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089/*
10090 * "expand()" function
10091 */
10092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010094 typval_T *argvars;
10095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097 char_u *s;
10098 int len;
10099 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010100 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010103 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010106 if (argvars[1].v_type != VAR_UNKNOWN
10107 && argvars[2].v_type != VAR_UNKNOWN
10108 && get_tv_number_chk(&argvars[2], &error)
10109 && !error)
10110 {
10111 rettv->v_type = VAR_LIST;
10112 rettv->vval.v_list = NULL;
10113 }
10114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 if (*s == '%' || *s == '#' || *s == '<')
10117 {
10118 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010119 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010121 if (rettv->v_type == VAR_LIST)
10122 {
10123 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10124 list_append_string(rettv->vval.v_list, result, -1);
10125 else
10126 vim_free(result);
10127 }
10128 else
10129 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 }
10131 else
10132 {
10133 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010134 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 if (argvars[1].v_type != VAR_UNKNOWN
10136 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010137 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 if (!error)
10139 {
10140 ExpandInit(&xpc);
10141 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010142 if (p_wic)
10143 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010144 if (rettv->v_type == VAR_STRING)
10145 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10146 options, WILD_ALL);
10147 else if (rettv_list_alloc(rettv) != FAIL)
10148 {
10149 int i;
10150
10151 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10152 for (i = 0; i < xpc.xp_numfiles; i++)
10153 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10154 ExpandCleanup(&xpc);
10155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 }
10157 else
10158 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 }
10160}
10161
10162/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010163 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010164 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010165 */
10166 static void
10167f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010168 typval_T *argvars;
10169 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010170{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010171 char *arg_errmsg = N_("extend() argument");
10172
Bram Moolenaare9a41262005-01-15 22:18:47 +000010173 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010174 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 list_T *l1, *l2;
10176 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010177 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010179
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 l1 = argvars[0].vval.v_list;
10181 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010182 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010183 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010184 {
10185 if (argvars[2].v_type != VAR_UNKNOWN)
10186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 before = get_tv_number_chk(&argvars[2], &error);
10188 if (error)
10189 return; /* type error; errmsg already given */
10190
Bram Moolenaar758711c2005-02-02 23:11:38 +000010191 if (before == l1->lv_len)
10192 item = NULL;
10193 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010194 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010195 item = list_find(l1, before);
10196 if (item == NULL)
10197 {
10198 EMSGN(_(e_listidx), before);
10199 return;
10200 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010201 }
10202 }
10203 else
10204 item = NULL;
10205 list_extend(l1, l2, item);
10206
Bram Moolenaare9a41262005-01-15 22:18:47 +000010207 copy_tv(&argvars[0], rettv);
10208 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010209 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010210 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10211 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010212 dict_T *d1, *d2;
10213 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 char_u *action;
10215 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010216 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010217 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218
10219 d1 = argvars[0].vval.v_dict;
10220 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010221 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010222 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010223 {
10224 /* Check the third argument. */
10225 if (argvars[2].v_type != VAR_UNKNOWN)
10226 {
10227 static char *(av[]) = {"keep", "force", "error"};
10228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 action = get_tv_string_chk(&argvars[2]);
10230 if (action == NULL)
10231 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 for (i = 0; i < 3; ++i)
10233 if (STRCMP(action, av[i]) == 0)
10234 break;
10235 if (i == 3)
10236 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010237 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010238 return;
10239 }
10240 }
10241 else
10242 action = (char_u *)"force";
10243
10244 /* Go over all entries in the second dict and add them to the
10245 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010246 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010249 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010251 --todo;
10252 di1 = dict_find(d1, hi2->hi_key, -1);
Bram Moolenaarbdb62052012-07-16 17:31:53 +020010253 if (d1->dv_scope != 0)
10254 {
10255 /* Disallow replacing a builtin function in l: and g:.
10256 * Check the key to be valid when adding to any
10257 * scope. */
10258 if (d1->dv_scope == VAR_DEF_SCOPE
10259 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10260 && var_check_func_name(hi2->hi_key,
10261 di1 == NULL))
10262 break;
10263 if (!valid_varname(hi2->hi_key))
10264 break;
10265 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010266 if (di1 == NULL)
10267 {
10268 di1 = dictitem_copy(HI2DI(hi2));
10269 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10270 dictitem_free(di1);
10271 }
10272 else if (*action == 'e')
10273 {
10274 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10275 break;
10276 }
Bram Moolenaar2fc88022012-05-18 12:07:05 +020010277 else if (*action == 'f' && HI2DI(hi2) != di1)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010278 {
10279 clear_tv(&di1->di_tv);
10280 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 }
10283 }
10284
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 copy_tv(&argvars[0], rettv);
10286 }
10287 }
10288 else
10289 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010290}
10291
10292/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010293 * "feedkeys()" function
10294 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010295 static void
10296f_feedkeys(argvars, rettv)
10297 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010298 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010299{
10300 int remap = TRUE;
10301 char_u *keys, *flags;
10302 char_u nbuf[NUMBUFLEN];
10303 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010304 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010305
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010306 /* This is not allowed in the sandbox. If the commands would still be
10307 * executed in the sandbox it would be OK, but it probably happens later,
10308 * when "sandbox" is no longer set. */
10309 if (check_secure())
10310 return;
10311
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010312 keys = get_tv_string(&argvars[0]);
10313 if (*keys != NUL)
10314 {
10315 if (argvars[1].v_type != VAR_UNKNOWN)
10316 {
10317 flags = get_tv_string_buf(&argvars[1], nbuf);
10318 for ( ; *flags != NUL; ++flags)
10319 {
10320 switch (*flags)
10321 {
10322 case 'n': remap = FALSE; break;
10323 case 'm': remap = TRUE; break;
10324 case 't': typed = TRUE; break;
10325 }
10326 }
10327 }
10328
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010329 /* Need to escape K_SPECIAL and CSI before putting the string in the
10330 * typeahead buffer. */
10331 keys_esc = vim_strsave_escape_csi(keys);
10332 if (keys_esc != NULL)
10333 {
10334 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010335 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010336 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010337 if (vgetc_busy)
10338 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010339 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010340 }
10341}
10342
10343/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 * "filereadable()" function
10345 */
10346 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010348 typval_T *argvars;
10349 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010351 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 char_u *p;
10353 int n;
10354
Bram Moolenaarc236c162008-07-13 17:41:49 +000010355#ifndef O_NONBLOCK
10356# define O_NONBLOCK 0
10357#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010359 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10360 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 {
10362 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010363 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 }
10365 else
10366 n = FALSE;
10367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369}
10370
10371/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010372 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 * rights to write into.
10374 */
10375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010380 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381}
10382
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010383static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010384
10385 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010386findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010387 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010388 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010389 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010390{
10391#ifdef FEAT_SEARCHPATH
10392 char_u *fname;
10393 char_u *fresult = NULL;
10394 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10395 char_u *p;
10396 char_u pathbuf[NUMBUFLEN];
10397 int count = 1;
10398 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010399 int error = FALSE;
10400#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010401
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010402 rettv->vval.v_string = NULL;
10403 rettv->v_type = VAR_STRING;
10404
10405#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010408 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010410 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10411 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010412 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010413 else
10414 {
10415 if (*p != NUL)
10416 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010419 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010420 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010421 }
10422
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010423 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10424 error = TRUE;
10425
10426 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 do
10429 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010430 if (rettv->v_type == VAR_STRING)
10431 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 fresult = find_file_in_path_option(first ? fname : NULL,
10433 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010434 0, first, path,
10435 find_what,
10436 curbuf->b_ffname,
10437 find_what == FINDFILE_DIR
10438 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010440
10441 if (fresult != NULL && rettv->v_type == VAR_LIST)
10442 list_append_string(rettv->vval.v_list, fresult, -1);
10443
10444 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010446
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010447 if (rettv->v_type == VAR_STRING)
10448 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010449#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010450}
10451
Bram Moolenaar33570922005-01-25 22:26:29 +000010452static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10453static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010454
10455/*
10456 * Implementation of map() and filter().
10457 */
10458 static void
10459filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010460 typval_T *argvars;
10461 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010462 int map;
10463{
10464 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010466 listitem_T *li, *nli;
10467 list_T *l = NULL;
10468 dictitem_T *di;
10469 hashtab_T *ht;
10470 hashitem_T *hi;
10471 dict_T *d = NULL;
10472 typval_T save_val;
10473 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010474 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010475 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010476 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10477 char *arg_errmsg = (map ? N_("map() argument")
10478 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010479 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010480 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010481
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010484 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010485 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 return;
10487 }
10488 else if (argvars[0].v_type == VAR_DICT)
10489 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010490 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010491 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010492 return;
10493 }
10494 else
10495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010496 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010497 return;
10498 }
10499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010500 expr = get_tv_string_buf_chk(&argvars[1], buf);
10501 /* On type errors, the preceding call has already displayed an error
10502 * message. Avoid a misleading error message for an empty string that
10503 * was not passed as argument. */
10504 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010506 prepare_vimvar(VV_VAL, &save_val);
10507 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010508
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010509 /* We reset "did_emsg" to be able to detect whether an error
10510 * occurred during evaluation of the expression. */
10511 save_did_emsg = did_emsg;
10512 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010513
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010514 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 vimvars[VV_KEY].vv_type = VAR_STRING;
10518
10519 ht = &d->dv_hashtab;
10520 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010521 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 if (!HASHITEM_EMPTY(hi))
10525 {
10526 --todo;
10527 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010528 if (tv_check_lock(di->di_tv.v_lock,
10529 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 break;
10531 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010532 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010533 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 break;
10535 if (!map && rem)
10536 dictitem_remove(d, di);
10537 clear_tv(&vimvars[VV_KEY].vv_tv);
10538 }
10539 }
10540 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 }
10542 else
10543 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010544 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10545
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 for (li = l->lv_first; li != NULL; li = nli)
10547 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010548 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010549 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010550 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010551 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010552 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010553 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010554 break;
10555 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010557 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010558 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010560
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010561 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010563
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010564 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010565 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010566
10567 copy_tv(&argvars[0], rettv);
10568}
10569
10570 static int
10571filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010572 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 char_u *expr;
10574 int map;
10575 int *remp;
10576{
Bram Moolenaar33570922005-01-25 22:26:29 +000010577 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010579 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580
Bram Moolenaar33570922005-01-25 22:26:29 +000010581 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 s = expr;
10583 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010584 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 if (*s != NUL) /* check for trailing chars after expr */
10586 {
10587 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010588 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010589 }
10590 if (map)
10591 {
10592 /* map(): replace the list item value */
10593 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010594 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595 *tv = rettv;
10596 }
10597 else
10598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 int error = FALSE;
10600
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 /* On type error, nothing has been removed; return FAIL to stop the
10605 * loop. The error message was given by get_tv_number_chk(). */
10606 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010607 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 retval = OK;
10610theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010612 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010613}
10614
10615/*
10616 * "filter()" function
10617 */
10618 static void
10619f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010620 typval_T *argvars;
10621 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010622{
10623 filter_map(argvars, rettv, FALSE);
10624}
10625
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010626/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010627 * "finddir({fname}[, {path}[, {count}]])" function
10628 */
10629 static void
10630f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010631 typval_T *argvars;
10632 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010633{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010634 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635}
10636
10637/*
10638 * "findfile({fname}[, {path}[, {count}]])" function
10639 */
10640 static void
10641f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 typval_T *argvars;
10643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010644{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010645 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010646}
10647
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010648#ifdef FEAT_FLOAT
10649/*
10650 * "float2nr({float})" function
10651 */
10652 static void
10653f_float2nr(argvars, rettv)
10654 typval_T *argvars;
10655 typval_T *rettv;
10656{
10657 float_T f;
10658
10659 if (get_float_arg(argvars, &f) == OK)
10660 {
10661 if (f < -0x7fffffff)
10662 rettv->vval.v_number = -0x7fffffff;
10663 else if (f > 0x7fffffff)
10664 rettv->vval.v_number = 0x7fffffff;
10665 else
10666 rettv->vval.v_number = (varnumber_T)f;
10667 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010668}
10669
10670/*
10671 * "floor({float})" function
10672 */
10673 static void
10674f_floor(argvars, rettv)
10675 typval_T *argvars;
10676 typval_T *rettv;
10677{
10678 float_T f;
10679
10680 rettv->v_type = VAR_FLOAT;
10681 if (get_float_arg(argvars, &f) == OK)
10682 rettv->vval.v_float = floor(f);
10683 else
10684 rettv->vval.v_float = 0.0;
10685}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010686
10687/*
10688 * "fmod()" function
10689 */
10690 static void
10691f_fmod(argvars, rettv)
10692 typval_T *argvars;
10693 typval_T *rettv;
10694{
10695 float_T fx, fy;
10696
10697 rettv->v_type = VAR_FLOAT;
10698 if (get_float_arg(argvars, &fx) == OK
10699 && get_float_arg(&argvars[1], &fy) == OK)
10700 rettv->vval.v_float = fmod(fx, fy);
10701 else
10702 rettv->vval.v_float = 0.0;
10703}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010704#endif
10705
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010707 * "fnameescape({string})" function
10708 */
10709 static void
10710f_fnameescape(argvars, rettv)
10711 typval_T *argvars;
10712 typval_T *rettv;
10713{
10714 rettv->vval.v_string = vim_strsave_fnameescape(
10715 get_tv_string(&argvars[0]), FALSE);
10716 rettv->v_type = VAR_STRING;
10717}
10718
10719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 * "fnamemodify({fname}, {mods})" function
10721 */
10722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010724 typval_T *argvars;
10725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726{
10727 char_u *fname;
10728 char_u *mods;
10729 int usedlen = 0;
10730 int len;
10731 char_u *fbuf = NULL;
10732 char_u buf[NUMBUFLEN];
10733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 fname = get_tv_string_chk(&argvars[0]);
10735 mods = get_tv_string_buf_chk(&argvars[1], buf);
10736 if (fname == NULL || mods == NULL)
10737 fname = NULL;
10738 else
10739 {
10740 len = (int)STRLEN(fname);
10741 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 vim_free(fbuf);
10750}
10751
Bram Moolenaar33570922005-01-25 22:26:29 +000010752static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
10754/*
10755 * "foldclosed()" function
10756 */
10757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010758foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010760 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010761 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762{
10763#ifdef FEAT_FOLDING
10764 linenr_T lnum;
10765 linenr_T first, last;
10766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10769 {
10770 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10771 {
10772 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 return;
10777 }
10778 }
10779#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781}
10782
10783/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010784 * "foldclosed()" function
10785 */
10786 static void
10787f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010790{
10791 foldclosed_both(argvars, rettv, FALSE);
10792}
10793
10794/*
10795 * "foldclosedend()" function
10796 */
10797 static void
10798f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010799 typval_T *argvars;
10800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010801{
10802 foldclosed_both(argvars, rettv, TRUE);
10803}
10804
10805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 * "foldlevel()" function
10807 */
10808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010810 typval_T *argvars UNUSED;
10811 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812{
10813#ifdef FEAT_FOLDING
10814 linenr_T lnum;
10815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820}
10821
10822/*
10823 * "foldtext()" function
10824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010827 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829{
10830#ifdef FEAT_FOLDING
10831 linenr_T lnum;
10832 char_u *s;
10833 char_u *r;
10834 int len;
10835 char *txt;
10836#endif
10837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838 rettv->v_type = VAR_STRING;
10839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010841 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10842 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10843 <= curbuf->b_ml.ml_line_count
10844 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 {
10846 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010847 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10848 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 {
10850 if (!linewhite(lnum))
10851 break;
10852 ++lnum;
10853 }
10854
10855 /* Find interesting text in this line. */
10856 s = skipwhite(ml_get(lnum));
10857 /* skip C comment-start */
10858 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010861 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010862 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010863 {
10864 s = skipwhite(ml_get(lnum + 1));
10865 if (*s == '*')
10866 s = skipwhite(s + 1);
10867 }
10868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 txt = _("+-%s%3ld lines: ");
10870 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 + 20 /* for %3ld */
10873 + STRLEN(s))); /* concatenated */
10874 if (r != NULL)
10875 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10877 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10878 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 len = (int)STRLEN(r);
10880 STRCAT(r, s);
10881 /* remove 'foldmarker' and 'commentstring' */
10882 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
10885 }
10886#endif
10887}
10888
10889/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010890 * "foldtextresult(lnum)" function
10891 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010893f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010894 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010895 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010896{
10897#ifdef FEAT_FOLDING
10898 linenr_T lnum;
10899 char_u *text;
10900 char_u buf[51];
10901 foldinfo_T foldinfo;
10902 int fold_count;
10903#endif
10904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 rettv->v_type = VAR_STRING;
10906 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010907#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 /* treat illegal types and illegal string values for {lnum} the same */
10910 if (lnum < 0)
10911 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010912 fold_count = foldedCount(curwin, lnum, &foldinfo);
10913 if (fold_count > 0)
10914 {
10915 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10916 &foldinfo, buf);
10917 if (text == buf)
10918 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010919 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010920 }
10921#endif
10922}
10923
10924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 * "foreground()" function
10926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010929 typval_T *argvars UNUSED;
10930 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#ifdef FEAT_GUI
10933 if (gui.in_use)
10934 gui_mch_set_foreground();
10935#else
10936# ifdef WIN32
10937 win32_set_foreground();
10938# endif
10939#endif
10940}
10941
10942/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943 * "function()" function
10944 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010947 typval_T *argvars;
10948 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010949{
10950 char_u *s;
10951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010952 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010953 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010954 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010955 /* Don't check an autoload name for existence here. */
10956 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010957 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010958 else
10959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 rettv->vval.v_string = vim_strsave(s);
10961 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010962 }
10963}
10964
10965/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010966 * "garbagecollect()" function
10967 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010968 static void
10969f_garbagecollect(argvars, rettv)
10970 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010971 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010972{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010973 /* This is postponed until we are back at the toplevel, because we may be
10974 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10975 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010976
10977 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10978 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010979}
10980
10981/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010982 * "get()" function
10983 */
10984 static void
10985f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 typval_T *argvars;
10987 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010988{
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 listitem_T *li;
10990 list_T *l;
10991 dictitem_T *di;
10992 dict_T *d;
10993 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994
Bram Moolenaare9a41262005-01-15 22:18:47 +000010995 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010996 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010999 int error = FALSE;
11000
11001 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11002 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011004 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 else if (argvars[0].v_type == VAR_DICT)
11007 {
11008 if ((d = argvars[0].vval.v_dict) != NULL)
11009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011010 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011011 if (di != NULL)
11012 tv = &di->di_tv;
11013 }
11014 }
11015 else
11016 EMSG2(_(e_listdictarg), "get()");
11017
11018 if (tv == NULL)
11019 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011020 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011021 copy_tv(&argvars[2], rettv);
11022 }
11023 else
11024 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011025}
11026
Bram Moolenaar342337a2005-07-21 21:11:17 +000011027static 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 +000011028
11029/*
11030 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011031 * Return a range (from start to end) of lines in rettv from the specified
11032 * buffer.
11033 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011034 */
11035 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011036get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011037 buf_T *buf;
11038 linenr_T start;
11039 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011040 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011041 typval_T *rettv;
11042{
11043 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011044
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011045 if (retlist && rettv_list_alloc(rettv) == FAIL)
11046 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011047
11048 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11049 return;
11050
11051 if (!retlist)
11052 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011053 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11054 p = ml_get_buf(buf, start, FALSE);
11055 else
11056 p = (char_u *)"";
11057
11058 rettv->v_type = VAR_STRING;
11059 rettv->vval.v_string = vim_strsave(p);
11060 }
11061 else
11062 {
11063 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011064 return;
11065
11066 if (start < 1)
11067 start = 1;
11068 if (end > buf->b_ml.ml_line_count)
11069 end = buf->b_ml.ml_line_count;
11070 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011071 if (list_append_string(rettv->vval.v_list,
11072 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011073 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011074 }
11075}
11076
11077/*
11078 * "getbufline()" function
11079 */
11080 static void
11081f_getbufline(argvars, rettv)
11082 typval_T *argvars;
11083 typval_T *rettv;
11084{
11085 linenr_T lnum;
11086 linenr_T end;
11087 buf_T *buf;
11088
11089 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11090 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011091 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011092 --emsg_off;
11093
Bram Moolenaar661b1822005-07-28 22:36:45 +000011094 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011095 if (argvars[2].v_type == VAR_UNKNOWN)
11096 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011097 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011098 end = get_tv_lnum_buf(&argvars[2], buf);
11099
Bram Moolenaar342337a2005-07-21 21:11:17 +000011100 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011101}
11102
Bram Moolenaar0d660222005-01-07 21:51:51 +000011103/*
11104 * "getbufvar()" function
11105 */
11106 static void
11107f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011108 typval_T *argvars;
11109 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011110{
11111 buf_T *buf;
11112 buf_T *save_curbuf;
11113 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011114 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011115 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011117 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11118 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011119 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011120 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011121
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011122 rettv->v_type = VAR_STRING;
11123 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011124
11125 if (buf != NULL && varname != NULL)
11126 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011127 /* set curbuf to be our buf, temporarily */
11128 save_curbuf = curbuf;
11129 curbuf = buf;
11130
Bram Moolenaar0d660222005-01-07 21:51:51 +000011131 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011132 {
11133 if (get_option_tv(&varname, rettv, TRUE) == OK)
11134 done = TRUE;
11135 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011136 else if (STRCMP(varname, "changedtick") == 0)
11137 {
11138 rettv->v_type = VAR_NUMBER;
11139 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011140 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011142 else
11143 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011144 /* Look up the variable. */
11145 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11146 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11147 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011148 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011150 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011151 done = TRUE;
11152 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011153 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011154
11155 /* restore previous notion of curbuf */
11156 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011157 }
11158
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011159 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11160 /* use the default value */
11161 copy_tv(&argvars[2], rettv);
11162
Bram Moolenaar0d660222005-01-07 21:51:51 +000011163 --emsg_off;
11164}
11165
11166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 * "getchar()" function
11168 */
11169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011170f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011171 typval_T *argvars;
11172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173{
11174 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011175 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011177 /* Position the cursor. Needed after a message that ends in a space. */
11178 windgoto(msg_row, msg_col);
11179
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 ++no_mapping;
11181 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011182 for (;;)
11183 {
11184 if (argvars[0].v_type == VAR_UNKNOWN)
11185 /* getchar(): blocking wait. */
11186 n = safe_vgetc();
11187 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11188 /* getchar(1): only check if char avail */
11189 n = vpeekc();
11190 else if (error || vpeekc() == NUL)
11191 /* illegal argument or getchar(0) and no char avail: return zero */
11192 n = 0;
11193 else
11194 /* getchar(0) and char avail: return char */
11195 n = safe_vgetc();
11196 if (n == K_IGNORE)
11197 continue;
11198 break;
11199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 --no_mapping;
11201 --allow_keys;
11202
Bram Moolenaar219b8702006-11-01 14:32:36 +000011203 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11204 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11205 vimvars[VV_MOUSE_COL].vv_nr = 0;
11206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 if (IS_SPECIAL(n) || mod_mask != 0)
11209 {
11210 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11211 int i = 0;
11212
11213 /* Turn a special key into three bytes, plus modifier. */
11214 if (mod_mask != 0)
11215 {
11216 temp[i++] = K_SPECIAL;
11217 temp[i++] = KS_MODIFIER;
11218 temp[i++] = mod_mask;
11219 }
11220 if (IS_SPECIAL(n))
11221 {
11222 temp[i++] = K_SPECIAL;
11223 temp[i++] = K_SECOND(n);
11224 temp[i++] = K_THIRD(n);
11225 }
11226#ifdef FEAT_MBYTE
11227 else if (has_mbyte)
11228 i += (*mb_char2bytes)(n, temp + i);
11229#endif
11230 else
11231 temp[i++] = n;
11232 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233 rettv->v_type = VAR_STRING;
11234 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011235
11236#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011237 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011238 {
11239 int row = mouse_row;
11240 int col = mouse_col;
11241 win_T *win;
11242 linenr_T lnum;
11243# ifdef FEAT_WINDOWS
11244 win_T *wp;
11245# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011246 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011247
11248 if (row >= 0 && col >= 0)
11249 {
11250 /* Find the window at the mouse coordinates and compute the
11251 * text position. */
11252 win = mouse_find_win(&row, &col);
11253 (void)mouse_comp_pos(win, &row, &col, &lnum);
11254# ifdef FEAT_WINDOWS
11255 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011256 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011257# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011258 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011259 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11260 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11261 }
11262 }
11263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 }
11265}
11266
11267/*
11268 * "getcharmod()" function
11269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011271f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011272 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
11279 * "getcmdline()" function
11280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011283 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 rettv->v_type = VAR_STRING;
11287 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288}
11289
11290/*
11291 * "getcmdpos()" function
11292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011302 * "getcmdtype()" function
11303 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011304 static void
11305f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011306 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011307 typval_T *rettv;
11308{
11309 rettv->v_type = VAR_STRING;
11310 rettv->vval.v_string = alloc(2);
11311 if (rettv->vval.v_string != NULL)
11312 {
11313 rettv->vval.v_string[0] = get_cmdline_type();
11314 rettv->vval.v_string[1] = NUL;
11315 }
11316}
11317
11318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 * "getcwd()" function
11320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011326 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011329 rettv->vval.v_string = NULL;
11330 cwd = alloc(MAXPATHL);
11331 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011333 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11334 {
11335 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011337 if (rettv->vval.v_string != NULL)
11338 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011340 }
11341 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343}
11344
11345/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011346 * "getfontname()" function
11347 */
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011350 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011351 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011352{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 rettv->v_type = VAR_STRING;
11354 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011355#ifdef FEAT_GUI
11356 if (gui.in_use)
11357 {
11358 GuiFont font;
11359 char_u *name = NULL;
11360
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011361 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011362 {
11363 /* Get the "Normal" font. Either the name saved by
11364 * hl_set_font_name() or from the font ID. */
11365 font = gui.norm_font;
11366 name = hl_get_font_name();
11367 }
11368 else
11369 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011371 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11372 return;
11373 font = gui_mch_get_font(name, FALSE);
11374 if (font == NOFONT)
11375 return; /* Invalid font name, return empty string. */
11376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011379 gui_mch_free_font(font);
11380 }
11381#endif
11382}
11383
11384/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011385 * "getfperm({fname})" function
11386 */
11387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011389 typval_T *argvars;
11390 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011391{
11392 char_u *fname;
11393 struct stat st;
11394 char_u *perm = NULL;
11395 char_u flags[] = "rwx";
11396 int i;
11397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011401 if (mch_stat((char *)fname, &st) >= 0)
11402 {
11403 perm = vim_strsave((char_u *)"---------");
11404 if (perm != NULL)
11405 {
11406 for (i = 0; i < 9; i++)
11407 {
11408 if (st.st_mode & (1 << (8 - i)))
11409 perm[i] = flags[i % 3];
11410 }
11411 }
11412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011414}
11415
11416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 * "getfsize({fname})" function
11418 */
11419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011421 typval_T *argvars;
11422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424 char_u *fname;
11425 struct stat st;
11426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011429 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
11431 if (mch_stat((char *)fname, &st) >= 0)
11432 {
11433 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011436 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011438
11439 /* non-perfect check for overflow */
11440 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11441 rettv->vval.v_number = -2;
11442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 }
11444 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446}
11447
11448/*
11449 * "getftime({fname})" function
11450 */
11451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011453 typval_T *argvars;
11454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
11456 char_u *fname;
11457 struct stat st;
11458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460
11461 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465}
11466
11467/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011468 * "getftype({fname})" function
11469 */
11470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011472 typval_T *argvars;
11473 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011474{
11475 char_u *fname;
11476 struct stat st;
11477 char_u *type = NULL;
11478 char *t;
11479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011483 if (mch_lstat((char *)fname, &st) >= 0)
11484 {
11485#ifdef S_ISREG
11486 if (S_ISREG(st.st_mode))
11487 t = "file";
11488 else if (S_ISDIR(st.st_mode))
11489 t = "dir";
11490# ifdef S_ISLNK
11491 else if (S_ISLNK(st.st_mode))
11492 t = "link";
11493# endif
11494# ifdef S_ISBLK
11495 else if (S_ISBLK(st.st_mode))
11496 t = "bdev";
11497# endif
11498# ifdef S_ISCHR
11499 else if (S_ISCHR(st.st_mode))
11500 t = "cdev";
11501# endif
11502# ifdef S_ISFIFO
11503 else if (S_ISFIFO(st.st_mode))
11504 t = "fifo";
11505# endif
11506# ifdef S_ISSOCK
11507 else if (S_ISSOCK(st.st_mode))
11508 t = "fifo";
11509# endif
11510 else
11511 t = "other";
11512#else
11513# ifdef S_IFMT
11514 switch (st.st_mode & S_IFMT)
11515 {
11516 case S_IFREG: t = "file"; break;
11517 case S_IFDIR: t = "dir"; break;
11518# ifdef S_IFLNK
11519 case S_IFLNK: t = "link"; break;
11520# endif
11521# ifdef S_IFBLK
11522 case S_IFBLK: t = "bdev"; break;
11523# endif
11524# ifdef S_IFCHR
11525 case S_IFCHR: t = "cdev"; break;
11526# endif
11527# ifdef S_IFIFO
11528 case S_IFIFO: t = "fifo"; break;
11529# endif
11530# ifdef S_IFSOCK
11531 case S_IFSOCK: t = "socket"; break;
11532# endif
11533 default: t = "other";
11534 }
11535# else
11536 if (mch_isdir(fname))
11537 t = "dir";
11538 else
11539 t = "file";
11540# endif
11541#endif
11542 type = vim_strsave((char_u *)t);
11543 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011544 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011545}
11546
11547/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011548 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549 */
11550 static void
11551f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 typval_T *argvars;
11553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554{
11555 linenr_T lnum;
11556 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011557 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558
11559 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011560 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011561 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011562 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011563 retlist = FALSE;
11564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011565 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011568 retlist = TRUE;
11569 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011570
Bram Moolenaar342337a2005-07-21 21:11:17 +000011571 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011572}
11573
11574/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011575 * "getmatches()" function
11576 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011577 static void
11578f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011579 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011580 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011581{
11582#ifdef FEAT_SEARCH_EXTRA
11583 dict_T *dict;
11584 matchitem_T *cur = curwin->w_match_head;
11585
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011586 if (rettv_list_alloc(rettv) == OK)
11587 {
11588 while (cur != NULL)
11589 {
11590 dict = dict_alloc();
11591 if (dict == NULL)
11592 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011593 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11594 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11595 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11596 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11597 list_append_dict(rettv->vval.v_list, dict);
11598 cur = cur->next;
11599 }
11600 }
11601#endif
11602}
11603
11604/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011605 * "getpid()" function
11606 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011607 static void
11608f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011609 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011610 typval_T *rettv;
11611{
11612 rettv->vval.v_number = mch_get_pid();
11613}
11614
11615/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011616 * "getpos(string)" function
11617 */
11618 static void
11619f_getpos(argvars, rettv)
11620 typval_T *argvars;
11621 typval_T *rettv;
11622{
11623 pos_T *fp;
11624 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011625 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011626
11627 if (rettv_list_alloc(rettv) == OK)
11628 {
11629 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011630 fp = var2fpos(&argvars[0], TRUE, &fnum);
11631 if (fnum != -1)
11632 list_append_number(l, (varnumber_T)fnum);
11633 else
11634 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011635 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11636 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011637 list_append_number(l, (fp != NULL)
11638 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011639 : (varnumber_T)0);
11640 list_append_number(l,
11641#ifdef FEAT_VIRTUALEDIT
11642 (fp != NULL) ? (varnumber_T)fp->coladd :
11643#endif
11644 (varnumber_T)0);
11645 }
11646 else
11647 rettv->vval.v_number = FALSE;
11648}
11649
11650/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011651 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011652 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011653 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011654f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011655 typval_T *argvars UNUSED;
11656 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011657{
11658#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011659 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011660#endif
11661
Bram Moolenaar2641f772005-03-25 21:58:17 +000011662#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011663 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011664 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011665 wp = NULL;
11666 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11667 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011668 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011669 if (wp == NULL)
11670 return;
11671 }
11672
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011673 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011674 }
11675#endif
11676}
11677
11678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 * "getreg()" function
11680 */
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 char_u *strregname;
11687 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011688 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011691 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 strregname = get_tv_string_chk(&argvars[0]);
11694 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011696 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011698 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 regname = (strregname == NULL ? '"' : *strregname);
11701 if (regname == 0)
11702 regname = '"';
11703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 rettv->vval.v_string = error ? NULL :
11706 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707}
11708
11709/*
11710 * "getregtype()" function
11711 */
11712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011713f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011714 typval_T *argvars;
11715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716{
11717 char_u *strregname;
11718 int regname;
11719 char_u buf[NUMBUFLEN + 2];
11720 long reglen = 0;
11721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011722 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011723 {
11724 strregname = get_tv_string_chk(&argvars[0]);
11725 if (strregname == NULL) /* type error; errmsg already given */
11726 {
11727 rettv->v_type = VAR_STRING;
11728 rettv->vval.v_string = NULL;
11729 return;
11730 }
11731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 else
11733 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735
11736 regname = (strregname == NULL ? '"' : *strregname);
11737 if (regname == 0)
11738 regname = '"';
11739
11740 buf[0] = NUL;
11741 buf[1] = NUL;
11742 switch (get_reg_type(regname, &reglen))
11743 {
11744 case MLINE: buf[0] = 'V'; break;
11745 case MCHAR: buf[0] = 'v'; break;
11746#ifdef FEAT_VISUAL
11747 case MBLOCK:
11748 buf[0] = Ctrl_V;
11749 sprintf((char *)buf + 1, "%ld", reglen + 1);
11750 break;
11751#endif
11752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755}
11756
11757/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011758 * "gettabvar()" function
11759 */
11760 static void
11761f_gettabvar(argvars, rettv)
11762 typval_T *argvars;
11763 typval_T *rettv;
11764{
11765 tabpage_T *tp;
11766 dictitem_T *v;
11767 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011768 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011769
11770 rettv->v_type = VAR_STRING;
11771 rettv->vval.v_string = NULL;
11772
11773 varname = get_tv_string_chk(&argvars[1]);
11774 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11775 if (tp != NULL && varname != NULL)
11776 {
11777 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011778 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011779 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011780 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011781 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011782 done = TRUE;
11783 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011784 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011785
11786 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011787 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011788}
11789
11790/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011791 * "gettabwinvar()" function
11792 */
11793 static void
11794f_gettabwinvar(argvars, rettv)
11795 typval_T *argvars;
11796 typval_T *rettv;
11797{
11798 getwinvar(argvars, rettv, 1);
11799}
11800
11801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802 * "getwinposx()" function
11803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011806 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810#ifdef FEAT_GUI
11811 if (gui.in_use)
11812 {
11813 int x, y;
11814
11815 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 }
11818#endif
11819}
11820
11821/*
11822 * "getwinposy()" function
11823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011826 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011829 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830#ifdef FEAT_GUI
11831 if (gui.in_use)
11832 {
11833 int x, y;
11834
11835 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 }
11838#endif
11839}
11840
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011841/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011842 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011843 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011844 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011845find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011846 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011847 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011848{
11849#ifdef FEAT_WINDOWS
11850 win_T *wp;
11851#endif
11852 int nr;
11853
11854 nr = get_tv_number_chk(vp, NULL);
11855
11856#ifdef FEAT_WINDOWS
11857 if (nr < 0)
11858 return NULL;
11859 if (nr == 0)
11860 return curwin;
11861
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011862 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11863 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011864 if (--nr <= 0)
11865 break;
11866 return wp;
11867#else
11868 if (nr == 0 || nr == 1)
11869 return curwin;
11870 return NULL;
11871#endif
11872}
11873
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874/*
11875 * "getwinvar()" function
11876 */
11877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011879 typval_T *argvars;
11880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011882 getwinvar(argvars, rettv, 0);
11883}
11884
11885/*
11886 * getwinvar() and gettabwinvar()
11887 */
11888 static void
11889getwinvar(argvars, rettv, off)
11890 typval_T *argvars;
11891 typval_T *rettv;
11892 int off; /* 1 for gettabwinvar() */
11893{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 win_T *win, *oldcurwin;
11895 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011897 tabpage_T *tp;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011898 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011900#ifdef FEAT_WINDOWS
11901 if (off == 1)
11902 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11903 else
11904 tp = curtab;
11905#endif
11906 win = find_win_by_nr(&argvars[off], tp);
11907 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011910 rettv->v_type = VAR_STRING;
11911 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912
11913 if (win != NULL && varname != NULL)
11914 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011915 /* Set curwin to be our win, temporarily. Also set curbuf, so
11916 * that we can get buffer-local options. */
11917 oldcurwin = curwin;
11918 curwin = win;
11919 curbuf = win->w_buffer;
11920
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011922 {
11923 if (get_option_tv(&varname, rettv, 1) == OK)
11924 done = TRUE;
11925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 else
11927 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011928 /* Look up the variable. */
11929 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11930 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011932 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011933 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011934 done = TRUE;
11935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011937
11938 /* restore previous notion of curwin */
11939 curwin = oldcurwin;
11940 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 }
11942
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011943 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11944 /* use the default return value */
11945 copy_tv(&argvars[off + 2], rettv);
11946
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947 --emsg_off;
11948}
11949
11950/*
11951 * "glob()" function
11952 */
11953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011954f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011955 typval_T *argvars;
11956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011957{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011958 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011960 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011962 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011963 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011965 if (argvars[1].v_type != VAR_UNKNOWN)
11966 {
11967 if (get_tv_number_chk(&argvars[1], &error))
11968 options |= WILD_KEEP_ALL;
11969 if (argvars[2].v_type != VAR_UNKNOWN
11970 && get_tv_number_chk(&argvars[2], &error))
11971 {
11972 rettv->v_type = VAR_LIST;
11973 rettv->vval.v_list = NULL;
11974 }
11975 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011976 if (!error)
11977 {
11978 ExpandInit(&xpc);
11979 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011980 if (p_wic)
11981 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011982 if (rettv->v_type == VAR_STRING)
11983 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011984 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011985 else if (rettv_list_alloc(rettv) != FAIL)
11986 {
11987 int i;
11988
11989 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11990 NULL, options, WILD_ALL_KEEP);
11991 for (i = 0; i < xpc.xp_numfiles; i++)
11992 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11993
11994 ExpandCleanup(&xpc);
11995 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011996 }
11997 else
11998 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999}
12000
12001/*
12002 * "globpath()" function
12003 */
12004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012006 typval_T *argvars;
12007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012012 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012014 /* When the optional second argument is non-zero, don't remove matches
12015 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12016 if (argvars[2].v_type != VAR_UNKNOWN
12017 && get_tv_number_chk(&argvars[2], &error))
12018 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012020 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012021 rettv->vval.v_string = NULL;
12022 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012023 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12024 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012025}
12026
12027/*
12028 * "has()" function
12029 */
12030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 typval_T *argvars;
12033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034{
12035 int i;
12036 char_u *name;
12037 int n = FALSE;
12038 static char *(has_list[]) =
12039 {
12040#ifdef AMIGA
12041 "amiga",
12042# ifdef FEAT_ARP
12043 "arp",
12044# endif
12045#endif
12046#ifdef __BEOS__
12047 "beos",
12048#endif
12049#ifdef MSDOS
12050# ifdef DJGPP
12051 "dos32",
12052# else
12053 "dos16",
12054# endif
12055#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012056#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 "mac",
12058#endif
12059#if defined(MACOS_X_UNIX)
12060 "macunix",
12061#endif
12062#ifdef OS2
12063 "os2",
12064#endif
12065#ifdef __QNX__
12066 "qnx",
12067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068#ifdef UNIX
12069 "unix",
12070#endif
12071#ifdef VMS
12072 "vms",
12073#endif
12074#ifdef WIN16
12075 "win16",
12076#endif
12077#ifdef WIN32
12078 "win32",
12079#endif
12080#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12081 "win32unix",
12082#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012083#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 "win64",
12085#endif
12086#ifdef EBCDIC
12087 "ebcdic",
12088#endif
12089#ifndef CASE_INSENSITIVE_FILENAME
12090 "fname_case",
12091#endif
12092#ifdef FEAT_ARABIC
12093 "arabic",
12094#endif
12095#ifdef FEAT_AUTOCMD
12096 "autocmd",
12097#endif
12098#ifdef FEAT_BEVAL
12099 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012100# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12101 "balloon_multiline",
12102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#endif
12104#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12105 "builtin_terms",
12106# ifdef ALL_BUILTIN_TCAPS
12107 "all_builtin_terms",
12108# endif
12109#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012110#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12111 || defined(FEAT_GUI_W32) \
12112 || defined(FEAT_GUI_MOTIF))
12113 "browsefilter",
12114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef FEAT_BYTEOFF
12116 "byte_offset",
12117#endif
12118#ifdef FEAT_CINDENT
12119 "cindent",
12120#endif
12121#ifdef FEAT_CLIENTSERVER
12122 "clientserver",
12123#endif
12124#ifdef FEAT_CLIPBOARD
12125 "clipboard",
12126#endif
12127#ifdef FEAT_CMDL_COMPL
12128 "cmdline_compl",
12129#endif
12130#ifdef FEAT_CMDHIST
12131 "cmdline_hist",
12132#endif
12133#ifdef FEAT_COMMENTS
12134 "comments",
12135#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012136#ifdef FEAT_CONCEAL
12137 "conceal",
12138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139#ifdef FEAT_CRYPT
12140 "cryptv",
12141#endif
12142#ifdef FEAT_CSCOPE
12143 "cscope",
12144#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012145#ifdef FEAT_CURSORBIND
12146 "cursorbind",
12147#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012148#ifdef CURSOR_SHAPE
12149 "cursorshape",
12150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151#ifdef DEBUG
12152 "debug",
12153#endif
12154#ifdef FEAT_CON_DIALOG
12155 "dialog_con",
12156#endif
12157#ifdef FEAT_GUI_DIALOG
12158 "dialog_gui",
12159#endif
12160#ifdef FEAT_DIFF
12161 "diff",
12162#endif
12163#ifdef FEAT_DIGRAPHS
12164 "digraphs",
12165#endif
12166#ifdef FEAT_DND
12167 "dnd",
12168#endif
12169#ifdef FEAT_EMACS_TAGS
12170 "emacs_tags",
12171#endif
12172 "eval", /* always present, of course! */
12173#ifdef FEAT_EX_EXTRA
12174 "ex_extra",
12175#endif
12176#ifdef FEAT_SEARCH_EXTRA
12177 "extra_search",
12178#endif
12179#ifdef FEAT_FKMAP
12180 "farsi",
12181#endif
12182#ifdef FEAT_SEARCHPATH
12183 "file_in_path",
12184#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012185#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012186 "filterpipe",
12187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#ifdef FEAT_FIND_ID
12189 "find_in_path",
12190#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012191#ifdef FEAT_FLOAT
12192 "float",
12193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194#ifdef FEAT_FOLDING
12195 "folding",
12196#endif
12197#ifdef FEAT_FOOTER
12198 "footer",
12199#endif
12200#if !defined(USE_SYSTEM) && defined(UNIX)
12201 "fork",
12202#endif
12203#ifdef FEAT_GETTEXT
12204 "gettext",
12205#endif
12206#ifdef FEAT_GUI
12207 "gui",
12208#endif
12209#ifdef FEAT_GUI_ATHENA
12210# ifdef FEAT_GUI_NEXTAW
12211 "gui_neXtaw",
12212# else
12213 "gui_athena",
12214# endif
12215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216#ifdef FEAT_GUI_GTK
12217 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012220#ifdef FEAT_GUI_GNOME
12221 "gui_gnome",
12222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223#ifdef FEAT_GUI_MAC
12224 "gui_mac",
12225#endif
12226#ifdef FEAT_GUI_MOTIF
12227 "gui_motif",
12228#endif
12229#ifdef FEAT_GUI_PHOTON
12230 "gui_photon",
12231#endif
12232#ifdef FEAT_GUI_W16
12233 "gui_win16",
12234#endif
12235#ifdef FEAT_GUI_W32
12236 "gui_win32",
12237#endif
12238#ifdef FEAT_HANGULIN
12239 "hangul_input",
12240#endif
12241#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12242 "iconv",
12243#endif
12244#ifdef FEAT_INS_EXPAND
12245 "insert_expand",
12246#endif
12247#ifdef FEAT_JUMPLIST
12248 "jumplist",
12249#endif
12250#ifdef FEAT_KEYMAP
12251 "keymap",
12252#endif
12253#ifdef FEAT_LANGMAP
12254 "langmap",
12255#endif
12256#ifdef FEAT_LIBCALL
12257 "libcall",
12258#endif
12259#ifdef FEAT_LINEBREAK
12260 "linebreak",
12261#endif
12262#ifdef FEAT_LISP
12263 "lispindent",
12264#endif
12265#ifdef FEAT_LISTCMDS
12266 "listcmds",
12267#endif
12268#ifdef FEAT_LOCALMAP
12269 "localmap",
12270#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012271#ifdef FEAT_LUA
12272# ifndef DYNAMIC_LUA
12273 "lua",
12274# endif
12275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276#ifdef FEAT_MENU
12277 "menu",
12278#endif
12279#ifdef FEAT_SESSION
12280 "mksession",
12281#endif
12282#ifdef FEAT_MODIFY_FNAME
12283 "modify_fname",
12284#endif
12285#ifdef FEAT_MOUSE
12286 "mouse",
12287#endif
12288#ifdef FEAT_MOUSESHAPE
12289 "mouseshape",
12290#endif
12291#if defined(UNIX) || defined(VMS)
12292# ifdef FEAT_MOUSE_DEC
12293 "mouse_dec",
12294# endif
12295# ifdef FEAT_MOUSE_GPM
12296 "mouse_gpm",
12297# endif
12298# ifdef FEAT_MOUSE_JSB
12299 "mouse_jsbterm",
12300# endif
12301# ifdef FEAT_MOUSE_NET
12302 "mouse_netterm",
12303# endif
12304# ifdef FEAT_MOUSE_PTERM
12305 "mouse_pterm",
12306# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012307# ifdef FEAT_MOUSE_SGR
12308 "mouse_sgr",
12309# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012310# ifdef FEAT_SYSMOUSE
12311 "mouse_sysmouse",
12312# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012313# ifdef FEAT_MOUSE_URXVT
12314 "mouse_urxvt",
12315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316# ifdef FEAT_MOUSE_XTERM
12317 "mouse_xterm",
12318# endif
12319#endif
12320#ifdef FEAT_MBYTE
12321 "multi_byte",
12322#endif
12323#ifdef FEAT_MBYTE_IME
12324 "multi_byte_ime",
12325#endif
12326#ifdef FEAT_MULTI_LANG
12327 "multi_lang",
12328#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012329#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012330#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012331 "mzscheme",
12332#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_OLE
12335 "ole",
12336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337#ifdef FEAT_PATH_EXTRA
12338 "path_extra",
12339#endif
12340#ifdef FEAT_PERL
12341#ifndef DYNAMIC_PERL
12342 "perl",
12343#endif
12344#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012345#ifdef FEAT_PERSISTENT_UNDO
12346 "persistent_undo",
12347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348#ifdef FEAT_PYTHON
12349#ifndef DYNAMIC_PYTHON
12350 "python",
12351#endif
12352#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012353#ifdef FEAT_PYTHON3
12354#ifndef DYNAMIC_PYTHON3
12355 "python3",
12356#endif
12357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358#ifdef FEAT_POSTSCRIPT
12359 "postscript",
12360#endif
12361#ifdef FEAT_PRINTER
12362 "printer",
12363#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012364#ifdef FEAT_PROFILE
12365 "profile",
12366#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012367#ifdef FEAT_RELTIME
12368 "reltime",
12369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370#ifdef FEAT_QUICKFIX
12371 "quickfix",
12372#endif
12373#ifdef FEAT_RIGHTLEFT
12374 "rightleft",
12375#endif
12376#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12377 "ruby",
12378#endif
12379#ifdef FEAT_SCROLLBIND
12380 "scrollbind",
12381#endif
12382#ifdef FEAT_CMDL_INFO
12383 "showcmd",
12384 "cmdline_info",
12385#endif
12386#ifdef FEAT_SIGNS
12387 "signs",
12388#endif
12389#ifdef FEAT_SMARTINDENT
12390 "smartindent",
12391#endif
12392#ifdef FEAT_SNIFF
12393 "sniff",
12394#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012395#ifdef STARTUPTIME
12396 "startuptime",
12397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#ifdef FEAT_STL_OPT
12399 "statusline",
12400#endif
12401#ifdef FEAT_SUN_WORKSHOP
12402 "sun_workshop",
12403#endif
12404#ifdef FEAT_NETBEANS_INTG
12405 "netbeans_intg",
12406#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012407#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012408 "spell",
12409#endif
12410#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411 "syntax",
12412#endif
12413#if defined(USE_SYSTEM) || !defined(UNIX)
12414 "system",
12415#endif
12416#ifdef FEAT_TAG_BINS
12417 "tag_binary",
12418#endif
12419#ifdef FEAT_TAG_OLDSTATIC
12420 "tag_old_static",
12421#endif
12422#ifdef FEAT_TAG_ANYWHITE
12423 "tag_any_white",
12424#endif
12425#ifdef FEAT_TCL
12426# ifndef DYNAMIC_TCL
12427 "tcl",
12428# endif
12429#endif
12430#ifdef TERMINFO
12431 "terminfo",
12432#endif
12433#ifdef FEAT_TERMRESPONSE
12434 "termresponse",
12435#endif
12436#ifdef FEAT_TEXTOBJ
12437 "textobjects",
12438#endif
12439#ifdef HAVE_TGETENT
12440 "tgetent",
12441#endif
12442#ifdef FEAT_TITLE
12443 "title",
12444#endif
12445#ifdef FEAT_TOOLBAR
12446 "toolbar",
12447#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012448#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12449 "unnamedplus",
12450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451#ifdef FEAT_USR_CMDS
12452 "user-commands", /* was accidentally included in 5.4 */
12453 "user_commands",
12454#endif
12455#ifdef FEAT_VIMINFO
12456 "viminfo",
12457#endif
12458#ifdef FEAT_VERTSPLIT
12459 "vertsplit",
12460#endif
12461#ifdef FEAT_VIRTUALEDIT
12462 "virtualedit",
12463#endif
12464#ifdef FEAT_VISUAL
12465 "visual",
12466#endif
12467#ifdef FEAT_VISUALEXTRA
12468 "visualextra",
12469#endif
12470#ifdef FEAT_VREPLACE
12471 "vreplace",
12472#endif
12473#ifdef FEAT_WILDIGN
12474 "wildignore",
12475#endif
12476#ifdef FEAT_WILDMENU
12477 "wildmenu",
12478#endif
12479#ifdef FEAT_WINDOWS
12480 "windows",
12481#endif
12482#ifdef FEAT_WAK
12483 "winaltkeys",
12484#endif
12485#ifdef FEAT_WRITEBACKUP
12486 "writebackup",
12487#endif
12488#ifdef FEAT_XIM
12489 "xim",
12490#endif
12491#ifdef FEAT_XFONTSET
12492 "xfontset",
12493#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012494#ifdef FEAT_XPM_W32
12495 "xpm_w32",
12496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497#ifdef USE_XSMP
12498 "xsmp",
12499#endif
12500#ifdef USE_XSMP_INTERACT
12501 "xsmp_interact",
12502#endif
12503#ifdef FEAT_XCLIPBOARD
12504 "xterm_clipboard",
12505#endif
12506#ifdef FEAT_XTERM_SAVE
12507 "xterm_save",
12508#endif
12509#if defined(UNIX) && defined(FEAT_X11)
12510 "X11",
12511#endif
12512 NULL
12513 };
12514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012516 for (i = 0; has_list[i] != NULL; ++i)
12517 if (STRICMP(name, has_list[i]) == 0)
12518 {
12519 n = TRUE;
12520 break;
12521 }
12522
12523 if (n == FALSE)
12524 {
12525 if (STRNICMP(name, "patch", 5) == 0)
12526 n = has_patch(atoi((char *)name + 5));
12527 else if (STRICMP(name, "vim_starting") == 0)
12528 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012529#ifdef FEAT_MBYTE
12530 else if (STRICMP(name, "multi_byte_encoding") == 0)
12531 n = has_mbyte;
12532#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012533#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12534 else if (STRICMP(name, "balloon_multiline") == 0)
12535 n = multiline_balloon_available();
12536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537#ifdef DYNAMIC_TCL
12538 else if (STRICMP(name, "tcl") == 0)
12539 n = tcl_enabled(FALSE);
12540#endif
12541#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12542 else if (STRICMP(name, "iconv") == 0)
12543 n = iconv_enabled(FALSE);
12544#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012545#ifdef DYNAMIC_LUA
12546 else if (STRICMP(name, "lua") == 0)
12547 n = lua_enabled(FALSE);
12548#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012549#ifdef DYNAMIC_MZSCHEME
12550 else if (STRICMP(name, "mzscheme") == 0)
12551 n = mzscheme_enabled(FALSE);
12552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553#ifdef DYNAMIC_RUBY
12554 else if (STRICMP(name, "ruby") == 0)
12555 n = ruby_enabled(FALSE);
12556#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012557#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558#ifdef DYNAMIC_PYTHON
12559 else if (STRICMP(name, "python") == 0)
12560 n = python_enabled(FALSE);
12561#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012562#endif
12563#ifdef FEAT_PYTHON3
12564#ifdef DYNAMIC_PYTHON3
12565 else if (STRICMP(name, "python3") == 0)
12566 n = python3_enabled(FALSE);
12567#endif
12568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569#ifdef DYNAMIC_PERL
12570 else if (STRICMP(name, "perl") == 0)
12571 n = perl_enabled(FALSE);
12572#endif
12573#ifdef FEAT_GUI
12574 else if (STRICMP(name, "gui_running") == 0)
12575 n = (gui.in_use || gui.starting);
12576# ifdef FEAT_GUI_W32
12577 else if (STRICMP(name, "gui_win32s") == 0)
12578 n = gui_is_win32s();
12579# endif
12580# ifdef FEAT_BROWSE
12581 else if (STRICMP(name, "browse") == 0)
12582 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12583# endif
12584#endif
12585#ifdef FEAT_SYN_HL
12586 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012587 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588#endif
12589#if defined(WIN3264)
12590 else if (STRICMP(name, "win95") == 0)
12591 n = mch_windows95();
12592#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012593#ifdef FEAT_NETBEANS_INTG
12594 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012595 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 }
12598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600}
12601
12602/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012603 * "has_key()" function
12604 */
12605 static void
12606f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012607 typval_T *argvars;
12608 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012609{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012610 if (argvars[0].v_type != VAR_DICT)
12611 {
12612 EMSG(_(e_dictreq));
12613 return;
12614 }
12615 if (argvars[0].vval.v_dict == NULL)
12616 return;
12617
12618 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012619 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012620}
12621
12622/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012623 * "haslocaldir()" function
12624 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012625 static void
12626f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012627 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012628 typval_T *rettv;
12629{
12630 rettv->vval.v_number = (curwin->w_localdir != NULL);
12631}
12632
12633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 * "hasmapto()" function
12635 */
12636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012638 typval_T *argvars;
12639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640{
12641 char_u *name;
12642 char_u *mode;
12643 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012644 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012646 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012647 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 mode = (char_u *)"nvo";
12649 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012652 if (argvars[2].v_type != VAR_UNKNOWN)
12653 abbr = get_tv_number(&argvars[2]);
12654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655
Bram Moolenaar2c932302006-03-18 21:42:09 +000012656 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660}
12661
12662/*
12663 * "histadd()" function
12664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669{
12670#ifdef FEAT_CMDHIST
12671 int histype;
12672 char_u *str;
12673 char_u buf[NUMBUFLEN];
12674#endif
12675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 if (check_restricted() || check_secure())
12678 return;
12679#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012680 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12681 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 if (histype >= 0)
12683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 if (*str != NUL)
12686 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012687 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 return;
12691 }
12692 }
12693#endif
12694}
12695
12696/*
12697 * "histdel()" function
12698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012701 typval_T *argvars UNUSED;
12702 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703{
12704#ifdef FEAT_CMDHIST
12705 int n;
12706 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12710 if (str == NULL)
12711 n = 0;
12712 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012715 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 else
12720 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 get_tv_string_buf(&argvars[1], buf));
12723 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724#endif
12725}
12726
12727/*
12728 * "histget()" function
12729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012732 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734{
12735#ifdef FEAT_CMDHIST
12736 int type;
12737 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12741 if (str == NULL)
12742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012744 {
12745 type = get_histtype(str);
12746 if (argvars[1].v_type == VAR_UNKNOWN)
12747 idx = get_history_idx(type);
12748 else
12749 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12750 /* -1 on type error */
12751 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757}
12758
12759/*
12760 * "histnr()" function
12761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012764 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766{
12767 int i;
12768
12769#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 char_u *history = get_tv_string_chk(&argvars[0]);
12771
12772 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 if (i >= HIST_CMD && i < HIST_COUNT)
12774 i = get_history_idx(i);
12775 else
12776#endif
12777 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012778 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779}
12780
12781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 * "highlightID(name)" function
12783 */
12784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *argvars;
12787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790}
12791
12792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012793 * "highlight_exists()" function
12794 */
12795 static void
12796f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012797 typval_T *argvars;
12798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012799{
12800 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12801}
12802
12803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 * "hostname()" function
12805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
12811 char_u hostname[256];
12812
12813 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->v_type = VAR_STRING;
12815 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816}
12817
12818/*
12819 * iconv() function
12820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825{
12826#ifdef FEAT_MBYTE
12827 char_u buf1[NUMBUFLEN];
12828 char_u buf2[NUMBUFLEN];
12829 char_u *from, *to, *str;
12830 vimconv_T vimconv;
12831#endif
12832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->v_type = VAR_STRING;
12834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835
12836#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 str = get_tv_string(&argvars[0]);
12838 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12839 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 vimconv.vc_type = CONV_NONE;
12841 convert_setup(&vimconv, from, to);
12842
12843 /* If the encodings are equal, no conversion needed. */
12844 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848
12849 convert_setup(&vimconv, NULL, NULL);
12850 vim_free(from);
12851 vim_free(to);
12852#endif
12853}
12854
12855/*
12856 * "indent()" function
12857 */
12858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *argvars;
12861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
12863 linenr_T lnum;
12864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012872/*
12873 * "index()" function
12874 */
12875 static void
12876f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 typval_T *argvars;
12878 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012879{
Bram Moolenaar33570922005-01-25 22:26:29 +000012880 list_T *l;
12881 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012882 long idx = 0;
12883 int ic = FALSE;
12884
12885 rettv->vval.v_number = -1;
12886 if (argvars[0].v_type != VAR_LIST)
12887 {
12888 EMSG(_(e_listreq));
12889 return;
12890 }
12891 l = argvars[0].vval.v_list;
12892 if (l != NULL)
12893 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012894 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012897 int error = FALSE;
12898
Bram Moolenaar758711c2005-02-02 23:11:38 +000012899 /* Start at specified item. Use the cached index that list_find()
12900 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012902 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012903 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012904 ic = get_tv_number_chk(&argvars[3], &error);
12905 if (error)
12906 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012907 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012908
Bram Moolenaar758711c2005-02-02 23:11:38 +000012909 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012910 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012911 {
12912 rettv->vval.v_number = idx;
12913 break;
12914 }
12915 }
12916}
12917
Bram Moolenaar071d4272004-06-13 20:20:40 +000012918static int inputsecret_flag = 0;
12919
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012920static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12921
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012923 * This function is used by f_input() and f_inputdialog() functions. The third
12924 * argument to f_input() specifies the type of completion to use at the
12925 * prompt. The third argument to f_inputdialog() specifies the value to return
12926 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 */
12928 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012929get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *argvars;
12931 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012932 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012934 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 char_u *p = NULL;
12936 int c;
12937 char_u buf[NUMBUFLEN];
12938 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012939 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012940 int xp_type = EXPAND_NOTHING;
12941 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012944 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
12946#ifdef NO_CONSOLE_INPUT
12947 /* While starting up, there is no place to enter text. */
12948 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012950#endif
12951
12952 cmd_silent = FALSE; /* Want to see the prompt. */
12953 if (prompt != NULL)
12954 {
12955 /* Only the part of the message after the last NL is considered as
12956 * prompt for the command line */
12957 p = vim_strrchr(prompt, '\n');
12958 if (p == NULL)
12959 p = prompt;
12960 else
12961 {
12962 ++p;
12963 c = *p;
12964 *p = NUL;
12965 msg_start();
12966 msg_clr_eos();
12967 msg_puts_attr(prompt, echo_attr);
12968 msg_didout = FALSE;
12969 msg_starthere();
12970 *p = c;
12971 }
12972 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012974 if (argvars[1].v_type != VAR_UNKNOWN)
12975 {
12976 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12977 if (defstr != NULL)
12978 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012980 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012981 {
12982 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012983 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012984 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012985
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012986 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012987 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012988
Bram Moolenaar4463f292005-09-25 22:20:24 +000012989 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12990 if (xp_name == NULL)
12991 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012992
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012993 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012994
Bram Moolenaar4463f292005-09-25 22:20:24 +000012995 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12996 &xp_arg) == FAIL)
12997 return;
12998 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012999 }
13000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013001 if (defstr != NULL)
13002 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013003 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13004 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013005 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013006 && argvars[1].v_type != VAR_UNKNOWN
13007 && argvars[2].v_type != VAR_UNKNOWN)
13008 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13009 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013010
13011 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013 /* since the user typed this, no need to wait for return */
13014 need_wait_return = FALSE;
13015 msg_didout = FALSE;
13016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 cmd_silent = cmd_silent_save;
13018}
13019
13020/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013021 * "input()" function
13022 * Also handles inputsecret() when inputsecret is set.
13023 */
13024 static void
13025f_input(argvars, rettv)
13026 typval_T *argvars;
13027 typval_T *rettv;
13028{
13029 get_user_input(argvars, rettv, FALSE);
13030}
13031
13032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033 * "inputdialog()" function
13034 */
13035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013037 typval_T *argvars;
13038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039{
13040#if defined(FEAT_GUI_TEXTDIALOG)
13041 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13042 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13043 {
13044 char_u *message;
13045 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013046 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013048 message = get_tv_string_chk(&argvars[0]);
13049 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013050 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013051 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 else
13053 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013054 if (message != NULL && defstr != NULL
13055 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013056 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 else
13059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 if (message != NULL && defstr != NULL
13061 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013062 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 rettv->vval.v_string = vim_strsave(
13064 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069 }
13070 else
13071#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013072 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073}
13074
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013075/*
13076 * "inputlist()" function
13077 */
13078 static void
13079f_inputlist(argvars, rettv)
13080 typval_T *argvars;
13081 typval_T *rettv;
13082{
13083 listitem_T *li;
13084 int selected;
13085 int mouse_used;
13086
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013087#ifdef NO_CONSOLE_INPUT
13088 /* While starting up, there is no place to enter text. */
13089 if (no_console_input())
13090 return;
13091#endif
13092 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13093 {
13094 EMSG2(_(e_listarg), "inputlist()");
13095 return;
13096 }
13097
13098 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013099 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013100 lines_left = Rows; /* avoid more prompt */
13101 msg_scroll = TRUE;
13102 msg_clr_eos();
13103
13104 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13105 {
13106 msg_puts(get_tv_string(&li->li_tv));
13107 msg_putchar('\n');
13108 }
13109
13110 /* Ask for choice. */
13111 selected = prompt_for_number(&mouse_used);
13112 if (mouse_used)
13113 selected -= lines_left;
13114
13115 rettv->vval.v_number = selected;
13116}
13117
13118
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13120
13121/*
13122 * "inputrestore()" function
13123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013126 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128{
13129 if (ga_userinput.ga_len > 0)
13130 {
13131 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13133 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013134 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 }
13136 else if (p_verbose > 1)
13137 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013138 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013140 }
13141}
13142
13143/*
13144 * "inputsave()" function
13145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013148 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013151 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152 if (ga_grow(&ga_userinput, 1) == OK)
13153 {
13154 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13155 + ga_userinput.ga_len);
13156 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013157 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 }
13159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161}
13162
13163/*
13164 * "inputsecret()" function
13165 */
13166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013168 typval_T *argvars;
13169 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
13171 ++cmdline_star;
13172 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013173 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 --cmdline_star;
13175 --inputsecret_flag;
13176}
13177
13178/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013179 * "insert()" function
13180 */
13181 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013182f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013183 typval_T *argvars;
13184 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013185{
13186 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 listitem_T *item;
13188 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190
13191 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013192 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013193 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013194 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 {
13196 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 before = get_tv_number_chk(&argvars[2], &error);
13198 if (error)
13199 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200
Bram Moolenaar758711c2005-02-02 23:11:38 +000013201 if (before == l->lv_len)
13202 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013203 else
13204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013205 item = list_find(l, before);
13206 if (item == NULL)
13207 {
13208 EMSGN(_(e_listidx), before);
13209 l = NULL;
13210 }
13211 }
13212 if (l != NULL)
13213 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013214 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013215 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216 }
13217 }
13218}
13219
13220/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013221 * "invert(expr)" function
13222 */
13223 static void
13224f_invert(argvars, rettv)
13225 typval_T *argvars;
13226 typval_T *rettv;
13227{
13228 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13229}
13230
13231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 * "isdirectory()" function
13233 */
13234 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013236 typval_T *argvars;
13237 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240}
13241
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013242/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013243 * "islocked()" function
13244 */
13245 static void
13246f_islocked(argvars, rettv)
13247 typval_T *argvars;
13248 typval_T *rettv;
13249{
13250 lval_T lv;
13251 char_u *end;
13252 dictitem_T *di;
13253
13254 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013255 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13256 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013257 if (end != NULL && lv.ll_name != NULL)
13258 {
13259 if (*end != NUL)
13260 EMSG(_(e_trailing));
13261 else
13262 {
13263 if (lv.ll_tv == NULL)
13264 {
13265 if (check_changedtick(lv.ll_name))
13266 rettv->vval.v_number = 1; /* always locked */
13267 else
13268 {
13269 di = find_var(lv.ll_name, NULL);
13270 if (di != NULL)
13271 {
13272 /* Consider a variable locked when:
13273 * 1. the variable itself is locked
13274 * 2. the value of the variable is locked.
13275 * 3. the List or Dict value is locked.
13276 */
13277 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13278 || tv_islocked(&di->di_tv));
13279 }
13280 }
13281 }
13282 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013283 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013284 else if (lv.ll_newkey != NULL)
13285 EMSG2(_(e_dictkey), lv.ll_newkey);
13286 else if (lv.ll_list != NULL)
13287 /* List item. */
13288 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13289 else
13290 /* Dictionary item. */
13291 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13292 }
13293 }
13294
13295 clear_lval(&lv);
13296}
13297
Bram Moolenaar33570922005-01-25 22:26:29 +000013298static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013299
13300/*
13301 * Turn a dict into a list:
13302 * "what" == 0: list of keys
13303 * "what" == 1: list of values
13304 * "what" == 2: list of items
13305 */
13306 static void
13307dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013308 typval_T *argvars;
13309 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013310 int what;
13311{
Bram Moolenaar33570922005-01-25 22:26:29 +000013312 list_T *l2;
13313 dictitem_T *di;
13314 hashitem_T *hi;
13315 listitem_T *li;
13316 listitem_T *li2;
13317 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013318 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013319
Bram Moolenaar8c711452005-01-14 21:53:12 +000013320 if (argvars[0].v_type != VAR_DICT)
13321 {
13322 EMSG(_(e_dictreq));
13323 return;
13324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013325 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013326 return;
13327
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013328 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013329 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013331 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013332 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013333 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013334 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013336 --todo;
13337 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013339 li = listitem_alloc();
13340 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013341 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013342 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013344 if (what == 0)
13345 {
13346 /* keys() */
13347 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013348 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013349 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13350 }
13351 else if (what == 1)
13352 {
13353 /* values() */
13354 copy_tv(&di->di_tv, &li->li_tv);
13355 }
13356 else
13357 {
13358 /* items() */
13359 l2 = list_alloc();
13360 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013361 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013362 li->li_tv.vval.v_list = l2;
13363 if (l2 == NULL)
13364 break;
13365 ++l2->lv_refcount;
13366
13367 li2 = listitem_alloc();
13368 if (li2 == NULL)
13369 break;
13370 list_append(l2, li2);
13371 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013372 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013373 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13374
13375 li2 = listitem_alloc();
13376 if (li2 == NULL)
13377 break;
13378 list_append(l2, li2);
13379 copy_tv(&di->di_tv, &li2->li_tv);
13380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381 }
13382 }
13383}
13384
13385/*
13386 * "items(dict)" function
13387 */
13388 static void
13389f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013390 typval_T *argvars;
13391 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013392{
13393 dict_list(argvars, rettv, 2);
13394}
13395
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013397 * "join()" function
13398 */
13399 static void
13400f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013401 typval_T *argvars;
13402 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013403{
13404 garray_T ga;
13405 char_u *sep;
13406
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013407 if (argvars[0].v_type != VAR_LIST)
13408 {
13409 EMSG(_(e_listreq));
13410 return;
13411 }
13412 if (argvars[0].vval.v_list == NULL)
13413 return;
13414 if (argvars[1].v_type == VAR_UNKNOWN)
13415 sep = (char_u *)" ";
13416 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013418
13419 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420
13421 if (sep != NULL)
13422 {
13423 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013424 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 ga_append(&ga, NUL);
13426 rettv->vval.v_string = (char_u *)ga.ga_data;
13427 }
13428 else
13429 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013430}
13431
13432/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433 * "keys()" function
13434 */
13435 static void
13436f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *argvars;
13438 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013439{
13440 dict_list(argvars, rettv, 0);
13441}
13442
13443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 * "last_buffer_nr()" function.
13445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450{
13451 int n = 0;
13452 buf_T *buf;
13453
13454 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13455 if (n < buf->b_fnum)
13456 n = buf->b_fnum;
13457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013458 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013459}
13460
13461/*
13462 * "len()" function
13463 */
13464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013466 typval_T *argvars;
13467 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013468{
13469 switch (argvars[0].v_type)
13470 {
13471 case VAR_STRING:
13472 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_number = (varnumber_T)STRLEN(
13474 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475 break;
13476 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013478 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013479 case VAR_DICT:
13480 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13481 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013483 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013484 break;
13485 }
13486}
13487
Bram Moolenaar33570922005-01-25 22:26:29 +000013488static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013489
13490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 typval_T *argvars;
13493 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013494 int type;
13495{
13496#ifdef FEAT_LIBCALL
13497 char_u *string_in;
13498 char_u **string_result;
13499 int nr_result;
13500#endif
13501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013503 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013505
13506 if (check_restricted() || check_secure())
13507 return;
13508
13509#ifdef FEAT_LIBCALL
13510 /* The first two args must be strings, otherwise its meaningless */
13511 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013513 string_in = NULL;
13514 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013515 string_in = argvars[2].vval.v_string;
13516 if (type == VAR_NUMBER)
13517 string_result = NULL;
13518 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520 if (mch_libcall(argvars[0].vval.v_string,
13521 argvars[1].vval.v_string,
13522 string_in,
13523 argvars[2].vval.v_number,
13524 string_result,
13525 &nr_result) == OK
13526 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013528 }
13529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530}
13531
13532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013533 * "libcall()" function
13534 */
13535 static void
13536f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013537 typval_T *argvars;
13538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013539{
13540 libcall_common(argvars, rettv, VAR_STRING);
13541}
13542
13543/*
13544 * "libcallnr()" function
13545 */
13546 static void
13547f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013548 typval_T *argvars;
13549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013550{
13551 libcall_common(argvars, rettv, VAR_NUMBER);
13552}
13553
13554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555 * "line(string)" function
13556 */
13557 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013559 typval_T *argvars;
13560 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561{
13562 linenr_T lnum = 0;
13563 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013564 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013566 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 if (fp != NULL)
13568 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570}
13571
13572/*
13573 * "line2byte(lnum)" function
13574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013577 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579{
13580#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#else
13583 linenr_T lnum;
13584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013587 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013589 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13590 if (rettv->vval.v_number >= 0)
13591 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592#endif
13593}
13594
13595/*
13596 * "lispindent(lnum)" function
13597 */
13598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013600 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602{
13603#ifdef FEAT_LISP
13604 pos_T pos;
13605 linenr_T lnum;
13606
13607 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13610 {
13611 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 curwin->w_cursor = pos;
13614 }
13615 else
13616#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618}
13619
13620/*
13621 * "localtime()" function
13622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013625 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629}
13630
Bram Moolenaar33570922005-01-25 22:26:29 +000013631static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632
13633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013635 typval_T *argvars;
13636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 int exact;
13638{
13639 char_u *keys;
13640 char_u *which;
13641 char_u buf[NUMBUFLEN];
13642 char_u *keys_buf = NULL;
13643 char_u *rhs;
13644 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013645 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013646 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013647 mapblock_T *mp;
13648 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
13650 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->v_type = VAR_STRING;
13652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 if (*keys == NUL)
13656 return;
13657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013660 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013662 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013663 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013664 if (argvars[3].v_type != VAR_UNKNOWN)
13665 get_dict = get_tv_number(&argvars[3]);
13666 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 else
13669 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013670 if (which == NULL)
13671 return;
13672
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 mode = get_map_mode(&which, 0);
13674
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013675 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013676 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678
13679 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013681 /* Return a string. */
13682 if (rhs != NULL)
13683 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
Bram Moolenaarbd743252010-10-20 21:23:33 +020013685 }
13686 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13687 {
13688 /* Return a dictionary. */
13689 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13690 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13691 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692
Bram Moolenaarbd743252010-10-20 21:23:33 +020013693 dict_add_nr_str(dict, "lhs", 0L, lhs);
13694 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13695 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13696 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13697 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13698 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13699 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13700 dict_add_nr_str(dict, "mode", 0L, mapmode);
13701
13702 vim_free(lhs);
13703 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 }
13705}
13706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013707#ifdef FEAT_FLOAT
13708/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013709 * "log()" function
13710 */
13711 static void
13712f_log(argvars, rettv)
13713 typval_T *argvars;
13714 typval_T *rettv;
13715{
13716 float_T f;
13717
13718 rettv->v_type = VAR_FLOAT;
13719 if (get_float_arg(argvars, &f) == OK)
13720 rettv->vval.v_float = log(f);
13721 else
13722 rettv->vval.v_float = 0.0;
13723}
13724
13725/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013726 * "log10()" function
13727 */
13728 static void
13729f_log10(argvars, rettv)
13730 typval_T *argvars;
13731 typval_T *rettv;
13732{
13733 float_T f;
13734
13735 rettv->v_type = VAR_FLOAT;
13736 if (get_float_arg(argvars, &f) == OK)
13737 rettv->vval.v_float = log10(f);
13738 else
13739 rettv->vval.v_float = 0.0;
13740}
13741#endif
13742
Bram Moolenaar1dced572012-04-05 16:54:08 +020013743#ifdef FEAT_LUA
13744/*
13745 * "luaeval()" function
13746 */
13747 static void
13748f_luaeval(argvars, rettv)
13749 typval_T *argvars;
13750 typval_T *rettv;
13751{
13752 char_u *str;
13753 char_u buf[NUMBUFLEN];
13754
13755 str = get_tv_string_buf(&argvars[0], buf);
13756 do_luaeval(str, argvars + 1, rettv);
13757}
13758#endif
13759
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013761 * "map()" function
13762 */
13763 static void
13764f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 typval_T *argvars;
13766 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013767{
13768 filter_map(argvars, rettv, TRUE);
13769}
13770
13771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 */
13774 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013775f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 typval_T *argvars;
13777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013779 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780}
13781
13782/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 */
13785 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 typval_T *argvars;
13788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013790 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791}
13792
Bram Moolenaar33570922005-01-25 22:26:29 +000013793static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794
13795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 int type;
13800{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013801 char_u *str = NULL;
13802 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803 char_u *pat;
13804 regmatch_T regmatch;
13805 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013806 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 char_u *save_cpo;
13808 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013809 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013810 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013811 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 list_T *l = NULL;
13813 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013814 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013815 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816
13817 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13818 save_cpo = p_cpo;
13819 p_cpo = (char_u *)"";
13820
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013821 rettv->vval.v_number = -1;
13822 if (type == 3)
13823 {
13824 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013825 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013826 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013827 }
13828 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->v_type = VAR_STRING;
13831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013834 if (argvars[0].v_type == VAR_LIST)
13835 {
13836 if ((l = argvars[0].vval.v_list) == NULL)
13837 goto theend;
13838 li = l->lv_first;
13839 }
13840 else
13841 expr = str = get_tv_string(&argvars[0]);
13842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013843 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13844 if (pat == NULL)
13845 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013846
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 int error = FALSE;
13850
13851 start = get_tv_number_chk(&argvars[2], &error);
13852 if (error)
13853 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013854 if (l != NULL)
13855 {
13856 li = list_find(l, start);
13857 if (li == NULL)
13858 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013859 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860 }
13861 else
13862 {
13863 if (start < 0)
13864 start = 0;
13865 if (start > (long)STRLEN(str))
13866 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013867 /* When "count" argument is there ignore matches before "start",
13868 * otherwise skip part of the string. Differs when pattern is "^"
13869 * or "\<". */
13870 if (argvars[3].v_type != VAR_UNKNOWN)
13871 startcol = start;
13872 else
13873 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013874 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013875
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013876 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877 nth = get_tv_number_chk(&argvars[3], &error);
13878 if (error)
13879 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 }
13881
13882 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13883 if (regmatch.regprog != NULL)
13884 {
13885 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013886
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013887 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013888 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013889 if (l != NULL)
13890 {
13891 if (li == NULL)
13892 {
13893 match = FALSE;
13894 break;
13895 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013896 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013897 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013898 if (str == NULL)
13899 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013900 }
13901
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013902 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013904 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013906 if (l == NULL && !match)
13907 break;
13908
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013909 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013910 if (l != NULL)
13911 {
13912 li = li->li_next;
13913 ++idx;
13914 }
13915 else
13916 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013917#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013918 startcol = (colnr_T)(regmatch.startp[0]
13919 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013920#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013921 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013922#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013923 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013924 }
13925
13926 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013928 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013929 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013930 int i;
13931
13932 /* return list with matched string and submatches */
13933 for (i = 0; i < NSUBEXP; ++i)
13934 {
13935 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013936 {
13937 if (list_append_string(rettv->vval.v_list,
13938 (char_u *)"", 0) == FAIL)
13939 break;
13940 }
13941 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013942 regmatch.startp[i],
13943 (int)(regmatch.endp[i] - regmatch.startp[i]))
13944 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013945 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013946 }
13947 }
13948 else if (type == 2)
13949 {
13950 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 if (l != NULL)
13952 copy_tv(&li->li_tv, rettv);
13953 else
13954 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013956 }
13957 else if (l != NULL)
13958 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 else
13960 {
13961 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 (varnumber_T)(regmatch.startp[0] - str);
13964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013967 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 }
13969 }
13970 vim_free(regmatch.regprog);
13971 }
13972
13973theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013974 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 p_cpo = save_cpo;
13976}
13977
13978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013979 * "match()" function
13980 */
13981 static void
13982f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013983 typval_T *argvars;
13984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013985{
13986 find_some_match(argvars, rettv, 1);
13987}
13988
13989/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013990 * "matchadd()" function
13991 */
13992 static void
13993f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013994 typval_T *argvars UNUSED;
13995 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013996{
13997#ifdef FEAT_SEARCH_EXTRA
13998 char_u buf[NUMBUFLEN];
13999 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14000 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14001 int prio = 10; /* default priority */
14002 int id = -1;
14003 int error = FALSE;
14004
14005 rettv->vval.v_number = -1;
14006
14007 if (grp == NULL || pat == NULL)
14008 return;
14009 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014010 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014011 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014012 if (argvars[3].v_type != VAR_UNKNOWN)
14013 id = get_tv_number_chk(&argvars[3], &error);
14014 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014015 if (error == TRUE)
14016 return;
14017 if (id >= 1 && id <= 3)
14018 {
14019 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14020 return;
14021 }
14022
14023 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14024#endif
14025}
14026
14027/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014028 * "matcharg()" function
14029 */
14030 static void
14031f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014032 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014033 typval_T *rettv;
14034{
14035 if (rettv_list_alloc(rettv) == OK)
14036 {
14037#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014038 int id = get_tv_number(&argvars[0]);
14039 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014040
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014041 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014042 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014043 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14044 {
14045 list_append_string(rettv->vval.v_list,
14046 syn_id2name(m->hlg_id), -1);
14047 list_append_string(rettv->vval.v_list, m->pattern, -1);
14048 }
14049 else
14050 {
14051 list_append_string(rettv->vval.v_list, NUL, -1);
14052 list_append_string(rettv->vval.v_list, NUL, -1);
14053 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014054 }
14055#endif
14056 }
14057}
14058
14059/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014060 * "matchdelete()" function
14061 */
14062 static void
14063f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014064 typval_T *argvars UNUSED;
14065 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014066{
14067#ifdef FEAT_SEARCH_EXTRA
14068 rettv->vval.v_number = match_delete(curwin,
14069 (int)get_tv_number(&argvars[0]), TRUE);
14070#endif
14071}
14072
14073/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014074 * "matchend()" function
14075 */
14076 static void
14077f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 typval_T *argvars;
14079 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014080{
14081 find_some_match(argvars, rettv, 0);
14082}
14083
14084/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014085 * "matchlist()" function
14086 */
14087 static void
14088f_matchlist(argvars, rettv)
14089 typval_T *argvars;
14090 typval_T *rettv;
14091{
14092 find_some_match(argvars, rettv, 3);
14093}
14094
14095/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096 * "matchstr()" function
14097 */
14098 static void
14099f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014100 typval_T *argvars;
14101 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014102{
14103 find_some_match(argvars, rettv, 2);
14104}
14105
Bram Moolenaar33570922005-01-25 22:26:29 +000014106static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014107
14108 static void
14109max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014110 typval_T *argvars;
14111 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014112 int domax;
14113{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014114 long n = 0;
14115 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014117
14118 if (argvars[0].v_type == VAR_LIST)
14119 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 list_T *l;
14121 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014122
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014123 l = argvars[0].vval.v_list;
14124 if (l != NULL)
14125 {
14126 li = l->lv_first;
14127 if (li != NULL)
14128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014129 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014130 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014131 {
14132 li = li->li_next;
14133 if (li == NULL)
14134 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014136 if (domax ? i > n : i < n)
14137 n = i;
14138 }
14139 }
14140 }
14141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014142 else if (argvars[0].v_type == VAR_DICT)
14143 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014145 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014146 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014147 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014148
14149 d = argvars[0].vval.v_dict;
14150 if (d != NULL)
14151 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014152 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014153 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014155 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014157 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014159 if (first)
14160 {
14161 n = i;
14162 first = FALSE;
14163 }
14164 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014165 n = i;
14166 }
14167 }
14168 }
14169 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014171 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014173}
14174
14175/*
14176 * "max()" function
14177 */
14178 static void
14179f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014180 typval_T *argvars;
14181 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014182{
14183 max_min(argvars, rettv, TRUE);
14184}
14185
14186/*
14187 * "min()" function
14188 */
14189 static void
14190f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014193{
14194 max_min(argvars, rettv, FALSE);
14195}
14196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014197static int mkdir_recurse __ARGS((char_u *dir, int prot));
14198
14199/*
14200 * Create the directory in which "dir" is located, and higher levels when
14201 * needed.
14202 */
14203 static int
14204mkdir_recurse(dir, prot)
14205 char_u *dir;
14206 int prot;
14207{
14208 char_u *p;
14209 char_u *updir;
14210 int r = FAIL;
14211
14212 /* Get end of directory name in "dir".
14213 * We're done when it's "/" or "c:/". */
14214 p = gettail_sep(dir);
14215 if (p <= get_past_head(dir))
14216 return OK;
14217
14218 /* If the directory exists we're done. Otherwise: create it.*/
14219 updir = vim_strnsave(dir, (int)(p - dir));
14220 if (updir == NULL)
14221 return FAIL;
14222 if (mch_isdir(updir))
14223 r = OK;
14224 else if (mkdir_recurse(updir, prot) == OK)
14225 r = vim_mkdir_emsg(updir, prot);
14226 vim_free(updir);
14227 return r;
14228}
14229
14230#ifdef vim_mkdir
14231/*
14232 * "mkdir()" function
14233 */
14234 static void
14235f_mkdir(argvars, rettv)
14236 typval_T *argvars;
14237 typval_T *rettv;
14238{
14239 char_u *dir;
14240 char_u buf[NUMBUFLEN];
14241 int prot = 0755;
14242
14243 rettv->vval.v_number = FAIL;
14244 if (check_restricted() || check_secure())
14245 return;
14246
14247 dir = get_tv_string_buf(&argvars[0], buf);
14248 if (argvars[1].v_type != VAR_UNKNOWN)
14249 {
14250 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 prot = get_tv_number_chk(&argvars[2], NULL);
14252 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014253 mkdir_recurse(dir, prot);
14254 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014256}
14257#endif
14258
Bram Moolenaar0d660222005-01-07 21:51:51 +000014259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 * "mode()" function
14261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014263f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014267 char_u buf[3];
14268
14269 buf[1] = NUL;
14270 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271
14272#ifdef FEAT_VISUAL
14273 if (VIsual_active)
14274 {
14275 if (VIsual_select)
14276 buf[0] = VIsual_mode + 's' - 'v';
14277 else
14278 buf[0] = VIsual_mode;
14279 }
14280 else
14281#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014282 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14283 || State == CONFIRM)
14284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014286 if (State == ASKMORE)
14287 buf[1] = 'm';
14288 else if (State == CONFIRM)
14289 buf[1] = '?';
14290 }
14291 else if (State == EXTERNCMD)
14292 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 else if (State & INSERT)
14294 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014295#ifdef FEAT_VREPLACE
14296 if (State & VREPLACE_FLAG)
14297 {
14298 buf[0] = 'R';
14299 buf[1] = 'v';
14300 }
14301 else
14302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 if (State & REPLACE_FLAG)
14304 buf[0] = 'R';
14305 else
14306 buf[0] = 'i';
14307 }
14308 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014311 if (exmode_active)
14312 buf[1] = 'v';
14313 }
14314 else if (exmode_active)
14315 {
14316 buf[0] = 'c';
14317 buf[1] = 'e';
14318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014322 if (finish_op)
14323 buf[1] = 'o';
14324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014326 /* Clear out the minor mode when the argument is not a non-zero number or
14327 * non-empty string. */
14328 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329 buf[1] = NUL;
14330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 rettv->vval.v_string = vim_strsave(buf);
14332 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333}
14334
Bram Moolenaar429fa852013-04-15 12:27:36 +020014335#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014336/*
14337 * "mzeval()" function
14338 */
14339 static void
14340f_mzeval(argvars, rettv)
14341 typval_T *argvars;
14342 typval_T *rettv;
14343{
14344 char_u *str;
14345 char_u buf[NUMBUFLEN];
14346
14347 str = get_tv_string_buf(&argvars[0], buf);
14348 do_mzeval(str, rettv);
14349}
Bram Moolenaar75676462013-01-30 14:55:42 +010014350
14351 void
14352mzscheme_call_vim(name, args, rettv)
14353 char_u *name;
14354 typval_T *args;
14355 typval_T *rettv;
14356{
14357 typval_T argvars[3];
14358
14359 argvars[0].v_type = VAR_STRING;
14360 argvars[0].vval.v_string = name;
14361 copy_tv(args, &argvars[1]);
14362 argvars[2].v_type = VAR_UNKNOWN;
14363 f_call(argvars, rettv);
14364 clear_tv(&argvars[1]);
14365}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014366#endif
14367
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014369 * "nextnonblank()" function
14370 */
14371 static void
14372f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014373 typval_T *argvars;
14374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014375{
14376 linenr_T lnum;
14377
14378 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014380 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014381 {
14382 lnum = 0;
14383 break;
14384 }
14385 if (*skipwhite(ml_get(lnum)) != NUL)
14386 break;
14387 }
14388 rettv->vval.v_number = lnum;
14389}
14390
14391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 * "nr2char()" function
14393 */
14394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014396 typval_T *argvars;
14397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398{
14399 char_u buf[NUMBUFLEN];
14400
14401#ifdef FEAT_MBYTE
14402 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014403 {
14404 int utf8 = 0;
14405
14406 if (argvars[1].v_type != VAR_UNKNOWN)
14407 utf8 = get_tv_number_chk(&argvars[1], NULL);
14408 if (utf8)
14409 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14410 else
14411 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413 else
14414#endif
14415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 buf[1] = NUL;
14418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->v_type = VAR_STRING;
14420 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014421}
14422
14423/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014424 * "or(expr, expr)" function
14425 */
14426 static void
14427f_or(argvars, rettv)
14428 typval_T *argvars;
14429 typval_T *rettv;
14430{
14431 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14432 | get_tv_number_chk(&argvars[1], NULL);
14433}
14434
14435/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014436 * "pathshorten()" function
14437 */
14438 static void
14439f_pathshorten(argvars, rettv)
14440 typval_T *argvars;
14441 typval_T *rettv;
14442{
14443 char_u *p;
14444
14445 rettv->v_type = VAR_STRING;
14446 p = get_tv_string_chk(&argvars[0]);
14447 if (p == NULL)
14448 rettv->vval.v_string = NULL;
14449 else
14450 {
14451 p = vim_strsave(p);
14452 rettv->vval.v_string = p;
14453 if (p != NULL)
14454 shorten_dir(p);
14455 }
14456}
14457
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014458#ifdef FEAT_FLOAT
14459/*
14460 * "pow()" function
14461 */
14462 static void
14463f_pow(argvars, rettv)
14464 typval_T *argvars;
14465 typval_T *rettv;
14466{
14467 float_T fx, fy;
14468
14469 rettv->v_type = VAR_FLOAT;
14470 if (get_float_arg(argvars, &fx) == OK
14471 && get_float_arg(&argvars[1], &fy) == OK)
14472 rettv->vval.v_float = pow(fx, fy);
14473 else
14474 rettv->vval.v_float = 0.0;
14475}
14476#endif
14477
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014478/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479 * "prevnonblank()" function
14480 */
14481 static void
14482f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 typval_T *argvars;
14484 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014485{
14486 linenr_T lnum;
14487
14488 lnum = get_tv_lnum(argvars);
14489 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14490 lnum = 0;
14491 else
14492 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14493 --lnum;
14494 rettv->vval.v_number = lnum;
14495}
14496
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014497#ifdef HAVE_STDARG_H
14498/* This dummy va_list is here because:
14499 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14500 * - locally in the function results in a "used before set" warning
14501 * - using va_start() to initialize it gives "function with fixed args" error */
14502static va_list ap;
14503#endif
14504
Bram Moolenaar8c711452005-01-14 21:53:12 +000014505/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014506 * "printf()" function
14507 */
14508 static void
14509f_printf(argvars, rettv)
14510 typval_T *argvars;
14511 typval_T *rettv;
14512{
14513 rettv->v_type = VAR_STRING;
14514 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014515#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014516 {
14517 char_u buf[NUMBUFLEN];
14518 int len;
14519 char_u *s;
14520 int saved_did_emsg = did_emsg;
14521 char *fmt;
14522
14523 /* Get the required length, allocate the buffer and do it for real. */
14524 did_emsg = FALSE;
14525 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014526 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014527 if (!did_emsg)
14528 {
14529 s = alloc(len + 1);
14530 if (s != NULL)
14531 {
14532 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014533 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014534 }
14535 }
14536 did_emsg |= saved_did_emsg;
14537 }
14538#endif
14539}
14540
14541/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014542 * "pumvisible()" function
14543 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014544 static void
14545f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014546 typval_T *argvars UNUSED;
14547 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014548{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014549#ifdef FEAT_INS_EXPAND
14550 if (pum_visible())
14551 rettv->vval.v_number = 1;
14552#endif
14553}
14554
Bram Moolenaardb913952012-06-29 12:54:53 +020014555#ifdef FEAT_PYTHON3
14556/*
14557 * "py3eval()" function
14558 */
14559 static void
14560f_py3eval(argvars, rettv)
14561 typval_T *argvars;
14562 typval_T *rettv;
14563{
14564 char_u *str;
14565 char_u buf[NUMBUFLEN];
14566
14567 str = get_tv_string_buf(&argvars[0], buf);
14568 do_py3eval(str, rettv);
14569}
14570#endif
14571
14572#ifdef FEAT_PYTHON
14573/*
14574 * "pyeval()" function
14575 */
14576 static void
14577f_pyeval(argvars, rettv)
14578 typval_T *argvars;
14579 typval_T *rettv;
14580{
14581 char_u *str;
14582 char_u buf[NUMBUFLEN];
14583
14584 str = get_tv_string_buf(&argvars[0], buf);
14585 do_pyeval(str, rettv);
14586}
14587#endif
14588
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014589/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014590 * "range()" function
14591 */
14592 static void
14593f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 typval_T *argvars;
14595 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014596{
14597 long start;
14598 long end;
14599 long stride = 1;
14600 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604 if (argvars[1].v_type == VAR_UNKNOWN)
14605 {
14606 end = start - 1;
14607 start = 0;
14608 }
14609 else
14610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014614 }
14615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 if (error)
14617 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014619 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014620 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014621 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 else
14623 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014624 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014625 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014626 if (list_append_number(rettv->vval.v_list,
14627 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014629 }
14630}
14631
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014632/*
14633 * "readfile()" function
14634 */
14635 static void
14636f_readfile(argvars, rettv)
14637 typval_T *argvars;
14638 typval_T *rettv;
14639{
14640 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014641 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014642 char_u *fname;
14643 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014644 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14645 int io_size = sizeof(buf);
14646 int readlen; /* size of last fread() */
14647 char_u *prev = NULL; /* previously read bytes, if any */
14648 long prevlen = 0; /* length of data in prev */
14649 long prevsize = 0; /* size of prev buffer */
14650 long maxline = MAXLNUM;
14651 long cnt = 0;
14652 char_u *p; /* position in buf */
14653 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014654
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014655 if (argvars[1].v_type != VAR_UNKNOWN)
14656 {
14657 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14658 binary = TRUE;
14659 if (argvars[2].v_type != VAR_UNKNOWN)
14660 maxline = get_tv_number(&argvars[2]);
14661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014663 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014664 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014665
14666 /* Always open the file in binary mode, library functions have a mind of
14667 * their own about CR-LF conversion. */
14668 fname = get_tv_string(&argvars[0]);
14669 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14670 {
14671 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14672 return;
14673 }
14674
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014675 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014677 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014679 /* This for loop processes what was read, but is also entered at end
14680 * of file so that either:
14681 * - an incomplete line gets written
14682 * - a "binary" file gets an empty line at the end if it ends in a
14683 * newline. */
14684 for (p = buf, start = buf;
14685 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14686 ++p)
14687 {
14688 if (*p == '\n' || readlen <= 0)
14689 {
14690 listitem_T *li;
14691 char_u *s = NULL;
14692 long_u len = p - start;
14693
14694 /* Finished a line. Remove CRs before NL. */
14695 if (readlen > 0 && !binary)
14696 {
14697 while (len > 0 && start[len - 1] == '\r')
14698 --len;
14699 /* removal may cross back to the "prev" string */
14700 if (len == 0)
14701 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14702 --prevlen;
14703 }
14704 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014705 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706 else
14707 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014708 /* Change "prev" buffer to be the right size. This way
14709 * the bytes are only copied once, and very long lines are
14710 * allocated only once. */
14711 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014713 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715 prev = NULL; /* the list will own the string */
14716 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014717 }
14718 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014719 if (s == NULL)
14720 {
14721 do_outofmem_msg((long_u) prevlen + len + 1);
14722 failed = TRUE;
14723 break;
14724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014726 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 {
14728 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014730 break;
14731 }
14732 li->li_tv.v_type = VAR_STRING;
14733 li->li_tv.v_lock = 0;
14734 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014735 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014736
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014737 start = p + 1; /* step over newline */
14738 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739 break;
14740 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 else if (*p == NUL)
14742 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014743#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014744 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14745 * when finding the BF and check the previous two bytes. */
14746 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014747 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014748 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14749 * + 1, these may be in the "prev" string. */
14750 char_u back1 = p >= buf + 1 ? p[-1]
14751 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14752 char_u back2 = p >= buf + 2 ? p[-2]
14753 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14754 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014756 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014757 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 char_u *dest = p - 2;
14759
14760 /* Usually a BOM is at the beginning of a file, and so at
14761 * the beginning of a line; then we can just step over it.
14762 */
14763 if (start == dest)
14764 start = p + 1;
14765 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014766 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014767 /* have to shuffle buf to close gap */
14768 int adjust_prevlen = 0;
14769
14770 if (dest < buf)
14771 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014772 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014773 dest = buf;
14774 }
14775 if (readlen > p - buf + 1)
14776 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14777 readlen -= 3 - adjust_prevlen;
14778 prevlen -= adjust_prevlen;
14779 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014780 }
14781 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014782 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783#endif
14784 } /* for */
14785
14786 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14787 break;
14788 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014789 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014790 /* There's part of a line in buf, store it in "prev". */
14791 if (p - start + prevlen >= prevsize)
14792 {
14793 /* need bigger "prev" buffer */
14794 char_u *newprev;
14795
14796 /* A common use case is ordinary text files and "prev" gets a
14797 * fragment of a line, so the first allocation is made
14798 * small, to avoid repeatedly 'allocing' large and
14799 * 'reallocing' small. */
14800 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014801 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014802 else
14803 {
14804 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014805 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 prevsize = grow50pc > growmin ? grow50pc : growmin;
14807 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014808 newprev = prev == NULL ? alloc(prevsize)
14809 : vim_realloc(prev, prevsize);
14810 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014811 {
14812 do_outofmem_msg((long_u)prevsize);
14813 failed = TRUE;
14814 break;
14815 }
14816 prev = newprev;
14817 }
14818 /* Add the line part to end of "prev". */
14819 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014820 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014823
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014824 /*
14825 * For a negative line count use only the lines at the end of the file,
14826 * free the rest.
14827 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014828 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014829 while (cnt > -maxline)
14830 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014831 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014832 --cnt;
14833 }
14834
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014835 if (failed)
14836 {
14837 list_free(rettv->vval.v_list, TRUE);
14838 /* readfile doc says an empty list is returned on error */
14839 rettv->vval.v_list = list_alloc();
14840 }
14841
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014842 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014843 fclose(fd);
14844}
14845
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014846#if defined(FEAT_RELTIME)
14847static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14848
14849/*
14850 * Convert a List to proftime_T.
14851 * Return FAIL when there is something wrong.
14852 */
14853 static int
14854list2proftime(arg, tm)
14855 typval_T *arg;
14856 proftime_T *tm;
14857{
14858 long n1, n2;
14859 int error = FALSE;
14860
14861 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14862 || arg->vval.v_list->lv_len != 2)
14863 return FAIL;
14864 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14865 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14866# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014867 tm->HighPart = n1;
14868 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014869# else
14870 tm->tv_sec = n1;
14871 tm->tv_usec = n2;
14872# endif
14873 return error ? FAIL : OK;
14874}
14875#endif /* FEAT_RELTIME */
14876
14877/*
14878 * "reltime()" function
14879 */
14880 static void
14881f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014882 typval_T *argvars UNUSED;
14883 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014884{
14885#ifdef FEAT_RELTIME
14886 proftime_T res;
14887 proftime_T start;
14888
14889 if (argvars[0].v_type == VAR_UNKNOWN)
14890 {
14891 /* No arguments: get current time. */
14892 profile_start(&res);
14893 }
14894 else if (argvars[1].v_type == VAR_UNKNOWN)
14895 {
14896 if (list2proftime(&argvars[0], &res) == FAIL)
14897 return;
14898 profile_end(&res);
14899 }
14900 else
14901 {
14902 /* Two arguments: compute the difference. */
14903 if (list2proftime(&argvars[0], &start) == FAIL
14904 || list2proftime(&argvars[1], &res) == FAIL)
14905 return;
14906 profile_sub(&res, &start);
14907 }
14908
14909 if (rettv_list_alloc(rettv) == OK)
14910 {
14911 long n1, n2;
14912
14913# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014914 n1 = res.HighPart;
14915 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014916# else
14917 n1 = res.tv_sec;
14918 n2 = res.tv_usec;
14919# endif
14920 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14921 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14922 }
14923#endif
14924}
14925
14926/*
14927 * "reltimestr()" function
14928 */
14929 static void
14930f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014931 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014932 typval_T *rettv;
14933{
14934#ifdef FEAT_RELTIME
14935 proftime_T tm;
14936#endif
14937
14938 rettv->v_type = VAR_STRING;
14939 rettv->vval.v_string = NULL;
14940#ifdef FEAT_RELTIME
14941 if (list2proftime(&argvars[0], &tm) == OK)
14942 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14943#endif
14944}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014945
Bram Moolenaar0d660222005-01-07 21:51:51 +000014946#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14947static void make_connection __ARGS((void));
14948static int check_connection __ARGS((void));
14949
14950 static void
14951make_connection()
14952{
14953 if (X_DISPLAY == NULL
14954# ifdef FEAT_GUI
14955 && !gui.in_use
14956# endif
14957 )
14958 {
14959 x_force_connect = TRUE;
14960 setup_term_clip();
14961 x_force_connect = FALSE;
14962 }
14963}
14964
14965 static int
14966check_connection()
14967{
14968 make_connection();
14969 if (X_DISPLAY == NULL)
14970 {
14971 EMSG(_("E240: No connection to Vim server"));
14972 return FAIL;
14973 }
14974 return OK;
14975}
14976#endif
14977
14978#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014979static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014980
14981 static void
14982remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 typval_T *argvars;
14984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014985 int expr;
14986{
14987 char_u *server_name;
14988 char_u *keys;
14989 char_u *r = NULL;
14990 char_u buf[NUMBUFLEN];
14991# ifdef WIN32
14992 HWND w;
14993# else
14994 Window w;
14995# endif
14996
14997 if (check_restricted() || check_secure())
14998 return;
14999
15000# ifdef FEAT_X11
15001 if (check_connection() == FAIL)
15002 return;
15003# endif
15004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015005 server_name = get_tv_string_chk(&argvars[0]);
15006 if (server_name == NULL)
15007 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015008 keys = get_tv_string_buf(&argvars[1], buf);
15009# ifdef WIN32
15010 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15011# else
15012 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15013 < 0)
15014# endif
15015 {
15016 if (r != NULL)
15017 EMSG(r); /* sending worked but evaluation failed */
15018 else
15019 EMSG2(_("E241: Unable to send to %s"), server_name);
15020 return;
15021 }
15022
15023 rettv->vval.v_string = r;
15024
15025 if (argvars[2].v_type != VAR_UNKNOWN)
15026 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015027 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015028 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015029 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015030
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015031 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015032 v.di_tv.v_type = VAR_STRING;
15033 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 idvar = get_tv_string_chk(&argvars[2]);
15035 if (idvar != NULL)
15036 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015037 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015038 }
15039}
15040#endif
15041
15042/*
15043 * "remote_expr()" function
15044 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045 static void
15046f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015049{
15050 rettv->v_type = VAR_STRING;
15051 rettv->vval.v_string = NULL;
15052#ifdef FEAT_CLIENTSERVER
15053 remote_common(argvars, rettv, TRUE);
15054#endif
15055}
15056
15057/*
15058 * "remote_foreground()" function
15059 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060 static void
15061f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015062 typval_T *argvars UNUSED;
15063 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015064{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015065#ifdef FEAT_CLIENTSERVER
15066# ifdef WIN32
15067 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 {
15069 char_u *server_name = get_tv_string_chk(&argvars[0]);
15070
15071 if (server_name != NULL)
15072 serverForeground(server_name);
15073 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074# else
15075 /* Send a foreground() expression to the server. */
15076 argvars[1].v_type = VAR_STRING;
15077 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15078 argvars[2].v_type = VAR_UNKNOWN;
15079 remote_common(argvars, rettv, TRUE);
15080 vim_free(argvars[1].vval.v_string);
15081# endif
15082#endif
15083}
15084
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 static void
15086f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015087 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089{
15090#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 char_u *s = NULL;
15093# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015094 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097
15098 if (check_restricted() || check_secure())
15099 {
15100 rettv->vval.v_number = -1;
15101 return;
15102 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015103 serverid = get_tv_string_chk(&argvars[0]);
15104 if (serverid == NULL)
15105 {
15106 rettv->vval.v_number = -1;
15107 return; /* type error; errmsg already given */
15108 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015110 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111 if (n == 0)
15112 rettv->vval.v_number = -1;
15113 else
15114 {
15115 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15116 rettv->vval.v_number = (s != NULL);
15117 }
15118# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015119 if (check_connection() == FAIL)
15120 return;
15121
15122 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015123 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124# endif
15125
15126 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015128 char_u *retvar;
15129
Bram Moolenaar33570922005-01-25 22:26:29 +000015130 v.di_tv.v_type = VAR_STRING;
15131 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015132 retvar = get_tv_string_chk(&argvars[1]);
15133 if (retvar != NULL)
15134 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136 }
15137#else
15138 rettv->vval.v_number = -1;
15139#endif
15140}
15141
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 static void
15143f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015146{
15147 char_u *r = NULL;
15148
15149#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 char_u *serverid = get_tv_string_chk(&argvars[0]);
15151
15152 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 {
15154# ifdef WIN32
15155 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015156 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015158 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 if (n != 0)
15160 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15161 if (r == NULL)
15162# else
15163 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165# endif
15166 EMSG(_("E277: Unable to read a server reply"));
15167 }
15168#endif
15169 rettv->v_type = VAR_STRING;
15170 rettv->vval.v_string = r;
15171}
15172
15173/*
15174 * "remote_send()" function
15175 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 static void
15177f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015178 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015179 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180{
15181 rettv->v_type = VAR_STRING;
15182 rettv->vval.v_string = NULL;
15183#ifdef FEAT_CLIENTSERVER
15184 remote_common(argvars, rettv, FALSE);
15185#endif
15186}
15187
15188/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015189 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 */
15191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015192f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *argvars;
15194 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015195{
Bram Moolenaar33570922005-01-25 22:26:29 +000015196 list_T *l;
15197 listitem_T *item, *item2;
15198 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015199 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015200 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 dict_T *d;
15203 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015204 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015205
Bram Moolenaar8c711452005-01-14 21:53:12 +000015206 if (argvars[0].v_type == VAR_DICT)
15207 {
15208 if (argvars[2].v_type != VAR_UNKNOWN)
15209 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015210 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015211 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 key = get_tv_string_chk(&argvars[1]);
15214 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 di = dict_find(d, key, -1);
15217 if (di == NULL)
15218 EMSG2(_(e_dictkey), key);
15219 else
15220 {
15221 *rettv = di->di_tv;
15222 init_tv(&di->di_tv);
15223 dictitem_remove(d, di);
15224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226 }
15227 }
15228 else if (argvars[0].v_type != VAR_LIST)
15229 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015230 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015231 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 int error = FALSE;
15234
15235 idx = get_tv_number_chk(&argvars[1], &error);
15236 if (error)
15237 ; /* type error: do nothing, errmsg already given */
15238 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239 EMSGN(_(e_listidx), idx);
15240 else
15241 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015242 if (argvars[2].v_type == VAR_UNKNOWN)
15243 {
15244 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015245 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015246 *rettv = item->li_tv;
15247 vim_free(item);
15248 }
15249 else
15250 {
15251 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015252 end = get_tv_number_chk(&argvars[2], &error);
15253 if (error)
15254 ; /* type error: do nothing */
15255 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015256 EMSGN(_(e_listidx), end);
15257 else
15258 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015259 int cnt = 0;
15260
15261 for (li = item; li != NULL; li = li->li_next)
15262 {
15263 ++cnt;
15264 if (li == item2)
15265 break;
15266 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015267 if (li == NULL) /* didn't find "item2" after "item" */
15268 EMSG(_(e_invrange));
15269 else
15270 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015271 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015272 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015274 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015275 l->lv_first = item;
15276 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015277 item->li_prev = NULL;
15278 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015279 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015280 }
15281 }
15282 }
15283 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015284 }
15285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286}
15287
15288/*
15289 * "rename({from}, {to})" function
15290 */
15291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *argvars;
15294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295{
15296 char_u buf[NUMBUFLEN];
15297
15298 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15302 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303}
15304
15305/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015306 * "repeat()" function
15307 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015308 static void
15309f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 typval_T *argvars;
15311 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015312{
15313 char_u *p;
15314 int n;
15315 int slen;
15316 int len;
15317 char_u *r;
15318 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015319
15320 n = get_tv_number(&argvars[1]);
15321 if (argvars[0].v_type == VAR_LIST)
15322 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015323 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015325 if (list_extend(rettv->vval.v_list,
15326 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015327 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015328 }
15329 else
15330 {
15331 p = get_tv_string(&argvars[0]);
15332 rettv->v_type = VAR_STRING;
15333 rettv->vval.v_string = NULL;
15334
15335 slen = (int)STRLEN(p);
15336 len = slen * n;
15337 if (len <= 0)
15338 return;
15339
15340 r = alloc(len + 1);
15341 if (r != NULL)
15342 {
15343 for (i = 0; i < n; i++)
15344 mch_memmove(r + i * slen, p, (size_t)slen);
15345 r[len] = NUL;
15346 }
15347
15348 rettv->vval.v_string = r;
15349 }
15350}
15351
15352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 * "resolve()" function
15354 */
15355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 typval_T *argvars;
15358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359{
15360 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015361#ifdef HAVE_READLINK
15362 char_u *buf = NULL;
15363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366#ifdef FEAT_SHORTCUT
15367 {
15368 char_u *v = NULL;
15369
15370 v = mch_resolve_shortcut(p);
15371 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 }
15376#else
15377# ifdef HAVE_READLINK
15378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 char_u *cpy;
15380 int len;
15381 char_u *remain = NULL;
15382 char_u *q;
15383 int is_relative_to_current = FALSE;
15384 int has_trailing_pathsep = FALSE;
15385 int limit = 100;
15386
15387 p = vim_strsave(p);
15388
15389 if (p[0] == '.' && (vim_ispathsep(p[1])
15390 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15391 is_relative_to_current = TRUE;
15392
15393 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015394 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015397 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399
15400 q = getnextcomp(p);
15401 if (*q != NUL)
15402 {
15403 /* Separate the first path component in "p", and keep the
15404 * remainder (beginning with the path separator). */
15405 remain = vim_strsave(q - 1);
15406 q[-1] = NUL;
15407 }
15408
Bram Moolenaard9462e32011-04-11 21:35:11 +020015409 buf = alloc(MAXPATHL + 1);
15410 if (buf == NULL)
15411 goto fail;
15412
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 for (;;)
15414 {
15415 for (;;)
15416 {
15417 len = readlink((char *)p, (char *)buf, MAXPATHL);
15418 if (len <= 0)
15419 break;
15420 buf[len] = NUL;
15421
15422 if (limit-- == 0)
15423 {
15424 vim_free(p);
15425 vim_free(remain);
15426 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428 goto fail;
15429 }
15430
15431 /* Ensure that the result will have a trailing path separator
15432 * if the argument has one. */
15433 if (remain == NULL && has_trailing_pathsep)
15434 add_pathsep(buf);
15435
15436 /* Separate the first path component in the link value and
15437 * concatenate the remainders. */
15438 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15439 if (*q != NUL)
15440 {
15441 if (remain == NULL)
15442 remain = vim_strsave(q - 1);
15443 else
15444 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015445 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 if (cpy != NULL)
15447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 vim_free(remain);
15449 remain = cpy;
15450 }
15451 }
15452 q[-1] = NUL;
15453 }
15454
15455 q = gettail(p);
15456 if (q > p && *q == NUL)
15457 {
15458 /* Ignore trailing path separator. */
15459 q[-1] = NUL;
15460 q = gettail(p);
15461 }
15462 if (q > p && !mch_isFullName(buf))
15463 {
15464 /* symlink is relative to directory of argument */
15465 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15466 if (cpy != NULL)
15467 {
15468 STRCPY(cpy, p);
15469 STRCPY(gettail(cpy), buf);
15470 vim_free(p);
15471 p = cpy;
15472 }
15473 }
15474 else
15475 {
15476 vim_free(p);
15477 p = vim_strsave(buf);
15478 }
15479 }
15480
15481 if (remain == NULL)
15482 break;
15483
15484 /* Append the first path component of "remain" to "p". */
15485 q = getnextcomp(remain + 1);
15486 len = q - remain - (*q != NUL);
15487 cpy = vim_strnsave(p, STRLEN(p) + len);
15488 if (cpy != NULL)
15489 {
15490 STRNCAT(cpy, remain, len);
15491 vim_free(p);
15492 p = cpy;
15493 }
15494 /* Shorten "remain". */
15495 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015496 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 else
15498 {
15499 vim_free(remain);
15500 remain = NULL;
15501 }
15502 }
15503
15504 /* If the result is a relative path name, make it explicitly relative to
15505 * the current directory if and only if the argument had this form. */
15506 if (!vim_ispathsep(*p))
15507 {
15508 if (is_relative_to_current
15509 && *p != NUL
15510 && !(p[0] == '.'
15511 && (p[1] == NUL
15512 || vim_ispathsep(p[1])
15513 || (p[1] == '.'
15514 && (p[2] == NUL
15515 || vim_ispathsep(p[2]))))))
15516 {
15517 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015518 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 if (cpy != NULL)
15520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 vim_free(p);
15522 p = cpy;
15523 }
15524 }
15525 else if (!is_relative_to_current)
15526 {
15527 /* Strip leading "./". */
15528 q = p;
15529 while (q[0] == '.' && vim_ispathsep(q[1]))
15530 q += 2;
15531 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015532 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 }
15534 }
15535
15536 /* Ensure that the result will have no trailing path separator
15537 * if the argument had none. But keep "/" or "//". */
15538 if (!has_trailing_pathsep)
15539 {
15540 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015541 if (after_pathsep(p, q))
15542 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 }
15544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 }
15547# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549# endif
15550#endif
15551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553
15554#ifdef HAVE_READLINK
15555fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015556 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 */
15564 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015565f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 typval_T *argvars;
15567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568{
Bram Moolenaar33570922005-01-25 22:26:29 +000015569 list_T *l;
15570 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 if (argvars[0].v_type != VAR_LIST)
15573 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015574 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015575 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 {
15577 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015578 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015579 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015580 while (li != NULL)
15581 {
15582 ni = li->li_prev;
15583 list_append(l, li);
15584 li = ni;
15585 }
15586 rettv->vval.v_list = l;
15587 rettv->v_type = VAR_LIST;
15588 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015589 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591}
15592
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015593#define SP_NOMOVE 0x01 /* don't move cursor */
15594#define SP_REPEAT 0x02 /* repeat to find outer pair */
15595#define SP_RETCOUNT 0x04 /* return matchcount */
15596#define SP_SETPCMARK 0x08 /* set previous context mark */
15597#define SP_START 0x10 /* accept match at start position */
15598#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15599#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015600
Bram Moolenaar33570922005-01-25 22:26:29 +000015601static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602
15603/*
15604 * Get flags for a search function.
15605 * Possibly sets "p_ws".
15606 * Returns BACKWARD, FORWARD or zero (for an error).
15607 */
15608 static int
15609get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611 int *flagsp;
15612{
15613 int dir = FORWARD;
15614 char_u *flags;
15615 char_u nbuf[NUMBUFLEN];
15616 int mask;
15617
15618 if (varp->v_type != VAR_UNKNOWN)
15619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015620 flags = get_tv_string_buf_chk(varp, nbuf);
15621 if (flags == NULL)
15622 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623 while (*flags != NUL)
15624 {
15625 switch (*flags)
15626 {
15627 case 'b': dir = BACKWARD; break;
15628 case 'w': p_ws = TRUE; break;
15629 case 'W': p_ws = FALSE; break;
15630 default: mask = 0;
15631 if (flagsp != NULL)
15632 switch (*flags)
15633 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015634 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015635 case 'e': mask = SP_END; break;
15636 case 'm': mask = SP_RETCOUNT; break;
15637 case 'n': mask = SP_NOMOVE; break;
15638 case 'p': mask = SP_SUBPAT; break;
15639 case 'r': mask = SP_REPEAT; break;
15640 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641 }
15642 if (mask == 0)
15643 {
15644 EMSG2(_(e_invarg2), flags);
15645 dir = 0;
15646 }
15647 else
15648 *flagsp |= mask;
15649 }
15650 if (dir == 0)
15651 break;
15652 ++flags;
15653 }
15654 }
15655 return dir;
15656}
15657
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015659 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015661 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015662search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015663 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015664 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015665 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015667 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 char_u *pat;
15669 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015670 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 int save_p_ws = p_ws;
15672 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015674 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015675 proftime_T tm;
15676#ifdef FEAT_RELTIME
15677 long time_limit = 0;
15678#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015679 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015680 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015682 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015684 if (dir == 0)
15685 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015686 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015687 if (flags & SP_START)
15688 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015689 if (flags & SP_END)
15690 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015691
Bram Moolenaar76929292008-01-06 19:07:36 +000015692 /* Optional arguments: line number to stop searching and timeout. */
15693 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015694 {
15695 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15696 if (lnum_stop < 0)
15697 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015698#ifdef FEAT_RELTIME
15699 if (argvars[3].v_type != VAR_UNKNOWN)
15700 {
15701 time_limit = get_tv_number_chk(&argvars[3], NULL);
15702 if (time_limit < 0)
15703 goto theend;
15704 }
15705#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015706 }
15707
Bram Moolenaar76929292008-01-06 19:07:36 +000015708#ifdef FEAT_RELTIME
15709 /* Set the time limit, if there is one. */
15710 profile_setlimit(time_limit, &tm);
15711#endif
15712
Bram Moolenaar231334e2005-07-25 20:46:57 +000015713 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015714 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015715 * Check to make sure only those flags are set.
15716 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15717 * flags cannot be set. Check for that condition also.
15718 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015719 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015720 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015723 goto theend;
15724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015726 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015727 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015728 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015729 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015731 if (flags & SP_SUBPAT)
15732 retval = subpatnum;
15733 else
15734 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015735 if (flags & SP_SETPCMARK)
15736 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015738 if (match_pos != NULL)
15739 {
15740 /* Store the match cursor position */
15741 match_pos->lnum = pos.lnum;
15742 match_pos->col = pos.col + 1;
15743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 /* "/$" will put the cursor after the end of the line, may need to
15745 * correct that here */
15746 check_cursor();
15747 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015748
15749 /* If 'n' flag is used: restore cursor position. */
15750 if (flags & SP_NOMOVE)
15751 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015752 else
15753 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015754theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015756
15757 return retval;
15758}
15759
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015760#ifdef FEAT_FLOAT
15761/*
15762 * "round({float})" function
15763 */
15764 static void
15765f_round(argvars, rettv)
15766 typval_T *argvars;
15767 typval_T *rettv;
15768{
15769 float_T f;
15770
15771 rettv->v_type = VAR_FLOAT;
15772 if (get_float_arg(argvars, &f) == OK)
15773 /* round() is not in C90, use ceil() or floor() instead. */
15774 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15775 else
15776 rettv->vval.v_float = 0.0;
15777}
15778#endif
15779
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015780/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015781 * "screencol()" function
15782 *
15783 * First column is 1 to be consistent with virtcol().
15784 */
15785 static void
15786f_screencol(argvars, rettv)
15787 typval_T *argvars UNUSED;
15788 typval_T *rettv;
15789{
15790 rettv->vval.v_number = screen_screencol() + 1;
15791}
15792
15793/*
15794 * "screenrow()" function
15795 */
15796 static void
15797f_screenrow(argvars, rettv)
15798 typval_T *argvars UNUSED;
15799 typval_T *rettv;
15800{
15801 rettv->vval.v_number = screen_screenrow() + 1;
15802}
15803
15804/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015805 * "search()" function
15806 */
15807 static void
15808f_search(argvars, rettv)
15809 typval_T *argvars;
15810 typval_T *rettv;
15811{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015812 int flags = 0;
15813
15814 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815}
15816
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015818 * "searchdecl()" function
15819 */
15820 static void
15821f_searchdecl(argvars, rettv)
15822 typval_T *argvars;
15823 typval_T *rettv;
15824{
15825 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015826 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015827 int error = FALSE;
15828 char_u *name;
15829
15830 rettv->vval.v_number = 1; /* default: FAIL */
15831
15832 name = get_tv_string_chk(&argvars[0]);
15833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015834 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015835 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015836 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15837 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15838 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015839 if (!error && name != NULL)
15840 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015841 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015842}
15843
15844/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015845 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 static int
15848searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015849 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015850 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851{
15852 char_u *spat, *mpat, *epat;
15853 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 int dir;
15856 int flags = 0;
15857 char_u nbuf1[NUMBUFLEN];
15858 char_u nbuf2[NUMBUFLEN];
15859 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015860 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015861 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015862 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015865 spat = get_tv_string_chk(&argvars[0]);
15866 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15867 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15868 if (spat == NULL || mpat == NULL || epat == NULL)
15869 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871 /* Handle the optional fourth argument: flags */
15872 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015873 if (dir == 0)
15874 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015875
15876 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015877 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15878 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015879 if ((flags & (SP_END | SP_SUBPAT)) != 0
15880 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015881 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015882 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015883 goto theend;
15884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015886 /* Using 'r' implies 'W', otherwise it doesn't work. */
15887 if (flags & SP_REPEAT)
15888 p_ws = FALSE;
15889
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015890 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015891 if (argvars[3].v_type == VAR_UNKNOWN
15892 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893 skip = (char_u *)"";
15894 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015896 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015897 if (argvars[5].v_type != VAR_UNKNOWN)
15898 {
15899 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15900 if (lnum_stop < 0)
15901 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015902#ifdef FEAT_RELTIME
15903 if (argvars[6].v_type != VAR_UNKNOWN)
15904 {
15905 time_limit = get_tv_number_chk(&argvars[6], NULL);
15906 if (time_limit < 0)
15907 goto theend;
15908 }
15909#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015910 }
15911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 if (skip == NULL)
15913 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015915 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015916 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015917
15918theend:
15919 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015920
15921 return retval;
15922}
15923
15924/*
15925 * "searchpair()" function
15926 */
15927 static void
15928f_searchpair(argvars, rettv)
15929 typval_T *argvars;
15930 typval_T *rettv;
15931{
15932 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15933}
15934
15935/*
15936 * "searchpairpos()" function
15937 */
15938 static void
15939f_searchpairpos(argvars, rettv)
15940 typval_T *argvars;
15941 typval_T *rettv;
15942{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015943 pos_T match_pos;
15944 int lnum = 0;
15945 int col = 0;
15946
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015947 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015948 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015949
15950 if (searchpair_cmn(argvars, &match_pos) > 0)
15951 {
15952 lnum = match_pos.lnum;
15953 col = match_pos.col;
15954 }
15955
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015956 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15957 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015958}
15959
15960/*
15961 * Search for a start/middle/end thing.
15962 * Used by searchpair(), see its documentation for the details.
15963 * Returns 0 or -1 for no match,
15964 */
15965 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015966do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15967 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015968 char_u *spat; /* start pattern */
15969 char_u *mpat; /* middle pattern */
15970 char_u *epat; /* end pattern */
15971 int dir; /* BACKWARD or FORWARD */
15972 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015973 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015974 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015975 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015976 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015977{
15978 char_u *save_cpo;
15979 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15980 long retval = 0;
15981 pos_T pos;
15982 pos_T firstpos;
15983 pos_T foundpos;
15984 pos_T save_cursor;
15985 pos_T save_pos;
15986 int n;
15987 int r;
15988 int nest = 1;
15989 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015990 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015991 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015992
15993 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15994 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015995 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015996
Bram Moolenaar76929292008-01-06 19:07:36 +000015997#ifdef FEAT_RELTIME
15998 /* Set the time limit, if there is one. */
15999 profile_setlimit(time_limit, &tm);
16000#endif
16001
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016002 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16003 * start/middle/end (pat3, for the top pair). */
16004 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16005 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16006 if (pat2 == NULL || pat3 == NULL)
16007 goto theend;
16008 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16009 if (*mpat == NUL)
16010 STRCPY(pat3, pat2);
16011 else
16012 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16013 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016014 if (flags & SP_START)
16015 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016016
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 save_cursor = curwin->w_cursor;
16018 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016019 clearpos(&firstpos);
16020 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 pat = pat3;
16022 for (;;)
16023 {
16024 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016025 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16027 /* didn't find it or found the first match again: FAIL */
16028 break;
16029
16030 if (firstpos.lnum == 0)
16031 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016032 if (equalpos(pos, foundpos))
16033 {
16034 /* Found the same position again. Can happen with a pattern that
16035 * has "\zs" at the end and searching backwards. Advance one
16036 * character and try again. */
16037 if (dir == BACKWARD)
16038 decl(&pos);
16039 else
16040 incl(&pos);
16041 }
16042 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016044 /* clear the start flag to avoid getting stuck here */
16045 options &= ~SEARCH_START;
16046
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047 /* If the skip pattern matches, ignore this match. */
16048 if (*skip != NUL)
16049 {
16050 save_pos = curwin->w_cursor;
16051 curwin->w_cursor = pos;
16052 r = eval_to_bool(skip, &err, NULL, FALSE);
16053 curwin->w_cursor = save_pos;
16054 if (err)
16055 {
16056 /* Evaluating {skip} caused an error, break here. */
16057 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016058 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 break;
16060 }
16061 if (r)
16062 continue;
16063 }
16064
16065 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16066 {
16067 /* Found end when searching backwards or start when searching
16068 * forward: nested pair. */
16069 ++nest;
16070 pat = pat2; /* nested, don't search for middle */
16071 }
16072 else
16073 {
16074 /* Found end when searching forward or start when searching
16075 * backward: end of (nested) pair; or found middle in outer pair. */
16076 if (--nest == 1)
16077 pat = pat3; /* outer level, search for middle */
16078 }
16079
16080 if (nest == 0)
16081 {
16082 /* Found the match: return matchcount or line number. */
16083 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016084 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016086 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016087 if (flags & SP_SETPCMARK)
16088 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 curwin->w_cursor = pos;
16090 if (!(flags & SP_REPEAT))
16091 break;
16092 nest = 1; /* search for next unmatched */
16093 }
16094 }
16095
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016096 if (match_pos != NULL)
16097 {
16098 /* Store the match cursor position */
16099 match_pos->lnum = curwin->w_cursor.lnum;
16100 match_pos->col = curwin->w_cursor.col + 1;
16101 }
16102
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016104 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016105 curwin->w_cursor = save_cursor;
16106
16107theend:
16108 vim_free(pat2);
16109 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016110 if (p_cpo == empty_option)
16111 p_cpo = save_cpo;
16112 else
16113 /* Darn, evaluating the {skip} expression changed the value. */
16114 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016115
16116 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117}
16118
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016119/*
16120 * "searchpos()" function
16121 */
16122 static void
16123f_searchpos(argvars, rettv)
16124 typval_T *argvars;
16125 typval_T *rettv;
16126{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016127 pos_T match_pos;
16128 int lnum = 0;
16129 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016130 int n;
16131 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016132
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016133 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016134 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016136 n = search_cmn(argvars, &match_pos, &flags);
16137 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016138 {
16139 lnum = match_pos.lnum;
16140 col = match_pos.col;
16141 }
16142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16144 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016145 if (flags & SP_SUBPAT)
16146 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016147}
16148
16149
Bram Moolenaar0d660222005-01-07 21:51:51 +000016150 static void
16151f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016155#ifdef FEAT_CLIENTSERVER
16156 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 char_u *server = get_tv_string_chk(&argvars[0]);
16158 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016161 if (server == NULL || reply == NULL)
16162 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016163 if (check_restricted() || check_secure())
16164 return;
16165# ifdef FEAT_X11
16166 if (check_connection() == FAIL)
16167 return;
16168# endif
16169
16170 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 EMSG(_("E258: Unable to send to client"));
16173 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175 rettv->vval.v_number = 0;
16176#else
16177 rettv->vval.v_number = -1;
16178#endif
16179}
16180
Bram Moolenaar0d660222005-01-07 21:51:51 +000016181 static void
16182f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016183 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185{
16186 char_u *r = NULL;
16187
16188#ifdef FEAT_CLIENTSERVER
16189# ifdef WIN32
16190 r = serverGetVimNames();
16191# else
16192 make_connection();
16193 if (X_DISPLAY != NULL)
16194 r = serverGetVimNames(X_DISPLAY);
16195# endif
16196#endif
16197 rettv->v_type = VAR_STRING;
16198 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199}
16200
16201/*
16202 * "setbufvar()" function
16203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016205f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016206 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016207 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208{
16209 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016212 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 char_u nbuf[NUMBUFLEN];
16214
16215 if (check_restricted() || check_secure())
16216 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016217 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16218 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016219 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220 varp = &argvars[2];
16221
16222 if (buf != NULL && varname != NULL && varp != NULL)
16223 {
16224 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226
16227 if (*varname == '&')
16228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016229 long numval;
16230 char_u *strval;
16231 int error = FALSE;
16232
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016234 numval = get_tv_number_chk(varp, &error);
16235 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 if (!error && strval != NULL)
16237 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 }
16239 else
16240 {
16241 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16242 if (bufvarname != NULL)
16243 {
16244 STRCPY(bufvarname, "b:");
16245 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016246 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 vim_free(bufvarname);
16248 }
16249 }
16250
16251 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
16256/*
16257 * "setcmdpos()" function
16258 */
16259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016261 typval_T *argvars;
16262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 int pos = (int)get_tv_number(&argvars[0]) - 1;
16265
16266 if (pos >= 0)
16267 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268}
16269
16270/*
16271 * "setline()" function
16272 */
16273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 typval_T *argvars;
16276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277{
16278 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016279 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016280 list_T *l = NULL;
16281 listitem_T *li = NULL;
16282 long added = 0;
16283 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016285 lnum = get_tv_lnum(&argvars[0]);
16286 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016288 l = argvars[1].vval.v_list;
16289 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016291 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016292 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016293
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016294 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016295 for (;;)
16296 {
16297 if (l != NULL)
16298 {
16299 /* list argument, get next string */
16300 if (li == NULL)
16301 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016302 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016303 li = li->li_next;
16304 }
16305
16306 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016307 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016308 break;
16309 if (lnum <= curbuf->b_ml.ml_line_count)
16310 {
16311 /* existing line, replace it */
16312 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16313 {
16314 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016315 if (lnum == curwin->w_cursor.lnum)
16316 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016317 rettv->vval.v_number = 0; /* OK */
16318 }
16319 }
16320 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16321 {
16322 /* lnum is one past the last line, append the line */
16323 ++added;
16324 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16325 rettv->vval.v_number = 0; /* OK */
16326 }
16327
16328 if (l == NULL) /* only one string argument */
16329 break;
16330 ++lnum;
16331 }
16332
16333 if (added > 0)
16334 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016337static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16338
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016340 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016341 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016342 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016343set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016344 win_T *wp UNUSED;
16345 typval_T *list_arg UNUSED;
16346 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016347 typval_T *rettv;
16348{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016349#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016350 char_u *act;
16351 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016352#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016353
Bram Moolenaar2641f772005-03-25 21:58:17 +000016354 rettv->vval.v_number = -1;
16355
16356#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016357 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016358 EMSG(_(e_listreq));
16359 else
16360 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016361 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016362
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016363 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016364 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016365 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 if (act == NULL)
16367 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016368 if (*act == 'a' || *act == 'r')
16369 action = *act;
16370 }
16371
Bram Moolenaar81484f42012-12-05 15:16:47 +010016372 if (l != NULL && set_errorlist(wp, l, action,
16373 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016374 rettv->vval.v_number = 0;
16375 }
16376#endif
16377}
16378
16379/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016380 * "setloclist()" function
16381 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016382 static void
16383f_setloclist(argvars, rettv)
16384 typval_T *argvars;
16385 typval_T *rettv;
16386{
16387 win_T *win;
16388
16389 rettv->vval.v_number = -1;
16390
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016391 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016392 if (win != NULL)
16393 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16394}
16395
16396/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016397 * "setmatches()" function
16398 */
16399 static void
16400f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016401 typval_T *argvars UNUSED;
16402 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016403{
16404#ifdef FEAT_SEARCH_EXTRA
16405 list_T *l;
16406 listitem_T *li;
16407 dict_T *d;
16408
16409 rettv->vval.v_number = -1;
16410 if (argvars[0].v_type != VAR_LIST)
16411 {
16412 EMSG(_(e_listreq));
16413 return;
16414 }
16415 if ((l = argvars[0].vval.v_list) != NULL)
16416 {
16417
16418 /* To some extent make sure that we are dealing with a list from
16419 * "getmatches()". */
16420 li = l->lv_first;
16421 while (li != NULL)
16422 {
16423 if (li->li_tv.v_type != VAR_DICT
16424 || (d = li->li_tv.vval.v_dict) == NULL)
16425 {
16426 EMSG(_(e_invarg));
16427 return;
16428 }
16429 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16430 && dict_find(d, (char_u *)"pattern", -1) != NULL
16431 && dict_find(d, (char_u *)"priority", -1) != NULL
16432 && dict_find(d, (char_u *)"id", -1) != NULL))
16433 {
16434 EMSG(_(e_invarg));
16435 return;
16436 }
16437 li = li->li_next;
16438 }
16439
16440 clear_matches(curwin);
16441 li = l->lv_first;
16442 while (li != NULL)
16443 {
16444 d = li->li_tv.vval.v_dict;
16445 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16446 get_dict_string(d, (char_u *)"pattern", FALSE),
16447 (int)get_dict_number(d, (char_u *)"priority"),
16448 (int)get_dict_number(d, (char_u *)"id"));
16449 li = li->li_next;
16450 }
16451 rettv->vval.v_number = 0;
16452 }
16453#endif
16454}
16455
16456/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016457 * "setpos()" function
16458 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016459 static void
16460f_setpos(argvars, rettv)
16461 typval_T *argvars;
16462 typval_T *rettv;
16463{
16464 pos_T pos;
16465 int fnum;
16466 char_u *name;
16467
Bram Moolenaar08250432008-02-13 11:42:46 +000016468 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016469 name = get_tv_string_chk(argvars);
16470 if (name != NULL)
16471 {
16472 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16473 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016474 if (--pos.col < 0)
16475 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016476 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016477 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016478 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016479 if (fnum == curbuf->b_fnum)
16480 {
16481 curwin->w_cursor = pos;
16482 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016483 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016484 }
16485 else
16486 EMSG(_(e_invarg));
16487 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016488 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16489 {
16490 /* set mark */
16491 if (setmark_pos(name[1], &pos, fnum) == OK)
16492 rettv->vval.v_number = 0;
16493 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016494 else
16495 EMSG(_(e_invarg));
16496 }
16497 }
16498}
16499
16500/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016501 * "setqflist()" function
16502 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016503 static void
16504f_setqflist(argvars, rettv)
16505 typval_T *argvars;
16506 typval_T *rettv;
16507{
16508 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16509}
16510
16511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016512 * "setreg()" function
16513 */
16514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016515f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016516 typval_T *argvars;
16517 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016518{
16519 int regname;
16520 char_u *strregname;
16521 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523 int append;
16524 char_u yank_type;
16525 long block_len;
16526
16527 block_len = -1;
16528 yank_type = MAUTO;
16529 append = FALSE;
16530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016532 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016534 if (strregname == NULL)
16535 return; /* type error; errmsg already given */
16536 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 if (regname == 0 || regname == '@')
16538 regname = '"';
16539 else if (regname == '=')
16540 return;
16541
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016544 stropt = get_tv_string_chk(&argvars[2]);
16545 if (stropt == NULL)
16546 return; /* type error */
16547 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548 switch (*stropt)
16549 {
16550 case 'a': case 'A': /* append */
16551 append = TRUE;
16552 break;
16553 case 'v': case 'c': /* character-wise selection */
16554 yank_type = MCHAR;
16555 break;
16556 case 'V': case 'l': /* line-wise selection */
16557 yank_type = MLINE;
16558 break;
16559#ifdef FEAT_VISUAL
16560 case 'b': case Ctrl_V: /* block-wise selection */
16561 yank_type = MBLOCK;
16562 if (VIM_ISDIGIT(stropt[1]))
16563 {
16564 ++stropt;
16565 block_len = getdigits(&stropt) - 1;
16566 --stropt;
16567 }
16568 break;
16569#endif
16570 }
16571 }
16572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 strval = get_tv_string_chk(&argvars[1]);
16574 if (strval != NULL)
16575 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016576 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016577 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578}
16579
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016580/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016581 * "settabvar()" function
16582 */
16583 static void
16584f_settabvar(argvars, rettv)
16585 typval_T *argvars;
16586 typval_T *rettv;
16587{
16588 tabpage_T *save_curtab;
16589 char_u *varname, *tabvarname;
16590 typval_T *varp;
16591 tabpage_T *tp;
16592
16593 rettv->vval.v_number = 0;
16594
16595 if (check_restricted() || check_secure())
16596 return;
16597
16598 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16599 varname = get_tv_string_chk(&argvars[1]);
16600 varp = &argvars[2];
16601
16602 if (tp != NULL && varname != NULL && varp != NULL)
16603 {
16604 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016605 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016606
16607 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16608 if (tabvarname != NULL)
16609 {
16610 STRCPY(tabvarname, "t:");
16611 STRCPY(tabvarname + 2, varname);
16612 set_var(tabvarname, varp, TRUE);
16613 vim_free(tabvarname);
16614 }
16615
16616 /* Restore current tabpage */
16617 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016618 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016619 }
16620}
16621
16622/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016623 * "settabwinvar()" function
16624 */
16625 static void
16626f_settabwinvar(argvars, rettv)
16627 typval_T *argvars;
16628 typval_T *rettv;
16629{
16630 setwinvar(argvars, rettv, 1);
16631}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632
16633/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016634 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016637f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016638 typval_T *argvars;
16639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016641 setwinvar(argvars, rettv, 0);
16642}
16643
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016644 int
16645switch_win(save_curwin, save_curtab, win, tp)
16646 win_T **save_curwin;
16647 tabpage_T **save_curtab;
16648 win_T *win;
16649 tabpage_T *tp;
16650{
16651#ifdef FEAT_WINDOWS
16652 /* set curwin to be our win, temporarily */
16653 *save_curwin = curwin;
16654 *save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016655 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016656 if (!win_valid(win))
16657 return FAIL;
16658 curwin = win;
16659 curbuf = curwin->w_buffer;
16660#endif
16661 return OK;
16662}
16663
16664 void
16665restore_win(save_curwin, save_curtab)
16666 win_T *save_curwin;
16667 tabpage_T *save_curtab;
16668{
16669#ifdef FEAT_WINDOWS
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020016670 /* Restore current tabpage and window, if still valid (autocommands can
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016671 * make them invalid). */
16672 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016673 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016674 if (win_valid(save_curwin))
16675 {
16676 curwin = save_curwin;
16677 curbuf = curwin->w_buffer;
16678 }
16679#endif
16680}
16681
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016682/*
16683 * "setwinvar()" and "settabwinvar()" functions
16684 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016685
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016686 static void
16687setwinvar(argvars, rettv, off)
16688 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016689 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016690 int off;
16691{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 win_T *win;
16693#ifdef FEAT_WINDOWS
16694 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016695 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696#endif
16697 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016698 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016700 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701
16702 if (check_restricted() || check_secure())
16703 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016704
16705#ifdef FEAT_WINDOWS
16706 if (off == 1)
16707 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16708 else
16709 tp = curtab;
16710#endif
16711 win = find_win_by_nr(&argvars[off], tp);
16712 varname = get_tv_string_chk(&argvars[off + 1]);
16713 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714
16715 if (win != NULL && varname != NULL && varp != NULL)
16716 {
16717#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016718 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016719 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720#endif
16721
16722 if (*varname == '&')
16723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016724 long numval;
16725 char_u *strval;
16726 int error = FALSE;
16727
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 numval = get_tv_number_chk(varp, &error);
16730 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 if (!error && strval != NULL)
16732 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 }
16734 else
16735 {
16736 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16737 if (winvarname != NULL)
16738 {
16739 STRCPY(winvarname, "w:");
16740 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016741 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016742 vim_free(winvarname);
16743 }
16744 }
16745
16746#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016747 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748#endif
16749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750}
16751
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016752#ifdef FEAT_CRYPT
16753/*
16754 * "sha256({string})" function
16755 */
16756 static void
16757f_sha256(argvars, rettv)
16758 typval_T *argvars;
16759 typval_T *rettv;
16760{
16761 char_u *p;
16762
16763 p = get_tv_string(&argvars[0]);
16764 rettv->vval.v_string = vim_strsave(
16765 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16766 rettv->v_type = VAR_STRING;
16767}
16768#endif /* FEAT_CRYPT */
16769
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016771 * "shellescape({string})" function
16772 */
16773 static void
16774f_shellescape(argvars, rettv)
16775 typval_T *argvars;
16776 typval_T *rettv;
16777{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016778 rettv->vval.v_string = vim_strsave_shellescape(
16779 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016780 rettv->v_type = VAR_STRING;
16781}
16782
16783/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016784 * shiftwidth() function
16785 */
16786 static void
16787f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016788 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016789 typval_T *rettv;
16790{
16791 rettv->vval.v_number = get_sw_value();
16792}
16793
16794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016795 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 */
16797 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016798f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016799 typval_T *argvars;
16800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804 p = get_tv_string(&argvars[0]);
16805 rettv->vval.v_string = vim_strsave(p);
16806 simplify_filename(rettv->vval.v_string); /* simplify in place */
16807 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808}
16809
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016810#ifdef FEAT_FLOAT
16811/*
16812 * "sin()" function
16813 */
16814 static void
16815f_sin(argvars, rettv)
16816 typval_T *argvars;
16817 typval_T *rettv;
16818{
16819 float_T f;
16820
16821 rettv->v_type = VAR_FLOAT;
16822 if (get_float_arg(argvars, &f) == OK)
16823 rettv->vval.v_float = sin(f);
16824 else
16825 rettv->vval.v_float = 0.0;
16826}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016827
16828/*
16829 * "sinh()" function
16830 */
16831 static void
16832f_sinh(argvars, rettv)
16833 typval_T *argvars;
16834 typval_T *rettv;
16835{
16836 float_T f;
16837
16838 rettv->v_type = VAR_FLOAT;
16839 if (get_float_arg(argvars, &f) == OK)
16840 rettv->vval.v_float = sinh(f);
16841 else
16842 rettv->vval.v_float = 0.0;
16843}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016844#endif
16845
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846static int
16847#ifdef __BORLANDC__
16848 _RTLENTRYF
16849#endif
16850 item_compare __ARGS((const void *s1, const void *s2));
16851static int
16852#ifdef __BORLANDC__
16853 _RTLENTRYF
16854#endif
16855 item_compare2 __ARGS((const void *s1, const void *s2));
16856
16857static int item_compare_ic;
16858static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016859static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016860static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861#define ITEM_COMPARE_FAIL 999
16862
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016866 static int
16867#ifdef __BORLANDC__
16868_RTLENTRYF
16869#endif
16870item_compare(s1, s2)
16871 const void *s1;
16872 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874 char_u *p1, *p2;
16875 char_u *tofree1, *tofree2;
16876 int res;
16877 char_u numbuf1[NUMBUFLEN];
16878 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016880 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16881 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016882 if (p1 == NULL)
16883 p1 = (char_u *)"";
16884 if (p2 == NULL)
16885 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016886 if (item_compare_ic)
16887 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 res = STRCMP(p1, p2);
16890 vim_free(tofree1);
16891 vim_free(tofree2);
16892 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893}
16894
16895 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016896#ifdef __BORLANDC__
16897_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016899item_compare2(s1, s2)
16900 const void *s1;
16901 const void *s2;
16902{
16903 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016904 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016905 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016908 /* shortcut after failure in previous call; compare all items equal */
16909 if (item_compare_func_err)
16910 return 0;
16911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016912 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16913 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016914 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16915 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016916
16917 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016918 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016919 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16920 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016921 clear_tv(&argv[0]);
16922 clear_tv(&argv[1]);
16923
16924 if (res == FAIL)
16925 res = ITEM_COMPARE_FAIL;
16926 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016927 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16928 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016929 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 clear_tv(&rettv);
16931 return res;
16932}
16933
16934/*
16935 * "sort({list})" function
16936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016937 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016939 typval_T *argvars;
16940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941{
Bram Moolenaar33570922005-01-25 22:26:29 +000016942 list_T *l;
16943 listitem_T *li;
16944 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945 long len;
16946 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 if (argvars[0].v_type != VAR_LIST)
16949 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 else
16951 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016953 if (l == NULL || tv_check_lock(l->lv_lock,
16954 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 return;
16956 rettv->vval.v_list = l;
16957 rettv->v_type = VAR_LIST;
16958 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 len = list_len(l);
16961 if (len <= 1)
16962 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 item_compare_ic = FALSE;
16965 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016966 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 if (argvars[1].v_type != VAR_UNKNOWN)
16968 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016969 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 else
16973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016974 int error = FALSE;
16975
16976 i = get_tv_number_chk(&argvars[1], &error);
16977 if (error)
16978 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 if (i == 1)
16980 item_compare_ic = TRUE;
16981 else
16982 item_compare_func = get_tv_string(&argvars[1]);
16983 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016984
16985 if (argvars[2].v_type != VAR_UNKNOWN)
16986 {
16987 /* optional third argument: {dict} */
16988 if (argvars[2].v_type != VAR_DICT)
16989 {
16990 EMSG(_(e_dictreq));
16991 return;
16992 }
16993 item_compare_selfdict = argvars[2].vval.v_dict;
16994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016998 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999 if (ptrs == NULL)
17000 return;
17001 i = 0;
17002 for (li = l->lv_first; li != NULL; li = li->li_next)
17003 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 /* test the compare function */
17007 if (item_compare_func != NULL
17008 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17009 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017010 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 {
17013 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 item_compare_func == NULL ? item_compare : item_compare2);
17016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017017 if (!item_compare_func_err)
17018 {
17019 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017020 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 l->lv_len = 0;
17022 for (i = 0; i < len; ++i)
17023 list_append(l, ptrs[i]);
17024 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 }
17026
17027 vim_free(ptrs);
17028 }
17029}
17030
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017031/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017032 * "soundfold({word})" function
17033 */
17034 static void
17035f_soundfold(argvars, rettv)
17036 typval_T *argvars;
17037 typval_T *rettv;
17038{
17039 char_u *s;
17040
17041 rettv->v_type = VAR_STRING;
17042 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017043#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017044 rettv->vval.v_string = eval_soundfold(s);
17045#else
17046 rettv->vval.v_string = vim_strsave(s);
17047#endif
17048}
17049
17050/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017051 * "spellbadword()" function
17052 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017053 static void
17054f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017055 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017056 typval_T *rettv;
17057{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017058 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017059 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017060 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017062 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017063 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017064
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017065#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017066 if (argvars[0].v_type == VAR_UNKNOWN)
17067 {
17068 /* Find the start and length of the badly spelled word. */
17069 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17070 if (len != 0)
17071 word = ml_get_cursor();
17072 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017073 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017074 {
17075 char_u *str = get_tv_string_chk(&argvars[0]);
17076 int capcol = -1;
17077
17078 if (str != NULL)
17079 {
17080 /* Check the argument for spelling. */
17081 while (*str != NUL)
17082 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017083 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017084 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017085 {
17086 word = str;
17087 break;
17088 }
17089 str += len;
17090 }
17091 }
17092 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017093#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017094
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017095 list_append_string(rettv->vval.v_list, word, len);
17096 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017097 attr == HLF_SPB ? "bad" :
17098 attr == HLF_SPR ? "rare" :
17099 attr == HLF_SPL ? "local" :
17100 attr == HLF_SPC ? "caps" :
17101 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017102}
17103
17104/*
17105 * "spellsuggest()" function
17106 */
17107 static void
17108f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017109 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017110 typval_T *rettv;
17111{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017112#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017113 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017114 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017115 int maxcount;
17116 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017117 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017118 listitem_T *li;
17119 int need_capital = FALSE;
17120#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017121
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017122 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017123 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017124
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017125#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017126 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017127 {
17128 str = get_tv_string(&argvars[0]);
17129 if (argvars[1].v_type != VAR_UNKNOWN)
17130 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017131 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017132 if (maxcount <= 0)
17133 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017134 if (argvars[2].v_type != VAR_UNKNOWN)
17135 {
17136 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17137 if (typeerr)
17138 return;
17139 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017140 }
17141 else
17142 maxcount = 25;
17143
Bram Moolenaar4770d092006-01-12 23:22:24 +000017144 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017145
17146 for (i = 0; i < ga.ga_len; ++i)
17147 {
17148 str = ((char_u **)ga.ga_data)[i];
17149
17150 li = listitem_alloc();
17151 if (li == NULL)
17152 vim_free(str);
17153 else
17154 {
17155 li->li_tv.v_type = VAR_STRING;
17156 li->li_tv.v_lock = 0;
17157 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017158 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017159 }
17160 }
17161 ga_clear(&ga);
17162 }
17163#endif
17164}
17165
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017167f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017168 typval_T *argvars;
17169 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170{
17171 char_u *str;
17172 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017173 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174 regmatch_T regmatch;
17175 char_u patbuf[NUMBUFLEN];
17176 char_u *save_cpo;
17177 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017179 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017180 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181
17182 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17183 save_cpo = p_cpo;
17184 p_cpo = (char_u *)"";
17185
17186 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017187 if (argvars[1].v_type != VAR_UNKNOWN)
17188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017189 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17190 if (pat == NULL)
17191 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017192 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017193 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017194 }
17195 if (pat == NULL || *pat == NUL)
17196 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017198 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017200 if (typeerr)
17201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
Bram Moolenaar0d660222005-01-07 21:51:51 +000017203 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17204 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017207 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017209 if (*str == NUL)
17210 match = FALSE; /* empty item at the end */
17211 else
17212 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017213 if (match)
17214 end = regmatch.startp[0];
17215 else
17216 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017217 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17218 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017219 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017220 if (list_append_string(rettv->vval.v_list, str,
17221 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223 }
17224 if (!match)
17225 break;
17226 /* Advance to just after the match. */
17227 if (regmatch.endp[0] > str)
17228 col = 0;
17229 else
17230 {
17231 /* Don't get stuck at the same match. */
17232#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017233 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017234#else
17235 col = 1;
17236#endif
17237 }
17238 str = regmatch.endp[0];
17239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017240
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243
Bram Moolenaar0d660222005-01-07 21:51:51 +000017244 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245}
17246
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017247#ifdef FEAT_FLOAT
17248/*
17249 * "sqrt()" function
17250 */
17251 static void
17252f_sqrt(argvars, rettv)
17253 typval_T *argvars;
17254 typval_T *rettv;
17255{
17256 float_T f;
17257
17258 rettv->v_type = VAR_FLOAT;
17259 if (get_float_arg(argvars, &f) == OK)
17260 rettv->vval.v_float = sqrt(f);
17261 else
17262 rettv->vval.v_float = 0.0;
17263}
17264
17265/*
17266 * "str2float()" function
17267 */
17268 static void
17269f_str2float(argvars, rettv)
17270 typval_T *argvars;
17271 typval_T *rettv;
17272{
17273 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17274
17275 if (*p == '+')
17276 p = skipwhite(p + 1);
17277 (void)string2float(p, &rettv->vval.v_float);
17278 rettv->v_type = VAR_FLOAT;
17279}
17280#endif
17281
Bram Moolenaar2c932302006-03-18 21:42:09 +000017282/*
17283 * "str2nr()" function
17284 */
17285 static void
17286f_str2nr(argvars, rettv)
17287 typval_T *argvars;
17288 typval_T *rettv;
17289{
17290 int base = 10;
17291 char_u *p;
17292 long n;
17293
17294 if (argvars[1].v_type != VAR_UNKNOWN)
17295 {
17296 base = get_tv_number(&argvars[1]);
17297 if (base != 8 && base != 10 && base != 16)
17298 {
17299 EMSG(_(e_invarg));
17300 return;
17301 }
17302 }
17303
17304 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017305 if (*p == '+')
17306 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017307 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17308 rettv->vval.v_number = n;
17309}
17310
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311#ifdef HAVE_STRFTIME
17312/*
17313 * "strftime({format}[, {time}])" function
17314 */
17315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017316f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 typval_T *argvars;
17318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319{
17320 char_u result_buf[256];
17321 struct tm *curtime;
17322 time_t seconds;
17323 char_u *p;
17324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017327 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017328 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 seconds = time(NULL);
17330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 curtime = localtime(&seconds);
17333 /* MSVC returns NULL for an invalid value of seconds. */
17334 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 else
17337 {
17338# ifdef FEAT_MBYTE
17339 vimconv_T conv;
17340 char_u *enc;
17341
17342 conv.vc_type = CONV_NONE;
17343 enc = enc_locale();
17344 convert_setup(&conv, p_enc, enc);
17345 if (conv.vc_type != CONV_NONE)
17346 p = string_convert(&conv, p, NULL);
17347# endif
17348 if (p != NULL)
17349 (void)strftime((char *)result_buf, sizeof(result_buf),
17350 (char *)p, curtime);
17351 else
17352 result_buf[0] = NUL;
17353
17354# ifdef FEAT_MBYTE
17355 if (conv.vc_type != CONV_NONE)
17356 vim_free(p);
17357 convert_setup(&conv, enc, p_enc);
17358 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 else
17361# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
17364# ifdef FEAT_MBYTE
17365 /* Release conversion descriptors */
17366 convert_setup(&conv, NULL, NULL);
17367 vim_free(enc);
17368# endif
17369 }
17370}
17371#endif
17372
17373/*
17374 * "stridx()" function
17375 */
17376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017377f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017378 typval_T *argvars;
17379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380{
17381 char_u buf[NUMBUFLEN];
17382 char_u *needle;
17383 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017384 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017386 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 needle = get_tv_string_chk(&argvars[1]);
17389 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017390 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017391 if (needle == NULL || haystack == NULL)
17392 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393
Bram Moolenaar33570922005-01-25 22:26:29 +000017394 if (argvars[2].v_type != VAR_UNKNOWN)
17395 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017396 int error = FALSE;
17397
17398 start_idx = get_tv_number_chk(&argvars[2], &error);
17399 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017400 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017401 if (start_idx >= 0)
17402 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017403 }
17404
17405 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17406 if (pos != NULL)
17407 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408}
17409
17410/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017411 * "string()" function
17412 */
17413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017415 typval_T *argvars;
17416 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017417{
17418 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017419 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017422 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017423 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017424 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017425 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
17428/*
17429 * "strlen()" function
17430 */
17431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017432f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017433 typval_T *argvars;
17434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 rettv->vval.v_number = (varnumber_T)(STRLEN(
17437 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438}
17439
17440/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017441 * "strchars()" function
17442 */
17443 static void
17444f_strchars(argvars, rettv)
17445 typval_T *argvars;
17446 typval_T *rettv;
17447{
17448 char_u *s = get_tv_string(&argvars[0]);
17449#ifdef FEAT_MBYTE
17450 varnumber_T len = 0;
17451
17452 while (*s != NUL)
17453 {
17454 mb_cptr2char_adv(&s);
17455 ++len;
17456 }
17457 rettv->vval.v_number = len;
17458#else
17459 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17460#endif
17461}
17462
17463/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017464 * "strdisplaywidth()" function
17465 */
17466 static void
17467f_strdisplaywidth(argvars, rettv)
17468 typval_T *argvars;
17469 typval_T *rettv;
17470{
17471 char_u *s = get_tv_string(&argvars[0]);
17472 int col = 0;
17473
17474 if (argvars[1].v_type != VAR_UNKNOWN)
17475 col = get_tv_number(&argvars[1]);
17476
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017477 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017478}
17479
17480/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017481 * "strwidth()" function
17482 */
17483 static void
17484f_strwidth(argvars, rettv)
17485 typval_T *argvars;
17486 typval_T *rettv;
17487{
17488 char_u *s = get_tv_string(&argvars[0]);
17489
17490 rettv->vval.v_number = (varnumber_T)(
17491#ifdef FEAT_MBYTE
17492 mb_string2cells(s, -1)
17493#else
17494 STRLEN(s)
17495#endif
17496 );
17497}
17498
17499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 * "strpart()" function
17501 */
17502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 typval_T *argvars;
17505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506{
17507 char_u *p;
17508 int n;
17509 int len;
17510 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017511 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017513 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 slen = (int)STRLEN(p);
17515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017516 n = get_tv_number_chk(&argvars[1], &error);
17517 if (error)
17518 len = 0;
17519 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 else
17522 len = slen - n; /* default len: all bytes that are available. */
17523
17524 /*
17525 * Only return the overlap between the specified part and the actual
17526 * string.
17527 */
17528 if (n < 0)
17529 {
17530 len += n;
17531 n = 0;
17532 }
17533 else if (n > slen)
17534 n = slen;
17535 if (len < 0)
17536 len = 0;
17537 else if (n + len > slen)
17538 len = slen - n;
17539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017540 rettv->v_type = VAR_STRING;
17541 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542}
17543
17544/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 * "strridx()" function
17546 */
17547 static void
17548f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 typval_T *argvars;
17550 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551{
17552 char_u buf[NUMBUFLEN];
17553 char_u *needle;
17554 char_u *haystack;
17555 char_u *rest;
17556 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017557 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017559 needle = get_tv_string_chk(&argvars[1]);
17560 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017561
17562 rettv->vval.v_number = -1;
17563 if (needle == NULL || haystack == NULL)
17564 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017565
17566 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017567 if (argvars[2].v_type != VAR_UNKNOWN)
17568 {
17569 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017570 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017571 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017572 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017573 }
17574 else
17575 end_idx = haystack_len;
17576
Bram Moolenaar0d660222005-01-07 21:51:51 +000017577 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017578 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017580 lastmatch = haystack + end_idx;
17581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017582 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017583 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017584 for (rest = haystack; *rest != '\0'; ++rest)
17585 {
17586 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017587 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 break;
17589 lastmatch = rest;
17590 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017591 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017592
17593 if (lastmatch == NULL)
17594 rettv->vval.v_number = -1;
17595 else
17596 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17597}
17598
17599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017600 * "strtrans()" function
17601 */
17602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017603f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 typval_T *argvars;
17605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017607 rettv->v_type = VAR_STRING;
17608 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609}
17610
17611/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017612 * "submatch()" function
17613 */
17614 static void
17615f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017616 typval_T *argvars;
17617 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017618{
17619 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017620 rettv->vval.v_string =
17621 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017622}
17623
17624/*
17625 * "substitute()" function
17626 */
17627 static void
17628f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017629 typval_T *argvars;
17630 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017631{
17632 char_u patbuf[NUMBUFLEN];
17633 char_u subbuf[NUMBUFLEN];
17634 char_u flagsbuf[NUMBUFLEN];
17635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017636 char_u *str = get_tv_string_chk(&argvars[0]);
17637 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17638 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17639 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17640
Bram Moolenaar0d660222005-01-07 21:51:51 +000017641 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017642 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17643 rettv->vval.v_string = NULL;
17644 else
17645 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017646}
17647
17648/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017649 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017652f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655{
17656 int id = 0;
17657#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017658 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 long col;
17660 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017661 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017663 lnum = get_tv_lnum(argvars); /* -1 on type error */
17664 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17665 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017667 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017668 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017669 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670#endif
17671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017672 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673}
17674
17675/*
17676 * "synIDattr(id, what [, mode])" function
17677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017680 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682{
17683 char_u *p = NULL;
17684#ifdef FEAT_SYN_HL
17685 int id;
17686 char_u *what;
17687 char_u *mode;
17688 char_u modebuf[NUMBUFLEN];
17689 int modec;
17690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017691 id = get_tv_number(&argvars[0]);
17692 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017697 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 modec = 0; /* replace invalid with current */
17699 }
17700 else
17701 {
17702#ifdef FEAT_GUI
17703 if (gui.in_use)
17704 modec = 'g';
17705 else
17706#endif
17707 if (t_colors > 1)
17708 modec = 'c';
17709 else
17710 modec = 't';
17711 }
17712
17713
17714 switch (TOLOWER_ASC(what[0]))
17715 {
17716 case 'b':
17717 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17718 p = highlight_color(id, what, modec);
17719 else /* bold */
17720 p = highlight_has_attr(id, HL_BOLD, modec);
17721 break;
17722
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017723 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 p = highlight_color(id, what, modec);
17725 break;
17726
17727 case 'i':
17728 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17729 p = highlight_has_attr(id, HL_INVERSE, modec);
17730 else /* italic */
17731 p = highlight_has_attr(id, HL_ITALIC, modec);
17732 break;
17733
17734 case 'n': /* name */
17735 p = get_highlight_name(NULL, id - 1);
17736 break;
17737
17738 case 'r': /* reverse */
17739 p = highlight_has_attr(id, HL_INVERSE, modec);
17740 break;
17741
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017742 case 's':
17743 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17744 p = highlight_color(id, what, modec);
17745 else /* standout */
17746 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 break;
17748
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017749 case 'u':
17750 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17751 /* underline */
17752 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17753 else
17754 /* undercurl */
17755 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 break;
17757 }
17758
17759 if (p != NULL)
17760 p = vim_strsave(p);
17761#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762 rettv->v_type = VAR_STRING;
17763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764}
17765
17766/*
17767 * "synIDtrans(id)" function
17768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017770f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017771 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773{
17774 int id;
17775
17776#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017777 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778
17779 if (id > 0)
17780 id = syn_get_final_id(id);
17781 else
17782#endif
17783 id = 0;
17784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017785 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786}
17787
17788/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017789 * "synconcealed(lnum, col)" function
17790 */
17791 static void
17792f_synconcealed(argvars, rettv)
17793 typval_T *argvars UNUSED;
17794 typval_T *rettv;
17795{
17796#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17797 long lnum;
17798 long col;
17799 int syntax_flags = 0;
17800 int cchar;
17801 int matchid = 0;
17802 char_u str[NUMBUFLEN];
17803#endif
17804
17805 rettv->v_type = VAR_LIST;
17806 rettv->vval.v_list = NULL;
17807
17808#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17809 lnum = get_tv_lnum(argvars); /* -1 on type error */
17810 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17811
17812 vim_memset(str, NUL, sizeof(str));
17813
17814 if (rettv_list_alloc(rettv) != FAIL)
17815 {
17816 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17817 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17818 && curwin->w_p_cole > 0)
17819 {
17820 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17821 syntax_flags = get_syntax_info(&matchid);
17822
17823 /* get the conceal character */
17824 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17825 {
17826 cchar = syn_get_sub_char();
17827 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17828 cchar = lcs_conceal;
17829 if (cchar != NUL)
17830 {
17831# ifdef FEAT_MBYTE
17832 if (has_mbyte)
17833 (*mb_char2bytes)(cchar, str);
17834 else
17835# endif
17836 str[0] = cchar;
17837 }
17838 }
17839 }
17840
17841 list_append_number(rettv->vval.v_list,
17842 (syntax_flags & HL_CONCEAL) != 0);
17843 /* -1 to auto-determine strlen */
17844 list_append_string(rettv->vval.v_list, str, -1);
17845 list_append_number(rettv->vval.v_list, matchid);
17846 }
17847#endif
17848}
17849
17850/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017851 * "synstack(lnum, col)" function
17852 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017853 static void
17854f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017855 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017856 typval_T *rettv;
17857{
17858#ifdef FEAT_SYN_HL
17859 long lnum;
17860 long col;
17861 int i;
17862 int id;
17863#endif
17864
17865 rettv->v_type = VAR_LIST;
17866 rettv->vval.v_list = NULL;
17867
17868#ifdef FEAT_SYN_HL
17869 lnum = get_tv_lnum(argvars); /* -1 on type error */
17870 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17871
17872 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017873 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017874 && rettv_list_alloc(rettv) != FAIL)
17875 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017876 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017877 for (i = 0; ; ++i)
17878 {
17879 id = syn_get_stack_item(i);
17880 if (id < 0)
17881 break;
17882 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17883 break;
17884 }
17885 }
17886#endif
17887}
17888
17889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 * "system()" function
17891 */
17892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017894 typval_T *argvars;
17895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017897 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017899 char_u *infile = NULL;
17900 char_u buf[NUMBUFLEN];
17901 int err = FALSE;
17902 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017904 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017905 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017906
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017907 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017908 {
17909 /*
17910 * Write the string to a temp file, to be used for input of the shell
17911 * command.
17912 */
17913 if ((infile = vim_tempname('i')) == NULL)
17914 {
17915 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017916 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017917 }
17918
17919 fd = mch_fopen((char *)infile, WRITEBIN);
17920 if (fd == NULL)
17921 {
17922 EMSG2(_(e_notopen), infile);
17923 goto done;
17924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017925 p = get_tv_string_buf_chk(&argvars[1], buf);
17926 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017927 {
17928 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017929 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017930 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017931 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17932 err = TRUE;
17933 if (fclose(fd) != 0)
17934 err = TRUE;
17935 if (err)
17936 {
17937 EMSG(_("E677: Error writing temp file"));
17938 goto done;
17939 }
17940 }
17941
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017942 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17943 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017944
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945#ifdef USE_CR
17946 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017947 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
17949 char_u *s;
17950
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017951 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 {
17953 if (*s == CAR)
17954 *s = NL;
17955 }
17956 }
17957#else
17958# ifdef USE_CRNL
17959 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017960 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961 {
17962 char_u *s, *d;
17963
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017964 d = res;
17965 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966 {
17967 if (s[0] == CAR && s[1] == NL)
17968 ++s;
17969 *d++ = *s;
17970 }
17971 *d = NUL;
17972 }
17973# endif
17974#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017975
17976done:
17977 if (infile != NULL)
17978 {
17979 mch_remove(infile);
17980 vim_free(infile);
17981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017982 rettv->v_type = VAR_STRING;
17983 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984}
17985
17986/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017987 * "tabpagebuflist()" function
17988 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017989 static void
17990f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017991 typval_T *argvars UNUSED;
17992 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017993{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017994#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017995 tabpage_T *tp;
17996 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017997
17998 if (argvars[0].v_type == VAR_UNKNOWN)
17999 wp = firstwin;
18000 else
18001 {
18002 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18003 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018004 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018005 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018006 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018007 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018008 for (; wp != NULL; wp = wp->w_next)
18009 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018010 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018011 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018012 }
18013#endif
18014}
18015
18016
18017/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018018 * "tabpagenr()" function
18019 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018020 static void
18021f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018022 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018023 typval_T *rettv;
18024{
18025 int nr = 1;
18026#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018027 char_u *arg;
18028
18029 if (argvars[0].v_type != VAR_UNKNOWN)
18030 {
18031 arg = get_tv_string_chk(&argvars[0]);
18032 nr = 0;
18033 if (arg != NULL)
18034 {
18035 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018036 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018037 else
18038 EMSG2(_(e_invexpr2), arg);
18039 }
18040 }
18041 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018042 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018043#endif
18044 rettv->vval.v_number = nr;
18045}
18046
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018047
18048#ifdef FEAT_WINDOWS
18049static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18050
18051/*
18052 * Common code for tabpagewinnr() and winnr().
18053 */
18054 static int
18055get_winnr(tp, argvar)
18056 tabpage_T *tp;
18057 typval_T *argvar;
18058{
18059 win_T *twin;
18060 int nr = 1;
18061 win_T *wp;
18062 char_u *arg;
18063
18064 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18065 if (argvar->v_type != VAR_UNKNOWN)
18066 {
18067 arg = get_tv_string_chk(argvar);
18068 if (arg == NULL)
18069 nr = 0; /* type error; errmsg already given */
18070 else if (STRCMP(arg, "$") == 0)
18071 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18072 else if (STRCMP(arg, "#") == 0)
18073 {
18074 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18075 if (twin == NULL)
18076 nr = 0;
18077 }
18078 else
18079 {
18080 EMSG2(_(e_invexpr2), arg);
18081 nr = 0;
18082 }
18083 }
18084
18085 if (nr > 0)
18086 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18087 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018088 {
18089 if (wp == NULL)
18090 {
18091 /* didn't find it in this tabpage */
18092 nr = 0;
18093 break;
18094 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018095 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018096 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018097 return nr;
18098}
18099#endif
18100
18101/*
18102 * "tabpagewinnr()" function
18103 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018104 static void
18105f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018106 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018107 typval_T *rettv;
18108{
18109 int nr = 1;
18110#ifdef FEAT_WINDOWS
18111 tabpage_T *tp;
18112
18113 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18114 if (tp == NULL)
18115 nr = 0;
18116 else
18117 nr = get_winnr(tp, &argvars[1]);
18118#endif
18119 rettv->vval.v_number = nr;
18120}
18121
18122
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018123/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018124 * "tagfiles()" function
18125 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018126 static void
18127f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018128 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018129 typval_T *rettv;
18130{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018131 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018132 tagname_T tn;
18133 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018134
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018135 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018136 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018137 fname = alloc(MAXPATHL);
18138 if (fname == NULL)
18139 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018141 for (first = TRUE; ; first = FALSE)
18142 if (get_tagfname(&tn, first, fname) == FAIL
18143 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018144 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018145 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018146 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018147}
18148
18149/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018150 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018151 */
18152 static void
18153f_taglist(argvars, rettv)
18154 typval_T *argvars;
18155 typval_T *rettv;
18156{
18157 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018158
18159 tag_pattern = get_tv_string(&argvars[0]);
18160
18161 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018162 if (*tag_pattern == NUL)
18163 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018164
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018165 if (rettv_list_alloc(rettv) == OK)
18166 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018167}
18168
18169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 * "tempname()" function
18171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018173f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018174 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176{
18177 static int x = 'A';
18178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018179 rettv->v_type = VAR_STRING;
18180 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181
18182 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18183 * names. Skip 'I' and 'O', they are used for shell redirection. */
18184 do
18185 {
18186 if (x == 'Z')
18187 x = '0';
18188 else if (x == '9')
18189 x = 'A';
18190 else
18191 {
18192#ifdef EBCDIC
18193 if (x == 'I')
18194 x = 'J';
18195 else if (x == 'R')
18196 x = 'S';
18197 else
18198#endif
18199 ++x;
18200 }
18201 } while (x == 'I' || x == 'O');
18202}
18203
18204/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018205 * "test(list)" function: Just checking the walls...
18206 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018207 static void
18208f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018209 typval_T *argvars UNUSED;
18210 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018211{
18212 /* Used for unit testing. Change the code below to your liking. */
18213#if 0
18214 listitem_T *li;
18215 list_T *l;
18216 char_u *bad, *good;
18217
18218 if (argvars[0].v_type != VAR_LIST)
18219 return;
18220 l = argvars[0].vval.v_list;
18221 if (l == NULL)
18222 return;
18223 li = l->lv_first;
18224 if (li == NULL)
18225 return;
18226 bad = get_tv_string(&li->li_tv);
18227 li = li->li_next;
18228 if (li == NULL)
18229 return;
18230 good = get_tv_string(&li->li_tv);
18231 rettv->vval.v_number = test_edit_score(bad, good);
18232#endif
18233}
18234
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018235#ifdef FEAT_FLOAT
18236/*
18237 * "tan()" function
18238 */
18239 static void
18240f_tan(argvars, rettv)
18241 typval_T *argvars;
18242 typval_T *rettv;
18243{
18244 float_T f;
18245
18246 rettv->v_type = VAR_FLOAT;
18247 if (get_float_arg(argvars, &f) == OK)
18248 rettv->vval.v_float = tan(f);
18249 else
18250 rettv->vval.v_float = 0.0;
18251}
18252
18253/*
18254 * "tanh()" function
18255 */
18256 static void
18257f_tanh(argvars, rettv)
18258 typval_T *argvars;
18259 typval_T *rettv;
18260{
18261 float_T f;
18262
18263 rettv->v_type = VAR_FLOAT;
18264 if (get_float_arg(argvars, &f) == OK)
18265 rettv->vval.v_float = tanh(f);
18266 else
18267 rettv->vval.v_float = 0.0;
18268}
18269#endif
18270
Bram Moolenaard52d9742005-08-21 22:20:28 +000018271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 * "tolower(string)" function
18273 */
18274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018275f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018276 typval_T *argvars;
18277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278{
18279 char_u *p;
18280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018281 p = vim_strsave(get_tv_string(&argvars[0]));
18282 rettv->v_type = VAR_STRING;
18283 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284
18285 if (p != NULL)
18286 while (*p != NUL)
18287 {
18288#ifdef FEAT_MBYTE
18289 int l;
18290
18291 if (enc_utf8)
18292 {
18293 int c, lc;
18294
18295 c = utf_ptr2char(p);
18296 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018297 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298 /* TODO: reallocate string when byte count changes. */
18299 if (utf_char2len(lc) == l)
18300 utf_char2bytes(lc, p);
18301 p += l;
18302 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018303 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 p += l; /* skip multi-byte character */
18305 else
18306#endif
18307 {
18308 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18309 ++p;
18310 }
18311 }
18312}
18313
18314/*
18315 * "toupper(string)" function
18316 */
18317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018318f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018319 typval_T *argvars;
18320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018322 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018323 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324}
18325
18326/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018327 * "tr(string, fromstr, tostr)" function
18328 */
18329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018330f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018331 typval_T *argvars;
18332 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018333{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018334 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018335 char_u *fromstr;
18336 char_u *tostr;
18337 char_u *p;
18338#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018339 int inlen;
18340 int fromlen;
18341 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018342 int idx;
18343 char_u *cpstr;
18344 int cplen;
18345 int first = TRUE;
18346#endif
18347 char_u buf[NUMBUFLEN];
18348 char_u buf2[NUMBUFLEN];
18349 garray_T ga;
18350
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018351 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018352 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18353 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018354
18355 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018356 rettv->v_type = VAR_STRING;
18357 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018358 if (fromstr == NULL || tostr == NULL)
18359 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018360 ga_init2(&ga, (int)sizeof(char), 80);
18361
18362#ifdef FEAT_MBYTE
18363 if (!has_mbyte)
18364#endif
18365 /* not multi-byte: fromstr and tostr must be the same length */
18366 if (STRLEN(fromstr) != STRLEN(tostr))
18367 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018368#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018369error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018370#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018371 EMSG2(_(e_invarg2), fromstr);
18372 ga_clear(&ga);
18373 return;
18374 }
18375
18376 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018377 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018378 {
18379#ifdef FEAT_MBYTE
18380 if (has_mbyte)
18381 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018382 inlen = (*mb_ptr2len)(in_str);
18383 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018384 cplen = inlen;
18385 idx = 0;
18386 for (p = fromstr; *p != NUL; p += fromlen)
18387 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018388 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018389 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018390 {
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 if (idx-- == 0)
18395 {
18396 cplen = tolen;
18397 cpstr = p;
18398 break;
18399 }
18400 }
18401 if (*p == NUL) /* tostr is shorter than fromstr */
18402 goto error;
18403 break;
18404 }
18405 ++idx;
18406 }
18407
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018408 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018409 {
18410 /* Check that fromstr and tostr have the same number of
18411 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018412 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018413 first = FALSE;
18414 for (p = tostr; *p != NUL; p += tolen)
18415 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018416 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018417 --idx;
18418 }
18419 if (idx != 0)
18420 goto error;
18421 }
18422
18423 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018424 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018425 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018426
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018427 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018428 }
18429 else
18430#endif
18431 {
18432 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018433 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018434 if (p != NULL)
18435 ga_append(&ga, tostr[p - fromstr]);
18436 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018437 ga_append(&ga, *in_str);
18438 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018439 }
18440 }
18441
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018442 /* add a terminating NUL */
18443 ga_grow(&ga, 1);
18444 ga_append(&ga, NUL);
18445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018446 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018447}
18448
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018449#ifdef FEAT_FLOAT
18450/*
18451 * "trunc({float})" function
18452 */
18453 static void
18454f_trunc(argvars, rettv)
18455 typval_T *argvars;
18456 typval_T *rettv;
18457{
18458 float_T f;
18459
18460 rettv->v_type = VAR_FLOAT;
18461 if (get_float_arg(argvars, &f) == OK)
18462 /* trunc() is not in C90, use floor() or ceil() instead. */
18463 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18464 else
18465 rettv->vval.v_float = 0.0;
18466}
18467#endif
18468
Bram Moolenaar8299df92004-07-10 09:47:34 +000018469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 * "type(expr)" function
18471 */
18472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018473f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018474 typval_T *argvars;
18475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018477 int n;
18478
18479 switch (argvars[0].v_type)
18480 {
18481 case VAR_NUMBER: n = 0; break;
18482 case VAR_STRING: n = 1; break;
18483 case VAR_FUNC: n = 2; break;
18484 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018485 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018486#ifdef FEAT_FLOAT
18487 case VAR_FLOAT: n = 5; break;
18488#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018489 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18490 }
18491 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492}
18493
18494/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018495 * "undofile(name)" function
18496 */
18497 static void
18498f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018499 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018500 typval_T *rettv;
18501{
18502 rettv->v_type = VAR_STRING;
18503#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018504 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018505 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018506
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018507 if (*fname == NUL)
18508 {
18509 /* If there is no file name there will be no undo file. */
18510 rettv->vval.v_string = NULL;
18511 }
18512 else
18513 {
18514 char_u *ffname = FullName_save(fname, FALSE);
18515
18516 if (ffname != NULL)
18517 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18518 vim_free(ffname);
18519 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018520 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018521#else
18522 rettv->vval.v_string = NULL;
18523#endif
18524}
18525
18526/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018527 * "undotree()" function
18528 */
18529 static void
18530f_undotree(argvars, rettv)
18531 typval_T *argvars UNUSED;
18532 typval_T *rettv;
18533{
18534 if (rettv_dict_alloc(rettv) == OK)
18535 {
18536 dict_T *dict = rettv->vval.v_dict;
18537 list_T *list;
18538
Bram Moolenaar730cde92010-06-27 05:18:54 +020018539 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018540 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018541 dict_add_nr_str(dict, "save_last",
18542 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018543 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18544 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018545 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018546
18547 list = list_alloc();
18548 if (list != NULL)
18549 {
18550 u_eval_tree(curbuf->b_u_oldhead, list);
18551 dict_add_list(dict, "entries", list);
18552 }
18553 }
18554}
18555
18556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018557 * "values(dict)" function
18558 */
18559 static void
18560f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018561 typval_T *argvars;
18562 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018563{
18564 dict_list(argvars, rettv, 1);
18565}
18566
18567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 * "virtcol(string)" function
18569 */
18570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018572 typval_T *argvars;
18573 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574{
18575 colnr_T vcol = 0;
18576 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018577 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018579 fp = var2fpos(&argvars[0], FALSE, &fnum);
18580 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18581 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 {
18583 getvvcol(curwin, fp, NULL, NULL, &vcol);
18584 ++vcol;
18585 }
18586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588}
18589
18590/*
18591 * "visualmode()" function
18592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018595 typval_T *argvars UNUSED;
18596 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597{
18598#ifdef FEAT_VISUAL
18599 char_u str[2];
18600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018601 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 str[0] = curbuf->b_visual_mode_eval;
18603 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018604 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605
18606 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018607 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609#endif
18610}
18611
18612/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018613 * "wildmenumode()" function
18614 */
18615 static void
18616f_wildmenumode(argvars, rettv)
18617 typval_T *argvars UNUSED;
18618 typval_T *rettv UNUSED;
18619{
18620#ifdef FEAT_WILDMENU
18621 if (wild_menu_showing)
18622 rettv->vval.v_number = 1;
18623#endif
18624}
18625
18626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 * "winbufnr(nr)" function
18628 */
18629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 typval_T *argvars;
18632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633{
18634 win_T *wp;
18635
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018636 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641}
18642
18643/*
18644 * "wincol()" function
18645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018648 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650{
18651 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653}
18654
18655/*
18656 * "winheight(nr)" function
18657 */
18658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018660 typval_T *argvars;
18661 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662{
18663 win_T *wp;
18664
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018665 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670}
18671
18672/*
18673 * "winline()" function
18674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679{
18680 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682}
18683
18684/*
18685 * "winnr()" function
18686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691{
18692 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018693
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018695 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698}
18699
18700/*
18701 * "winrestcmd()" function
18702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707{
18708#ifdef FEAT_WINDOWS
18709 win_T *wp;
18710 int winnr = 1;
18711 garray_T ga;
18712 char_u buf[50];
18713
18714 ga_init2(&ga, (int)sizeof(char), 70);
18715 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18716 {
18717 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18718 ga_concat(&ga, buf);
18719# ifdef FEAT_VERTSPLIT
18720 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18721 ga_concat(&ga, buf);
18722# endif
18723 ++winnr;
18724 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018725 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018727 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018729 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732}
18733
18734/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018735 * "winrestview()" function
18736 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018737 static void
18738f_winrestview(argvars, rettv)
18739 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018740 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018741{
18742 dict_T *dict;
18743
18744 if (argvars[0].v_type != VAR_DICT
18745 || (dict = argvars[0].vval.v_dict) == NULL)
18746 EMSG(_(e_invarg));
18747 else
18748 {
18749 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18750 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18751#ifdef FEAT_VIRTUALEDIT
18752 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18753#endif
18754 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018755 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018756
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018757 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018758#ifdef FEAT_DIFF
18759 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18760#endif
18761 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18762 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18763
18764 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018765 win_new_height(curwin, curwin->w_height);
18766# ifdef FEAT_VERTSPLIT
18767 win_new_width(curwin, W_WIDTH(curwin));
18768# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018769 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018770
18771 if (curwin->w_topline == 0)
18772 curwin->w_topline = 1;
18773 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18774 curwin->w_topline = curbuf->b_ml.ml_line_count;
18775#ifdef FEAT_DIFF
18776 check_topfill(curwin, TRUE);
18777#endif
18778 }
18779}
18780
18781/*
18782 * "winsaveview()" function
18783 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018784 static void
18785f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018786 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018787 typval_T *rettv;
18788{
18789 dict_T *dict;
18790
Bram Moolenaara800b422010-06-27 01:15:55 +020018791 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018792 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018793 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018794
18795 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18796 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18797#ifdef FEAT_VIRTUALEDIT
18798 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18799#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018800 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018801 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18802
18803 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18804#ifdef FEAT_DIFF
18805 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18806#endif
18807 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18808 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18809}
18810
18811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 * "winwidth(nr)" function
18813 */
18814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 typval_T *argvars;
18817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818{
18819 win_T *wp;
18820
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018821 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 else
18825#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018826 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018828 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018829#endif
18830}
18831
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018833 * "writefile()" function
18834 */
18835 static void
18836f_writefile(argvars, rettv)
18837 typval_T *argvars;
18838 typval_T *rettv;
18839{
18840 int binary = FALSE;
18841 char_u *fname;
18842 FILE *fd;
18843 listitem_T *li;
18844 char_u *s;
18845 int ret = 0;
18846 int c;
18847
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018848 if (check_restricted() || check_secure())
18849 return;
18850
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018851 if (argvars[0].v_type != VAR_LIST)
18852 {
18853 EMSG2(_(e_listarg), "writefile()");
18854 return;
18855 }
18856 if (argvars[0].vval.v_list == NULL)
18857 return;
18858
18859 if (argvars[2].v_type != VAR_UNKNOWN
18860 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18861 binary = TRUE;
18862
18863 /* Always open the file in binary mode, library functions have a mind of
18864 * their own about CR-LF conversion. */
18865 fname = get_tv_string(&argvars[1]);
18866 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18867 {
18868 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18869 ret = -1;
18870 }
18871 else
18872 {
18873 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18874 li = li->li_next)
18875 {
18876 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18877 {
18878 if (*s == '\n')
18879 c = putc(NUL, fd);
18880 else
18881 c = putc(*s, fd);
18882 if (c == EOF)
18883 {
18884 ret = -1;
18885 break;
18886 }
18887 }
18888 if (!binary || li->li_next != NULL)
18889 if (putc('\n', fd) == EOF)
18890 {
18891 ret = -1;
18892 break;
18893 }
18894 if (ret < 0)
18895 {
18896 EMSG(_(e_write));
18897 break;
18898 }
18899 }
18900 fclose(fd);
18901 }
18902
18903 rettv->vval.v_number = ret;
18904}
18905
18906/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018907 * "xor(expr, expr)" function
18908 */
18909 static void
18910f_xor(argvars, rettv)
18911 typval_T *argvars;
18912 typval_T *rettv;
18913{
18914 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18915 ^ get_tv_number_chk(&argvars[1], NULL);
18916}
18917
18918
18919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018921 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 */
18923 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018924var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018925 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018926 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018927 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018929 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018931 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932
Bram Moolenaara5525202006-03-02 22:52:09 +000018933 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018934 if (varp->v_type == VAR_LIST)
18935 {
18936 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018937 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018938 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018939 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018940
18941 l = varp->vval.v_list;
18942 if (l == NULL)
18943 return NULL;
18944
18945 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018946 pos.lnum = list_find_nr(l, 0L, &error);
18947 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018948 return NULL; /* invalid line number */
18949
18950 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018951 pos.col = list_find_nr(l, 1L, &error);
18952 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018953 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018954 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018955
18956 /* We accept "$" for the column number: last column. */
18957 li = list_find(l, 1L);
18958 if (li != NULL && li->li_tv.v_type == VAR_STRING
18959 && li->li_tv.vval.v_string != NULL
18960 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18961 pos.col = len + 1;
18962
Bram Moolenaara5525202006-03-02 22:52:09 +000018963 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018964 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018965 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018966 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018967
Bram Moolenaara5525202006-03-02 22:52:09 +000018968#ifdef FEAT_VIRTUALEDIT
18969 /* Get the virtual offset. Defaults to zero. */
18970 pos.coladd = list_find_nr(l, 2L, &error);
18971 if (error)
18972 pos.coladd = 0;
18973#endif
18974
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018975 return &pos;
18976 }
18977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018978 name = get_tv_string_chk(varp);
18979 if (name == NULL)
18980 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018981 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018983#ifdef FEAT_VISUAL
18984 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18985 {
18986 if (VIsual_active)
18987 return &VIsual;
18988 return &curwin->w_cursor;
18989 }
18990#endif
18991 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018993 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18995 return NULL;
18996 return pp;
18997 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018998
18999#ifdef FEAT_VIRTUALEDIT
19000 pos.coladd = 0;
19001#endif
19002
Bram Moolenaar477933c2007-07-17 14:32:23 +000019003 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019004 {
19005 pos.col = 0;
19006 if (name[1] == '0') /* "w0": first visible line */
19007 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019008 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019009 pos.lnum = curwin->w_topline;
19010 return &pos;
19011 }
19012 else if (name[1] == '$') /* "w$": last visible line */
19013 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019014 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019015 pos.lnum = curwin->w_botline - 1;
19016 return &pos;
19017 }
19018 }
19019 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019021 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 {
19023 pos.lnum = curbuf->b_ml.ml_line_count;
19024 pos.col = 0;
19025 }
19026 else
19027 {
19028 pos.lnum = curwin->w_cursor.lnum;
19029 pos.col = (colnr_T)STRLEN(ml_get_curline());
19030 }
19031 return &pos;
19032 }
19033 return NULL;
19034}
19035
19036/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019037 * Convert list in "arg" into a position and optional file number.
19038 * When "fnump" is NULL there is no file number, only 3 items.
19039 * Note that the column is passed on as-is, the caller may want to decrement
19040 * it to use 1 for the first column.
19041 * Return FAIL when conversion is not possible, doesn't check the position for
19042 * validity.
19043 */
19044 static int
19045list2fpos(arg, posp, fnump)
19046 typval_T *arg;
19047 pos_T *posp;
19048 int *fnump;
19049{
19050 list_T *l = arg->vval.v_list;
19051 long i = 0;
19052 long n;
19053
Bram Moolenaarbde35262006-07-23 20:12:24 +000019054 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19055 * when "fnump" isn't NULL and "coladd" is optional. */
19056 if (arg->v_type != VAR_LIST
19057 || l == NULL
19058 || l->lv_len < (fnump == NULL ? 2 : 3)
19059 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019060 return FAIL;
19061
19062 if (fnump != NULL)
19063 {
19064 n = list_find_nr(l, i++, NULL); /* fnum */
19065 if (n < 0)
19066 return FAIL;
19067 if (n == 0)
19068 n = curbuf->b_fnum; /* current buffer */
19069 *fnump = n;
19070 }
19071
19072 n = list_find_nr(l, i++, NULL); /* lnum */
19073 if (n < 0)
19074 return FAIL;
19075 posp->lnum = n;
19076
19077 n = list_find_nr(l, i++, NULL); /* col */
19078 if (n < 0)
19079 return FAIL;
19080 posp->col = n;
19081
19082#ifdef FEAT_VIRTUALEDIT
19083 n = list_find_nr(l, i, NULL);
19084 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019085 posp->coladd = 0;
19086 else
19087 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019088#endif
19089
19090 return OK;
19091}
19092
19093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094 * Get the length of an environment variable name.
19095 * Advance "arg" to the first character after the name.
19096 * Return 0 for error.
19097 */
19098 static int
19099get_env_len(arg)
19100 char_u **arg;
19101{
19102 char_u *p;
19103 int len;
19104
19105 for (p = *arg; vim_isIDc(*p); ++p)
19106 ;
19107 if (p == *arg) /* no name found */
19108 return 0;
19109
19110 len = (int)(p - *arg);
19111 *arg = p;
19112 return len;
19113}
19114
19115/*
19116 * Get the length of the name of a function or internal variable.
19117 * "arg" is advanced to the first non-white character after the name.
19118 * Return 0 if something is wrong.
19119 */
19120 static int
19121get_id_len(arg)
19122 char_u **arg;
19123{
19124 char_u *p;
19125 int len;
19126
19127 /* Find the end of the name. */
19128 for (p = *arg; eval_isnamec(*p); ++p)
19129 ;
19130 if (p == *arg) /* no name found */
19131 return 0;
19132
19133 len = (int)(p - *arg);
19134 *arg = skipwhite(p);
19135
19136 return len;
19137}
19138
19139/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019140 * Get the length of the name of a variable or function.
19141 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019143 * Return -1 if curly braces expansion failed.
19144 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 * If the name contains 'magic' {}'s, expand them and return the
19146 * expanded name in an allocated string via 'alias' - caller must free.
19147 */
19148 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019149get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 char_u **arg;
19151 char_u **alias;
19152 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019153 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154{
19155 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 char_u *p;
19157 char_u *expr_start;
19158 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159
19160 *alias = NULL; /* default to no alias */
19161
19162 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19163 && (*arg)[2] == (int)KE_SNR)
19164 {
19165 /* hard coded <SNR>, already translated */
19166 *arg += 3;
19167 return get_id_len(arg) + 3;
19168 }
19169 len = eval_fname_script(*arg);
19170 if (len > 0)
19171 {
19172 /* literal "<SID>", "s:" or "<SNR>" */
19173 *arg += len;
19174 }
19175
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019179 p = find_name_end(*arg, &expr_start, &expr_end,
19180 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 if (expr_start != NULL)
19182 {
19183 char_u *temp_string;
19184
19185 if (!evaluate)
19186 {
19187 len += (int)(p - *arg);
19188 *arg = skipwhite(p);
19189 return len;
19190 }
19191
19192 /*
19193 * Include any <SID> etc in the expanded string:
19194 * Thus the -len here.
19195 */
19196 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19197 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019198 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199 *alias = temp_string;
19200 *arg = skipwhite(p);
19201 return (int)STRLEN(temp_string);
19202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203
19204 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019205 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 EMSG2(_(e_invexpr2), *arg);
19207
19208 return len;
19209}
19210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211/*
19212 * Find the end of a variable or function name, taking care of magic braces.
19213 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19214 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019215 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216 * Return a pointer to just after the name. Equal to "arg" if there is no
19217 * valid name.
19218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019220find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 char_u *arg;
19222 char_u **expr_start;
19223 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019224 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226 int mb_nest = 0;
19227 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 char_u *p;
19229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019230 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232 *expr_start = NULL;
19233 *expr_end = NULL;
19234 }
19235
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019236 /* Quick check for valid starting character. */
19237 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19238 return arg;
19239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019240 for (p = arg; *p != NUL
19241 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019242 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019243 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019245 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019246 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019247 if (*p == '\'')
19248 {
19249 /* skip over 'string' to avoid counting [ and ] inside it. */
19250 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19251 ;
19252 if (*p == NUL)
19253 break;
19254 }
19255 else if (*p == '"')
19256 {
19257 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19258 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19259 if (*p == '\\' && p[1] != NUL)
19260 ++p;
19261 if (*p == NUL)
19262 break;
19263 }
19264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019265 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019267 if (*p == '[')
19268 ++br_nest;
19269 else if (*p == ']')
19270 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019275 if (*p == '{')
19276 {
19277 mb_nest++;
19278 if (expr_start != NULL && *expr_start == NULL)
19279 *expr_start = p;
19280 }
19281 else if (*p == '}')
19282 {
19283 mb_nest--;
19284 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19285 *expr_end = p;
19286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 }
19289
19290 return p;
19291}
19292
19293/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019294 * Expands out the 'magic' {}'s in a variable/function name.
19295 * Note that this can call itself recursively, to deal with
19296 * constructs like foo{bar}{baz}{bam}
19297 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19298 * "in_start" ^
19299 * "expr_start" ^
19300 * "expr_end" ^
19301 * "in_end" ^
19302 *
19303 * Returns a new allocated string, which the caller must free.
19304 * Returns NULL for failure.
19305 */
19306 static char_u *
19307make_expanded_name(in_start, expr_start, expr_end, in_end)
19308 char_u *in_start;
19309 char_u *expr_start;
19310 char_u *expr_end;
19311 char_u *in_end;
19312{
19313 char_u c1;
19314 char_u *retval = NULL;
19315 char_u *temp_result;
19316 char_u *nextcmd = NULL;
19317
19318 if (expr_end == NULL || in_end == NULL)
19319 return NULL;
19320 *expr_start = NUL;
19321 *expr_end = NUL;
19322 c1 = *in_end;
19323 *in_end = NUL;
19324
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019325 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019326 if (temp_result != NULL && nextcmd == NULL)
19327 {
19328 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19329 + (in_end - expr_end) + 1));
19330 if (retval != NULL)
19331 {
19332 STRCPY(retval, in_start);
19333 STRCAT(retval, temp_result);
19334 STRCAT(retval, expr_end + 1);
19335 }
19336 }
19337 vim_free(temp_result);
19338
19339 *in_end = c1; /* put char back for error messages */
19340 *expr_start = '{';
19341 *expr_end = '}';
19342
19343 if (retval != NULL)
19344 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019345 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019346 if (expr_start != NULL)
19347 {
19348 /* Further expansion! */
19349 temp_result = make_expanded_name(retval, expr_start,
19350 expr_end, temp_result);
19351 vim_free(retval);
19352 retval = temp_result;
19353 }
19354 }
19355
19356 return retval;
19357}
19358
19359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019361 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 */
19363 static int
19364eval_isnamec(c)
19365 int c;
19366{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019367 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19368}
19369
19370/*
19371 * Return TRUE if character "c" can be used as the first character in a
19372 * variable or function name (excluding '{' and '}').
19373 */
19374 static int
19375eval_isnamec1(c)
19376 int c;
19377{
19378 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379}
19380
19381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 * Set number v: variable to "val".
19383 */
19384 void
19385set_vim_var_nr(idx, val)
19386 int idx;
19387 long val;
19388{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019389 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390}
19391
19392/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019393 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019394 */
19395 long
19396get_vim_var_nr(idx)
19397 int idx;
19398{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019399 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400}
19401
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019402/*
19403 * Get string v: variable value. Uses a static buffer, can only be used once.
19404 */
19405 char_u *
19406get_vim_var_str(idx)
19407 int idx;
19408{
19409 return get_tv_string(&vimvars[idx].vv_tv);
19410}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019411
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019413 * Get List v: variable value. Caller must take care of reference count when
19414 * needed.
19415 */
19416 list_T *
19417get_vim_var_list(idx)
19418 int idx;
19419{
19420 return vimvars[idx].vv_list;
19421}
19422
19423/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019424 * Set v:char to character "c".
19425 */
19426 void
19427set_vim_var_char(c)
19428 int c;
19429{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019430 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019431
19432#ifdef FEAT_MBYTE
19433 if (has_mbyte)
19434 buf[(*mb_char2bytes)(c, buf)] = NUL;
19435 else
19436#endif
19437 {
19438 buf[0] = c;
19439 buf[1] = NUL;
19440 }
19441 set_vim_var_string(VV_CHAR, buf, -1);
19442}
19443
19444/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019445 * Set v:count to "count" and v:count1 to "count1".
19446 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 */
19448 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019449set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 long count;
19451 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019452 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019454 if (set_prevcount)
19455 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019456 vimvars[VV_COUNT].vv_nr = count;
19457 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458}
19459
19460/*
19461 * Set string v: variable to a copy of "val".
19462 */
19463 void
19464set_vim_var_string(idx, val, len)
19465 int idx;
19466 char_u *val;
19467 int len; /* length of "val" to use or -1 (whole string) */
19468{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019469 /* Need to do this (at least) once, since we can't initialize a union.
19470 * Will always be invoked when "v:progname" is set. */
19471 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19472
Bram Moolenaare9a41262005-01-15 22:18:47 +000019473 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019475 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019477 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019479 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480}
19481
19482/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019483 * Set List v: variable to "val".
19484 */
19485 void
19486set_vim_var_list(idx, val)
19487 int idx;
19488 list_T *val;
19489{
19490 list_unref(vimvars[idx].vv_list);
19491 vimvars[idx].vv_list = val;
19492 if (val != NULL)
19493 ++val->lv_refcount;
19494}
19495
19496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497 * Set v:register if needed.
19498 */
19499 void
19500set_reg_var(c)
19501 int c;
19502{
19503 char_u regname;
19504
19505 if (c == 0 || c == ' ')
19506 regname = '"';
19507 else
19508 regname = c;
19509 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019510 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 set_vim_var_string(VV_REG, &regname, 1);
19512}
19513
19514/*
19515 * Get or set v:exception. If "oldval" == NULL, return the current value.
19516 * Otherwise, restore the value to "oldval" and return NULL.
19517 * Must always be called in pairs to save and restore v:exception! Does not
19518 * take care of memory allocations.
19519 */
19520 char_u *
19521v_exception(oldval)
19522 char_u *oldval;
19523{
19524 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019525 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526
Bram Moolenaare9a41262005-01-15 22:18:47 +000019527 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 return NULL;
19529}
19530
19531/*
19532 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19533 * Otherwise, restore the value to "oldval" and return NULL.
19534 * Must always be called in pairs to save and restore v:throwpoint! Does not
19535 * take care of memory allocations.
19536 */
19537 char_u *
19538v_throwpoint(oldval)
19539 char_u *oldval;
19540{
19541 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019542 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543
Bram Moolenaare9a41262005-01-15 22:18:47 +000019544 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 return NULL;
19546}
19547
19548#if defined(FEAT_AUTOCMD) || defined(PROTO)
19549/*
19550 * Set v:cmdarg.
19551 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19552 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19553 * Must always be called in pairs!
19554 */
19555 char_u *
19556set_cmdarg(eap, oldarg)
19557 exarg_T *eap;
19558 char_u *oldarg;
19559{
19560 char_u *oldval;
19561 char_u *newval;
19562 unsigned len;
19563
Bram Moolenaare9a41262005-01-15 22:18:47 +000019564 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019565 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019567 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019568 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019569 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 }
19571
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019572 if (eap->force_bin == FORCE_BIN)
19573 len = 6;
19574 else if (eap->force_bin == FORCE_NOBIN)
19575 len = 8;
19576 else
19577 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019578
19579 if (eap->read_edit)
19580 len += 7;
19581
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019582 if (eap->force_ff != 0)
19583 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19584# ifdef FEAT_MBYTE
19585 if (eap->force_enc != 0)
19586 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019587 if (eap->bad_char != 0)
19588 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019589# endif
19590
19591 newval = alloc(len + 1);
19592 if (newval == NULL)
19593 return NULL;
19594
19595 if (eap->force_bin == FORCE_BIN)
19596 sprintf((char *)newval, " ++bin");
19597 else if (eap->force_bin == FORCE_NOBIN)
19598 sprintf((char *)newval, " ++nobin");
19599 else
19600 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019601
19602 if (eap->read_edit)
19603 STRCAT(newval, " ++edit");
19604
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019605 if (eap->force_ff != 0)
19606 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19607 eap->cmd + eap->force_ff);
19608# ifdef FEAT_MBYTE
19609 if (eap->force_enc != 0)
19610 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19611 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019612 if (eap->bad_char == BAD_KEEP)
19613 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19614 else if (eap->bad_char == BAD_DROP)
19615 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19616 else if (eap->bad_char != 0)
19617 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019618# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019619 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019620 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621}
19622#endif
19623
19624/*
19625 * Get the value of internal variable "name".
19626 * Return OK or FAIL.
19627 */
19628 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019629get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 char_u *name;
19631 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019632 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019633 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634{
19635 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 typval_T *tv = NULL;
19637 typval_T atv;
19638 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640
19641 /* truncate the name, so that we can use strcmp() */
19642 cc = name[len];
19643 name[len] = NUL;
19644
19645 /*
19646 * Check for "b:changedtick".
19647 */
19648 if (STRCMP(name, "b:changedtick") == 0)
19649 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019650 atv.v_type = VAR_NUMBER;
19651 atv.vval.v_number = curbuf->b_changedtick;
19652 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 }
19654
19655 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 * Check for user-defined variables.
19657 */
19658 else
19659 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019660 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 }
19664
Bram Moolenaare9a41262005-01-15 22:18:47 +000019665 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019667 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019668 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 ret = FAIL;
19670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019671 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019672 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673
19674 name[len] = cc;
19675
19676 return ret;
19677}
19678
19679/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019680 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19681 * Also handle function call with Funcref variable: func(expr)
19682 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19683 */
19684 static int
19685handle_subscript(arg, rettv, evaluate, verbose)
19686 char_u **arg;
19687 typval_T *rettv;
19688 int evaluate; /* do more than finding the end */
19689 int verbose; /* give error messages */
19690{
19691 int ret = OK;
19692 dict_T *selfdict = NULL;
19693 char_u *s;
19694 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019695 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019696
19697 while (ret == OK
19698 && (**arg == '['
19699 || (**arg == '.' && rettv->v_type == VAR_DICT)
19700 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19701 && !vim_iswhite(*(*arg - 1)))
19702 {
19703 if (**arg == '(')
19704 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019705 /* need to copy the funcref so that we can clear rettv */
19706 functv = *rettv;
19707 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019708
19709 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019710 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019711 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019712 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19713 &len, evaluate, selfdict);
19714
19715 /* Clear the funcref afterwards, so that deleting it while
19716 * evaluating the arguments is possible (see test55). */
19717 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019718
19719 /* Stop the expression evaluation when immediately aborting on
19720 * error, or when an interrupt occurred or an exception was thrown
19721 * but not caught. */
19722 if (aborting())
19723 {
19724 if (ret == OK)
19725 clear_tv(rettv);
19726 ret = FAIL;
19727 }
19728 dict_unref(selfdict);
19729 selfdict = NULL;
19730 }
19731 else /* **arg == '[' || **arg == '.' */
19732 {
19733 dict_unref(selfdict);
19734 if (rettv->v_type == VAR_DICT)
19735 {
19736 selfdict = rettv->vval.v_dict;
19737 if (selfdict != NULL)
19738 ++selfdict->dv_refcount;
19739 }
19740 else
19741 selfdict = NULL;
19742 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19743 {
19744 clear_tv(rettv);
19745 ret = FAIL;
19746 }
19747 }
19748 }
19749 dict_unref(selfdict);
19750 return ret;
19751}
19752
19753/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019754 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 * value).
19756 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019757 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019758alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019759{
Bram Moolenaar33570922005-01-25 22:26:29 +000019760 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019761}
19762
19763/*
19764 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 * The string "s" must have been allocated, it is consumed.
19766 * Return NULL for out of memory, the variable otherwise.
19767 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 char_u *s;
19771{
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 rettv = alloc_tv();
19775 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 rettv->v_type = VAR_STRING;
19778 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 }
19780 else
19781 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783}
19784
19785/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019786 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019788 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019789free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019790 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791{
19792 if (varp != NULL)
19793 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 switch (varp->v_type)
19795 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019797 func_unref(varp->vval.v_string);
19798 /*FALLTHROUGH*/
19799 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019800 vim_free(varp->vval.v_string);
19801 break;
19802 case VAR_LIST:
19803 list_unref(varp->vval.v_list);
19804 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019805 case VAR_DICT:
19806 dict_unref(varp->vval.v_dict);
19807 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019808 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019809#ifdef FEAT_FLOAT
19810 case VAR_FLOAT:
19811#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019812 case VAR_UNKNOWN:
19813 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019814 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019815 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019816 break;
19817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 vim_free(varp);
19819 }
19820}
19821
19822/*
19823 * Free the memory for a variable value and set the value to NULL or 0.
19824 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019825 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828{
19829 if (varp != NULL)
19830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019831 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019833 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019834 func_unref(varp->vval.v_string);
19835 /*FALLTHROUGH*/
19836 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019837 vim_free(varp->vval.v_string);
19838 varp->vval.v_string = NULL;
19839 break;
19840 case VAR_LIST:
19841 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019842 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019843 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019844 case VAR_DICT:
19845 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019846 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019847 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019849 varp->vval.v_number = 0;
19850 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019851#ifdef FEAT_FLOAT
19852 case VAR_FLOAT:
19853 varp->vval.v_float = 0.0;
19854 break;
19855#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856 case VAR_UNKNOWN:
19857 break;
19858 default:
19859 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019861 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 }
19863}
19864
19865/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019866 * Set the value of a variable to NULL without freeing items.
19867 */
19868 static void
19869init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019871{
19872 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019873 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019874}
19875
19876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 * Get the number value of a variable.
19878 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019879 * For incompatible types, return 0.
19880 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19881 * caller of incompatible types: it sets *denote to TRUE if "denote"
19882 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 */
19884 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019885get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019886 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019888 int error = FALSE;
19889
19890 return get_tv_number_chk(varp, &error); /* return 0L on error */
19891}
19892
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019893 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019894get_tv_number_chk(varp, denote)
19895 typval_T *varp;
19896 int *denote;
19897{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019898 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019900 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019902 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019903 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019904#ifdef FEAT_FLOAT
19905 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019906 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019907 break;
19908#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019910 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019911 break;
19912 case VAR_STRING:
19913 if (varp->vval.v_string != NULL)
19914 vim_str2nr(varp->vval.v_string, NULL, NULL,
19915 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019916 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019917 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019918 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019919 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019920 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019921 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019922 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019923 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019924 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019927 if (denote == NULL) /* useful for values that must be unsigned */
19928 n = -1;
19929 else
19930 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019931 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932}
19933
19934/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019935 * Get the lnum from the first argument.
19936 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019937 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 */
19939 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019940get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942{
Bram Moolenaar33570922005-01-25 22:26:29 +000019943 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944 linenr_T lnum;
19945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019946 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947 if (lnum == 0) /* no valid number, try using line() */
19948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 rettv.v_type = VAR_NUMBER;
19950 f_line(argvars, &rettv);
19951 lnum = rettv.vval.v_number;
19952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 }
19954 return lnum;
19955}
19956
19957/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019958 * Get the lnum from the first argument.
19959 * Also accepts "$", then "buf" is used.
19960 * Returns 0 on error.
19961 */
19962 static linenr_T
19963get_tv_lnum_buf(argvars, buf)
19964 typval_T *argvars;
19965 buf_T *buf;
19966{
19967 if (argvars[0].v_type == VAR_STRING
19968 && argvars[0].vval.v_string != NULL
19969 && argvars[0].vval.v_string[0] == '$'
19970 && buf != NULL)
19971 return buf->b_ml.ml_line_count;
19972 return get_tv_number_chk(&argvars[0], NULL);
19973}
19974
19975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 * Get the string value of a variable.
19977 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019978 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19979 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 * If the String variable has never been set, return an empty string.
19981 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019982 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19983 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 */
19985 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019987 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988{
19989 static char_u mybuf[NUMBUFLEN];
19990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019991 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992}
19993
19994 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019996 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019997 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 char_u *res = get_tv_string_buf_chk(varp, buf);
20000
20001 return res != NULL ? res : (char_u *)"";
20002}
20003
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020004 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020005get_tv_string_chk(varp)
20006 typval_T *varp;
20007{
20008 static char_u mybuf[NUMBUFLEN];
20009
20010 return get_tv_string_buf_chk(varp, mybuf);
20011}
20012
20013 static char_u *
20014get_tv_string_buf_chk(varp, buf)
20015 typval_T *varp;
20016 char_u *buf;
20017{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020020 case VAR_NUMBER:
20021 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20022 return buf;
20023 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020024 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020025 break;
20026 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020027 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020028 break;
20029 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020030 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020032#ifdef FEAT_FLOAT
20033 case VAR_FLOAT:
20034 EMSG(_("E806: using Float as a String"));
20035 break;
20036#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020037 case VAR_STRING:
20038 if (varp->vval.v_string != NULL)
20039 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020040 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020041 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020045 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046}
20047
20048/*
20049 * Find variable "name" in the list of variables.
20050 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020051 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020052 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020055 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020056find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020061 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062
Bram Moolenaara7043832005-01-21 11:56:39 +000020063 ht = find_var_ht(name, &varname);
20064 if (htp != NULL)
20065 *htp = ht;
20066 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020068 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020072 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020073 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020075 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020076find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020078 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020079 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020080 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020081{
Bram Moolenaar33570922005-01-25 22:26:29 +000020082 hashitem_T *hi;
20083
20084 if (*varname == NUL)
20085 {
20086 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020087 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020088 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020089 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020090 case 'g': return &globvars_var;
20091 case 'v': return &vimvars_var;
20092 case 'b': return &curbuf->b_bufvar;
20093 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020094#ifdef FEAT_WINDOWS
20095 case 't': return &curtab->tp_winvar;
20096#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020097 case 'l': return current_funccal == NULL
20098 ? NULL : &current_funccal->l_vars_var;
20099 case 'a': return current_funccal == NULL
20100 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020101 }
20102 return NULL;
20103 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020104
20105 hi = hash_find(ht, varname);
20106 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020107 {
20108 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020109 * worked find the variable again. Don't auto-load a script if it was
20110 * loaded already, otherwise it would be loaded every time when
20111 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020112 if (ht == &globvarht && !writing)
20113 {
20114 /* Note: script_autoload() may make "hi" invalid. It must either
20115 * be obtained again or not used. */
20116 if (!script_autoload(varname, FALSE) || aborting())
20117 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020118 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020119 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020120 if (HASHITEM_EMPTY(hi))
20121 return NULL;
20122 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020123 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020124}
20125
20126/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020127 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020128 * Set "varname" to the start of name without ':'.
20129 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020130 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020131find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 char_u *name;
20133 char_u **varname;
20134{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020135 hashitem_T *hi;
20136
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (name[1] != ':')
20138 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020139 /* The name must not start with a colon or #. */
20140 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 return NULL;
20142 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020143
20144 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020145 hi = hash_find(&compat_hashtab, name);
20146 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020147 return &compat_hashtab;
20148
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 return &globvarht; /* global variable */
20151 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 }
20153 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020154 if (*name == 'g') /* global variable */
20155 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020156 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20157 */
20158 if (vim_strchr(name + 2, ':') != NULL
20159 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020160 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020162 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020164 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020165#ifdef FEAT_WINDOWS
20166 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020167 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020168#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 if (*name == 'v') /* v: variable */
20170 return &vimvarht;
20171 if (*name == 'a' && current_funccal != NULL) /* function argument */
20172 return &current_funccal->l_avars.dv_hashtab;
20173 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20174 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 if (*name == 's' /* script variable */
20176 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20177 return &SCRIPT_VARS(current_SID);
20178 return NULL;
20179}
20180
20181/*
20182 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020183 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 * Returns NULL when it doesn't exist.
20185 */
20186 char_u *
20187get_var_value(name)
20188 char_u *name;
20189{
Bram Moolenaar33570922005-01-25 22:26:29 +000020190 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191
Bram Moolenaara7043832005-01-21 11:56:39 +000020192 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020193 if (v == NULL)
20194 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020195 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196}
20197
20198/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 * sourcing this script and when executing functions defined in the script.
20201 */
20202 void
20203new_script_vars(id)
20204 scid_T id;
20205{
Bram Moolenaara7043832005-01-21 11:56:39 +000020206 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020207 hashtab_T *ht;
20208 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020209
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20211 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020212 /* Re-allocating ga_data means that an ht_array pointing to
20213 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020214 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020215 for (i = 1; i <= ga_scripts.ga_len; ++i)
20216 {
20217 ht = &SCRIPT_VARS(i);
20218 if (ht->ht_mask == HT_INIT_SIZE - 1)
20219 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020220 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020222 }
20223
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 while (ga_scripts.ga_len < id)
20225 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020226 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020227 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020228 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 }
20231 }
20232}
20233
20234/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20236 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 */
20238 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020239init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 dict_T *dict;
20241 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020242 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020245 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020246 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020247 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020248 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 dict_var->di_tv.vval.v_dict = dict;
20250 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020251 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020252 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20253 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254}
20255
20256/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020257 * Unreference a dictionary initialized by init_var_dict().
20258 */
20259 void
20260unref_var_dict(dict)
20261 dict_T *dict;
20262{
20263 /* Now the dict needs to be freed if no one else is using it, go back to
20264 * normal reference counting. */
20265 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20266 dict_unref(dict);
20267}
20268
20269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020271 * Frees all allocated variables and the value they contain.
20272 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 */
20274 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020275vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020276 hashtab_T *ht;
20277{
20278 vars_clear_ext(ht, TRUE);
20279}
20280
20281/*
20282 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20283 */
20284 static void
20285vars_clear_ext(ht, free_val)
20286 hashtab_T *ht;
20287 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288{
Bram Moolenaara7043832005-01-21 11:56:39 +000020289 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020290 hashitem_T *hi;
20291 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292
Bram Moolenaar33570922005-01-25 22:26:29 +000020293 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020294 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020295 for (hi = ht->ht_array; todo > 0; ++hi)
20296 {
20297 if (!HASHITEM_EMPTY(hi))
20298 {
20299 --todo;
20300
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020302 * ht_array might change then. hash_clear() takes care of it
20303 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020304 v = HI2DI(hi);
20305 if (free_val)
20306 clear_tv(&v->di_tv);
20307 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20308 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020309 }
20310 }
20311 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020312 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313}
20314
Bram Moolenaara7043832005-01-21 11:56:39 +000020315/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020316 * Delete a variable from hashtab "ht" at item "hi".
20317 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020320delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020321 hashtab_T *ht;
20322 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323{
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020325
20326 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020327 clear_tv(&di->di_tv);
20328 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329}
20330
20331/*
20332 * List the value of one internal variable.
20333 */
20334 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020335list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020336 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020338 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020340 char_u *tofree;
20341 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020343
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020344 current_copyID += COPYID_INC;
20345 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020347 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020348 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349}
20350
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020352list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353 char_u *prefix;
20354 char_u *name;
20355 int type;
20356 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020357 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358{
Bram Moolenaar31859182007-08-14 20:41:13 +000020359 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20360 msg_start();
20361 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 if (name != NULL) /* "a:" vars don't have a name stored */
20363 msg_puts(name);
20364 msg_putchar(' ');
20365 msg_advance(22);
20366 if (type == VAR_NUMBER)
20367 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020368 else if (type == VAR_FUNC)
20369 msg_putchar('*');
20370 else if (type == VAR_LIST)
20371 {
20372 msg_putchar('[');
20373 if (*string == '[')
20374 ++string;
20375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020376 else if (type == VAR_DICT)
20377 {
20378 msg_putchar('{');
20379 if (*string == '{')
20380 ++string;
20381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 else
20383 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020384
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020386
20387 if (type == VAR_FUNC)
20388 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020389 if (*first)
20390 {
20391 msg_clr_eos();
20392 *first = FALSE;
20393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394}
20395
20396/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020397 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 * If the variable already exists, the value is updated.
20399 * Otherwise the variable is created.
20400 */
20401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020402set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020405 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406{
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020411 ht = find_var_ht(name, &varname);
20412 if (ht == NULL || *varname == NUL)
20413 {
20414 EMSG2(_(e_illvar), name);
20415 return;
20416 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020417 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020418
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020419 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20420 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020421
Bram Moolenaar33570922005-01-25 22:26:29 +000020422 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020425 if (var_check_ro(v->di_flags, name)
20426 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020427 return;
20428 if (v->di_tv.v_type != tv->v_type
20429 && !((v->di_tv.v_type == VAR_STRING
20430 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020431 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020432 || tv->v_type == VAR_NUMBER))
20433#ifdef FEAT_FLOAT
20434 && !((v->di_tv.v_type == VAR_NUMBER
20435 || v->di_tv.v_type == VAR_FLOAT)
20436 && (tv->v_type == VAR_NUMBER
20437 || tv->v_type == VAR_FLOAT))
20438#endif
20439 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020440 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020441 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020442 return;
20443 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020444
20445 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020446 * Handle setting internal v: variables separately: we don't change
20447 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020448 */
20449 if (ht == &vimvarht)
20450 {
20451 if (v->di_tv.v_type == VAR_STRING)
20452 {
20453 vim_free(v->di_tv.vval.v_string);
20454 if (copy || tv->v_type != VAR_STRING)
20455 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20456 else
20457 {
20458 /* Take over the string to avoid an extra alloc/free. */
20459 v->di_tv.vval.v_string = tv->vval.v_string;
20460 tv->vval.v_string = NULL;
20461 }
20462 }
20463 else if (v->di_tv.v_type != VAR_NUMBER)
20464 EMSG2(_(e_intern2), "set_var()");
20465 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020466 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020468 if (STRCMP(varname, "searchforward") == 0)
20469 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20470 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020471 return;
20472 }
20473
20474 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
20476 else /* add a new variable */
20477 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020478 /* Can't add "v:" variable. */
20479 if (ht == &vimvarht)
20480 {
20481 EMSG2(_(e_illvar), name);
20482 return;
20483 }
20484
Bram Moolenaar92124a32005-06-17 22:03:40 +000020485 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020486 if (!valid_varname(varname))
20487 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020488
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020489 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20490 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020491 if (v == NULL)
20492 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020494 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020496 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 return;
20498 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020499 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020501
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020502 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020503 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020504 else
20505 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020506 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020507 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510}
20511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020512/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020513 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020514 * Also give an error message.
20515 */
20516 static int
20517var_check_ro(flags, name)
20518 int flags;
20519 char_u *name;
20520{
20521 if (flags & DI_FLAGS_RO)
20522 {
20523 EMSG2(_(e_readonlyvar), name);
20524 return TRUE;
20525 }
20526 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20527 {
20528 EMSG2(_(e_readonlysbx), name);
20529 return TRUE;
20530 }
20531 return FALSE;
20532}
20533
20534/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020535 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20536 * Also give an error message.
20537 */
20538 static int
20539var_check_fixed(flags, name)
20540 int flags;
20541 char_u *name;
20542{
20543 if (flags & DI_FLAGS_FIX)
20544 {
20545 EMSG2(_("E795: Cannot delete variable %s"), name);
20546 return TRUE;
20547 }
20548 return FALSE;
20549}
20550
20551/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020552 * Check if a funcref is assigned to a valid variable name.
20553 * Return TRUE and give an error if not.
20554 */
20555 static int
20556var_check_func_name(name, new_var)
20557 char_u *name; /* points to start of variable name */
20558 int new_var; /* TRUE when creating the variable */
20559{
20560 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20561 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20562 ? name[2] : name[0]))
20563 {
20564 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20565 name);
20566 return TRUE;
20567 }
20568 /* Don't allow hiding a function. When "v" is not NULL we might be
20569 * assigning another function to the same var, the type is checked
20570 * below. */
20571 if (new_var && function_exists(name))
20572 {
20573 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20574 name);
20575 return TRUE;
20576 }
20577 return FALSE;
20578}
20579
20580/*
20581 * Check if a variable name is valid.
20582 * Return FALSE and give an error if not.
20583 */
20584 static int
20585valid_varname(varname)
20586 char_u *varname;
20587{
20588 char_u *p;
20589
20590 for (p = varname; *p != NUL; ++p)
20591 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20592 && *p != AUTOLOAD_CHAR)
20593 {
20594 EMSG2(_(e_illvar), varname);
20595 return FALSE;
20596 }
20597 return TRUE;
20598}
20599
20600/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020601 * Return TRUE if typeval "tv" is set to be locked (immutable).
20602 * Also give an error message, using "name".
20603 */
20604 static int
20605tv_check_lock(lock, name)
20606 int lock;
20607 char_u *name;
20608{
20609 if (lock & VAR_LOCKED)
20610 {
20611 EMSG2(_("E741: Value is locked: %s"),
20612 name == NULL ? (char_u *)_("Unknown") : name);
20613 return TRUE;
20614 }
20615 if (lock & VAR_FIXED)
20616 {
20617 EMSG2(_("E742: Cannot change value of %s"),
20618 name == NULL ? (char_u *)_("Unknown") : name);
20619 return TRUE;
20620 }
20621 return FALSE;
20622}
20623
20624/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020625 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020626 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020627 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020628 * It is OK for "from" and "to" to point to the same item. This is used to
20629 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020630 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020631 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020633 typval_T *from;
20634 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020636 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020637 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020638 switch (from->v_type)
20639 {
20640 case VAR_NUMBER:
20641 to->vval.v_number = from->vval.v_number;
20642 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020643#ifdef FEAT_FLOAT
20644 case VAR_FLOAT:
20645 to->vval.v_float = from->vval.v_float;
20646 break;
20647#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020648 case VAR_STRING:
20649 case VAR_FUNC:
20650 if (from->vval.v_string == NULL)
20651 to->vval.v_string = NULL;
20652 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020654 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020655 if (from->v_type == VAR_FUNC)
20656 func_ref(to->vval.v_string);
20657 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020658 break;
20659 case VAR_LIST:
20660 if (from->vval.v_list == NULL)
20661 to->vval.v_list = NULL;
20662 else
20663 {
20664 to->vval.v_list = from->vval.v_list;
20665 ++to->vval.v_list->lv_refcount;
20666 }
20667 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020668 case VAR_DICT:
20669 if (from->vval.v_dict == NULL)
20670 to->vval.v_dict = NULL;
20671 else
20672 {
20673 to->vval.v_dict = from->vval.v_dict;
20674 ++to->vval.v_dict->dv_refcount;
20675 }
20676 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020677 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020678 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020679 break;
20680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681}
20682
20683/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020684 * Make a copy of an item.
20685 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020686 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20687 * reference to an already copied list/dict can be used.
20688 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020689 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020690 static int
20691item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020692 typval_T *from;
20693 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020694 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020695 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020696{
20697 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020698 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020699
Bram Moolenaar33570922005-01-25 22:26:29 +000020700 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020701 {
20702 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020703 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020704 }
20705 ++recurse;
20706
20707 switch (from->v_type)
20708 {
20709 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020710#ifdef FEAT_FLOAT
20711 case VAR_FLOAT:
20712#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020713 case VAR_STRING:
20714 case VAR_FUNC:
20715 copy_tv(from, to);
20716 break;
20717 case VAR_LIST:
20718 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020719 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020720 if (from->vval.v_list == NULL)
20721 to->vval.v_list = NULL;
20722 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20723 {
20724 /* use the copy made earlier */
20725 to->vval.v_list = from->vval.v_list->lv_copylist;
20726 ++to->vval.v_list->lv_refcount;
20727 }
20728 else
20729 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20730 if (to->vval.v_list == NULL)
20731 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020732 break;
20733 case VAR_DICT:
20734 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020735 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020736 if (from->vval.v_dict == NULL)
20737 to->vval.v_dict = NULL;
20738 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20739 {
20740 /* use the copy made earlier */
20741 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20742 ++to->vval.v_dict->dv_refcount;
20743 }
20744 else
20745 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20746 if (to->vval.v_dict == NULL)
20747 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020748 break;
20749 default:
20750 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020751 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020752 }
20753 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020754 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020755}
20756
20757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 * ":echo expr1 ..." print each argument separated with a space, add a
20759 * newline at the end.
20760 * ":echon expr1 ..." print each argument plain.
20761 */
20762 void
20763ex_echo(eap)
20764 exarg_T *eap;
20765{
20766 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020767 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020768 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 char_u *p;
20770 int needclr = TRUE;
20771 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020772 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773
20774 if (eap->skip)
20775 ++emsg_skip;
20776 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20777 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020778 /* If eval1() causes an error message the text from the command may
20779 * still need to be cleared. E.g., "echo 22,44". */
20780 need_clr_eos = needclr;
20781
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020783 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 {
20785 /*
20786 * Report the invalid expression unless the expression evaluation
20787 * has been cancelled due to an aborting error, an interrupt, or an
20788 * exception.
20789 */
20790 if (!aborting())
20791 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020792 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 break;
20794 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020795 need_clr_eos = FALSE;
20796
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 if (!eap->skip)
20798 {
20799 if (atstart)
20800 {
20801 atstart = FALSE;
20802 /* Call msg_start() after eval1(), evaluating the expression
20803 * may cause a message to appear. */
20804 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020805 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020806 /* Mark the saved text as finishing the line, so that what
20807 * follows is displayed on a new line when scrolling back
20808 * at the more prompt. */
20809 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 }
20813 else if (eap->cmdidx == CMD_echo)
20814 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020815 current_copyID += COPYID_INC;
20816 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020817 if (p != NULL)
20818 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020820 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020822 if (*p != TAB && needclr)
20823 {
20824 /* remove any text still there from the command */
20825 msg_clr_eos();
20826 needclr = FALSE;
20827 }
20828 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 }
20830 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020831 {
20832#ifdef FEAT_MBYTE
20833 if (has_mbyte)
20834 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020835 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020836
20837 (void)msg_outtrans_len_attr(p, i, echo_attr);
20838 p += i - 1;
20839 }
20840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020842 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020845 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848 arg = skipwhite(arg);
20849 }
20850 eap->nextcmd = check_nextcmd(arg);
20851
20852 if (eap->skip)
20853 --emsg_skip;
20854 else
20855 {
20856 /* remove text that may still be there from the command */
20857 if (needclr)
20858 msg_clr_eos();
20859 if (eap->cmdidx == CMD_echo)
20860 msg_end();
20861 }
20862}
20863
20864/*
20865 * ":echohl {name}".
20866 */
20867 void
20868ex_echohl(eap)
20869 exarg_T *eap;
20870{
20871 int id;
20872
20873 id = syn_name2id(eap->arg);
20874 if (id == 0)
20875 echo_attr = 0;
20876 else
20877 echo_attr = syn_id2attr(id);
20878}
20879
20880/*
20881 * ":execute expr1 ..." execute the result of an expression.
20882 * ":echomsg expr1 ..." Print a message
20883 * ":echoerr expr1 ..." Print an error
20884 * Each gets spaces around each argument and a newline at the end for
20885 * echo commands
20886 */
20887 void
20888ex_execute(eap)
20889 exarg_T *eap;
20890{
20891 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020892 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 int ret = OK;
20894 char_u *p;
20895 garray_T ga;
20896 int len;
20897 int save_did_emsg;
20898
20899 ga_init2(&ga, 1, 80);
20900
20901 if (eap->skip)
20902 ++emsg_skip;
20903 while (*arg != NUL && *arg != '|' && *arg != '\n')
20904 {
20905 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020906 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 {
20908 /*
20909 * Report the invalid expression unless the expression evaluation
20910 * has been cancelled due to an aborting error, an interrupt, or an
20911 * exception.
20912 */
20913 if (!aborting())
20914 EMSG2(_(e_invexpr2), p);
20915 ret = FAIL;
20916 break;
20917 }
20918
20919 if (!eap->skip)
20920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020921 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 len = (int)STRLEN(p);
20923 if (ga_grow(&ga, len + 2) == FAIL)
20924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020925 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 ret = FAIL;
20927 break;
20928 }
20929 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 ga.ga_len += len;
20933 }
20934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020935 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 arg = skipwhite(arg);
20937 }
20938
20939 if (ret != FAIL && ga.ga_data != NULL)
20940 {
20941 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020944 out_flush();
20945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 else if (eap->cmdidx == CMD_echoerr)
20947 {
20948 /* We don't want to abort following commands, restore did_emsg. */
20949 save_did_emsg = did_emsg;
20950 EMSG((char_u *)ga.ga_data);
20951 if (!force_abort)
20952 did_emsg = save_did_emsg;
20953 }
20954 else if (eap->cmdidx == CMD_execute)
20955 do_cmdline((char_u *)ga.ga_data,
20956 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20957 }
20958
20959 ga_clear(&ga);
20960
20961 if (eap->skip)
20962 --emsg_skip;
20963
20964 eap->nextcmd = check_nextcmd(arg);
20965}
20966
20967/*
20968 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20969 * "arg" points to the "&" or '+' when called, to "option" when returning.
20970 * Returns NULL when no option name found. Otherwise pointer to the char
20971 * after the option name.
20972 */
20973 static char_u *
20974find_option_end(arg, opt_flags)
20975 char_u **arg;
20976 int *opt_flags;
20977{
20978 char_u *p = *arg;
20979
20980 ++p;
20981 if (*p == 'g' && p[1] == ':')
20982 {
20983 *opt_flags = OPT_GLOBAL;
20984 p += 2;
20985 }
20986 else if (*p == 'l' && p[1] == ':')
20987 {
20988 *opt_flags = OPT_LOCAL;
20989 p += 2;
20990 }
20991 else
20992 *opt_flags = 0;
20993
20994 if (!ASCII_ISALPHA(*p))
20995 return NULL;
20996 *arg = p;
20997
20998 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20999 p += 4; /* termcap option */
21000 else
21001 while (ASCII_ISALPHA(*p))
21002 ++p;
21003 return p;
21004}
21005
21006/*
21007 * ":function"
21008 */
21009 void
21010ex_function(eap)
21011 exarg_T *eap;
21012{
21013 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021014 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 int j;
21016 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 char_u *name = NULL;
21019 char_u *p;
21020 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021021 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 garray_T newargs;
21023 garray_T newlines;
21024 int varargs = FALSE;
21025 int mustend = FALSE;
21026 int flags = 0;
21027 ufunc_T *fp;
21028 int indent;
21029 int nesting;
21030 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021031 dictitem_T *v;
21032 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021033 static int func_nr = 0; /* number for nameless function */
21034 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021035 hashtab_T *ht;
21036 int todo;
21037 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021038 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039
21040 /*
21041 * ":function" without argument: list functions.
21042 */
21043 if (ends_excmd(*eap->arg))
21044 {
21045 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021047 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021048 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021049 {
21050 if (!HASHITEM_EMPTY(hi))
21051 {
21052 --todo;
21053 fp = HI2UF(hi);
21054 if (!isdigit(*fp->uf_name))
21055 list_func_head(fp, FALSE);
21056 }
21057 }
21058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059 eap->nextcmd = check_nextcmd(eap->arg);
21060 return;
21061 }
21062
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021063 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021064 * ":function /pat": list functions matching pattern.
21065 */
21066 if (*eap->arg == '/')
21067 {
21068 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21069 if (!eap->skip)
21070 {
21071 regmatch_T regmatch;
21072
21073 c = *p;
21074 *p = NUL;
21075 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21076 *p = c;
21077 if (regmatch.regprog != NULL)
21078 {
21079 regmatch.rm_ic = p_ic;
21080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021081 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021082 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21083 {
21084 if (!HASHITEM_EMPTY(hi))
21085 {
21086 --todo;
21087 fp = HI2UF(hi);
21088 if (!isdigit(*fp->uf_name)
21089 && vim_regexec(&regmatch, fp->uf_name, 0))
21090 list_func_head(fp, FALSE);
21091 }
21092 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021093 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021094 }
21095 }
21096 if (*p == '/')
21097 ++p;
21098 eap->nextcmd = check_nextcmd(p);
21099 return;
21100 }
21101
21102 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021103 * Get the function name. There are these situations:
21104 * func normal function name
21105 * "name" == func, "fudi.fd_dict" == NULL
21106 * dict.func new dictionary entry
21107 * "name" == NULL, "fudi.fd_dict" set,
21108 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21109 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021110 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021111 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21112 * dict.func existing dict entry that's not a Funcref
21113 * "name" == NULL, "fudi.fd_dict" set,
21114 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021117 name = trans_function_name(&p, eap->skip, 0, &fudi);
21118 paren = (vim_strchr(p, '(') != NULL);
21119 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 {
21121 /*
21122 * Return on an invalid expression in braces, unless the expression
21123 * evaluation has been cancelled due to an aborting error, an
21124 * interrupt, or an exception.
21125 */
21126 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021127 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021128 if (!eap->skip && fudi.fd_newkey != NULL)
21129 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021130 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 else
21134 eap->skip = TRUE;
21135 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021136
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 /* An error in a function call during evaluation of an expression in magic
21138 * braces should not cause the function not to be defined. */
21139 saved_did_emsg = did_emsg;
21140 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141
21142 /*
21143 * ":function func" with only function name: list function.
21144 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021145 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 {
21147 if (!ends_excmd(*skipwhite(p)))
21148 {
21149 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021150 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151 }
21152 eap->nextcmd = check_nextcmd(p);
21153 if (eap->nextcmd != NULL)
21154 *p = NUL;
21155 if (!eap->skip && !got_int)
21156 {
21157 fp = find_func(name);
21158 if (fp != NULL)
21159 {
21160 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021161 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021163 if (FUNCLINE(fp, j) == NULL)
21164 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 msg_putchar('\n');
21166 msg_outnum((long)(j + 1));
21167 if (j < 9)
21168 msg_putchar(' ');
21169 if (j < 99)
21170 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021171 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 out_flush(); /* show a line at a time */
21173 ui_breakcheck();
21174 }
21175 if (!got_int)
21176 {
21177 msg_putchar('\n');
21178 msg_puts((char_u *)" endfunction");
21179 }
21180 }
21181 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021182 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021184 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 }
21186
21187 /*
21188 * ":function name(arg1, arg2)" Define function.
21189 */
21190 p = skipwhite(p);
21191 if (*p != '(')
21192 {
21193 if (!eap->skip)
21194 {
21195 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021196 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 }
21198 /* attempt to continue by skipping some text */
21199 if (vim_strchr(p, '(') != NULL)
21200 p = vim_strchr(p, '(');
21201 }
21202 p = skipwhite(p + 1);
21203
21204 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21205 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21206
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021207 if (!eap->skip)
21208 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021209 /* Check the name of the function. Unless it's a dictionary function
21210 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021211 if (name != NULL)
21212 arg = name;
21213 else
21214 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021215 if (arg != NULL && (fudi.fd_di == NULL
21216 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021217 {
21218 if (*arg == K_SPECIAL)
21219 j = 3;
21220 else
21221 j = 0;
21222 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21223 : eval_isnamec(arg[j])))
21224 ++j;
21225 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021226 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021227 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021228 /* Disallow using the g: dict. */
21229 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21230 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021231 }
21232
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 /*
21234 * Isolate the arguments: "arg1, arg2, ...)"
21235 */
21236 while (*p != ')')
21237 {
21238 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21239 {
21240 varargs = TRUE;
21241 p += 3;
21242 mustend = TRUE;
21243 }
21244 else
21245 {
21246 arg = p;
21247 while (ASCII_ISALNUM(*p) || *p == '_')
21248 ++p;
21249 if (arg == p || isdigit(*arg)
21250 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21251 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21252 {
21253 if (!eap->skip)
21254 EMSG2(_("E125: Illegal argument: %s"), arg);
21255 break;
21256 }
21257 if (ga_grow(&newargs, 1) == FAIL)
21258 goto erret;
21259 c = *p;
21260 *p = NUL;
21261 arg = vim_strsave(arg);
21262 if (arg == NULL)
21263 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021264
21265 /* Check for duplicate argument name. */
21266 for (i = 0; i < newargs.ga_len; ++i)
21267 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21268 {
21269 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21270 goto erret;
21271 }
21272
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21274 *p = c;
21275 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 if (*p == ',')
21277 ++p;
21278 else
21279 mustend = TRUE;
21280 }
21281 p = skipwhite(p);
21282 if (mustend && *p != ')')
21283 {
21284 if (!eap->skip)
21285 EMSG2(_(e_invarg2), eap->arg);
21286 break;
21287 }
21288 }
21289 ++p; /* skip the ')' */
21290
Bram Moolenaare9a41262005-01-15 22:18:47 +000021291 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 for (;;)
21293 {
21294 p = skipwhite(p);
21295 if (STRNCMP(p, "range", 5) == 0)
21296 {
21297 flags |= FC_RANGE;
21298 p += 5;
21299 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021300 else if (STRNCMP(p, "dict", 4) == 0)
21301 {
21302 flags |= FC_DICT;
21303 p += 4;
21304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 else if (STRNCMP(p, "abort", 5) == 0)
21306 {
21307 flags |= FC_ABORT;
21308 p += 5;
21309 }
21310 else
21311 break;
21312 }
21313
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021314 /* When there is a line break use what follows for the function body.
21315 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21316 if (*p == '\n')
21317 line_arg = p + 1;
21318 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 EMSG(_(e_trailing));
21320
21321 /*
21322 * Read the body of the function, until ":endfunction" is found.
21323 */
21324 if (KeyTyped)
21325 {
21326 /* Check if the function already exists, don't let the user type the
21327 * whole function before telling him it doesn't work! For a script we
21328 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021329 if (!eap->skip && !eap->forceit)
21330 {
21331 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21332 EMSG(_(e_funcdict));
21333 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021334 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021337 if (!eap->skip && did_emsg)
21338 goto erret;
21339
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340 msg_putchar('\n'); /* don't overwrite the function name */
21341 cmdline_row = msg_row;
21342 }
21343
21344 indent = 2;
21345 nesting = 0;
21346 for (;;)
21347 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021348 if (KeyTyped)
21349 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021351 sourcing_lnum_off = sourcing_lnum;
21352
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021353 if (line_arg != NULL)
21354 {
21355 /* Use eap->arg, split up in parts by line breaks. */
21356 theline = line_arg;
21357 p = vim_strchr(theline, '\n');
21358 if (p == NULL)
21359 line_arg += STRLEN(line_arg);
21360 else
21361 {
21362 *p = NUL;
21363 line_arg = p + 1;
21364 }
21365 }
21366 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 theline = getcmdline(':', 0L, indent);
21368 else
21369 theline = eap->getline(':', eap->cookie, indent);
21370 if (KeyTyped)
21371 lines_left = Rows - 1;
21372 if (theline == NULL)
21373 {
21374 EMSG(_("E126: Missing :endfunction"));
21375 goto erret;
21376 }
21377
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021378 /* Detect line continuation: sourcing_lnum increased more than one. */
21379 if (sourcing_lnum > sourcing_lnum_off + 1)
21380 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21381 else
21382 sourcing_lnum_off = 0;
21383
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 if (skip_until != NULL)
21385 {
21386 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21387 * don't check for ":endfunc". */
21388 if (STRCMP(theline, skip_until) == 0)
21389 {
21390 vim_free(skip_until);
21391 skip_until = NULL;
21392 }
21393 }
21394 else
21395 {
21396 /* skip ':' and blanks*/
21397 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21398 ;
21399
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021400 /* Check for "endfunction". */
21401 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021403 if (line_arg == NULL)
21404 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 break;
21406 }
21407
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021408 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 * at "end". */
21410 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21411 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021412 else if (STRNCMP(p, "if", 2) == 0
21413 || STRNCMP(p, "wh", 2) == 0
21414 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 || STRNCMP(p, "try", 3) == 0)
21416 indent += 2;
21417
21418 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021419 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021421 if (*p == '!')
21422 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 p += eval_fname_script(p);
21424 if (ASCII_ISALPHA(*p))
21425 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021426 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 if (*skipwhite(p) == '(')
21428 {
21429 ++nesting;
21430 indent += 2;
21431 }
21432 }
21433 }
21434
21435 /* Check for ":append" or ":insert". */
21436 p = skip_range(p, NULL);
21437 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21438 || (p[0] == 'i'
21439 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21440 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21441 skip_until = vim_strsave((char_u *)".");
21442
21443 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21444 arg = skipwhite(skiptowhite(p));
21445 if (arg[0] == '<' && arg[1] =='<'
21446 && ((p[0] == 'p' && p[1] == 'y'
21447 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21448 || (p[0] == 'p' && p[1] == 'e'
21449 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21450 || (p[0] == 't' && p[1] == 'c'
21451 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021452 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21453 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21455 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021456 || (p[0] == 'm' && p[1] == 'z'
21457 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 ))
21459 {
21460 /* ":python <<" continues until a dot, like ":append" */
21461 p = skipwhite(arg + 2);
21462 if (*p == NUL)
21463 skip_until = vim_strsave((char_u *)".");
21464 else
21465 skip_until = vim_strsave(p);
21466 }
21467 }
21468
21469 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021470 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021471 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021472 if (line_arg == NULL)
21473 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021475 }
21476
21477 /* Copy the line to newly allocated memory. get_one_sourceline()
21478 * allocates 250 bytes per line, this saves 80% on average. The cost
21479 * is an extra alloc/free. */
21480 p = vim_strsave(theline);
21481 if (p != NULL)
21482 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021483 if (line_arg == NULL)
21484 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021485 theline = p;
21486 }
21487
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021488 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21489
21490 /* Add NULL lines for continuation lines, so that the line count is
21491 * equal to the index in the growarray. */
21492 while (sourcing_lnum_off-- > 0)
21493 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021494
21495 /* Check for end of eap->arg. */
21496 if (line_arg != NULL && *line_arg == NUL)
21497 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 }
21499
21500 /* Don't define the function when skipping commands or when an error was
21501 * detected. */
21502 if (eap->skip || did_emsg)
21503 goto erret;
21504
21505 /*
21506 * If there are no errors, add the function
21507 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021508 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021509 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021510 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021511 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021512 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021513 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021514 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515 goto erret;
21516 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021517
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021518 fp = find_func(name);
21519 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021521 if (!eap->forceit)
21522 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021523 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021524 goto erret;
21525 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021526 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021527 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021528 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021529 name);
21530 goto erret;
21531 }
21532 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021533 ga_clear_strings(&(fp->uf_args));
21534 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021535 vim_free(name);
21536 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 }
21539 else
21540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021541 char numbuf[20];
21542
21543 fp = NULL;
21544 if (fudi.fd_newkey == NULL && !eap->forceit)
21545 {
21546 EMSG(_(e_funcdict));
21547 goto erret;
21548 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021549 if (fudi.fd_di == NULL)
21550 {
21551 /* Can't add a function to a locked dictionary */
21552 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21553 goto erret;
21554 }
21555 /* Can't change an existing function if it is locked */
21556 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21557 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021558
21559 /* Give the function a sequential number. Can only be used with a
21560 * Funcref! */
21561 vim_free(name);
21562 sprintf(numbuf, "%d", ++func_nr);
21563 name = vim_strsave((char_u *)numbuf);
21564 if (name == NULL)
21565 goto erret;
21566 }
21567
21568 if (fp == NULL)
21569 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021570 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021571 {
21572 int slen, plen;
21573 char_u *scriptname;
21574
21575 /* Check that the autoload name matches the script name. */
21576 j = FAIL;
21577 if (sourcing_name != NULL)
21578 {
21579 scriptname = autoload_name(name);
21580 if (scriptname != NULL)
21581 {
21582 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021583 plen = (int)STRLEN(p);
21584 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021585 if (slen > plen && fnamecmp(p,
21586 sourcing_name + slen - plen) == 0)
21587 j = OK;
21588 vim_free(scriptname);
21589 }
21590 }
21591 if (j == FAIL)
21592 {
21593 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21594 goto erret;
21595 }
21596 }
21597
21598 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 if (fp == NULL)
21600 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021601
21602 if (fudi.fd_dict != NULL)
21603 {
21604 if (fudi.fd_di == NULL)
21605 {
21606 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021607 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 if (fudi.fd_di == NULL)
21609 {
21610 vim_free(fp);
21611 goto erret;
21612 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021613 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21614 {
21615 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021616 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021617 goto erret;
21618 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 }
21620 else
21621 /* overwrite existing dict entry */
21622 clear_tv(&fudi.fd_di->di_tv);
21623 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021624 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021625 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021626 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021627
21628 /* behave like "dict" was used */
21629 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021630 }
21631
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021633 STRCPY(fp->uf_name, name);
21634 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021636 fp->uf_args = newargs;
21637 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021638#ifdef FEAT_PROFILE
21639 fp->uf_tml_count = NULL;
21640 fp->uf_tml_total = NULL;
21641 fp->uf_tml_self = NULL;
21642 fp->uf_profiling = FALSE;
21643 if (prof_def_func())
21644 func_do_profile(fp);
21645#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021646 fp->uf_varargs = varargs;
21647 fp->uf_flags = flags;
21648 fp->uf_calls = 0;
21649 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021650 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651
21652erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 ga_clear_strings(&newargs);
21654 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021655ret_free:
21656 vim_free(skip_until);
21657 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660}
21661
21662/*
21663 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021664 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021666 * flags:
21667 * TFN_INT: internal function name OK
21668 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 * Advances "pp" to just after the function name (if no error).
21670 */
21671 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 char_u **pp;
21674 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021675 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021676 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021678 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 char_u *start;
21680 char_u *end;
21681 int lead;
21682 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021684 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685
21686 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021687 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021689
21690 /* Check for hard coded <SNR>: already translated function ID (from a user
21691 * command). */
21692 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21693 && (*pp)[2] == (int)KE_SNR)
21694 {
21695 *pp += 3;
21696 len = get_id_len(pp) + 3;
21697 return vim_strnsave(start, len);
21698 }
21699
21700 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21701 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021703 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021705
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021706 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21707 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021708 if (end == start)
21709 {
21710 if (!skip)
21711 EMSG(_("E129: Function name required"));
21712 goto theend;
21713 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021714 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021715 {
21716 /*
21717 * Report an invalid expression in braces, unless the expression
21718 * evaluation has been cancelled due to an aborting error, an
21719 * interrupt, or an exception.
21720 */
21721 if (!aborting())
21722 {
21723 if (end != NULL)
21724 EMSG2(_(e_invarg2), start);
21725 }
21726 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021727 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021728 goto theend;
21729 }
21730
21731 if (lv.ll_tv != NULL)
21732 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021733 if (fdp != NULL)
21734 {
21735 fdp->fd_dict = lv.ll_dict;
21736 fdp->fd_newkey = lv.ll_newkey;
21737 lv.ll_newkey = NULL;
21738 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021740 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21741 {
21742 name = vim_strsave(lv.ll_tv->vval.v_string);
21743 *pp = end;
21744 }
21745 else
21746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021747 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21748 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021749 EMSG(_(e_funcref));
21750 else
21751 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021752 name = NULL;
21753 }
21754 goto theend;
21755 }
21756
21757 if (lv.ll_name == NULL)
21758 {
21759 /* Error found, but continue after the function name. */
21760 *pp = end;
21761 goto theend;
21762 }
21763
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021764 /* Check if the name is a Funcref. If so, use the value. */
21765 if (lv.ll_exp_name != NULL)
21766 {
21767 len = (int)STRLEN(lv.ll_exp_name);
21768 name = deref_func_name(lv.ll_exp_name, &len);
21769 if (name == lv.ll_exp_name)
21770 name = NULL;
21771 }
21772 else
21773 {
21774 len = (int)(end - *pp);
21775 name = deref_func_name(*pp, &len);
21776 if (name == *pp)
21777 name = NULL;
21778 }
21779 if (name != NULL)
21780 {
21781 name = vim_strsave(name);
21782 *pp = end;
21783 goto theend;
21784 }
21785
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021786 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021787 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021788 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021789 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21790 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21791 {
21792 /* When there was "s:" already or the name expanded to get a
21793 * leading "s:" then remove it. */
21794 lv.ll_name += 2;
21795 len -= 2;
21796 lead = 2;
21797 }
21798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021799 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021800 {
21801 if (lead == 2) /* skip over "s:" */
21802 lv.ll_name += 2;
21803 len = (int)(end - lv.ll_name);
21804 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021805
21806 /*
21807 * Copy the function name to allocated memory.
21808 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21809 * Accept <SNR>123_name() outside a script.
21810 */
21811 if (skip)
21812 lead = 0; /* do nothing */
21813 else if (lead > 0)
21814 {
21815 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021816 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21817 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021818 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021819 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021820 if (current_SID <= 0)
21821 {
21822 EMSG(_(e_usingsid));
21823 goto theend;
21824 }
21825 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21826 lead += (int)STRLEN(sid_buf);
21827 }
21828 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021829 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021830 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021831 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021832 goto theend;
21833 }
21834 name = alloc((unsigned)(len + lead + 1));
21835 if (name != NULL)
21836 {
21837 if (lead > 0)
21838 {
21839 name[0] = K_SPECIAL;
21840 name[1] = KS_EXTRA;
21841 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021842 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021843 STRCPY(name + 3, sid_buf);
21844 }
21845 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21846 name[len + lead] = NUL;
21847 }
21848 *pp = end;
21849
21850theend:
21851 clear_lval(&lv);
21852 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853}
21854
21855/*
21856 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21857 * Return 2 if "p" starts with "s:".
21858 * Return 0 otherwise.
21859 */
21860 static int
21861eval_fname_script(p)
21862 char_u *p;
21863{
21864 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21865 || STRNICMP(p + 1, "SNR>", 4) == 0))
21866 return 5;
21867 if (p[0] == 's' && p[1] == ':')
21868 return 2;
21869 return 0;
21870}
21871
21872/*
21873 * Return TRUE if "p" starts with "<SID>" or "s:".
21874 * Only works if eval_fname_script() returned non-zero for "p"!
21875 */
21876 static int
21877eval_fname_sid(p)
21878 char_u *p;
21879{
21880 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21881}
21882
21883/*
21884 * List the head of the function: "name(arg1, arg2)".
21885 */
21886 static void
21887list_func_head(fp, indent)
21888 ufunc_T *fp;
21889 int indent;
21890{
21891 int j;
21892
21893 msg_start();
21894 if (indent)
21895 MSG_PUTS(" ");
21896 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021897 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 {
21899 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021900 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 }
21902 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021903 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021905 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 {
21907 if (j)
21908 MSG_PUTS(", ");
21909 msg_puts(FUNCARG(fp, j));
21910 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021911 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 {
21913 if (j)
21914 MSG_PUTS(", ");
21915 MSG_PUTS("...");
21916 }
21917 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021918 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021919 if (p_verbose > 0)
21920 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921}
21922
21923/*
21924 * Find a function by name, return pointer to it in ufuncs.
21925 * Return NULL for unknown function.
21926 */
21927 static ufunc_T *
21928find_func(name)
21929 char_u *name;
21930{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021931 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021933 hi = hash_find(&func_hashtab, name);
21934 if (!HASHITEM_EMPTY(hi))
21935 return HI2UF(hi);
21936 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937}
21938
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021939#if defined(EXITFREE) || defined(PROTO)
21940 void
21941free_all_functions()
21942{
21943 hashitem_T *hi;
21944
21945 /* Need to start all over every time, because func_free() may change the
21946 * hash table. */
21947 while (func_hashtab.ht_used > 0)
21948 for (hi = func_hashtab.ht_array; ; ++hi)
21949 if (!HASHITEM_EMPTY(hi))
21950 {
21951 func_free(HI2UF(hi));
21952 break;
21953 }
21954}
21955#endif
21956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021957/*
21958 * Return TRUE if a function "name" exists.
21959 */
21960 static int
21961function_exists(name)
21962 char_u *name;
21963{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021964 char_u *nm = name;
21965 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021966 int n = FALSE;
21967
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021968 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021969 nm = skipwhite(nm);
21970
21971 /* Only accept "funcname", "funcname ", "funcname (..." and
21972 * "funcname(...", not "funcname!...". */
21973 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021974 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021975 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021976 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021977 else
21978 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021979 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021980 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021981 return n;
21982}
21983
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021984/*
21985 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021986 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021987 */
21988 static int
21989builtin_function(name)
21990 char_u *name;
21991{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021992 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21993 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021994}
21995
Bram Moolenaar05159a02005-02-26 23:04:13 +000021996#if defined(FEAT_PROFILE) || defined(PROTO)
21997/*
21998 * Start profiling function "fp".
21999 */
22000 static void
22001func_do_profile(fp)
22002 ufunc_T *fp;
22003{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022004 int len = fp->uf_lines.ga_len;
22005
22006 if (len == 0)
22007 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022008 fp->uf_tm_count = 0;
22009 profile_zero(&fp->uf_tm_self);
22010 profile_zero(&fp->uf_tm_total);
22011 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022012 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022013 if (fp->uf_tml_total == NULL)
22014 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022015 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022016 if (fp->uf_tml_self == NULL)
22017 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022018 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 fp->uf_tml_idx = -1;
22020 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22021 || fp->uf_tml_self == NULL)
22022 return; /* out of memory */
22023
22024 fp->uf_profiling = TRUE;
22025}
22026
22027/*
22028 * Dump the profiling results for all functions in file "fd".
22029 */
22030 void
22031func_dump_profile(fd)
22032 FILE *fd;
22033{
22034 hashitem_T *hi;
22035 int todo;
22036 ufunc_T *fp;
22037 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022038 ufunc_T **sorttab;
22039 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022040
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022041 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022042 if (todo == 0)
22043 return; /* nothing to dump */
22044
Bram Moolenaar73830342005-02-28 22:48:19 +000022045 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22046
Bram Moolenaar05159a02005-02-26 23:04:13 +000022047 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22048 {
22049 if (!HASHITEM_EMPTY(hi))
22050 {
22051 --todo;
22052 fp = HI2UF(hi);
22053 if (fp->uf_profiling)
22054 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022055 if (sorttab != NULL)
22056 sorttab[st_len++] = fp;
22057
Bram Moolenaar05159a02005-02-26 23:04:13 +000022058 if (fp->uf_name[0] == K_SPECIAL)
22059 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22060 else
22061 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22062 if (fp->uf_tm_count == 1)
22063 fprintf(fd, "Called 1 time\n");
22064 else
22065 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22066 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22067 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22068 fprintf(fd, "\n");
22069 fprintf(fd, "count total (s) self (s)\n");
22070
22071 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22072 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022073 if (FUNCLINE(fp, i) == NULL)
22074 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022075 prof_func_line(fd, fp->uf_tml_count[i],
22076 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022077 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22078 }
22079 fprintf(fd, "\n");
22080 }
22081 }
22082 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022083
22084 if (sorttab != NULL && st_len > 0)
22085 {
22086 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22087 prof_total_cmp);
22088 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22089 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22090 prof_self_cmp);
22091 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22092 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022093
22094 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022095}
Bram Moolenaar73830342005-02-28 22:48:19 +000022096
22097 static void
22098prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22099 FILE *fd;
22100 ufunc_T **sorttab;
22101 int st_len;
22102 char *title;
22103 int prefer_self; /* when equal print only self time */
22104{
22105 int i;
22106 ufunc_T *fp;
22107
22108 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22109 fprintf(fd, "count total (s) self (s) function\n");
22110 for (i = 0; i < 20 && i < st_len; ++i)
22111 {
22112 fp = sorttab[i];
22113 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22114 prefer_self);
22115 if (fp->uf_name[0] == K_SPECIAL)
22116 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22117 else
22118 fprintf(fd, " %s()\n", fp->uf_name);
22119 }
22120 fprintf(fd, "\n");
22121}
22122
22123/*
22124 * Print the count and times for one function or function line.
22125 */
22126 static void
22127prof_func_line(fd, count, total, self, prefer_self)
22128 FILE *fd;
22129 int count;
22130 proftime_T *total;
22131 proftime_T *self;
22132 int prefer_self; /* when equal print only self time */
22133{
22134 if (count > 0)
22135 {
22136 fprintf(fd, "%5d ", count);
22137 if (prefer_self && profile_equal(total, self))
22138 fprintf(fd, " ");
22139 else
22140 fprintf(fd, "%s ", profile_msg(total));
22141 if (!prefer_self && profile_equal(total, self))
22142 fprintf(fd, " ");
22143 else
22144 fprintf(fd, "%s ", profile_msg(self));
22145 }
22146 else
22147 fprintf(fd, " ");
22148}
22149
22150/*
22151 * Compare function for total time sorting.
22152 */
22153 static int
22154#ifdef __BORLANDC__
22155_RTLENTRYF
22156#endif
22157prof_total_cmp(s1, s2)
22158 const void *s1;
22159 const void *s2;
22160{
22161 ufunc_T *p1, *p2;
22162
22163 p1 = *(ufunc_T **)s1;
22164 p2 = *(ufunc_T **)s2;
22165 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22166}
22167
22168/*
22169 * Compare function for self time sorting.
22170 */
22171 static int
22172#ifdef __BORLANDC__
22173_RTLENTRYF
22174#endif
22175prof_self_cmp(s1, s2)
22176 const void *s1;
22177 const void *s2;
22178{
22179 ufunc_T *p1, *p2;
22180
22181 p1 = *(ufunc_T **)s1;
22182 p2 = *(ufunc_T **)s2;
22183 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22184}
22185
Bram Moolenaar05159a02005-02-26 23:04:13 +000022186#endif
22187
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022188/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022189 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022190 * Return TRUE if a package was loaded.
22191 */
22192 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022193script_autoload(name, reload)
22194 char_u *name;
22195 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022196{
22197 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022198 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022199 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022200 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022201
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022202 /* Return quickly when autoload disabled. */
22203 if (no_autoload)
22204 return FALSE;
22205
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022206 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022207 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022208 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022209 return FALSE;
22210
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022211 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022212
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022213 /* Find the name in the list of previously loaded package names. Skip
22214 * "autoload/", it's always the same. */
22215 for (i = 0; i < ga_loaded.ga_len; ++i)
22216 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22217 break;
22218 if (!reload && i < ga_loaded.ga_len)
22219 ret = FALSE; /* was loaded already */
22220 else
22221 {
22222 /* Remember the name if it wasn't loaded already. */
22223 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22224 {
22225 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22226 tofree = NULL;
22227 }
22228
22229 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022230 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022231 ret = TRUE;
22232 }
22233
22234 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022235 return ret;
22236}
22237
22238/*
22239 * Return the autoload script name for a function or variable name.
22240 * Returns NULL when out of memory.
22241 */
22242 static char_u *
22243autoload_name(name)
22244 char_u *name;
22245{
22246 char_u *p;
22247 char_u *scriptname;
22248
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022249 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022250 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22251 if (scriptname == NULL)
22252 return FALSE;
22253 STRCPY(scriptname, "autoload/");
22254 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022255 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022256 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022257 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022258 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022259 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022260}
22261
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22263
22264/*
22265 * Function given to ExpandGeneric() to obtain the list of user defined
22266 * function names.
22267 */
22268 char_u *
22269get_user_func_name(xp, idx)
22270 expand_T *xp;
22271 int idx;
22272{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022273 static long_u done;
22274 static hashitem_T *hi;
22275 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022279 done = 0;
22280 hi = func_hashtab.ht_array;
22281 }
22282 if (done < func_hashtab.ht_used)
22283 {
22284 if (done++ > 0)
22285 ++hi;
22286 while (HASHITEM_EMPTY(hi))
22287 ++hi;
22288 fp = HI2UF(hi);
22289
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022290 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022291 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022292
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22294 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295
22296 cat_func_name(IObuff, fp);
22297 if (xp->xp_context != EXPAND_USER_FUNC)
22298 {
22299 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022300 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 STRCAT(IObuff, ")");
22302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 return IObuff;
22304 }
22305 return NULL;
22306}
22307
22308#endif /* FEAT_CMDL_COMPL */
22309
22310/*
22311 * Copy the function name of "fp" to buffer "buf".
22312 * "buf" must be able to hold the function name plus three bytes.
22313 * Takes care of script-local function names.
22314 */
22315 static void
22316cat_func_name(buf, fp)
22317 char_u *buf;
22318 ufunc_T *fp;
22319{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022320 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 {
22322 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022323 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 }
22325 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022326 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327}
22328
22329/*
22330 * ":delfunction {name}"
22331 */
22332 void
22333ex_delfunction(eap)
22334 exarg_T *eap;
22335{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 char_u *p;
22338 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022339 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340
22341 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 name = trans_function_name(&p, eap->skip, 0, &fudi);
22343 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 {
22346 if (fudi.fd_dict != NULL && !eap->skip)
22347 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 if (!ends_excmd(*skipwhite(p)))
22351 {
22352 vim_free(name);
22353 EMSG(_(e_trailing));
22354 return;
22355 }
22356 eap->nextcmd = check_nextcmd(p);
22357 if (eap->nextcmd != NULL)
22358 *p = NUL;
22359
22360 if (!eap->skip)
22361 fp = find_func(name);
22362 vim_free(name);
22363
22364 if (!eap->skip)
22365 {
22366 if (fp == NULL)
22367 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022368 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 return;
22370 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022371 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 {
22373 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22374 return;
22375 }
22376
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022377 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379 /* Delete the dict item that refers to the function, it will
22380 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022381 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383 else
22384 func_free(fp);
22385 }
22386}
22387
22388/*
22389 * Free a function and remove it from the list of functions.
22390 */
22391 static void
22392func_free(fp)
22393 ufunc_T *fp;
22394{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022395 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022396
22397 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022398 ga_clear_strings(&(fp->uf_args));
22399 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022400#ifdef FEAT_PROFILE
22401 vim_free(fp->uf_tml_count);
22402 vim_free(fp->uf_tml_total);
22403 vim_free(fp->uf_tml_self);
22404#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022405
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022406 /* remove the function from the function hashtable */
22407 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22408 if (HASHITEM_EMPTY(hi))
22409 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022410 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022411 hash_remove(&func_hashtab, hi);
22412
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022413 vim_free(fp);
22414}
22415
22416/*
22417 * Unreference a Function: decrement the reference count and free it when it
22418 * becomes zero. Only for numbered functions.
22419 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022420 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022421func_unref(name)
22422 char_u *name;
22423{
22424 ufunc_T *fp;
22425
22426 if (name != NULL && isdigit(*name))
22427 {
22428 fp = find_func(name);
22429 if (fp == NULL)
22430 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022431 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022432 {
22433 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022434 * when "uf_calls" becomes zero. */
22435 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022436 func_free(fp);
22437 }
22438 }
22439}
22440
22441/*
22442 * Count a reference to a Function.
22443 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022444 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022445func_ref(name)
22446 char_u *name;
22447{
22448 ufunc_T *fp;
22449
22450 if (name != NULL && isdigit(*name))
22451 {
22452 fp = find_func(name);
22453 if (fp == NULL)
22454 EMSG2(_(e_intern2), "func_ref()");
22455 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022456 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 }
22458}
22459
22460/*
22461 * Call a user function.
22462 */
22463 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022464call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 ufunc_T *fp; /* pointer to function */
22466 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 typval_T *argvars; /* arguments */
22468 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 linenr_T firstline; /* first line of range */
22470 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472{
Bram Moolenaar33570922005-01-25 22:26:29 +000022473 char_u *save_sourcing_name;
22474 linenr_T save_sourcing_lnum;
22475 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022476 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022477 int save_did_emsg;
22478 static int depth = 0;
22479 dictitem_T *v;
22480 int fixvar_idx = 0; /* index in fixvar[] */
22481 int i;
22482 int ai;
22483 char_u numbuf[NUMBUFLEN];
22484 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022485#ifdef FEAT_PROFILE
22486 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022487 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489
22490 /* If depth of calling is getting too high, don't execute the function */
22491 if (depth >= p_mfd)
22492 {
22493 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022494 rettv->v_type = VAR_NUMBER;
22495 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 return;
22497 }
22498 ++depth;
22499
22500 line_breakcheck(); /* check for CTRL-C hit */
22501
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022502 fc = (funccall_T *)alloc(sizeof(funccall_T));
22503 fc->caller = current_funccal;
22504 current_funccal = fc;
22505 fc->func = fp;
22506 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022507 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022508 fc->linenr = 0;
22509 fc->returned = FALSE;
22510 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022512 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22513 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022516 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022517 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22518 * each argument variable and saves a lot of time.
22519 */
22520 /*
22521 * Init l: variables.
22522 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022523 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022524 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022525 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022526 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22527 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022528 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022529 name = v->di_key;
22530 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022531 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022532 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022534 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022535 v->di_tv.vval.v_dict = selfdict;
22536 ++selfdict->dv_refcount;
22537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022538
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 /*
22540 * Init a: variables.
22541 * Set a:0 to "argcount".
22542 * Set a:000 to a list with room for the "..." arguments.
22543 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022544 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022545 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022546 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022547 /* Use "name" to avoid a warning from some compiler that checks the
22548 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022549 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022550 name = v->di_key;
22551 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022553 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022555 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022556 v->di_tv.vval.v_list = &fc->l_varlist;
22557 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22558 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22559 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022560
22561 /*
22562 * Set a:firstline to "firstline" and a:lastline to "lastline".
22563 * Set a:name to named arguments.
22564 * Set a:N to the "..." arguments.
22565 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022566 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022568 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022569 (varnumber_T)lastline);
22570 for (i = 0; i < argcount; ++i)
22571 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022572 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 if (ai < 0)
22574 /* named argument a:name */
22575 name = FUNCARG(fp, i);
22576 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 /* "..." argument a:1, a:2, etc. */
22579 sprintf((char *)numbuf, "%d", ai + 1);
22580 name = numbuf;
22581 }
22582 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22583 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022584 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022585 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22586 }
22587 else
22588 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022589 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22590 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 if (v == NULL)
22592 break;
22593 v->di_flags = DI_FLAGS_RO;
22594 }
22595 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022596 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022597
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022598 /* Note: the values are copied directly to avoid alloc/free.
22599 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022601 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022602
22603 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22604 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022605 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22606 fc->l_listitems[ai].li_tv = argvars[i];
22607 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022608 }
22609 }
22610
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 /* Don't redraw while executing the function. */
22612 ++RedrawingDisabled;
22613 save_sourcing_name = sourcing_name;
22614 save_sourcing_lnum = sourcing_lnum;
22615 sourcing_lnum = 1;
22616 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022617 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 if (sourcing_name != NULL)
22619 {
22620 if (save_sourcing_name != NULL
22621 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22622 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22623 else
22624 STRCPY(sourcing_name, "function ");
22625 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22626
22627 if (p_verbose >= 12)
22628 {
22629 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022630 verbose_enter_scroll();
22631
Bram Moolenaar555b2802005-05-19 21:08:39 +000022632 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 if (p_verbose >= 14)
22634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022636 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022637 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022638 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639
22640 msg_puts((char_u *)"(");
22641 for (i = 0; i < argcount; ++i)
22642 {
22643 if (i > 0)
22644 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022645 if (argvars[i].v_type == VAR_NUMBER)
22646 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 else
22648 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022649 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22650 if (s != NULL)
22651 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022652 if (vim_strsize(s) > MSG_BUF_CLEN)
22653 {
22654 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22655 s = buf;
22656 }
22657 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022658 vim_free(tofree);
22659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 }
22661 }
22662 msg_puts((char_u *)")");
22663 }
22664 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022665
22666 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 --no_wait_return;
22668 }
22669 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022670#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022671 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022672 {
22673 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22674 func_do_profile(fp);
22675 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022676 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022677 {
22678 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022679 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022680 profile_zero(&fp->uf_tm_children);
22681 }
22682 script_prof_save(&wait_start);
22683 }
22684#endif
22685
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022687 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 save_did_emsg = did_emsg;
22689 did_emsg = FALSE;
22690
22691 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022692 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22694
22695 --RedrawingDisabled;
22696
22697 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022698 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022700 clear_tv(rettv);
22701 rettv->v_type = VAR_NUMBER;
22702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 }
22704
Bram Moolenaar05159a02005-02-26 23:04:13 +000022705#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022706 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022707 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022708 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022709 profile_end(&call_start);
22710 profile_sub_wait(&wait_start, &call_start);
22711 profile_add(&fp->uf_tm_total, &call_start);
22712 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022713 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022714 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022715 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22716 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022717 }
22718 }
22719#endif
22720
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 /* when being verbose, mention the return value */
22722 if (p_verbose >= 12)
22723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022725 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022728 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022729 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022730 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022731 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022734 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022735 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022736 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022737 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022738
Bram Moolenaar555b2802005-05-19 21:08:39 +000022739 /* The value may be very long. Skip the middle part, so that we
22740 * have some idea how it starts and ends. smsg() would always
22741 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022742 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022743 if (s != NULL)
22744 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022745 if (vim_strsize(s) > MSG_BUF_CLEN)
22746 {
22747 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22748 s = buf;
22749 }
22750 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022751 vim_free(tofree);
22752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753 }
22754 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022755
22756 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 --no_wait_return;
22758 }
22759
22760 vim_free(sourcing_name);
22761 sourcing_name = save_sourcing_name;
22762 sourcing_lnum = save_sourcing_lnum;
22763 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022764#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022765 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022766 script_prof_restore(&wait_start);
22767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768
22769 if (p_verbose >= 12 && sourcing_name != NULL)
22770 {
22771 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022772 verbose_enter_scroll();
22773
Bram Moolenaar555b2802005-05-19 21:08:39 +000022774 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022776
22777 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 --no_wait_return;
22779 }
22780
22781 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022782 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022784
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022785 /* If the a:000 list and the l: and a: dicts are not referenced we can
22786 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022787 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22788 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22789 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22790 {
22791 free_funccal(fc, FALSE);
22792 }
22793 else
22794 {
22795 hashitem_T *hi;
22796 listitem_T *li;
22797 int todo;
22798
22799 /* "fc" is still in use. This can happen when returning "a:000" or
22800 * assigning "l:" to a global variable.
22801 * Link "fc" in the list for garbage collection later. */
22802 fc->caller = previous_funccal;
22803 previous_funccal = fc;
22804
22805 /* Make a copy of the a: variables, since we didn't do that above. */
22806 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22807 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22808 {
22809 if (!HASHITEM_EMPTY(hi))
22810 {
22811 --todo;
22812 v = HI2DI(hi);
22813 copy_tv(&v->di_tv, &v->di_tv);
22814 }
22815 }
22816
22817 /* Make a copy of the a:000 items, since we didn't do that above. */
22818 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22819 copy_tv(&li->li_tv, &li->li_tv);
22820 }
22821}
22822
22823/*
22824 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022825 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022826 */
22827 static int
22828can_free_funccal(fc, copyID)
22829 funccall_T *fc;
22830 int copyID;
22831{
22832 return (fc->l_varlist.lv_copyID != copyID
22833 && fc->l_vars.dv_copyID != copyID
22834 && fc->l_avars.dv_copyID != copyID);
22835}
22836
22837/*
22838 * Free "fc" and what it contains.
22839 */
22840 static void
22841free_funccal(fc, free_val)
22842 funccall_T *fc;
22843 int free_val; /* a: vars were allocated */
22844{
22845 listitem_T *li;
22846
22847 /* The a: variables typevals may not have been allocated, only free the
22848 * allocated variables. */
22849 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22850
22851 /* free all l: variables */
22852 vars_clear(&fc->l_vars.dv_hashtab);
22853
22854 /* Free the a:000 variables if they were allocated. */
22855 if (free_val)
22856 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22857 clear_tv(&li->li_tv);
22858
22859 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860}
22861
22862/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022863 * Add a number variable "name" to dict "dp" with value "nr".
22864 */
22865 static void
22866add_nr_var(dp, v, name, nr)
22867 dict_T *dp;
22868 dictitem_T *v;
22869 char *name;
22870 varnumber_T nr;
22871{
22872 STRCPY(v->di_key, name);
22873 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22874 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22875 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022876 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 v->di_tv.vval.v_number = nr;
22878}
22879
22880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 * ":return [expr]"
22882 */
22883 void
22884ex_return(eap)
22885 exarg_T *eap;
22886{
22887 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 int returning = FALSE;
22890
22891 if (current_funccal == NULL)
22892 {
22893 EMSG(_("E133: :return not inside a function"));
22894 return;
22895 }
22896
22897 if (eap->skip)
22898 ++emsg_skip;
22899
22900 eap->nextcmd = NULL;
22901 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022902 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 {
22904 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022905 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022907 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 }
22909 /* It's safer to return also on error. */
22910 else if (!eap->skip)
22911 {
22912 /*
22913 * Return unless the expression evaluation has been cancelled due to an
22914 * aborting error, an interrupt, or an exception.
22915 */
22916 if (!aborting())
22917 returning = do_return(eap, FALSE, TRUE, NULL);
22918 }
22919
22920 /* When skipping or the return gets pending, advance to the next command
22921 * in this line (!returning). Otherwise, ignore the rest of the line.
22922 * Following lines will be ignored by get_func_line(). */
22923 if (returning)
22924 eap->nextcmd = NULL;
22925 else if (eap->nextcmd == NULL) /* no argument */
22926 eap->nextcmd = check_nextcmd(arg);
22927
22928 if (eap->skip)
22929 --emsg_skip;
22930}
22931
22932/*
22933 * Return from a function. Possibly makes the return pending. Also called
22934 * for a pending return at the ":endtry" or after returning from an extra
22935 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022936 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022937 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 * FALSE when the return gets pending.
22939 */
22940 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022941do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 exarg_T *eap;
22943 int reanimate;
22944 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022945 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946{
22947 int idx;
22948 struct condstack *cstack = eap->cstack;
22949
22950 if (reanimate)
22951 /* Undo the return. */
22952 current_funccal->returned = FALSE;
22953
22954 /*
22955 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22956 * not in its finally clause (which then is to be executed next) is found.
22957 * In this case, make the ":return" pending for execution at the ":endtry".
22958 * Otherwise, return normally.
22959 */
22960 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22961 if (idx >= 0)
22962 {
22963 cstack->cs_pending[idx] = CSTP_RETURN;
22964
22965 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022966 /* A pending return again gets pending. "rettv" points to an
22967 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022969 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 else
22971 {
22972 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022973 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022975 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022977 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 {
22979 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022980 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022981 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 else
22983 EMSG(_(e_outofmem));
22984 }
22985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022986 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987
22988 if (reanimate)
22989 {
22990 /* The pending return value could be overwritten by a ":return"
22991 * without argument in a finally clause; reset the default
22992 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022993 current_funccal->rettv->v_type = VAR_NUMBER;
22994 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 }
22996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022997 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 }
22999 else
23000 {
23001 current_funccal->returned = TRUE;
23002
23003 /* If the return is carried out now, store the return value. For
23004 * a return immediately after reanimation, the value is already
23005 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023006 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023008 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023011 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
23013 }
23014
23015 return idx < 0;
23016}
23017
23018/*
23019 * Free the variable with a pending return value.
23020 */
23021 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023022discard_pending_return(rettv)
23023 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024{
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026}
23027
23028/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023029 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 * is an allocated string. Used by report_pending() for verbose messages.
23031 */
23032 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023033get_return_cmd(rettv)
23034 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023036 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023037 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023038 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023040 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023041 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023042 if (s == NULL)
23043 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023044
23045 STRCPY(IObuff, ":return ");
23046 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23047 if (STRLEN(s) + 8 >= IOSIZE)
23048 STRCPY(IObuff + IOSIZE - 4, "...");
23049 vim_free(tofree);
23050 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051}
23052
23053/*
23054 * Get next function line.
23055 * Called by do_cmdline() to get the next line.
23056 * Returns allocated string, or NULL for end of function.
23057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 char_u *
23059get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023060 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023062 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063{
Bram Moolenaar33570922005-01-25 22:26:29 +000023064 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023065 ufunc_T *fp = fcp->func;
23066 char_u *retval;
23067 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068
23069 /* If breakpoints have been added/deleted need to check for it. */
23070 if (fcp->dbg_tick != debug_tick)
23071 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023072 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 sourcing_lnum);
23074 fcp->dbg_tick = debug_tick;
23075 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023076#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023077 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023078 func_line_end(cookie);
23079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080
Bram Moolenaar05159a02005-02-26 23:04:13 +000023081 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023082 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23083 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 retval = NULL;
23085 else
23086 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023087 /* Skip NULL lines (continuation lines). */
23088 while (fcp->linenr < gap->ga_len
23089 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23090 ++fcp->linenr;
23091 if (fcp->linenr >= gap->ga_len)
23092 retval = NULL;
23093 else
23094 {
23095 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23096 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023097#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023098 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023099 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023100#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 }
23103
23104 /* Did we encounter a breakpoint? */
23105 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23106 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023107 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023109 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 sourcing_lnum);
23111 fcp->dbg_tick = debug_tick;
23112 }
23113
23114 return retval;
23115}
23116
Bram Moolenaar05159a02005-02-26 23:04:13 +000023117#if defined(FEAT_PROFILE) || defined(PROTO)
23118/*
23119 * Called when starting to read a function line.
23120 * "sourcing_lnum" must be correct!
23121 * When skipping lines it may not actually be executed, but we won't find out
23122 * until later and we need to store the time now.
23123 */
23124 void
23125func_line_start(cookie)
23126 void *cookie;
23127{
23128 funccall_T *fcp = (funccall_T *)cookie;
23129 ufunc_T *fp = fcp->func;
23130
23131 if (fp->uf_profiling && sourcing_lnum >= 1
23132 && sourcing_lnum <= fp->uf_lines.ga_len)
23133 {
23134 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023135 /* Skip continuation lines. */
23136 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23137 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023138 fp->uf_tml_execed = FALSE;
23139 profile_start(&fp->uf_tml_start);
23140 profile_zero(&fp->uf_tml_children);
23141 profile_get_wait(&fp->uf_tml_wait);
23142 }
23143}
23144
23145/*
23146 * Called when actually executing a function line.
23147 */
23148 void
23149func_line_exec(cookie)
23150 void *cookie;
23151{
23152 funccall_T *fcp = (funccall_T *)cookie;
23153 ufunc_T *fp = fcp->func;
23154
23155 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23156 fp->uf_tml_execed = TRUE;
23157}
23158
23159/*
23160 * Called when done with a function line.
23161 */
23162 void
23163func_line_end(cookie)
23164 void *cookie;
23165{
23166 funccall_T *fcp = (funccall_T *)cookie;
23167 ufunc_T *fp = fcp->func;
23168
23169 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23170 {
23171 if (fp->uf_tml_execed)
23172 {
23173 ++fp->uf_tml_count[fp->uf_tml_idx];
23174 profile_end(&fp->uf_tml_start);
23175 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023176 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023177 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23178 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023179 }
23180 fp->uf_tml_idx = -1;
23181 }
23182}
23183#endif
23184
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185/*
23186 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023187 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 */
23189 int
23190func_has_ended(cookie)
23191 void *cookie;
23192{
Bram Moolenaar33570922005-01-25 22:26:29 +000023193 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194
23195 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23196 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023197 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 || fcp->returned);
23199}
23200
23201/*
23202 * return TRUE if cookie indicates a function which "abort"s on errors.
23203 */
23204 int
23205func_has_abort(cookie)
23206 void *cookie;
23207{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023208 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209}
23210
23211#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23212typedef enum
23213{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023214 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23215 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23216 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217} var_flavour_T;
23218
23219static var_flavour_T var_flavour __ARGS((char_u *varname));
23220
23221 static var_flavour_T
23222var_flavour(varname)
23223 char_u *varname;
23224{
23225 char_u *p = varname;
23226
23227 if (ASCII_ISUPPER(*p))
23228 {
23229 while (*(++p))
23230 if (ASCII_ISLOWER(*p))
23231 return VAR_FLAVOUR_SESSION;
23232 return VAR_FLAVOUR_VIMINFO;
23233 }
23234 else
23235 return VAR_FLAVOUR_DEFAULT;
23236}
23237#endif
23238
23239#if defined(FEAT_VIMINFO) || defined(PROTO)
23240/*
23241 * Restore global vars that start with a capital from the viminfo file
23242 */
23243 int
23244read_viminfo_varlist(virp, writing)
23245 vir_T *virp;
23246 int writing;
23247{
23248 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023249 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251
23252 if (!writing && (find_viminfo_parameter('!') != NULL))
23253 {
23254 tab = vim_strchr(virp->vir_line + 1, '\t');
23255 if (tab != NULL)
23256 {
23257 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023258 switch (*tab)
23259 {
23260 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023261#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023262 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023263#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023264 case 'D': type = VAR_DICT; break;
23265 case 'L': type = VAR_LIST; break;
23266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267
23268 tab = vim_strchr(tab, '\t');
23269 if (tab != NULL)
23270 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023271 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023272 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023273 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023275#ifdef FEAT_FLOAT
23276 else if (type == VAR_FLOAT)
23277 (void)string2float(tab + 1, &tv.vval.v_float);
23278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023280 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023281 if (type == VAR_DICT || type == VAR_LIST)
23282 {
23283 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23284
23285 if (etv == NULL)
23286 /* Failed to parse back the dict or list, use it as a
23287 * string. */
23288 tv.v_type = VAR_STRING;
23289 else
23290 {
23291 vim_free(tv.vval.v_string);
23292 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023293 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023294 }
23295 }
23296
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023297 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023298
23299 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023300 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023301 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23302 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 }
23304 }
23305 }
23306
23307 return viminfo_readline(virp);
23308}
23309
23310/*
23311 * Write global vars that start with a capital to the viminfo file
23312 */
23313 void
23314write_viminfo_varlist(fp)
23315 FILE *fp;
23316{
Bram Moolenaar33570922005-01-25 22:26:29 +000023317 hashitem_T *hi;
23318 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023319 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023320 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023321 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023322 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023323 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324
23325 if (find_viminfo_parameter('!') == NULL)
23326 return;
23327
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023328 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023329
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023330 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023331 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023333 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023335 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023336 this_var = HI2DI(hi);
23337 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023338 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023339 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023340 {
23341 case VAR_STRING: s = "STR"; break;
23342 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023343#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023344 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023345#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023346 case VAR_DICT: s = "DIC"; break;
23347 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023348 default: continue;
23349 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023350 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023351 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023352 if (p != NULL)
23353 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023354 vim_free(tofree);
23355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 }
23357 }
23358}
23359#endif
23360
23361#if defined(FEAT_SESSION) || defined(PROTO)
23362 int
23363store_session_globals(fd)
23364 FILE *fd;
23365{
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 hashitem_T *hi;
23367 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023368 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 char_u *p, *t;
23370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023371 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023374 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023376 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023377 this_var = HI2DI(hi);
23378 if ((this_var->di_tv.v_type == VAR_NUMBER
23379 || this_var->di_tv.v_type == VAR_STRING)
23380 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023381 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023382 /* Escape special characters with a backslash. Turn a LF and
23383 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023384 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023385 (char_u *)"\\\"\n\r");
23386 if (p == NULL) /* out of memory */
23387 break;
23388 for (t = p; *t != NUL; ++t)
23389 if (*t == '\n')
23390 *t = 'n';
23391 else if (*t == '\r')
23392 *t = 'r';
23393 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 this_var->di_key,
23395 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23396 : ' ',
23397 p,
23398 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23399 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023400 || put_eol(fd) == FAIL)
23401 {
23402 vim_free(p);
23403 return FAIL;
23404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 vim_free(p);
23406 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023407#ifdef FEAT_FLOAT
23408 else if (this_var->di_tv.v_type == VAR_FLOAT
23409 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23410 {
23411 float_T f = this_var->di_tv.vval.v_float;
23412 int sign = ' ';
23413
23414 if (f < 0)
23415 {
23416 f = -f;
23417 sign = '-';
23418 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023419 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023420 this_var->di_key, sign, f) < 0)
23421 || put_eol(fd) == FAIL)
23422 return FAIL;
23423 }
23424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 }
23426 }
23427 return OK;
23428}
23429#endif
23430
Bram Moolenaar661b1822005-07-28 22:36:45 +000023431/*
23432 * Display script name where an item was last set.
23433 * Should only be invoked when 'verbose' is non-zero.
23434 */
23435 void
23436last_set_msg(scriptID)
23437 scid_T scriptID;
23438{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023439 char_u *p;
23440
Bram Moolenaar661b1822005-07-28 22:36:45 +000023441 if (scriptID != 0)
23442 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023443 p = home_replace_save(NULL, get_scriptname(scriptID));
23444 if (p != NULL)
23445 {
23446 verbose_enter();
23447 MSG_PUTS(_("\n\tLast set from "));
23448 MSG_PUTS(p);
23449 vim_free(p);
23450 verbose_leave();
23451 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023452 }
23453}
23454
Bram Moolenaard812df62008-11-09 12:46:09 +000023455/*
23456 * List v:oldfiles in a nice way.
23457 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023458 void
23459ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023460 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023461{
23462 list_T *l = vimvars[VV_OLDFILES].vv_list;
23463 listitem_T *li;
23464 int nr = 0;
23465
23466 if (l == NULL)
23467 msg((char_u *)_("No old files"));
23468 else
23469 {
23470 msg_start();
23471 msg_scroll = TRUE;
23472 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23473 {
23474 msg_outnum((long)++nr);
23475 MSG_PUTS(": ");
23476 msg_outtrans(get_tv_string(&li->li_tv));
23477 msg_putchar('\n');
23478 out_flush(); /* output one line at a time */
23479 ui_breakcheck();
23480 }
23481 /* Assume "got_int" was set to truncate the listing. */
23482 got_int = FALSE;
23483
23484#ifdef FEAT_BROWSE_CMD
23485 if (cmdmod.browse)
23486 {
23487 quit_more = FALSE;
23488 nr = prompt_for_number(FALSE);
23489 msg_starthere();
23490 if (nr > 0)
23491 {
23492 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23493 (long)nr);
23494
23495 if (p != NULL)
23496 {
23497 p = expand_env_save(p);
23498 eap->arg = p;
23499 eap->cmdidx = CMD_edit;
23500 cmdmod.browse = FALSE;
23501 do_exedit(eap, NULL);
23502 vim_free(p);
23503 }
23504 }
23505 }
23506#endif
23507 }
23508}
23509
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510#endif /* FEAT_EVAL */
23511
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023513#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514
23515#ifdef WIN3264
23516/*
23517 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23518 */
23519static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23520static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23521static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23522
23523/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023524 * Get the short path (8.3) for the filename in "fnamep".
23525 * Only works for a valid file name.
23526 * When the path gets longer "fnamep" is changed and the allocated buffer
23527 * is put in "bufp".
23528 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23529 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 */
23531 static int
23532get_short_pathname(fnamep, bufp, fnamelen)
23533 char_u **fnamep;
23534 char_u **bufp;
23535 int *fnamelen;
23536{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023537 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 char_u *newbuf;
23539
23540 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 l = GetShortPathName(*fnamep, *fnamep, len);
23542 if (l > len - 1)
23543 {
23544 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023545 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 newbuf = vim_strnsave(*fnamep, l);
23547 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023548 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549
23550 vim_free(*bufp);
23551 *fnamep = *bufp = newbuf;
23552
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023553 /* Really should always succeed, as the buffer is big enough. */
23554 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 }
23556
23557 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023558 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559}
23560
23561/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023562 * Get the short path (8.3) for the filename in "fname". The converted
23563 * path is returned in "bufp".
23564 *
23565 * Some of the directories specified in "fname" may not exist. This function
23566 * will shorten the existing directories at the beginning of the path and then
23567 * append the remaining non-existing path.
23568 *
23569 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023570 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023571 * bufp - Pointer to an allocated buffer for the filename.
23572 * fnamelen - Length of the filename pointed to by fname
23573 *
23574 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 */
23576 static int
23577shortpath_for_invalid_fname(fname, bufp, fnamelen)
23578 char_u **fname;
23579 char_u **bufp;
23580 int *fnamelen;
23581{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023582 char_u *short_fname, *save_fname, *pbuf_unused;
23583 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023585 int old_len, len;
23586 int new_len, sfx_len;
23587 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588
23589 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023590 old_len = *fnamelen;
23591 save_fname = vim_strnsave(*fname, old_len);
23592 pbuf_unused = NULL;
23593 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023595 endp = save_fname + old_len - 1; /* Find the end of the copy */
23596 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023598 /*
23599 * Try shortening the supplied path till it succeeds by removing one
23600 * directory at a time from the tail of the path.
23601 */
23602 len = 0;
23603 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023605 /* go back one path-separator */
23606 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23607 --endp;
23608 if (endp <= save_fname)
23609 break; /* processed the complete path */
23610
23611 /*
23612 * Replace the path separator with a NUL and try to shorten the
23613 * resulting path.
23614 */
23615 ch = *endp;
23616 *endp = 0;
23617 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023618 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023619 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23620 {
23621 retval = FAIL;
23622 goto theend;
23623 }
23624 *endp = ch; /* preserve the string */
23625
23626 if (len > 0)
23627 break; /* successfully shortened the path */
23628
23629 /* failed to shorten the path. Skip the path separator */
23630 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 }
23632
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023633 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023635 /*
23636 * Succeeded in shortening the path. Now concatenate the shortened
23637 * path with the remaining path at the tail.
23638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023640 /* Compute the length of the new path. */
23641 sfx_len = (int)(save_endp - endp) + 1;
23642 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023644 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023646 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023648 /* There is not enough space in the currently allocated string,
23649 * copy it to a buffer big enough. */
23650 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023652 {
23653 retval = FAIL;
23654 goto theend;
23655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 }
23657 else
23658 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023659 /* Transfer short_fname to the main buffer (it's big enough),
23660 * unless get_short_pathname() did its work in-place. */
23661 *fname = *bufp = save_fname;
23662 if (short_fname != save_fname)
23663 vim_strncpy(save_fname, short_fname, len);
23664 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023666
23667 /* concat the not-shortened part of the path */
23668 vim_strncpy(*fname + len, endp, sfx_len);
23669 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023671
23672theend:
23673 vim_free(pbuf_unused);
23674 vim_free(save_fname);
23675
23676 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677}
23678
23679/*
23680 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023681 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 */
23683 static int
23684shortpath_for_partial(fnamep, bufp, fnamelen)
23685 char_u **fnamep;
23686 char_u **bufp;
23687 int *fnamelen;
23688{
23689 int sepcount, len, tflen;
23690 char_u *p;
23691 char_u *pbuf, *tfname;
23692 int hasTilde;
23693
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023694 /* Count up the path separators from the RHS.. so we know which part
23695 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023697 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 if (vim_ispathsep(*p))
23699 ++sepcount;
23700
23701 /* Need full path first (use expand_env() to remove a "~/") */
23702 hasTilde = (**fnamep == '~');
23703 if (hasTilde)
23704 pbuf = tfname = expand_env_save(*fnamep);
23705 else
23706 pbuf = tfname = FullName_save(*fnamep, FALSE);
23707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023708 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023710 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23711 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712
23713 if (len == 0)
23714 {
23715 /* Don't have a valid filename, so shorten the rest of the
23716 * path if we can. This CAN give us invalid 8.3 filenames, but
23717 * there's not a lot of point in guessing what it might be.
23718 */
23719 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023720 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23721 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 }
23723
23724 /* Count the paths backward to find the beginning of the desired string. */
23725 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023726 {
23727#ifdef FEAT_MBYTE
23728 if (has_mbyte)
23729 p -= mb_head_off(tfname, p);
23730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 if (vim_ispathsep(*p))
23732 {
23733 if (sepcount == 0 || (hasTilde && sepcount == 1))
23734 break;
23735 else
23736 sepcount --;
23737 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 if (hasTilde)
23740 {
23741 --p;
23742 if (p >= tfname)
23743 *p = '~';
23744 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 }
23747 else
23748 ++p;
23749
23750 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23751 vim_free(*bufp);
23752 *fnamelen = (int)STRLEN(p);
23753 *bufp = pbuf;
23754 *fnamep = p;
23755
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023756 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757}
23758#endif /* WIN3264 */
23759
23760/*
23761 * Adjust a filename, according to a string of modifiers.
23762 * *fnamep must be NUL terminated when called. When returning, the length is
23763 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023764 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 * When there is an error, *fnamep is set to NULL.
23766 */
23767 int
23768modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23769 char_u *src; /* string with modifiers */
23770 int *usedlen; /* characters after src that are used */
23771 char_u **fnamep; /* file name so far */
23772 char_u **bufp; /* buffer for allocated file name or NULL */
23773 int *fnamelen; /* length of fnamep */
23774{
23775 int valid = 0;
23776 char_u *tail;
23777 char_u *s, *p, *pbuf;
23778 char_u dirname[MAXPATHL];
23779 int c;
23780 int has_fullname = 0;
23781#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023782 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 int has_shortname = 0;
23784#endif
23785
23786repeat:
23787 /* ":p" - full path/file_name */
23788 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23789 {
23790 has_fullname = 1;
23791
23792 valid |= VALID_PATH;
23793 *usedlen += 2;
23794
23795 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23796 if ((*fnamep)[0] == '~'
23797#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23798 && ((*fnamep)[1] == '/'
23799# ifdef BACKSLASH_IN_FILENAME
23800 || (*fnamep)[1] == '\\'
23801# endif
23802 || (*fnamep)[1] == NUL)
23803
23804#endif
23805 )
23806 {
23807 *fnamep = expand_env_save(*fnamep);
23808 vim_free(*bufp); /* free any allocated file name */
23809 *bufp = *fnamep;
23810 if (*fnamep == NULL)
23811 return -1;
23812 }
23813
23814 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023815 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
23817 if (vim_ispathsep(*p)
23818 && p[1] == '.'
23819 && (p[2] == NUL
23820 || vim_ispathsep(p[2])
23821 || (p[2] == '.'
23822 && (p[3] == NUL || vim_ispathsep(p[3])))))
23823 break;
23824 }
23825
23826 /* FullName_save() is slow, don't use it when not needed. */
23827 if (*p != NUL || !vim_isAbsName(*fnamep))
23828 {
23829 *fnamep = FullName_save(*fnamep, *p != NUL);
23830 vim_free(*bufp); /* free any allocated file name */
23831 *bufp = *fnamep;
23832 if (*fnamep == NULL)
23833 return -1;
23834 }
23835
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023836#ifdef WIN3264
23837# if _WIN32_WINNT >= 0x0500
23838 if (vim_strchr(*fnamep, '~') != NULL)
23839 {
23840 /* Expand 8.3 filename to full path. Needed to make sure the same
23841 * file does not have two different names.
23842 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23843 p = alloc(_MAX_PATH + 1);
23844 if (p != NULL)
23845 {
23846 if (GetLongPathName(*fnamep, p, MAXPATHL))
23847 {
23848 vim_free(*bufp);
23849 *bufp = *fnamep = p;
23850 }
23851 else
23852 vim_free(p);
23853 }
23854 }
23855# endif
23856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 /* Append a path separator to a directory. */
23858 if (mch_isdir(*fnamep))
23859 {
23860 /* Make room for one or two extra characters. */
23861 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23862 vim_free(*bufp); /* free any allocated file name */
23863 *bufp = *fnamep;
23864 if (*fnamep == NULL)
23865 return -1;
23866 add_pathsep(*fnamep);
23867 }
23868 }
23869
23870 /* ":." - path relative to the current directory */
23871 /* ":~" - path relative to the home directory */
23872 /* ":8" - shortname path - postponed till after */
23873 while (src[*usedlen] == ':'
23874 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23875 {
23876 *usedlen += 2;
23877 if (c == '8')
23878 {
23879#ifdef WIN3264
23880 has_shortname = 1; /* Postpone this. */
23881#endif
23882 continue;
23883 }
23884 pbuf = NULL;
23885 /* Need full path first (use expand_env() to remove a "~/") */
23886 if (!has_fullname)
23887 {
23888 if (c == '.' && **fnamep == '~')
23889 p = pbuf = expand_env_save(*fnamep);
23890 else
23891 p = pbuf = FullName_save(*fnamep, FALSE);
23892 }
23893 else
23894 p = *fnamep;
23895
23896 has_fullname = 0;
23897
23898 if (p != NULL)
23899 {
23900 if (c == '.')
23901 {
23902 mch_dirname(dirname, MAXPATHL);
23903 s = shorten_fname(p, dirname);
23904 if (s != NULL)
23905 {
23906 *fnamep = s;
23907 if (pbuf != NULL)
23908 {
23909 vim_free(*bufp); /* free any allocated file name */
23910 *bufp = pbuf;
23911 pbuf = NULL;
23912 }
23913 }
23914 }
23915 else
23916 {
23917 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23918 /* Only replace it when it starts with '~' */
23919 if (*dirname == '~')
23920 {
23921 s = vim_strsave(dirname);
23922 if (s != NULL)
23923 {
23924 *fnamep = s;
23925 vim_free(*bufp);
23926 *bufp = s;
23927 }
23928 }
23929 }
23930 vim_free(pbuf);
23931 }
23932 }
23933
23934 tail = gettail(*fnamep);
23935 *fnamelen = (int)STRLEN(*fnamep);
23936
23937 /* ":h" - head, remove "/file_name", can be repeated */
23938 /* Don't remove the first "/" or "c:\" */
23939 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23940 {
23941 valid |= VALID_HEAD;
23942 *usedlen += 2;
23943 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023944 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023945 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 *fnamelen = (int)(tail - *fnamep);
23947#ifdef VMS
23948 if (*fnamelen > 0)
23949 *fnamelen += 1; /* the path separator is part of the path */
23950#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023951 if (*fnamelen == 0)
23952 {
23953 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23954 p = vim_strsave((char_u *)".");
23955 if (p == NULL)
23956 return -1;
23957 vim_free(*bufp);
23958 *bufp = *fnamep = tail = p;
23959 *fnamelen = 1;
23960 }
23961 else
23962 {
23963 while (tail > s && !after_pathsep(s, tail))
23964 mb_ptr_back(*fnamep, tail);
23965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 }
23967
23968 /* ":8" - shortname */
23969 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23970 {
23971 *usedlen += 2;
23972#ifdef WIN3264
23973 has_shortname = 1;
23974#endif
23975 }
23976
23977#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023978 /*
23979 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 */
23981 if (has_shortname)
23982 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023983 /* Copy the string if it is shortened by :h and when it wasn't copied
23984 * yet, because we are going to change it in place. Avoids changing
23985 * the buffer name for "%:8". */
23986 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 {
23988 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023989 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 return -1;
23991 vim_free(*bufp);
23992 *bufp = *fnamep = p;
23993 }
23994
23995 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023996 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 if (!has_fullname && !vim_isAbsName(*fnamep))
23998 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023999 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 return -1;
24001 }
24002 else
24003 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024004 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005
Bram Moolenaardc935552011-08-17 15:23:23 +020024006 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024008 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 return -1;
24010
24011 if (l == 0)
24012 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024013 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024015 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 return -1;
24017 }
24018 *fnamelen = l;
24019 }
24020 }
24021#endif /* WIN3264 */
24022
24023 /* ":t" - tail, just the basename */
24024 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24025 {
24026 *usedlen += 2;
24027 *fnamelen -= (int)(tail - *fnamep);
24028 *fnamep = tail;
24029 }
24030
24031 /* ":e" - extension, can be repeated */
24032 /* ":r" - root, without extension, can be repeated */
24033 while (src[*usedlen] == ':'
24034 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24035 {
24036 /* find a '.' in the tail:
24037 * - for second :e: before the current fname
24038 * - otherwise: The last '.'
24039 */
24040 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24041 s = *fnamep - 2;
24042 else
24043 s = *fnamep + *fnamelen - 1;
24044 for ( ; s > tail; --s)
24045 if (s[0] == '.')
24046 break;
24047 if (src[*usedlen + 1] == 'e') /* :e */
24048 {
24049 if (s > tail)
24050 {
24051 *fnamelen += (int)(*fnamep - (s + 1));
24052 *fnamep = s + 1;
24053#ifdef VMS
24054 /* cut version from the extension */
24055 s = *fnamep + *fnamelen - 1;
24056 for ( ; s > *fnamep; --s)
24057 if (s[0] == ';')
24058 break;
24059 if (s > *fnamep)
24060 *fnamelen = s - *fnamep;
24061#endif
24062 }
24063 else if (*fnamep <= tail)
24064 *fnamelen = 0;
24065 }
24066 else /* :r */
24067 {
24068 if (s > tail) /* remove one extension */
24069 *fnamelen = (int)(s - *fnamep);
24070 }
24071 *usedlen += 2;
24072 }
24073
24074 /* ":s?pat?foo?" - substitute */
24075 /* ":gs?pat?foo?" - global substitute */
24076 if (src[*usedlen] == ':'
24077 && (src[*usedlen + 1] == 's'
24078 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24079 {
24080 char_u *str;
24081 char_u *pat;
24082 char_u *sub;
24083 int sep;
24084 char_u *flags;
24085 int didit = FALSE;
24086
24087 flags = (char_u *)"";
24088 s = src + *usedlen + 2;
24089 if (src[*usedlen + 1] == 'g')
24090 {
24091 flags = (char_u *)"g";
24092 ++s;
24093 }
24094
24095 sep = *s++;
24096 if (sep)
24097 {
24098 /* find end of pattern */
24099 p = vim_strchr(s, sep);
24100 if (p != NULL)
24101 {
24102 pat = vim_strnsave(s, (int)(p - s));
24103 if (pat != NULL)
24104 {
24105 s = p + 1;
24106 /* find end of substitution */
24107 p = vim_strchr(s, sep);
24108 if (p != NULL)
24109 {
24110 sub = vim_strnsave(s, (int)(p - s));
24111 str = vim_strnsave(*fnamep, *fnamelen);
24112 if (sub != NULL && str != NULL)
24113 {
24114 *usedlen = (int)(p + 1 - src);
24115 s = do_string_sub(str, pat, sub, flags);
24116 if (s != NULL)
24117 {
24118 *fnamep = s;
24119 *fnamelen = (int)STRLEN(s);
24120 vim_free(*bufp);
24121 *bufp = s;
24122 didit = TRUE;
24123 }
24124 }
24125 vim_free(sub);
24126 vim_free(str);
24127 }
24128 vim_free(pat);
24129 }
24130 }
24131 /* after using ":s", repeat all the modifiers */
24132 if (didit)
24133 goto repeat;
24134 }
24135 }
24136
24137 return valid;
24138}
24139
24140/*
24141 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24142 * "flags" can be "g" to do a global substitute.
24143 * Returns an allocated string, NULL for error.
24144 */
24145 char_u *
24146do_string_sub(str, pat, sub, flags)
24147 char_u *str;
24148 char_u *pat;
24149 char_u *sub;
24150 char_u *flags;
24151{
24152 int sublen;
24153 regmatch_T regmatch;
24154 int i;
24155 int do_all;
24156 char_u *tail;
24157 garray_T ga;
24158 char_u *ret;
24159 char_u *save_cpo;
24160
24161 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24162 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024163 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164
24165 ga_init2(&ga, 1, 200);
24166
24167 do_all = (flags[0] == 'g');
24168
24169 regmatch.rm_ic = p_ic;
24170 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24171 if (regmatch.regprog != NULL)
24172 {
24173 tail = str;
24174 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24175 {
24176 /*
24177 * Get some space for a temporary buffer to do the substitution
24178 * into. It will contain:
24179 * - The text up to where the match is.
24180 * - The substituted text.
24181 * - The text after the match.
24182 */
24183 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24184 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24185 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24186 {
24187 ga_clear(&ga);
24188 break;
24189 }
24190
24191 /* copy the text up to where the match is */
24192 i = (int)(regmatch.startp[0] - tail);
24193 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24194 /* add the substituted text */
24195 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24196 + ga.ga_len + i, TRUE, TRUE, FALSE);
24197 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 /* avoid getting stuck on a match with an empty string */
24199 if (tail == regmatch.endp[0])
24200 {
24201 if (*tail == NUL)
24202 break;
24203 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24204 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 }
24206 else
24207 {
24208 tail = regmatch.endp[0];
24209 if (*tail == NUL)
24210 break;
24211 }
24212 if (!do_all)
24213 break;
24214 }
24215
24216 if (ga.ga_data != NULL)
24217 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24218
24219 vim_free(regmatch.regprog);
24220 }
24221
24222 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24223 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024224 if (p_cpo == empty_option)
24225 p_cpo = save_cpo;
24226 else
24227 /* Darn, evaluating {sub} expression changed the value. */
24228 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229
24230 return ret;
24231}
24232
24233#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */