blob: 756baa39b89736e1c836eadaa8be8b36f04cd8d3 [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 Moolenaar105bc352013-05-17 16:03:57 +020011897 tabpage_T *tp, *oldtabpage;
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 Moolenaar105bc352013-05-17 16:03:57 +020011915 /* Set curwin to be our win, temporarily. Also set the tabpage,
11916 * otherwise the window is not valid. */
11917 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011918
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011920 {
11921 if (get_option_tv(&varname, rettv, 1) == OK)
11922 done = TRUE;
11923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 else
11925 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011926 /* Look up the variable. */
11927 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11928 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011930 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011932 done = TRUE;
11933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011935
11936 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011937 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 }
11939
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011940 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11941 /* use the default return value */
11942 copy_tv(&argvars[off + 2], rettv);
11943
Bram Moolenaar071d4272004-06-13 20:20:40 +000011944 --emsg_off;
11945}
11946
11947/*
11948 * "glob()" function
11949 */
11950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *argvars;
11953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011955 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011957 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011959 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011960 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011962 if (argvars[1].v_type != VAR_UNKNOWN)
11963 {
11964 if (get_tv_number_chk(&argvars[1], &error))
11965 options |= WILD_KEEP_ALL;
11966 if (argvars[2].v_type != VAR_UNKNOWN
11967 && get_tv_number_chk(&argvars[2], &error))
11968 {
11969 rettv->v_type = VAR_LIST;
11970 rettv->vval.v_list = NULL;
11971 }
11972 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011973 if (!error)
11974 {
11975 ExpandInit(&xpc);
11976 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011977 if (p_wic)
11978 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011979 if (rettv->v_type == VAR_STRING)
11980 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011981 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011982 else if (rettv_list_alloc(rettv) != FAIL)
11983 {
11984 int i;
11985
11986 ExpandOne(&xpc, get_tv_string(&argvars[0]),
11987 NULL, options, WILD_ALL_KEEP);
11988 for (i = 0; i < xpc.xp_numfiles; i++)
11989 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11990
11991 ExpandCleanup(&xpc);
11992 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011993 }
11994 else
11995 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996}
11997
11998/*
11999 * "globpath()" function
12000 */
12001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012003 typval_T *argvars;
12004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012011 /* When the optional second argument is non-zero, don't remove matches
12012 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12013 if (argvars[2].v_type != VAR_UNKNOWN
12014 && get_tv_number_chk(&argvars[2], &error))
12015 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012016 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012017 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 rettv->vval.v_string = NULL;
12019 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012020 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12021 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022}
12023
12024/*
12025 * "has()" function
12026 */
12027 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012029 typval_T *argvars;
12030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031{
12032 int i;
12033 char_u *name;
12034 int n = FALSE;
12035 static char *(has_list[]) =
12036 {
12037#ifdef AMIGA
12038 "amiga",
12039# ifdef FEAT_ARP
12040 "arp",
12041# endif
12042#endif
12043#ifdef __BEOS__
12044 "beos",
12045#endif
12046#ifdef MSDOS
12047# ifdef DJGPP
12048 "dos32",
12049# else
12050 "dos16",
12051# endif
12052#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012053#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054 "mac",
12055#endif
12056#if defined(MACOS_X_UNIX)
12057 "macunix",
12058#endif
12059#ifdef OS2
12060 "os2",
12061#endif
12062#ifdef __QNX__
12063 "qnx",
12064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065#ifdef UNIX
12066 "unix",
12067#endif
12068#ifdef VMS
12069 "vms",
12070#endif
12071#ifdef WIN16
12072 "win16",
12073#endif
12074#ifdef WIN32
12075 "win32",
12076#endif
12077#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12078 "win32unix",
12079#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012080#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 "win64",
12082#endif
12083#ifdef EBCDIC
12084 "ebcdic",
12085#endif
12086#ifndef CASE_INSENSITIVE_FILENAME
12087 "fname_case",
12088#endif
12089#ifdef FEAT_ARABIC
12090 "arabic",
12091#endif
12092#ifdef FEAT_AUTOCMD
12093 "autocmd",
12094#endif
12095#ifdef FEAT_BEVAL
12096 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012097# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12098 "balloon_multiline",
12099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100#endif
12101#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12102 "builtin_terms",
12103# ifdef ALL_BUILTIN_TCAPS
12104 "all_builtin_terms",
12105# endif
12106#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012107#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12108 || defined(FEAT_GUI_W32) \
12109 || defined(FEAT_GUI_MOTIF))
12110 "browsefilter",
12111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112#ifdef FEAT_BYTEOFF
12113 "byte_offset",
12114#endif
12115#ifdef FEAT_CINDENT
12116 "cindent",
12117#endif
12118#ifdef FEAT_CLIENTSERVER
12119 "clientserver",
12120#endif
12121#ifdef FEAT_CLIPBOARD
12122 "clipboard",
12123#endif
12124#ifdef FEAT_CMDL_COMPL
12125 "cmdline_compl",
12126#endif
12127#ifdef FEAT_CMDHIST
12128 "cmdline_hist",
12129#endif
12130#ifdef FEAT_COMMENTS
12131 "comments",
12132#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012133#ifdef FEAT_CONCEAL
12134 "conceal",
12135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136#ifdef FEAT_CRYPT
12137 "cryptv",
12138#endif
12139#ifdef FEAT_CSCOPE
12140 "cscope",
12141#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012142#ifdef FEAT_CURSORBIND
12143 "cursorbind",
12144#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012145#ifdef CURSOR_SHAPE
12146 "cursorshape",
12147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148#ifdef DEBUG
12149 "debug",
12150#endif
12151#ifdef FEAT_CON_DIALOG
12152 "dialog_con",
12153#endif
12154#ifdef FEAT_GUI_DIALOG
12155 "dialog_gui",
12156#endif
12157#ifdef FEAT_DIFF
12158 "diff",
12159#endif
12160#ifdef FEAT_DIGRAPHS
12161 "digraphs",
12162#endif
12163#ifdef FEAT_DND
12164 "dnd",
12165#endif
12166#ifdef FEAT_EMACS_TAGS
12167 "emacs_tags",
12168#endif
12169 "eval", /* always present, of course! */
12170#ifdef FEAT_EX_EXTRA
12171 "ex_extra",
12172#endif
12173#ifdef FEAT_SEARCH_EXTRA
12174 "extra_search",
12175#endif
12176#ifdef FEAT_FKMAP
12177 "farsi",
12178#endif
12179#ifdef FEAT_SEARCHPATH
12180 "file_in_path",
12181#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012182#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012183 "filterpipe",
12184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185#ifdef FEAT_FIND_ID
12186 "find_in_path",
12187#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012188#ifdef FEAT_FLOAT
12189 "float",
12190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191#ifdef FEAT_FOLDING
12192 "folding",
12193#endif
12194#ifdef FEAT_FOOTER
12195 "footer",
12196#endif
12197#if !defined(USE_SYSTEM) && defined(UNIX)
12198 "fork",
12199#endif
12200#ifdef FEAT_GETTEXT
12201 "gettext",
12202#endif
12203#ifdef FEAT_GUI
12204 "gui",
12205#endif
12206#ifdef FEAT_GUI_ATHENA
12207# ifdef FEAT_GUI_NEXTAW
12208 "gui_neXtaw",
12209# else
12210 "gui_athena",
12211# endif
12212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213#ifdef FEAT_GUI_GTK
12214 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012217#ifdef FEAT_GUI_GNOME
12218 "gui_gnome",
12219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220#ifdef FEAT_GUI_MAC
12221 "gui_mac",
12222#endif
12223#ifdef FEAT_GUI_MOTIF
12224 "gui_motif",
12225#endif
12226#ifdef FEAT_GUI_PHOTON
12227 "gui_photon",
12228#endif
12229#ifdef FEAT_GUI_W16
12230 "gui_win16",
12231#endif
12232#ifdef FEAT_GUI_W32
12233 "gui_win32",
12234#endif
12235#ifdef FEAT_HANGULIN
12236 "hangul_input",
12237#endif
12238#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12239 "iconv",
12240#endif
12241#ifdef FEAT_INS_EXPAND
12242 "insert_expand",
12243#endif
12244#ifdef FEAT_JUMPLIST
12245 "jumplist",
12246#endif
12247#ifdef FEAT_KEYMAP
12248 "keymap",
12249#endif
12250#ifdef FEAT_LANGMAP
12251 "langmap",
12252#endif
12253#ifdef FEAT_LIBCALL
12254 "libcall",
12255#endif
12256#ifdef FEAT_LINEBREAK
12257 "linebreak",
12258#endif
12259#ifdef FEAT_LISP
12260 "lispindent",
12261#endif
12262#ifdef FEAT_LISTCMDS
12263 "listcmds",
12264#endif
12265#ifdef FEAT_LOCALMAP
12266 "localmap",
12267#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012268#ifdef FEAT_LUA
12269# ifndef DYNAMIC_LUA
12270 "lua",
12271# endif
12272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273#ifdef FEAT_MENU
12274 "menu",
12275#endif
12276#ifdef FEAT_SESSION
12277 "mksession",
12278#endif
12279#ifdef FEAT_MODIFY_FNAME
12280 "modify_fname",
12281#endif
12282#ifdef FEAT_MOUSE
12283 "mouse",
12284#endif
12285#ifdef FEAT_MOUSESHAPE
12286 "mouseshape",
12287#endif
12288#if defined(UNIX) || defined(VMS)
12289# ifdef FEAT_MOUSE_DEC
12290 "mouse_dec",
12291# endif
12292# ifdef FEAT_MOUSE_GPM
12293 "mouse_gpm",
12294# endif
12295# ifdef FEAT_MOUSE_JSB
12296 "mouse_jsbterm",
12297# endif
12298# ifdef FEAT_MOUSE_NET
12299 "mouse_netterm",
12300# endif
12301# ifdef FEAT_MOUSE_PTERM
12302 "mouse_pterm",
12303# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012304# ifdef FEAT_MOUSE_SGR
12305 "mouse_sgr",
12306# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012307# ifdef FEAT_SYSMOUSE
12308 "mouse_sysmouse",
12309# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012310# ifdef FEAT_MOUSE_URXVT
12311 "mouse_urxvt",
12312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313# ifdef FEAT_MOUSE_XTERM
12314 "mouse_xterm",
12315# endif
12316#endif
12317#ifdef FEAT_MBYTE
12318 "multi_byte",
12319#endif
12320#ifdef FEAT_MBYTE_IME
12321 "multi_byte_ime",
12322#endif
12323#ifdef FEAT_MULTI_LANG
12324 "multi_lang",
12325#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012326#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012327#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012328 "mzscheme",
12329#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331#ifdef FEAT_OLE
12332 "ole",
12333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334#ifdef FEAT_PATH_EXTRA
12335 "path_extra",
12336#endif
12337#ifdef FEAT_PERL
12338#ifndef DYNAMIC_PERL
12339 "perl",
12340#endif
12341#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012342#ifdef FEAT_PERSISTENT_UNDO
12343 "persistent_undo",
12344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345#ifdef FEAT_PYTHON
12346#ifndef DYNAMIC_PYTHON
12347 "python",
12348#endif
12349#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012350#ifdef FEAT_PYTHON3
12351#ifndef DYNAMIC_PYTHON3
12352 "python3",
12353#endif
12354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355#ifdef FEAT_POSTSCRIPT
12356 "postscript",
12357#endif
12358#ifdef FEAT_PRINTER
12359 "printer",
12360#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012361#ifdef FEAT_PROFILE
12362 "profile",
12363#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012364#ifdef FEAT_RELTIME
12365 "reltime",
12366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367#ifdef FEAT_QUICKFIX
12368 "quickfix",
12369#endif
12370#ifdef FEAT_RIGHTLEFT
12371 "rightleft",
12372#endif
12373#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12374 "ruby",
12375#endif
12376#ifdef FEAT_SCROLLBIND
12377 "scrollbind",
12378#endif
12379#ifdef FEAT_CMDL_INFO
12380 "showcmd",
12381 "cmdline_info",
12382#endif
12383#ifdef FEAT_SIGNS
12384 "signs",
12385#endif
12386#ifdef FEAT_SMARTINDENT
12387 "smartindent",
12388#endif
12389#ifdef FEAT_SNIFF
12390 "sniff",
12391#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012392#ifdef STARTUPTIME
12393 "startuptime",
12394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395#ifdef FEAT_STL_OPT
12396 "statusline",
12397#endif
12398#ifdef FEAT_SUN_WORKSHOP
12399 "sun_workshop",
12400#endif
12401#ifdef FEAT_NETBEANS_INTG
12402 "netbeans_intg",
12403#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012404#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012405 "spell",
12406#endif
12407#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 "syntax",
12409#endif
12410#if defined(USE_SYSTEM) || !defined(UNIX)
12411 "system",
12412#endif
12413#ifdef FEAT_TAG_BINS
12414 "tag_binary",
12415#endif
12416#ifdef FEAT_TAG_OLDSTATIC
12417 "tag_old_static",
12418#endif
12419#ifdef FEAT_TAG_ANYWHITE
12420 "tag_any_white",
12421#endif
12422#ifdef FEAT_TCL
12423# ifndef DYNAMIC_TCL
12424 "tcl",
12425# endif
12426#endif
12427#ifdef TERMINFO
12428 "terminfo",
12429#endif
12430#ifdef FEAT_TERMRESPONSE
12431 "termresponse",
12432#endif
12433#ifdef FEAT_TEXTOBJ
12434 "textobjects",
12435#endif
12436#ifdef HAVE_TGETENT
12437 "tgetent",
12438#endif
12439#ifdef FEAT_TITLE
12440 "title",
12441#endif
12442#ifdef FEAT_TOOLBAR
12443 "toolbar",
12444#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012445#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12446 "unnamedplus",
12447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448#ifdef FEAT_USR_CMDS
12449 "user-commands", /* was accidentally included in 5.4 */
12450 "user_commands",
12451#endif
12452#ifdef FEAT_VIMINFO
12453 "viminfo",
12454#endif
12455#ifdef FEAT_VERTSPLIT
12456 "vertsplit",
12457#endif
12458#ifdef FEAT_VIRTUALEDIT
12459 "virtualedit",
12460#endif
12461#ifdef FEAT_VISUAL
12462 "visual",
12463#endif
12464#ifdef FEAT_VISUALEXTRA
12465 "visualextra",
12466#endif
12467#ifdef FEAT_VREPLACE
12468 "vreplace",
12469#endif
12470#ifdef FEAT_WILDIGN
12471 "wildignore",
12472#endif
12473#ifdef FEAT_WILDMENU
12474 "wildmenu",
12475#endif
12476#ifdef FEAT_WINDOWS
12477 "windows",
12478#endif
12479#ifdef FEAT_WAK
12480 "winaltkeys",
12481#endif
12482#ifdef FEAT_WRITEBACKUP
12483 "writebackup",
12484#endif
12485#ifdef FEAT_XIM
12486 "xim",
12487#endif
12488#ifdef FEAT_XFONTSET
12489 "xfontset",
12490#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012491#ifdef FEAT_XPM_W32
12492 "xpm_w32",
12493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494#ifdef USE_XSMP
12495 "xsmp",
12496#endif
12497#ifdef USE_XSMP_INTERACT
12498 "xsmp_interact",
12499#endif
12500#ifdef FEAT_XCLIPBOARD
12501 "xterm_clipboard",
12502#endif
12503#ifdef FEAT_XTERM_SAVE
12504 "xterm_save",
12505#endif
12506#if defined(UNIX) && defined(FEAT_X11)
12507 "X11",
12508#endif
12509 NULL
12510 };
12511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 for (i = 0; has_list[i] != NULL; ++i)
12514 if (STRICMP(name, has_list[i]) == 0)
12515 {
12516 n = TRUE;
12517 break;
12518 }
12519
12520 if (n == FALSE)
12521 {
12522 if (STRNICMP(name, "patch", 5) == 0)
12523 n = has_patch(atoi((char *)name + 5));
12524 else if (STRICMP(name, "vim_starting") == 0)
12525 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012526#ifdef FEAT_MBYTE
12527 else if (STRICMP(name, "multi_byte_encoding") == 0)
12528 n = has_mbyte;
12529#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012530#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12531 else if (STRICMP(name, "balloon_multiline") == 0)
12532 n = multiline_balloon_available();
12533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534#ifdef DYNAMIC_TCL
12535 else if (STRICMP(name, "tcl") == 0)
12536 n = tcl_enabled(FALSE);
12537#endif
12538#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12539 else if (STRICMP(name, "iconv") == 0)
12540 n = iconv_enabled(FALSE);
12541#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012542#ifdef DYNAMIC_LUA
12543 else if (STRICMP(name, "lua") == 0)
12544 n = lua_enabled(FALSE);
12545#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012546#ifdef DYNAMIC_MZSCHEME
12547 else if (STRICMP(name, "mzscheme") == 0)
12548 n = mzscheme_enabled(FALSE);
12549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550#ifdef DYNAMIC_RUBY
12551 else if (STRICMP(name, "ruby") == 0)
12552 n = ruby_enabled(FALSE);
12553#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012554#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555#ifdef DYNAMIC_PYTHON
12556 else if (STRICMP(name, "python") == 0)
12557 n = python_enabled(FALSE);
12558#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012559#endif
12560#ifdef FEAT_PYTHON3
12561#ifdef DYNAMIC_PYTHON3
12562 else if (STRICMP(name, "python3") == 0)
12563 n = python3_enabled(FALSE);
12564#endif
12565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566#ifdef DYNAMIC_PERL
12567 else if (STRICMP(name, "perl") == 0)
12568 n = perl_enabled(FALSE);
12569#endif
12570#ifdef FEAT_GUI
12571 else if (STRICMP(name, "gui_running") == 0)
12572 n = (gui.in_use || gui.starting);
12573# ifdef FEAT_GUI_W32
12574 else if (STRICMP(name, "gui_win32s") == 0)
12575 n = gui_is_win32s();
12576# endif
12577# ifdef FEAT_BROWSE
12578 else if (STRICMP(name, "browse") == 0)
12579 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12580# endif
12581#endif
12582#ifdef FEAT_SYN_HL
12583 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012584 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585#endif
12586#if defined(WIN3264)
12587 else if (STRICMP(name, "win95") == 0)
12588 n = mch_windows95();
12589#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012590#ifdef FEAT_NETBEANS_INTG
12591 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012592 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 }
12595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597}
12598
12599/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012600 * "has_key()" function
12601 */
12602 static void
12603f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012604 typval_T *argvars;
12605 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012606{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012607 if (argvars[0].v_type != VAR_DICT)
12608 {
12609 EMSG(_(e_dictreq));
12610 return;
12611 }
12612 if (argvars[0].vval.v_dict == NULL)
12613 return;
12614
12615 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012616 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012617}
12618
12619/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012620 * "haslocaldir()" function
12621 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012622 static void
12623f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012624 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012625 typval_T *rettv;
12626{
12627 rettv->vval.v_number = (curwin->w_localdir != NULL);
12628}
12629
12630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 * "hasmapto()" function
12632 */
12633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012635 typval_T *argvars;
12636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637{
12638 char_u *name;
12639 char_u *mode;
12640 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012641 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012644 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 mode = (char_u *)"nvo";
12646 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012649 if (argvars[2].v_type != VAR_UNKNOWN)
12650 abbr = get_tv_number(&argvars[2]);
12651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652
Bram Moolenaar2c932302006-03-18 21:42:09 +000012653 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657}
12658
12659/*
12660 * "histadd()" function
12661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012663f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666{
12667#ifdef FEAT_CMDHIST
12668 int histype;
12669 char_u *str;
12670 char_u buf[NUMBUFLEN];
12671#endif
12672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 if (check_restricted() || check_secure())
12675 return;
12676#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012677 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12678 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 if (histype >= 0)
12680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012681 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682 if (*str != NUL)
12683 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012684 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687 return;
12688 }
12689 }
12690#endif
12691}
12692
12693/*
12694 * "histdel()" function
12695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012698 typval_T *argvars UNUSED;
12699 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700{
12701#ifdef FEAT_CMDHIST
12702 int n;
12703 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012706 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12707 if (str == NULL)
12708 n = 0;
12709 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012711 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012712 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012715 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716 else
12717 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012719 get_tv_string_buf(&argvars[1], buf));
12720 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721#endif
12722}
12723
12724/*
12725 * "histget()" function
12726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731{
12732#ifdef FEAT_CMDHIST
12733 int type;
12734 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012737 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12738 if (str == NULL)
12739 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 {
12742 type = get_histtype(str);
12743 if (argvars[1].v_type == VAR_UNKNOWN)
12744 idx = get_history_idx(type);
12745 else
12746 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12747 /* -1 on type error */
12748 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754}
12755
12756/*
12757 * "histnr()" function
12758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012761 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012762 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763{
12764 int i;
12765
12766#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 char_u *history = get_tv_string_chk(&argvars[0]);
12768
12769 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 if (i >= HIST_CMD && i < HIST_COUNT)
12771 i = get_history_idx(i);
12772 else
12773#endif
12774 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776}
12777
12778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 * "highlightID(name)" function
12780 */
12781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *argvars;
12784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787}
12788
12789/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012790 * "highlight_exists()" function
12791 */
12792 static void
12793f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012794 typval_T *argvars;
12795 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012796{
12797 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12798}
12799
12800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 * "hostname()" function
12802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012805 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807{
12808 char_u hostname[256];
12809
12810 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->v_type = VAR_STRING;
12812 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813}
12814
12815/*
12816 * iconv() function
12817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012820 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822{
12823#ifdef FEAT_MBYTE
12824 char_u buf1[NUMBUFLEN];
12825 char_u buf2[NUMBUFLEN];
12826 char_u *from, *to, *str;
12827 vimconv_T vimconv;
12828#endif
12829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832
12833#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 str = get_tv_string(&argvars[0]);
12835 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12836 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 vimconv.vc_type = CONV_NONE;
12838 convert_setup(&vimconv, from, to);
12839
12840 /* If the encodings are equal, no conversion needed. */
12841 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845
12846 convert_setup(&vimconv, NULL, NULL);
12847 vim_free(from);
12848 vim_free(to);
12849#endif
12850}
12851
12852/*
12853 * "indent()" function
12854 */
12855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012857 typval_T *argvars;
12858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859{
12860 linenr_T lnum;
12861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867}
12868
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012869/*
12870 * "index()" function
12871 */
12872 static void
12873f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 typval_T *argvars;
12875 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012876{
Bram Moolenaar33570922005-01-25 22:26:29 +000012877 list_T *l;
12878 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012879 long idx = 0;
12880 int ic = FALSE;
12881
12882 rettv->vval.v_number = -1;
12883 if (argvars[0].v_type != VAR_LIST)
12884 {
12885 EMSG(_(e_listreq));
12886 return;
12887 }
12888 l = argvars[0].vval.v_list;
12889 if (l != NULL)
12890 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012891 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012894 int error = FALSE;
12895
Bram Moolenaar758711c2005-02-02 23:11:38 +000012896 /* Start at specified item. Use the cached index that list_find()
12897 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012898 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012899 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012900 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 ic = get_tv_number_chk(&argvars[3], &error);
12902 if (error)
12903 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012904 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012905
Bram Moolenaar758711c2005-02-02 23:11:38 +000012906 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012907 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012908 {
12909 rettv->vval.v_number = idx;
12910 break;
12911 }
12912 }
12913}
12914
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915static int inputsecret_flag = 0;
12916
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012917static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12918
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012920 * This function is used by f_input() and f_inputdialog() functions. The third
12921 * argument to f_input() specifies the type of completion to use at the
12922 * prompt. The third argument to f_inputdialog() specifies the value to return
12923 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 */
12925 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012926get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012927 typval_T *argvars;
12928 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012929 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012931 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 char_u *p = NULL;
12933 int c;
12934 char_u buf[NUMBUFLEN];
12935 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012936 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012937 int xp_type = EXPAND_NOTHING;
12938 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012941 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
12943#ifdef NO_CONSOLE_INPUT
12944 /* While starting up, there is no place to enter text. */
12945 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947#endif
12948
12949 cmd_silent = FALSE; /* Want to see the prompt. */
12950 if (prompt != NULL)
12951 {
12952 /* Only the part of the message after the last NL is considered as
12953 * prompt for the command line */
12954 p = vim_strrchr(prompt, '\n');
12955 if (p == NULL)
12956 p = prompt;
12957 else
12958 {
12959 ++p;
12960 c = *p;
12961 *p = NUL;
12962 msg_start();
12963 msg_clr_eos();
12964 msg_puts_attr(prompt, echo_attr);
12965 msg_didout = FALSE;
12966 msg_starthere();
12967 *p = c;
12968 }
12969 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012971 if (argvars[1].v_type != VAR_UNKNOWN)
12972 {
12973 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12974 if (defstr != NULL)
12975 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012977 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012978 {
12979 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012980 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012981 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012982
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020012983 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000012984 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012985
Bram Moolenaar4463f292005-09-25 22:20:24 +000012986 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12987 if (xp_name == NULL)
12988 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012989
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012990 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012991
Bram Moolenaar4463f292005-09-25 22:20:24 +000012992 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12993 &xp_arg) == FAIL)
12994 return;
12995 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012996 }
12997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012998 if (defstr != NULL)
12999 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013000 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13001 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013002 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013003 && argvars[1].v_type != VAR_UNKNOWN
13004 && argvars[2].v_type != VAR_UNKNOWN)
13005 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13006 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013007
13008 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 /* since the user typed this, no need to wait for return */
13011 need_wait_return = FALSE;
13012 msg_didout = FALSE;
13013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 cmd_silent = cmd_silent_save;
13015}
13016
13017/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013018 * "input()" function
13019 * Also handles inputsecret() when inputsecret is set.
13020 */
13021 static void
13022f_input(argvars, rettv)
13023 typval_T *argvars;
13024 typval_T *rettv;
13025{
13026 get_user_input(argvars, rettv, FALSE);
13027}
13028
13029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 * "inputdialog()" function
13031 */
13032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013033f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013034 typval_T *argvars;
13035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036{
13037#if defined(FEAT_GUI_TEXTDIALOG)
13038 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13039 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13040 {
13041 char_u *message;
13042 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013045 message = get_tv_string_chk(&argvars[0]);
13046 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013047 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013048 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 else
13050 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 if (message != NULL && defstr != NULL
13052 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013053 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013054 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 else
13056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 if (message != NULL && defstr != NULL
13058 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013059 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->vval.v_string = vim_strsave(
13061 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013063 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013065 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 }
13067 else
13068#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013069 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070}
13071
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013072/*
13073 * "inputlist()" function
13074 */
13075 static void
13076f_inputlist(argvars, rettv)
13077 typval_T *argvars;
13078 typval_T *rettv;
13079{
13080 listitem_T *li;
13081 int selected;
13082 int mouse_used;
13083
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013084#ifdef NO_CONSOLE_INPUT
13085 /* While starting up, there is no place to enter text. */
13086 if (no_console_input())
13087 return;
13088#endif
13089 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13090 {
13091 EMSG2(_(e_listarg), "inputlist()");
13092 return;
13093 }
13094
13095 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013096 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013097 lines_left = Rows; /* avoid more prompt */
13098 msg_scroll = TRUE;
13099 msg_clr_eos();
13100
13101 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13102 {
13103 msg_puts(get_tv_string(&li->li_tv));
13104 msg_putchar('\n');
13105 }
13106
13107 /* Ask for choice. */
13108 selected = prompt_for_number(&mouse_used);
13109 if (mouse_used)
13110 selected -= lines_left;
13111
13112 rettv->vval.v_number = selected;
13113}
13114
13115
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13117
13118/*
13119 * "inputrestore()" function
13120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013123 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013124 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013125{
13126 if (ga_userinput.ga_len > 0)
13127 {
13128 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13130 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013131 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 }
13133 else if (p_verbose > 1)
13134 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013135 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 }
13138}
13139
13140/*
13141 * "inputsave()" function
13142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013148 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 if (ga_grow(&ga_userinput, 1) == OK)
13150 {
13151 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13152 + ga_userinput.ga_len);
13153 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013154 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 }
13156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013157 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158}
13159
13160/*
13161 * "inputsecret()" function
13162 */
13163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013164f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *argvars;
13166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167{
13168 ++cmdline_star;
13169 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 --cmdline_star;
13172 --inputsecret_flag;
13173}
13174
13175/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176 * "insert()" function
13177 */
13178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013180 typval_T *argvars;
13181 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013182{
13183 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013184 listitem_T *item;
13185 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013186 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187
13188 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013189 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013190 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013191 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013192 {
13193 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194 before = get_tv_number_chk(&argvars[2], &error);
13195 if (error)
13196 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013197
Bram Moolenaar758711c2005-02-02 23:11:38 +000013198 if (before == l->lv_len)
13199 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013200 else
13201 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013202 item = list_find(l, before);
13203 if (item == NULL)
13204 {
13205 EMSGN(_(e_listidx), before);
13206 l = NULL;
13207 }
13208 }
13209 if (l != NULL)
13210 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013211 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013212 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 }
13214 }
13215}
13216
13217/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013218 * "invert(expr)" function
13219 */
13220 static void
13221f_invert(argvars, rettv)
13222 typval_T *argvars;
13223 typval_T *rettv;
13224{
13225 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13226}
13227
13228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 * "isdirectory()" function
13230 */
13231 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013232f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 typval_T *argvars;
13234 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237}
13238
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013239/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013240 * "islocked()" function
13241 */
13242 static void
13243f_islocked(argvars, rettv)
13244 typval_T *argvars;
13245 typval_T *rettv;
13246{
13247 lval_T lv;
13248 char_u *end;
13249 dictitem_T *di;
13250
13251 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013252 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13253 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013254 if (end != NULL && lv.ll_name != NULL)
13255 {
13256 if (*end != NUL)
13257 EMSG(_(e_trailing));
13258 else
13259 {
13260 if (lv.ll_tv == NULL)
13261 {
13262 if (check_changedtick(lv.ll_name))
13263 rettv->vval.v_number = 1; /* always locked */
13264 else
13265 {
13266 di = find_var(lv.ll_name, NULL);
13267 if (di != NULL)
13268 {
13269 /* Consider a variable locked when:
13270 * 1. the variable itself is locked
13271 * 2. the value of the variable is locked.
13272 * 3. the List or Dict value is locked.
13273 */
13274 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13275 || tv_islocked(&di->di_tv));
13276 }
13277 }
13278 }
13279 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013280 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013281 else if (lv.ll_newkey != NULL)
13282 EMSG2(_(e_dictkey), lv.ll_newkey);
13283 else if (lv.ll_list != NULL)
13284 /* List item. */
13285 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13286 else
13287 /* Dictionary item. */
13288 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13289 }
13290 }
13291
13292 clear_lval(&lv);
13293}
13294
Bram Moolenaar33570922005-01-25 22:26:29 +000013295static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013296
13297/*
13298 * Turn a dict into a list:
13299 * "what" == 0: list of keys
13300 * "what" == 1: list of values
13301 * "what" == 2: list of items
13302 */
13303 static void
13304dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013305 typval_T *argvars;
13306 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013307 int what;
13308{
Bram Moolenaar33570922005-01-25 22:26:29 +000013309 list_T *l2;
13310 dictitem_T *di;
13311 hashitem_T *hi;
13312 listitem_T *li;
13313 listitem_T *li2;
13314 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013315 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013316
Bram Moolenaar8c711452005-01-14 21:53:12 +000013317 if (argvars[0].v_type != VAR_DICT)
13318 {
13319 EMSG(_(e_dictreq));
13320 return;
13321 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013322 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013323 return;
13324
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013325 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013326 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013327
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013328 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013329 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013332 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013333 --todo;
13334 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013335
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013336 li = listitem_alloc();
13337 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013338 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013339 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013341 if (what == 0)
13342 {
13343 /* keys() */
13344 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013345 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013346 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13347 }
13348 else if (what == 1)
13349 {
13350 /* values() */
13351 copy_tv(&di->di_tv, &li->li_tv);
13352 }
13353 else
13354 {
13355 /* items() */
13356 l2 = list_alloc();
13357 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013358 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013359 li->li_tv.vval.v_list = l2;
13360 if (l2 == NULL)
13361 break;
13362 ++l2->lv_refcount;
13363
13364 li2 = listitem_alloc();
13365 if (li2 == NULL)
13366 break;
13367 list_append(l2, li2);
13368 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013369 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013370 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13371
13372 li2 = listitem_alloc();
13373 if (li2 == NULL)
13374 break;
13375 list_append(l2, li2);
13376 copy_tv(&di->di_tv, &li2->li_tv);
13377 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013378 }
13379 }
13380}
13381
13382/*
13383 * "items(dict)" function
13384 */
13385 static void
13386f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013389{
13390 dict_list(argvars, rettv, 2);
13391}
13392
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013394 * "join()" function
13395 */
13396 static void
13397f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013400{
13401 garray_T ga;
13402 char_u *sep;
13403
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404 if (argvars[0].v_type != VAR_LIST)
13405 {
13406 EMSG(_(e_listreq));
13407 return;
13408 }
13409 if (argvars[0].vval.v_list == NULL)
13410 return;
13411 if (argvars[1].v_type == VAR_UNKNOWN)
13412 sep = (char_u *)" ";
13413 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013414 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013415
13416 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013417
13418 if (sep != NULL)
13419 {
13420 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013421 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 ga_append(&ga, NUL);
13423 rettv->vval.v_string = (char_u *)ga.ga_data;
13424 }
13425 else
13426 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013427}
13428
13429/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430 * "keys()" function
13431 */
13432 static void
13433f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013434 typval_T *argvars;
13435 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013436{
13437 dict_list(argvars, rettv, 0);
13438}
13439
13440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 * "last_buffer_nr()" function.
13442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013445 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447{
13448 int n = 0;
13449 buf_T *buf;
13450
13451 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13452 if (n < buf->b_fnum)
13453 n = buf->b_fnum;
13454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013456}
13457
13458/*
13459 * "len()" function
13460 */
13461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013463 typval_T *argvars;
13464 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013465{
13466 switch (argvars[0].v_type)
13467 {
13468 case VAR_STRING:
13469 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013470 rettv->vval.v_number = (varnumber_T)STRLEN(
13471 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013472 break;
13473 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013474 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013476 case VAR_DICT:
13477 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13478 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013479 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013480 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013481 break;
13482 }
13483}
13484
Bram Moolenaar33570922005-01-25 22:26:29 +000013485static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486
13487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013488libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013489 typval_T *argvars;
13490 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013491 int type;
13492{
13493#ifdef FEAT_LIBCALL
13494 char_u *string_in;
13495 char_u **string_result;
13496 int nr_result;
13497#endif
13498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013500 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013502
13503 if (check_restricted() || check_secure())
13504 return;
13505
13506#ifdef FEAT_LIBCALL
13507 /* The first two args must be strings, otherwise its meaningless */
13508 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13509 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013510 string_in = NULL;
13511 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013512 string_in = argvars[2].vval.v_string;
13513 if (type == VAR_NUMBER)
13514 string_result = NULL;
13515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013517 if (mch_libcall(argvars[0].vval.v_string,
13518 argvars[1].vval.v_string,
13519 string_in,
13520 argvars[2].vval.v_number,
13521 string_result,
13522 &nr_result) == OK
13523 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013524 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013525 }
13526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527}
13528
13529/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013530 * "libcall()" function
13531 */
13532 static void
13533f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013534 typval_T *argvars;
13535 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536{
13537 libcall_common(argvars, rettv, VAR_STRING);
13538}
13539
13540/*
13541 * "libcallnr()" function
13542 */
13543 static void
13544f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013545 typval_T *argvars;
13546 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013547{
13548 libcall_common(argvars, rettv, VAR_NUMBER);
13549}
13550
13551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013552 * "line(string)" function
13553 */
13554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558{
13559 linenr_T lnum = 0;
13560 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013561 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013563 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 if (fp != NULL)
13565 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013566 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567}
13568
13569/*
13570 * "line2byte(lnum)" function
13571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013574 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576{
13577#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579#else
13580 linenr_T lnum;
13581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13587 if (rettv->vval.v_number >= 0)
13588 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589#endif
13590}
13591
13592/*
13593 * "lispindent(lnum)" function
13594 */
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013597 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600#ifdef FEAT_LISP
13601 pos_T pos;
13602 linenr_T lnum;
13603
13604 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013605 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13607 {
13608 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 curwin->w_cursor = pos;
13611 }
13612 else
13613#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615}
13616
13617/*
13618 * "localtime()" function
13619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013622 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626}
13627
Bram Moolenaar33570922005-01-25 22:26:29 +000013628static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629
13630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *argvars;
13633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 int exact;
13635{
13636 char_u *keys;
13637 char_u *which;
13638 char_u buf[NUMBUFLEN];
13639 char_u *keys_buf = NULL;
13640 char_u *rhs;
13641 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013642 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013643 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013644 mapblock_T *mp;
13645 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646
13647 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 rettv->v_type = VAR_STRING;
13649 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 if (*keys == NUL)
13653 return;
13654
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013655 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013659 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013660 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013661 if (argvars[3].v_type != VAR_UNKNOWN)
13662 get_dict = get_tv_number(&argvars[3]);
13663 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 else
13666 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 if (which == NULL)
13668 return;
13669
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670 mode = get_map_mode(&which, 0);
13671
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013672 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013673 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013675
13676 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678 /* Return a string. */
13679 if (rhs != NULL)
13680 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681
Bram Moolenaarbd743252010-10-20 21:23:33 +020013682 }
13683 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13684 {
13685 /* Return a dictionary. */
13686 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13687 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13688 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689
Bram Moolenaarbd743252010-10-20 21:23:33 +020013690 dict_add_nr_str(dict, "lhs", 0L, lhs);
13691 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13692 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13693 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13694 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13695 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13696 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13697 dict_add_nr_str(dict, "mode", 0L, mapmode);
13698
13699 vim_free(lhs);
13700 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 }
13702}
13703
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013704#ifdef FEAT_FLOAT
13705/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013706 * "log()" function
13707 */
13708 static void
13709f_log(argvars, rettv)
13710 typval_T *argvars;
13711 typval_T *rettv;
13712{
13713 float_T f;
13714
13715 rettv->v_type = VAR_FLOAT;
13716 if (get_float_arg(argvars, &f) == OK)
13717 rettv->vval.v_float = log(f);
13718 else
13719 rettv->vval.v_float = 0.0;
13720}
13721
13722/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013723 * "log10()" function
13724 */
13725 static void
13726f_log10(argvars, rettv)
13727 typval_T *argvars;
13728 typval_T *rettv;
13729{
13730 float_T f;
13731
13732 rettv->v_type = VAR_FLOAT;
13733 if (get_float_arg(argvars, &f) == OK)
13734 rettv->vval.v_float = log10(f);
13735 else
13736 rettv->vval.v_float = 0.0;
13737}
13738#endif
13739
Bram Moolenaar1dced572012-04-05 16:54:08 +020013740#ifdef FEAT_LUA
13741/*
13742 * "luaeval()" function
13743 */
13744 static void
13745f_luaeval(argvars, rettv)
13746 typval_T *argvars;
13747 typval_T *rettv;
13748{
13749 char_u *str;
13750 char_u buf[NUMBUFLEN];
13751
13752 str = get_tv_string_buf(&argvars[0], buf);
13753 do_luaeval(str, argvars + 1, rettv);
13754}
13755#endif
13756
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013758 * "map()" function
13759 */
13760 static void
13761f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013762 typval_T *argvars;
13763 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013764{
13765 filter_map(argvars, rettv, TRUE);
13766}
13767
13768/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013769 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 */
13771 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013772f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 typval_T *argvars;
13774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013776 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777}
13778
13779/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013780 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 */
13782 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013783f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 typval_T *argvars;
13785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013787 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788}
13789
Bram Moolenaar33570922005-01-25 22:26:29 +000013790static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
13792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013794 typval_T *argvars;
13795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 int type;
13797{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013798 char_u *str = NULL;
13799 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 char_u *pat;
13801 regmatch_T regmatch;
13802 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013803 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 char_u *save_cpo;
13805 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013806 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013807 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013808 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013809 list_T *l = NULL;
13810 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013811 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013812 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813
13814 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13815 save_cpo = p_cpo;
13816 p_cpo = (char_u *)"";
13817
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013818 rettv->vval.v_number = -1;
13819 if (type == 3)
13820 {
13821 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013822 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013823 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013824 }
13825 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013827 rettv->v_type = VAR_STRING;
13828 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013831 if (argvars[0].v_type == VAR_LIST)
13832 {
13833 if ((l = argvars[0].vval.v_list) == NULL)
13834 goto theend;
13835 li = l->lv_first;
13836 }
13837 else
13838 expr = str = get_tv_string(&argvars[0]);
13839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013840 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13841 if (pat == NULL)
13842 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013843
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013844 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013846 int error = FALSE;
13847
13848 start = get_tv_number_chk(&argvars[2], &error);
13849 if (error)
13850 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013851 if (l != NULL)
13852 {
13853 li = list_find(l, start);
13854 if (li == NULL)
13855 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013856 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013857 }
13858 else
13859 {
13860 if (start < 0)
13861 start = 0;
13862 if (start > (long)STRLEN(str))
13863 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013864 /* When "count" argument is there ignore matches before "start",
13865 * otherwise skip part of the string. Differs when pattern is "^"
13866 * or "\<". */
13867 if (argvars[3].v_type != VAR_UNKNOWN)
13868 startcol = start;
13869 else
13870 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013871 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013872
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013873 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013874 nth = get_tv_number_chk(&argvars[3], &error);
13875 if (error)
13876 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 }
13878
13879 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13880 if (regmatch.regprog != NULL)
13881 {
13882 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013883
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013884 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013885 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013886 if (l != NULL)
13887 {
13888 if (li == NULL)
13889 {
13890 match = FALSE;
13891 break;
13892 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013893 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013894 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013895 if (str == NULL)
13896 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013897 }
13898
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013899 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013900
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013902 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013903 if (l == NULL && !match)
13904 break;
13905
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013906 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013907 if (l != NULL)
13908 {
13909 li = li->li_next;
13910 ++idx;
13911 }
13912 else
13913 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013914#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013915 startcol = (colnr_T)(regmatch.startp[0]
13916 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013917#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013918 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013919#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013921 }
13922
13923 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013925 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013927 int i;
13928
13929 /* return list with matched string and submatches */
13930 for (i = 0; i < NSUBEXP; ++i)
13931 {
13932 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013933 {
13934 if (list_append_string(rettv->vval.v_list,
13935 (char_u *)"", 0) == FAIL)
13936 break;
13937 }
13938 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013939 regmatch.startp[i],
13940 (int)(regmatch.endp[i] - regmatch.startp[i]))
13941 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013942 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013943 }
13944 }
13945 else if (type == 2)
13946 {
13947 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013948 if (l != NULL)
13949 copy_tv(&li->li_tv, rettv);
13950 else
13951 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013953 }
13954 else if (l != NULL)
13955 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 else
13957 {
13958 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 (varnumber_T)(regmatch.startp[0] - str);
13961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013964 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 }
13966 }
13967 vim_free(regmatch.regprog);
13968 }
13969
13970theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013971 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 p_cpo = save_cpo;
13973}
13974
13975/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976 * "match()" function
13977 */
13978 static void
13979f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013980 typval_T *argvars;
13981 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013982{
13983 find_some_match(argvars, rettv, 1);
13984}
13985
13986/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013987 * "matchadd()" function
13988 */
13989 static void
13990f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013991 typval_T *argvars UNUSED;
13992 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013993{
13994#ifdef FEAT_SEARCH_EXTRA
13995 char_u buf[NUMBUFLEN];
13996 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13997 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13998 int prio = 10; /* default priority */
13999 int id = -1;
14000 int error = FALSE;
14001
14002 rettv->vval.v_number = -1;
14003
14004 if (grp == NULL || pat == NULL)
14005 return;
14006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014007 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014008 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014009 if (argvars[3].v_type != VAR_UNKNOWN)
14010 id = get_tv_number_chk(&argvars[3], &error);
14011 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014012 if (error == TRUE)
14013 return;
14014 if (id >= 1 && id <= 3)
14015 {
14016 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14017 return;
14018 }
14019
14020 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14021#endif
14022}
14023
14024/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014025 * "matcharg()" function
14026 */
14027 static void
14028f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014029 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014030 typval_T *rettv;
14031{
14032 if (rettv_list_alloc(rettv) == OK)
14033 {
14034#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014035 int id = get_tv_number(&argvars[0]);
14036 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014037
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014038 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014039 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014040 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14041 {
14042 list_append_string(rettv->vval.v_list,
14043 syn_id2name(m->hlg_id), -1);
14044 list_append_string(rettv->vval.v_list, m->pattern, -1);
14045 }
14046 else
14047 {
14048 list_append_string(rettv->vval.v_list, NUL, -1);
14049 list_append_string(rettv->vval.v_list, NUL, -1);
14050 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014051 }
14052#endif
14053 }
14054}
14055
14056/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014057 * "matchdelete()" function
14058 */
14059 static void
14060f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014061 typval_T *argvars UNUSED;
14062 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014063{
14064#ifdef FEAT_SEARCH_EXTRA
14065 rettv->vval.v_number = match_delete(curwin,
14066 (int)get_tv_number(&argvars[0]), TRUE);
14067#endif
14068}
14069
14070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014071 * "matchend()" function
14072 */
14073 static void
14074f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014075 typval_T *argvars;
14076 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014077{
14078 find_some_match(argvars, rettv, 0);
14079}
14080
14081/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014082 * "matchlist()" function
14083 */
14084 static void
14085f_matchlist(argvars, rettv)
14086 typval_T *argvars;
14087 typval_T *rettv;
14088{
14089 find_some_match(argvars, rettv, 3);
14090}
14091
14092/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014093 * "matchstr()" function
14094 */
14095 static void
14096f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014097 typval_T *argvars;
14098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014099{
14100 find_some_match(argvars, rettv, 2);
14101}
14102
Bram Moolenaar33570922005-01-25 22:26:29 +000014103static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014104
14105 static void
14106max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014107 typval_T *argvars;
14108 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014109 int domax;
14110{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014111 long n = 0;
14112 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014114
14115 if (argvars[0].v_type == VAR_LIST)
14116 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 list_T *l;
14118 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014119
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014120 l = argvars[0].vval.v_list;
14121 if (l != NULL)
14122 {
14123 li = l->lv_first;
14124 if (li != NULL)
14125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014127 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014128 {
14129 li = li->li_next;
14130 if (li == NULL)
14131 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014132 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014133 if (domax ? i > n : i < n)
14134 n = i;
14135 }
14136 }
14137 }
14138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014139 else if (argvars[0].v_type == VAR_DICT)
14140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014141 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014142 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014143 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014144 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014145
14146 d = argvars[0].vval.v_dict;
14147 if (d != NULL)
14148 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014149 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014150 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014152 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014154 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014156 if (first)
14157 {
14158 n = i;
14159 first = FALSE;
14160 }
14161 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014162 n = i;
14163 }
14164 }
14165 }
14166 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014167 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014168 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170}
14171
14172/*
14173 * "max()" function
14174 */
14175 static void
14176f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 typval_T *argvars;
14178 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014179{
14180 max_min(argvars, rettv, TRUE);
14181}
14182
14183/*
14184 * "min()" function
14185 */
14186 static void
14187f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *argvars;
14189 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014190{
14191 max_min(argvars, rettv, FALSE);
14192}
14193
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014194static int mkdir_recurse __ARGS((char_u *dir, int prot));
14195
14196/*
14197 * Create the directory in which "dir" is located, and higher levels when
14198 * needed.
14199 */
14200 static int
14201mkdir_recurse(dir, prot)
14202 char_u *dir;
14203 int prot;
14204{
14205 char_u *p;
14206 char_u *updir;
14207 int r = FAIL;
14208
14209 /* Get end of directory name in "dir".
14210 * We're done when it's "/" or "c:/". */
14211 p = gettail_sep(dir);
14212 if (p <= get_past_head(dir))
14213 return OK;
14214
14215 /* If the directory exists we're done. Otherwise: create it.*/
14216 updir = vim_strnsave(dir, (int)(p - dir));
14217 if (updir == NULL)
14218 return FAIL;
14219 if (mch_isdir(updir))
14220 r = OK;
14221 else if (mkdir_recurse(updir, prot) == OK)
14222 r = vim_mkdir_emsg(updir, prot);
14223 vim_free(updir);
14224 return r;
14225}
14226
14227#ifdef vim_mkdir
14228/*
14229 * "mkdir()" function
14230 */
14231 static void
14232f_mkdir(argvars, rettv)
14233 typval_T *argvars;
14234 typval_T *rettv;
14235{
14236 char_u *dir;
14237 char_u buf[NUMBUFLEN];
14238 int prot = 0755;
14239
14240 rettv->vval.v_number = FAIL;
14241 if (check_restricted() || check_secure())
14242 return;
14243
14244 dir = get_tv_string_buf(&argvars[0], buf);
14245 if (argvars[1].v_type != VAR_UNKNOWN)
14246 {
14247 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 prot = get_tv_number_chk(&argvars[2], NULL);
14249 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014250 mkdir_recurse(dir, prot);
14251 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014253}
14254#endif
14255
Bram Moolenaar0d660222005-01-07 21:51:51 +000014256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 * "mode()" function
14258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014261 typval_T *argvars;
14262 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014264 char_u buf[3];
14265
14266 buf[1] = NUL;
14267 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268
14269#ifdef FEAT_VISUAL
14270 if (VIsual_active)
14271 {
14272 if (VIsual_select)
14273 buf[0] = VIsual_mode + 's' - 'v';
14274 else
14275 buf[0] = VIsual_mode;
14276 }
14277 else
14278#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014279 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14280 || State == CONFIRM)
14281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014283 if (State == ASKMORE)
14284 buf[1] = 'm';
14285 else if (State == CONFIRM)
14286 buf[1] = '?';
14287 }
14288 else if (State == EXTERNCMD)
14289 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 else if (State & INSERT)
14291 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014292#ifdef FEAT_VREPLACE
14293 if (State & VREPLACE_FLAG)
14294 {
14295 buf[0] = 'R';
14296 buf[1] = 'v';
14297 }
14298 else
14299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 if (State & REPLACE_FLAG)
14301 buf[0] = 'R';
14302 else
14303 buf[0] = 'i';
14304 }
14305 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014308 if (exmode_active)
14309 buf[1] = 'v';
14310 }
14311 else if (exmode_active)
14312 {
14313 buf[0] = 'c';
14314 buf[1] = 'e';
14315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014319 if (finish_op)
14320 buf[1] = 'o';
14321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014323 /* Clear out the minor mode when the argument is not a non-zero number or
14324 * non-empty string. */
14325 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014326 buf[1] = NUL;
14327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328 rettv->vval.v_string = vim_strsave(buf);
14329 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330}
14331
Bram Moolenaar429fa852013-04-15 12:27:36 +020014332#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014333/*
14334 * "mzeval()" function
14335 */
14336 static void
14337f_mzeval(argvars, rettv)
14338 typval_T *argvars;
14339 typval_T *rettv;
14340{
14341 char_u *str;
14342 char_u buf[NUMBUFLEN];
14343
14344 str = get_tv_string_buf(&argvars[0], buf);
14345 do_mzeval(str, rettv);
14346}
Bram Moolenaar75676462013-01-30 14:55:42 +010014347
14348 void
14349mzscheme_call_vim(name, args, rettv)
14350 char_u *name;
14351 typval_T *args;
14352 typval_T *rettv;
14353{
14354 typval_T argvars[3];
14355
14356 argvars[0].v_type = VAR_STRING;
14357 argvars[0].vval.v_string = name;
14358 copy_tv(args, &argvars[1]);
14359 argvars[2].v_type = VAR_UNKNOWN;
14360 f_call(argvars, rettv);
14361 clear_tv(&argvars[1]);
14362}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014363#endif
14364
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014366 * "nextnonblank()" function
14367 */
14368 static void
14369f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014370 typval_T *argvars;
14371 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014372{
14373 linenr_T lnum;
14374
14375 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014378 {
14379 lnum = 0;
14380 break;
14381 }
14382 if (*skipwhite(ml_get(lnum)) != NUL)
14383 break;
14384 }
14385 rettv->vval.v_number = lnum;
14386}
14387
14388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389 * "nr2char()" function
14390 */
14391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014393 typval_T *argvars;
14394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395{
14396 char_u buf[NUMBUFLEN];
14397
14398#ifdef FEAT_MBYTE
14399 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014400 {
14401 int utf8 = 0;
14402
14403 if (argvars[1].v_type != VAR_UNKNOWN)
14404 utf8 = get_tv_number_chk(&argvars[1], NULL);
14405 if (utf8)
14406 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14407 else
14408 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 else
14411#endif
14412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 buf[1] = NUL;
14415 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 rettv->v_type = VAR_STRING;
14417 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014418}
14419
14420/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014421 * "or(expr, expr)" function
14422 */
14423 static void
14424f_or(argvars, rettv)
14425 typval_T *argvars;
14426 typval_T *rettv;
14427{
14428 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14429 | get_tv_number_chk(&argvars[1], NULL);
14430}
14431
14432/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014433 * "pathshorten()" function
14434 */
14435 static void
14436f_pathshorten(argvars, rettv)
14437 typval_T *argvars;
14438 typval_T *rettv;
14439{
14440 char_u *p;
14441
14442 rettv->v_type = VAR_STRING;
14443 p = get_tv_string_chk(&argvars[0]);
14444 if (p == NULL)
14445 rettv->vval.v_string = NULL;
14446 else
14447 {
14448 p = vim_strsave(p);
14449 rettv->vval.v_string = p;
14450 if (p != NULL)
14451 shorten_dir(p);
14452 }
14453}
14454
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014455#ifdef FEAT_FLOAT
14456/*
14457 * "pow()" function
14458 */
14459 static void
14460f_pow(argvars, rettv)
14461 typval_T *argvars;
14462 typval_T *rettv;
14463{
14464 float_T fx, fy;
14465
14466 rettv->v_type = VAR_FLOAT;
14467 if (get_float_arg(argvars, &fx) == OK
14468 && get_float_arg(&argvars[1], &fy) == OK)
14469 rettv->vval.v_float = pow(fx, fy);
14470 else
14471 rettv->vval.v_float = 0.0;
14472}
14473#endif
14474
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014475/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476 * "prevnonblank()" function
14477 */
14478 static void
14479f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014480 typval_T *argvars;
14481 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014482{
14483 linenr_T lnum;
14484
14485 lnum = get_tv_lnum(argvars);
14486 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14487 lnum = 0;
14488 else
14489 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14490 --lnum;
14491 rettv->vval.v_number = lnum;
14492}
14493
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014494#ifdef HAVE_STDARG_H
14495/* This dummy va_list is here because:
14496 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14497 * - locally in the function results in a "used before set" warning
14498 * - using va_start() to initialize it gives "function with fixed args" error */
14499static va_list ap;
14500#endif
14501
Bram Moolenaar8c711452005-01-14 21:53:12 +000014502/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014503 * "printf()" function
14504 */
14505 static void
14506f_printf(argvars, rettv)
14507 typval_T *argvars;
14508 typval_T *rettv;
14509{
14510 rettv->v_type = VAR_STRING;
14511 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014512#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014513 {
14514 char_u buf[NUMBUFLEN];
14515 int len;
14516 char_u *s;
14517 int saved_did_emsg = did_emsg;
14518 char *fmt;
14519
14520 /* Get the required length, allocate the buffer and do it for real. */
14521 did_emsg = FALSE;
14522 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014523 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014524 if (!did_emsg)
14525 {
14526 s = alloc(len + 1);
14527 if (s != NULL)
14528 {
14529 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014530 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014531 }
14532 }
14533 did_emsg |= saved_did_emsg;
14534 }
14535#endif
14536}
14537
14538/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014539 * "pumvisible()" function
14540 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014541 static void
14542f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014543 typval_T *argvars UNUSED;
14544 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014545{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014546#ifdef FEAT_INS_EXPAND
14547 if (pum_visible())
14548 rettv->vval.v_number = 1;
14549#endif
14550}
14551
Bram Moolenaardb913952012-06-29 12:54:53 +020014552#ifdef FEAT_PYTHON3
14553/*
14554 * "py3eval()" function
14555 */
14556 static void
14557f_py3eval(argvars, rettv)
14558 typval_T *argvars;
14559 typval_T *rettv;
14560{
14561 char_u *str;
14562 char_u buf[NUMBUFLEN];
14563
14564 str = get_tv_string_buf(&argvars[0], buf);
14565 do_py3eval(str, rettv);
14566}
14567#endif
14568
14569#ifdef FEAT_PYTHON
14570/*
14571 * "pyeval()" function
14572 */
14573 static void
14574f_pyeval(argvars, rettv)
14575 typval_T *argvars;
14576 typval_T *rettv;
14577{
14578 char_u *str;
14579 char_u buf[NUMBUFLEN];
14580
14581 str = get_tv_string_buf(&argvars[0], buf);
14582 do_pyeval(str, rettv);
14583}
14584#endif
14585
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014586/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014587 * "range()" function
14588 */
14589 static void
14590f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *argvars;
14592 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014593{
14594 long start;
14595 long end;
14596 long stride = 1;
14597 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014601 if (argvars[1].v_type == VAR_UNKNOWN)
14602 {
14603 end = start - 1;
14604 start = 0;
14605 }
14606 else
14607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014608 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014609 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014610 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014611 }
14612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 if (error)
14614 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014615 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014616 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014617 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014618 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014619 else
14620 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014621 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014623 if (list_append_number(rettv->vval.v_list,
14624 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014625 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626 }
14627}
14628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014629/*
14630 * "readfile()" function
14631 */
14632 static void
14633f_readfile(argvars, rettv)
14634 typval_T *argvars;
14635 typval_T *rettv;
14636{
14637 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014638 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014639 char_u *fname;
14640 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014641 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14642 int io_size = sizeof(buf);
14643 int readlen; /* size of last fread() */
14644 char_u *prev = NULL; /* previously read bytes, if any */
14645 long prevlen = 0; /* length of data in prev */
14646 long prevsize = 0; /* size of prev buffer */
14647 long maxline = MAXLNUM;
14648 long cnt = 0;
14649 char_u *p; /* position in buf */
14650 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014651
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014652 if (argvars[1].v_type != VAR_UNKNOWN)
14653 {
14654 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14655 binary = TRUE;
14656 if (argvars[2].v_type != VAR_UNKNOWN)
14657 maxline = get_tv_number(&argvars[2]);
14658 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014659
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014660 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014661 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014662
14663 /* Always open the file in binary mode, library functions have a mind of
14664 * their own about CR-LF conversion. */
14665 fname = get_tv_string(&argvars[0]);
14666 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14667 {
14668 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14669 return;
14670 }
14671
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014672 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014673 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014674 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014675
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014676 /* This for loop processes what was read, but is also entered at end
14677 * of file so that either:
14678 * - an incomplete line gets written
14679 * - a "binary" file gets an empty line at the end if it ends in a
14680 * newline. */
14681 for (p = buf, start = buf;
14682 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14683 ++p)
14684 {
14685 if (*p == '\n' || readlen <= 0)
14686 {
14687 listitem_T *li;
14688 char_u *s = NULL;
14689 long_u len = p - start;
14690
14691 /* Finished a line. Remove CRs before NL. */
14692 if (readlen > 0 && !binary)
14693 {
14694 while (len > 0 && start[len - 1] == '\r')
14695 --len;
14696 /* removal may cross back to the "prev" string */
14697 if (len == 0)
14698 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14699 --prevlen;
14700 }
14701 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014702 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014703 else
14704 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014705 /* Change "prev" buffer to be the right size. This way
14706 * the bytes are only copied once, and very long lines are
14707 * allocated only once. */
14708 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014710 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014711 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014712 prev = NULL; /* the list will own the string */
14713 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 }
14715 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014716 if (s == NULL)
14717 {
14718 do_outofmem_msg((long_u) prevlen + len + 1);
14719 failed = TRUE;
14720 break;
14721 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014722
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014723 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014724 {
14725 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014726 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727 break;
14728 }
14729 li->li_tv.v_type = VAR_STRING;
14730 li->li_tv.v_lock = 0;
14731 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014732 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014733
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014734 start = p + 1; /* step over newline */
14735 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014736 break;
14737 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014738 else if (*p == NUL)
14739 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014740#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14742 * when finding the BF and check the previous two bytes. */
14743 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014744 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014745 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14746 * + 1, these may be in the "prev" string. */
14747 char_u back1 = p >= buf + 1 ? p[-1]
14748 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14749 char_u back2 = p >= buf + 2 ? p[-2]
14750 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14751 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014754 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014755 char_u *dest = p - 2;
14756
14757 /* Usually a BOM is at the beginning of a file, and so at
14758 * the beginning of a line; then we can just step over it.
14759 */
14760 if (start == dest)
14761 start = p + 1;
14762 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014763 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014764 /* have to shuffle buf to close gap */
14765 int adjust_prevlen = 0;
14766
14767 if (dest < buf)
14768 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014769 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014770 dest = buf;
14771 }
14772 if (readlen > p - buf + 1)
14773 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14774 readlen -= 3 - adjust_prevlen;
14775 prevlen -= adjust_prevlen;
14776 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014777 }
14778 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014779 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014780#endif
14781 } /* for */
14782
14783 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14784 break;
14785 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014787 /* There's part of a line in buf, store it in "prev". */
14788 if (p - start + prevlen >= prevsize)
14789 {
14790 /* need bigger "prev" buffer */
14791 char_u *newprev;
14792
14793 /* A common use case is ordinary text files and "prev" gets a
14794 * fragment of a line, so the first allocation is made
14795 * small, to avoid repeatedly 'allocing' large and
14796 * 'reallocing' small. */
14797 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014798 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014799 else
14800 {
14801 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014802 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014803 prevsize = grow50pc > growmin ? grow50pc : growmin;
14804 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014805 newprev = prev == NULL ? alloc(prevsize)
14806 : vim_realloc(prev, prevsize);
14807 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014808 {
14809 do_outofmem_msg((long_u)prevsize);
14810 failed = TRUE;
14811 break;
14812 }
14813 prev = newprev;
14814 }
14815 /* Add the line part to end of "prev". */
14816 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014817 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014818 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014819 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014820
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014821 /*
14822 * For a negative line count use only the lines at the end of the file,
14823 * free the rest.
14824 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014825 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014826 while (cnt > -maxline)
14827 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014828 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014829 --cnt;
14830 }
14831
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014832 if (failed)
14833 {
14834 list_free(rettv->vval.v_list, TRUE);
14835 /* readfile doc says an empty list is returned on error */
14836 rettv->vval.v_list = list_alloc();
14837 }
14838
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014839 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014840 fclose(fd);
14841}
14842
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014843#if defined(FEAT_RELTIME)
14844static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14845
14846/*
14847 * Convert a List to proftime_T.
14848 * Return FAIL when there is something wrong.
14849 */
14850 static int
14851list2proftime(arg, tm)
14852 typval_T *arg;
14853 proftime_T *tm;
14854{
14855 long n1, n2;
14856 int error = FALSE;
14857
14858 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14859 || arg->vval.v_list->lv_len != 2)
14860 return FAIL;
14861 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14862 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14863# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014864 tm->HighPart = n1;
14865 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014866# else
14867 tm->tv_sec = n1;
14868 tm->tv_usec = n2;
14869# endif
14870 return error ? FAIL : OK;
14871}
14872#endif /* FEAT_RELTIME */
14873
14874/*
14875 * "reltime()" function
14876 */
14877 static void
14878f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014879 typval_T *argvars UNUSED;
14880 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014881{
14882#ifdef FEAT_RELTIME
14883 proftime_T res;
14884 proftime_T start;
14885
14886 if (argvars[0].v_type == VAR_UNKNOWN)
14887 {
14888 /* No arguments: get current time. */
14889 profile_start(&res);
14890 }
14891 else if (argvars[1].v_type == VAR_UNKNOWN)
14892 {
14893 if (list2proftime(&argvars[0], &res) == FAIL)
14894 return;
14895 profile_end(&res);
14896 }
14897 else
14898 {
14899 /* Two arguments: compute the difference. */
14900 if (list2proftime(&argvars[0], &start) == FAIL
14901 || list2proftime(&argvars[1], &res) == FAIL)
14902 return;
14903 profile_sub(&res, &start);
14904 }
14905
14906 if (rettv_list_alloc(rettv) == OK)
14907 {
14908 long n1, n2;
14909
14910# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014911 n1 = res.HighPart;
14912 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014913# else
14914 n1 = res.tv_sec;
14915 n2 = res.tv_usec;
14916# endif
14917 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14918 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14919 }
14920#endif
14921}
14922
14923/*
14924 * "reltimestr()" function
14925 */
14926 static void
14927f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014928 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014929 typval_T *rettv;
14930{
14931#ifdef FEAT_RELTIME
14932 proftime_T tm;
14933#endif
14934
14935 rettv->v_type = VAR_STRING;
14936 rettv->vval.v_string = NULL;
14937#ifdef FEAT_RELTIME
14938 if (list2proftime(&argvars[0], &tm) == OK)
14939 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14940#endif
14941}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014942
Bram Moolenaar0d660222005-01-07 21:51:51 +000014943#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14944static void make_connection __ARGS((void));
14945static int check_connection __ARGS((void));
14946
14947 static void
14948make_connection()
14949{
14950 if (X_DISPLAY == NULL
14951# ifdef FEAT_GUI
14952 && !gui.in_use
14953# endif
14954 )
14955 {
14956 x_force_connect = TRUE;
14957 setup_term_clip();
14958 x_force_connect = FALSE;
14959 }
14960}
14961
14962 static int
14963check_connection()
14964{
14965 make_connection();
14966 if (X_DISPLAY == NULL)
14967 {
14968 EMSG(_("E240: No connection to Vim server"));
14969 return FAIL;
14970 }
14971 return OK;
14972}
14973#endif
14974
14975#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014976static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014977
14978 static void
14979remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014980 typval_T *argvars;
14981 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982 int expr;
14983{
14984 char_u *server_name;
14985 char_u *keys;
14986 char_u *r = NULL;
14987 char_u buf[NUMBUFLEN];
14988# ifdef WIN32
14989 HWND w;
14990# else
14991 Window w;
14992# endif
14993
14994 if (check_restricted() || check_secure())
14995 return;
14996
14997# ifdef FEAT_X11
14998 if (check_connection() == FAIL)
14999 return;
15000# endif
15001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015002 server_name = get_tv_string_chk(&argvars[0]);
15003 if (server_name == NULL)
15004 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015005 keys = get_tv_string_buf(&argvars[1], buf);
15006# ifdef WIN32
15007 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15008# else
15009 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15010 < 0)
15011# endif
15012 {
15013 if (r != NULL)
15014 EMSG(r); /* sending worked but evaluation failed */
15015 else
15016 EMSG2(_("E241: Unable to send to %s"), server_name);
15017 return;
15018 }
15019
15020 rettv->vval.v_string = r;
15021
15022 if (argvars[2].v_type != VAR_UNKNOWN)
15023 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015024 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015025 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015026 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015028 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015029 v.di_tv.v_type = VAR_STRING;
15030 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015031 idvar = get_tv_string_chk(&argvars[2]);
15032 if (idvar != NULL)
15033 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015034 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 }
15036}
15037#endif
15038
15039/*
15040 * "remote_expr()" function
15041 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015042 static void
15043f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046{
15047 rettv->v_type = VAR_STRING;
15048 rettv->vval.v_string = NULL;
15049#ifdef FEAT_CLIENTSERVER
15050 remote_common(argvars, rettv, TRUE);
15051#endif
15052}
15053
15054/*
15055 * "remote_foreground()" function
15056 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057 static void
15058f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015059 typval_T *argvars UNUSED;
15060 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015062#ifdef FEAT_CLIENTSERVER
15063# ifdef WIN32
15064 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015065 {
15066 char_u *server_name = get_tv_string_chk(&argvars[0]);
15067
15068 if (server_name != NULL)
15069 serverForeground(server_name);
15070 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071# else
15072 /* Send a foreground() expression to the server. */
15073 argvars[1].v_type = VAR_STRING;
15074 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15075 argvars[2].v_type = VAR_UNKNOWN;
15076 remote_common(argvars, rettv, TRUE);
15077 vim_free(argvars[1].vval.v_string);
15078# endif
15079#endif
15080}
15081
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082 static void
15083f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015084 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015086{
15087#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015089 char_u *s = NULL;
15090# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015091 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094
15095 if (check_restricted() || check_secure())
15096 {
15097 rettv->vval.v_number = -1;
15098 return;
15099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015100 serverid = get_tv_string_chk(&argvars[0]);
15101 if (serverid == NULL)
15102 {
15103 rettv->vval.v_number = -1;
15104 return; /* type error; errmsg already given */
15105 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015107 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 if (n == 0)
15109 rettv->vval.v_number = -1;
15110 else
15111 {
15112 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15113 rettv->vval.v_number = (s != NULL);
15114 }
15115# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015116 if (check_connection() == FAIL)
15117 return;
15118
15119 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015120 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121# endif
15122
15123 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015125 char_u *retvar;
15126
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 v.di_tv.v_type = VAR_STRING;
15128 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015129 retvar = get_tv_string_chk(&argvars[1]);
15130 if (retvar != NULL)
15131 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133 }
15134#else
15135 rettv->vval.v_number = -1;
15136#endif
15137}
15138
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 static void
15140f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015143{
15144 char_u *r = NULL;
15145
15146#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015147 char_u *serverid = get_tv_string_chk(&argvars[0]);
15148
15149 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 {
15151# ifdef WIN32
15152 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015153 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015155 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 if (n != 0)
15157 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15158 if (r == NULL)
15159# else
15160 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015161 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162# endif
15163 EMSG(_("E277: Unable to read a server reply"));
15164 }
15165#endif
15166 rettv->v_type = VAR_STRING;
15167 rettv->vval.v_string = r;
15168}
15169
15170/*
15171 * "remote_send()" function
15172 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 static void
15174f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015175 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015177{
15178 rettv->v_type = VAR_STRING;
15179 rettv->vval.v_string = NULL;
15180#ifdef FEAT_CLIENTSERVER
15181 remote_common(argvars, rettv, FALSE);
15182#endif
15183}
15184
15185/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015186 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015187 */
15188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015189f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 typval_T *argvars;
15191 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015192{
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 list_T *l;
15194 listitem_T *item, *item2;
15195 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015196 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015197 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015198 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 dict_T *d;
15200 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015201 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015202
Bram Moolenaar8c711452005-01-14 21:53:12 +000015203 if (argvars[0].v_type == VAR_DICT)
15204 {
15205 if (argvars[2].v_type != VAR_UNKNOWN)
15206 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015207 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015208 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015210 key = get_tv_string_chk(&argvars[1]);
15211 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 di = dict_find(d, key, -1);
15214 if (di == NULL)
15215 EMSG2(_(e_dictkey), key);
15216 else
15217 {
15218 *rettv = di->di_tv;
15219 init_tv(&di->di_tv);
15220 dictitem_remove(d, di);
15221 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015222 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015223 }
15224 }
15225 else if (argvars[0].v_type != VAR_LIST)
15226 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015227 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015228 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 int error = FALSE;
15231
15232 idx = get_tv_number_chk(&argvars[1], &error);
15233 if (error)
15234 ; /* type error: do nothing, errmsg already given */
15235 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015236 EMSGN(_(e_listidx), idx);
15237 else
15238 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015239 if (argvars[2].v_type == VAR_UNKNOWN)
15240 {
15241 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015242 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015243 *rettv = item->li_tv;
15244 vim_free(item);
15245 }
15246 else
15247 {
15248 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 end = get_tv_number_chk(&argvars[2], &error);
15250 if (error)
15251 ; /* type error: do nothing */
15252 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015253 EMSGN(_(e_listidx), end);
15254 else
15255 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015256 int cnt = 0;
15257
15258 for (li = item; li != NULL; li = li->li_next)
15259 {
15260 ++cnt;
15261 if (li == item2)
15262 break;
15263 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015264 if (li == NULL) /* didn't find "item2" after "item" */
15265 EMSG(_(e_invrange));
15266 else
15267 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015268 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015269 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015270 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015271 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015272 l->lv_first = item;
15273 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015274 item->li_prev = NULL;
15275 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015276 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015277 }
15278 }
15279 }
15280 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015281 }
15282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283}
15284
15285/*
15286 * "rename({from}, {to})" function
15287 */
15288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015289f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015290 typval_T *argvars;
15291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292{
15293 char_u buf[NUMBUFLEN];
15294
15295 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015298 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15299 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300}
15301
15302/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015303 * "repeat()" function
15304 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015305 static void
15306f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *argvars;
15308 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015309{
15310 char_u *p;
15311 int n;
15312 int slen;
15313 int len;
15314 char_u *r;
15315 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015316
15317 n = get_tv_number(&argvars[1]);
15318 if (argvars[0].v_type == VAR_LIST)
15319 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015320 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015321 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015322 if (list_extend(rettv->vval.v_list,
15323 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015325 }
15326 else
15327 {
15328 p = get_tv_string(&argvars[0]);
15329 rettv->v_type = VAR_STRING;
15330 rettv->vval.v_string = NULL;
15331
15332 slen = (int)STRLEN(p);
15333 len = slen * n;
15334 if (len <= 0)
15335 return;
15336
15337 r = alloc(len + 1);
15338 if (r != NULL)
15339 {
15340 for (i = 0; i < n; i++)
15341 mch_memmove(r + i * slen, p, (size_t)slen);
15342 r[len] = NUL;
15343 }
15344
15345 rettv->vval.v_string = r;
15346 }
15347}
15348
15349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 * "resolve()" function
15351 */
15352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015354 typval_T *argvars;
15355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356{
15357 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015358#ifdef HAVE_READLINK
15359 char_u *buf = NULL;
15360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363#ifdef FEAT_SHORTCUT
15364 {
15365 char_u *v = NULL;
15366
15367 v = mch_resolve_shortcut(p);
15368 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015371 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 }
15373#else
15374# ifdef HAVE_READLINK
15375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 char_u *cpy;
15377 int len;
15378 char_u *remain = NULL;
15379 char_u *q;
15380 int is_relative_to_current = FALSE;
15381 int has_trailing_pathsep = FALSE;
15382 int limit = 100;
15383
15384 p = vim_strsave(p);
15385
15386 if (p[0] == '.' && (vim_ispathsep(p[1])
15387 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15388 is_relative_to_current = TRUE;
15389
15390 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015391 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015394 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396
15397 q = getnextcomp(p);
15398 if (*q != NUL)
15399 {
15400 /* Separate the first path component in "p", and keep the
15401 * remainder (beginning with the path separator). */
15402 remain = vim_strsave(q - 1);
15403 q[-1] = NUL;
15404 }
15405
Bram Moolenaard9462e32011-04-11 21:35:11 +020015406 buf = alloc(MAXPATHL + 1);
15407 if (buf == NULL)
15408 goto fail;
15409
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 for (;;)
15411 {
15412 for (;;)
15413 {
15414 len = readlink((char *)p, (char *)buf, MAXPATHL);
15415 if (len <= 0)
15416 break;
15417 buf[len] = NUL;
15418
15419 if (limit-- == 0)
15420 {
15421 vim_free(p);
15422 vim_free(remain);
15423 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 goto fail;
15426 }
15427
15428 /* Ensure that the result will have a trailing path separator
15429 * if the argument has one. */
15430 if (remain == NULL && has_trailing_pathsep)
15431 add_pathsep(buf);
15432
15433 /* Separate the first path component in the link value and
15434 * concatenate the remainders. */
15435 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15436 if (*q != NUL)
15437 {
15438 if (remain == NULL)
15439 remain = vim_strsave(q - 1);
15440 else
15441 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015442 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 if (cpy != NULL)
15444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 vim_free(remain);
15446 remain = cpy;
15447 }
15448 }
15449 q[-1] = NUL;
15450 }
15451
15452 q = gettail(p);
15453 if (q > p && *q == NUL)
15454 {
15455 /* Ignore trailing path separator. */
15456 q[-1] = NUL;
15457 q = gettail(p);
15458 }
15459 if (q > p && !mch_isFullName(buf))
15460 {
15461 /* symlink is relative to directory of argument */
15462 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15463 if (cpy != NULL)
15464 {
15465 STRCPY(cpy, p);
15466 STRCPY(gettail(cpy), buf);
15467 vim_free(p);
15468 p = cpy;
15469 }
15470 }
15471 else
15472 {
15473 vim_free(p);
15474 p = vim_strsave(buf);
15475 }
15476 }
15477
15478 if (remain == NULL)
15479 break;
15480
15481 /* Append the first path component of "remain" to "p". */
15482 q = getnextcomp(remain + 1);
15483 len = q - remain - (*q != NUL);
15484 cpy = vim_strnsave(p, STRLEN(p) + len);
15485 if (cpy != NULL)
15486 {
15487 STRNCAT(cpy, remain, len);
15488 vim_free(p);
15489 p = cpy;
15490 }
15491 /* Shorten "remain". */
15492 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015493 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 else
15495 {
15496 vim_free(remain);
15497 remain = NULL;
15498 }
15499 }
15500
15501 /* If the result is a relative path name, make it explicitly relative to
15502 * the current directory if and only if the argument had this form. */
15503 if (!vim_ispathsep(*p))
15504 {
15505 if (is_relative_to_current
15506 && *p != NUL
15507 && !(p[0] == '.'
15508 && (p[1] == NUL
15509 || vim_ispathsep(p[1])
15510 || (p[1] == '.'
15511 && (p[2] == NUL
15512 || vim_ispathsep(p[2]))))))
15513 {
15514 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015515 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 if (cpy != NULL)
15517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518 vim_free(p);
15519 p = cpy;
15520 }
15521 }
15522 else if (!is_relative_to_current)
15523 {
15524 /* Strip leading "./". */
15525 q = p;
15526 while (q[0] == '.' && vim_ispathsep(q[1]))
15527 q += 2;
15528 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015529 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 }
15531 }
15532
15533 /* Ensure that the result will have no trailing path separator
15534 * if the argument had none. But keep "/" or "//". */
15535 if (!has_trailing_pathsep)
15536 {
15537 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015538 if (after_pathsep(p, q))
15539 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 }
15541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015542 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 }
15544# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015545 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546# endif
15547#endif
15548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550
15551#ifdef HAVE_READLINK
15552fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015553 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556}
15557
15558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 */
15561 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015563 typval_T *argvars;
15564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565{
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 list_T *l;
15567 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568
Bram Moolenaar0d660222005-01-07 21:51:51 +000015569 if (argvars[0].v_type != VAR_LIST)
15570 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015571 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015572 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015573 {
15574 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015575 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015576 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015577 while (li != NULL)
15578 {
15579 ni = li->li_prev;
15580 list_append(l, li);
15581 li = ni;
15582 }
15583 rettv->vval.v_list = l;
15584 rettv->v_type = VAR_LIST;
15585 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015586 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588}
15589
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015590#define SP_NOMOVE 0x01 /* don't move cursor */
15591#define SP_REPEAT 0x02 /* repeat to find outer pair */
15592#define SP_RETCOUNT 0x04 /* return matchcount */
15593#define SP_SETPCMARK 0x08 /* set previous context mark */
15594#define SP_START 0x10 /* accept match at start position */
15595#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15596#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015597
Bram Moolenaar33570922005-01-25 22:26:29 +000015598static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599
15600/*
15601 * Get flags for a search function.
15602 * Possibly sets "p_ws".
15603 * Returns BACKWARD, FORWARD or zero (for an error).
15604 */
15605 static int
15606get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015607 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015608 int *flagsp;
15609{
15610 int dir = FORWARD;
15611 char_u *flags;
15612 char_u nbuf[NUMBUFLEN];
15613 int mask;
15614
15615 if (varp->v_type != VAR_UNKNOWN)
15616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015617 flags = get_tv_string_buf_chk(varp, nbuf);
15618 if (flags == NULL)
15619 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015620 while (*flags != NUL)
15621 {
15622 switch (*flags)
15623 {
15624 case 'b': dir = BACKWARD; break;
15625 case 'w': p_ws = TRUE; break;
15626 case 'W': p_ws = FALSE; break;
15627 default: mask = 0;
15628 if (flagsp != NULL)
15629 switch (*flags)
15630 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015631 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015632 case 'e': mask = SP_END; break;
15633 case 'm': mask = SP_RETCOUNT; break;
15634 case 'n': mask = SP_NOMOVE; break;
15635 case 'p': mask = SP_SUBPAT; break;
15636 case 'r': mask = SP_REPEAT; break;
15637 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015638 }
15639 if (mask == 0)
15640 {
15641 EMSG2(_(e_invarg2), flags);
15642 dir = 0;
15643 }
15644 else
15645 *flagsp |= mask;
15646 }
15647 if (dir == 0)
15648 break;
15649 ++flags;
15650 }
15651 }
15652 return dir;
15653}
15654
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015656 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015658 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015659search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015660 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015661 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015662 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015664 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 char_u *pat;
15666 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015667 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 int save_p_ws = p_ws;
15669 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015670 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015671 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015672 proftime_T tm;
15673#ifdef FEAT_RELTIME
15674 long time_limit = 0;
15675#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015676 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015677 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015680 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015681 if (dir == 0)
15682 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015683 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015684 if (flags & SP_START)
15685 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015686 if (flags & SP_END)
15687 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015688
Bram Moolenaar76929292008-01-06 19:07:36 +000015689 /* Optional arguments: line number to stop searching and timeout. */
15690 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015691 {
15692 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15693 if (lnum_stop < 0)
15694 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015695#ifdef FEAT_RELTIME
15696 if (argvars[3].v_type != VAR_UNKNOWN)
15697 {
15698 time_limit = get_tv_number_chk(&argvars[3], NULL);
15699 if (time_limit < 0)
15700 goto theend;
15701 }
15702#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015703 }
15704
Bram Moolenaar76929292008-01-06 19:07:36 +000015705#ifdef FEAT_RELTIME
15706 /* Set the time limit, if there is one. */
15707 profile_setlimit(time_limit, &tm);
15708#endif
15709
Bram Moolenaar231334e2005-07-25 20:46:57 +000015710 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015711 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015712 * Check to make sure only those flags are set.
15713 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15714 * flags cannot be set. Check for that condition also.
15715 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015716 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015717 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015719 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015720 goto theend;
15721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015723 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015724 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015725 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015726 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015728 if (flags & SP_SUBPAT)
15729 retval = subpatnum;
15730 else
15731 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015732 if (flags & SP_SETPCMARK)
15733 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015735 if (match_pos != NULL)
15736 {
15737 /* Store the match cursor position */
15738 match_pos->lnum = pos.lnum;
15739 match_pos->col = pos.col + 1;
15740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 /* "/$" will put the cursor after the end of the line, may need to
15742 * correct that here */
15743 check_cursor();
15744 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015745
15746 /* If 'n' flag is used: restore cursor position. */
15747 if (flags & SP_NOMOVE)
15748 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015749 else
15750 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015751theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015753
15754 return retval;
15755}
15756
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015757#ifdef FEAT_FLOAT
15758/*
15759 * "round({float})" function
15760 */
15761 static void
15762f_round(argvars, rettv)
15763 typval_T *argvars;
15764 typval_T *rettv;
15765{
15766 float_T f;
15767
15768 rettv->v_type = VAR_FLOAT;
15769 if (get_float_arg(argvars, &f) == OK)
15770 /* round() is not in C90, use ceil() or floor() instead. */
15771 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15772 else
15773 rettv->vval.v_float = 0.0;
15774}
15775#endif
15776
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015777/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015778 * "screencol()" function
15779 *
15780 * First column is 1 to be consistent with virtcol().
15781 */
15782 static void
15783f_screencol(argvars, rettv)
15784 typval_T *argvars UNUSED;
15785 typval_T *rettv;
15786{
15787 rettv->vval.v_number = screen_screencol() + 1;
15788}
15789
15790/*
15791 * "screenrow()" function
15792 */
15793 static void
15794f_screenrow(argvars, rettv)
15795 typval_T *argvars UNUSED;
15796 typval_T *rettv;
15797{
15798 rettv->vval.v_number = screen_screenrow() + 1;
15799}
15800
15801/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015802 * "search()" function
15803 */
15804 static void
15805f_search(argvars, rettv)
15806 typval_T *argvars;
15807 typval_T *rettv;
15808{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015809 int flags = 0;
15810
15811 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812}
15813
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015815 * "searchdecl()" function
15816 */
15817 static void
15818f_searchdecl(argvars, rettv)
15819 typval_T *argvars;
15820 typval_T *rettv;
15821{
15822 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015823 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015824 int error = FALSE;
15825 char_u *name;
15826
15827 rettv->vval.v_number = 1; /* default: FAIL */
15828
15829 name = get_tv_string_chk(&argvars[0]);
15830 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015831 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015832 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015833 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15834 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15835 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015836 if (!error && name != NULL)
15837 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015838 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015839}
15840
15841/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015842 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015844 static int
15845searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015846 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848{
15849 char_u *spat, *mpat, *epat;
15850 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015852 int dir;
15853 int flags = 0;
15854 char_u nbuf1[NUMBUFLEN];
15855 char_u nbuf2[NUMBUFLEN];
15856 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015857 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015858 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015859 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862 spat = get_tv_string_chk(&argvars[0]);
15863 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15864 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15865 if (spat == NULL || mpat == NULL || epat == NULL)
15866 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 /* Handle the optional fourth argument: flags */
15869 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015870 if (dir == 0)
15871 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015872
15873 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015874 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15875 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015876 if ((flags & (SP_END | SP_SUBPAT)) != 0
15877 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015878 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015879 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015880 goto theend;
15881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015882
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015883 /* Using 'r' implies 'W', otherwise it doesn't work. */
15884 if (flags & SP_REPEAT)
15885 p_ws = FALSE;
15886
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015887 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015888 if (argvars[3].v_type == VAR_UNKNOWN
15889 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 skip = (char_u *)"";
15891 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015893 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015894 if (argvars[5].v_type != VAR_UNKNOWN)
15895 {
15896 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15897 if (lnum_stop < 0)
15898 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015899#ifdef FEAT_RELTIME
15900 if (argvars[6].v_type != VAR_UNKNOWN)
15901 {
15902 time_limit = get_tv_number_chk(&argvars[6], NULL);
15903 if (time_limit < 0)
15904 goto theend;
15905 }
15906#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015907 }
15908 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015909 if (skip == NULL)
15910 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015912 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015913 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015914
15915theend:
15916 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015917
15918 return retval;
15919}
15920
15921/*
15922 * "searchpair()" function
15923 */
15924 static void
15925f_searchpair(argvars, rettv)
15926 typval_T *argvars;
15927 typval_T *rettv;
15928{
15929 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15930}
15931
15932/*
15933 * "searchpairpos()" function
15934 */
15935 static void
15936f_searchpairpos(argvars, rettv)
15937 typval_T *argvars;
15938 typval_T *rettv;
15939{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015940 pos_T match_pos;
15941 int lnum = 0;
15942 int col = 0;
15943
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015944 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015945 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015946
15947 if (searchpair_cmn(argvars, &match_pos) > 0)
15948 {
15949 lnum = match_pos.lnum;
15950 col = match_pos.col;
15951 }
15952
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015953 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15954 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015955}
15956
15957/*
15958 * Search for a start/middle/end thing.
15959 * Used by searchpair(), see its documentation for the details.
15960 * Returns 0 or -1 for no match,
15961 */
15962 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015963do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15964 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015965 char_u *spat; /* start pattern */
15966 char_u *mpat; /* middle pattern */
15967 char_u *epat; /* end pattern */
15968 int dir; /* BACKWARD or FORWARD */
15969 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015970 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015971 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015972 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015973 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015974{
15975 char_u *save_cpo;
15976 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15977 long retval = 0;
15978 pos_T pos;
15979 pos_T firstpos;
15980 pos_T foundpos;
15981 pos_T save_cursor;
15982 pos_T save_pos;
15983 int n;
15984 int r;
15985 int nest = 1;
15986 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015987 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015988 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015989
15990 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15991 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015992 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015993
Bram Moolenaar76929292008-01-06 19:07:36 +000015994#ifdef FEAT_RELTIME
15995 /* Set the time limit, if there is one. */
15996 profile_setlimit(time_limit, &tm);
15997#endif
15998
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015999 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16000 * start/middle/end (pat3, for the top pair). */
16001 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16002 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16003 if (pat2 == NULL || pat3 == NULL)
16004 goto theend;
16005 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16006 if (*mpat == NUL)
16007 STRCPY(pat3, pat2);
16008 else
16009 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16010 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016011 if (flags & SP_START)
16012 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016013
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014 save_cursor = curwin->w_cursor;
16015 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016016 clearpos(&firstpos);
16017 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 pat = pat3;
16019 for (;;)
16020 {
16021 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016022 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16024 /* didn't find it or found the first match again: FAIL */
16025 break;
16026
16027 if (firstpos.lnum == 0)
16028 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016029 if (equalpos(pos, foundpos))
16030 {
16031 /* Found the same position again. Can happen with a pattern that
16032 * has "\zs" at the end and searching backwards. Advance one
16033 * character and try again. */
16034 if (dir == BACKWARD)
16035 decl(&pos);
16036 else
16037 incl(&pos);
16038 }
16039 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016041 /* clear the start flag to avoid getting stuck here */
16042 options &= ~SEARCH_START;
16043
Bram Moolenaar071d4272004-06-13 20:20:40 +000016044 /* If the skip pattern matches, ignore this match. */
16045 if (*skip != NUL)
16046 {
16047 save_pos = curwin->w_cursor;
16048 curwin->w_cursor = pos;
16049 r = eval_to_bool(skip, &err, NULL, FALSE);
16050 curwin->w_cursor = save_pos;
16051 if (err)
16052 {
16053 /* Evaluating {skip} caused an error, break here. */
16054 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016055 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 break;
16057 }
16058 if (r)
16059 continue;
16060 }
16061
16062 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16063 {
16064 /* Found end when searching backwards or start when searching
16065 * forward: nested pair. */
16066 ++nest;
16067 pat = pat2; /* nested, don't search for middle */
16068 }
16069 else
16070 {
16071 /* Found end when searching forward or start when searching
16072 * backward: end of (nested) pair; or found middle in outer pair. */
16073 if (--nest == 1)
16074 pat = pat3; /* outer level, search for middle */
16075 }
16076
16077 if (nest == 0)
16078 {
16079 /* Found the match: return matchcount or line number. */
16080 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016081 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016082 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016083 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016084 if (flags & SP_SETPCMARK)
16085 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 curwin->w_cursor = pos;
16087 if (!(flags & SP_REPEAT))
16088 break;
16089 nest = 1; /* search for next unmatched */
16090 }
16091 }
16092
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016093 if (match_pos != NULL)
16094 {
16095 /* Store the match cursor position */
16096 match_pos->lnum = curwin->w_cursor.lnum;
16097 match_pos->col = curwin->w_cursor.col + 1;
16098 }
16099
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016101 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 curwin->w_cursor = save_cursor;
16103
16104theend:
16105 vim_free(pat2);
16106 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016107 if (p_cpo == empty_option)
16108 p_cpo = save_cpo;
16109 else
16110 /* Darn, evaluating the {skip} expression changed the value. */
16111 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016112
16113 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114}
16115
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016116/*
16117 * "searchpos()" function
16118 */
16119 static void
16120f_searchpos(argvars, rettv)
16121 typval_T *argvars;
16122 typval_T *rettv;
16123{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016124 pos_T match_pos;
16125 int lnum = 0;
16126 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016127 int n;
16128 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016129
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016130 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016131 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016132
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016133 n = search_cmn(argvars, &match_pos, &flags);
16134 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135 {
16136 lnum = match_pos.lnum;
16137 col = match_pos.col;
16138 }
16139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016140 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16141 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016142 if (flags & SP_SUBPAT)
16143 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016144}
16145
16146
Bram Moolenaar0d660222005-01-07 21:51:51 +000016147 static void
16148f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016149 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016152#ifdef FEAT_CLIENTSERVER
16153 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016154 char_u *server = get_tv_string_chk(&argvars[0]);
16155 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156
Bram Moolenaar0d660222005-01-07 21:51:51 +000016157 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016158 if (server == NULL || reply == NULL)
16159 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160 if (check_restricted() || check_secure())
16161 return;
16162# ifdef FEAT_X11
16163 if (check_connection() == FAIL)
16164 return;
16165# endif
16166
16167 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169 EMSG(_("E258: Unable to send to client"));
16170 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016172 rettv->vval.v_number = 0;
16173#else
16174 rettv->vval.v_number = -1;
16175#endif
16176}
16177
Bram Moolenaar0d660222005-01-07 21:51:51 +000016178 static void
16179f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016180 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016182{
16183 char_u *r = NULL;
16184
16185#ifdef FEAT_CLIENTSERVER
16186# ifdef WIN32
16187 r = serverGetVimNames();
16188# else
16189 make_connection();
16190 if (X_DISPLAY != NULL)
16191 r = serverGetVimNames(X_DISPLAY);
16192# endif
16193#endif
16194 rettv->v_type = VAR_STRING;
16195 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196}
16197
16198/*
16199 * "setbufvar()" function
16200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016202f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016203 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016204 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205{
16206 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 char_u nbuf[NUMBUFLEN];
16211
16212 if (check_restricted() || check_secure())
16213 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016214 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16215 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016216 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 varp = &argvars[2];
16218
16219 if (buf != NULL && varname != NULL && varp != NULL)
16220 {
16221 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223
16224 if (*varname == '&')
16225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016226 long numval;
16227 char_u *strval;
16228 int error = FALSE;
16229
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016231 numval = get_tv_number_chk(varp, &error);
16232 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016233 if (!error && strval != NULL)
16234 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 }
16236 else
16237 {
16238 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16239 if (bufvarname != NULL)
16240 {
16241 STRCPY(bufvarname, "b:");
16242 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016243 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 vim_free(bufvarname);
16245 }
16246 }
16247
16248 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251}
16252
16253/*
16254 * "setcmdpos()" function
16255 */
16256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 typval_T *argvars;
16259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261 int pos = (int)get_tv_number(&argvars[0]) - 1;
16262
16263 if (pos >= 0)
16264 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265}
16266
16267/*
16268 * "setline()" function
16269 */
16270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016271f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 typval_T *argvars;
16273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274{
16275 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016276 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016277 list_T *l = NULL;
16278 listitem_T *li = NULL;
16279 long added = 0;
16280 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016282 lnum = get_tv_lnum(&argvars[0]);
16283 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016285 l = argvars[1].vval.v_list;
16286 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016288 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016290
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016291 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016292 for (;;)
16293 {
16294 if (l != NULL)
16295 {
16296 /* list argument, get next string */
16297 if (li == NULL)
16298 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016299 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016300 li = li->li_next;
16301 }
16302
16303 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016305 break;
16306 if (lnum <= curbuf->b_ml.ml_line_count)
16307 {
16308 /* existing line, replace it */
16309 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16310 {
16311 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016312 if (lnum == curwin->w_cursor.lnum)
16313 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016314 rettv->vval.v_number = 0; /* OK */
16315 }
16316 }
16317 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16318 {
16319 /* lnum is one past the last line, append the line */
16320 ++added;
16321 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16322 rettv->vval.v_number = 0; /* OK */
16323 }
16324
16325 if (l == NULL) /* only one string argument */
16326 break;
16327 ++lnum;
16328 }
16329
16330 if (added > 0)
16331 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332}
16333
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016334static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16335
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016337 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016338 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016339 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016340set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016341 win_T *wp UNUSED;
16342 typval_T *list_arg UNUSED;
16343 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016344 typval_T *rettv;
16345{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016346#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016347 char_u *act;
16348 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016349#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016350
Bram Moolenaar2641f772005-03-25 21:58:17 +000016351 rettv->vval.v_number = -1;
16352
16353#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016354 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016355 EMSG(_(e_listreq));
16356 else
16357 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016358 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016359
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016360 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016361 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016362 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016363 if (act == NULL)
16364 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016365 if (*act == 'a' || *act == 'r')
16366 action = *act;
16367 }
16368
Bram Moolenaar81484f42012-12-05 15:16:47 +010016369 if (l != NULL && set_errorlist(wp, l, action,
16370 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016371 rettv->vval.v_number = 0;
16372 }
16373#endif
16374}
16375
16376/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016377 * "setloclist()" function
16378 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016379 static void
16380f_setloclist(argvars, rettv)
16381 typval_T *argvars;
16382 typval_T *rettv;
16383{
16384 win_T *win;
16385
16386 rettv->vval.v_number = -1;
16387
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016388 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016389 if (win != NULL)
16390 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16391}
16392
16393/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016394 * "setmatches()" function
16395 */
16396 static void
16397f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016398 typval_T *argvars UNUSED;
16399 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016400{
16401#ifdef FEAT_SEARCH_EXTRA
16402 list_T *l;
16403 listitem_T *li;
16404 dict_T *d;
16405
16406 rettv->vval.v_number = -1;
16407 if (argvars[0].v_type != VAR_LIST)
16408 {
16409 EMSG(_(e_listreq));
16410 return;
16411 }
16412 if ((l = argvars[0].vval.v_list) != NULL)
16413 {
16414
16415 /* To some extent make sure that we are dealing with a list from
16416 * "getmatches()". */
16417 li = l->lv_first;
16418 while (li != NULL)
16419 {
16420 if (li->li_tv.v_type != VAR_DICT
16421 || (d = li->li_tv.vval.v_dict) == NULL)
16422 {
16423 EMSG(_(e_invarg));
16424 return;
16425 }
16426 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16427 && dict_find(d, (char_u *)"pattern", -1) != NULL
16428 && dict_find(d, (char_u *)"priority", -1) != NULL
16429 && dict_find(d, (char_u *)"id", -1) != NULL))
16430 {
16431 EMSG(_(e_invarg));
16432 return;
16433 }
16434 li = li->li_next;
16435 }
16436
16437 clear_matches(curwin);
16438 li = l->lv_first;
16439 while (li != NULL)
16440 {
16441 d = li->li_tv.vval.v_dict;
16442 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16443 get_dict_string(d, (char_u *)"pattern", FALSE),
16444 (int)get_dict_number(d, (char_u *)"priority"),
16445 (int)get_dict_number(d, (char_u *)"id"));
16446 li = li->li_next;
16447 }
16448 rettv->vval.v_number = 0;
16449 }
16450#endif
16451}
16452
16453/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016454 * "setpos()" function
16455 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016456 static void
16457f_setpos(argvars, rettv)
16458 typval_T *argvars;
16459 typval_T *rettv;
16460{
16461 pos_T pos;
16462 int fnum;
16463 char_u *name;
16464
Bram Moolenaar08250432008-02-13 11:42:46 +000016465 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016466 name = get_tv_string_chk(argvars);
16467 if (name != NULL)
16468 {
16469 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16470 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016471 if (--pos.col < 0)
16472 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016473 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016474 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016475 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016476 if (fnum == curbuf->b_fnum)
16477 {
16478 curwin->w_cursor = pos;
16479 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016480 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016481 }
16482 else
16483 EMSG(_(e_invarg));
16484 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016485 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16486 {
16487 /* set mark */
16488 if (setmark_pos(name[1], &pos, fnum) == OK)
16489 rettv->vval.v_number = 0;
16490 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016491 else
16492 EMSG(_(e_invarg));
16493 }
16494 }
16495}
16496
16497/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016498 * "setqflist()" function
16499 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016500 static void
16501f_setqflist(argvars, rettv)
16502 typval_T *argvars;
16503 typval_T *rettv;
16504{
16505 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16506}
16507
16508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016509 * "setreg()" function
16510 */
16511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016512f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 typval_T *argvars;
16514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515{
16516 int regname;
16517 char_u *strregname;
16518 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016519 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 int append;
16521 char_u yank_type;
16522 long block_len;
16523
16524 block_len = -1;
16525 yank_type = MAUTO;
16526 append = FALSE;
16527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016528 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016529 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 if (strregname == NULL)
16532 return; /* type error; errmsg already given */
16533 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016534 if (regname == 0 || regname == '@')
16535 regname = '"';
16536 else if (regname == '=')
16537 return;
16538
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016539 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016541 stropt = get_tv_string_chk(&argvars[2]);
16542 if (stropt == NULL)
16543 return; /* type error */
16544 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545 switch (*stropt)
16546 {
16547 case 'a': case 'A': /* append */
16548 append = TRUE;
16549 break;
16550 case 'v': case 'c': /* character-wise selection */
16551 yank_type = MCHAR;
16552 break;
16553 case 'V': case 'l': /* line-wise selection */
16554 yank_type = MLINE;
16555 break;
16556#ifdef FEAT_VISUAL
16557 case 'b': case Ctrl_V: /* block-wise selection */
16558 yank_type = MBLOCK;
16559 if (VIM_ISDIGIT(stropt[1]))
16560 {
16561 ++stropt;
16562 block_len = getdigits(&stropt) - 1;
16563 --stropt;
16564 }
16565 break;
16566#endif
16567 }
16568 }
16569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 strval = get_tv_string_chk(&argvars[1]);
16571 if (strval != NULL)
16572 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016573 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016574 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575}
16576
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016577/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016578 * "settabvar()" function
16579 */
16580 static void
16581f_settabvar(argvars, rettv)
16582 typval_T *argvars;
16583 typval_T *rettv;
16584{
16585 tabpage_T *save_curtab;
16586 char_u *varname, *tabvarname;
16587 typval_T *varp;
16588 tabpage_T *tp;
16589
16590 rettv->vval.v_number = 0;
16591
16592 if (check_restricted() || check_secure())
16593 return;
16594
16595 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16596 varname = get_tv_string_chk(&argvars[1]);
16597 varp = &argvars[2];
16598
16599 if (tp != NULL && varname != NULL && varp != NULL)
16600 {
16601 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016602 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016603
16604 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16605 if (tabvarname != NULL)
16606 {
16607 STRCPY(tabvarname, "t:");
16608 STRCPY(tabvarname + 2, varname);
16609 set_var(tabvarname, varp, TRUE);
16610 vim_free(tabvarname);
16611 }
16612
16613 /* Restore current tabpage */
16614 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016615 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016616 }
16617}
16618
16619/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016620 * "settabwinvar()" function
16621 */
16622 static void
16623f_settabwinvar(argvars, rettv)
16624 typval_T *argvars;
16625 typval_T *rettv;
16626{
16627 setwinvar(argvars, rettv, 1);
16628}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629
16630/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016631 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016635 typval_T *argvars;
16636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016638 setwinvar(argvars, rettv, 0);
16639}
16640
16641/*
16642 * "setwinvar()" and "settabwinvar()" functions
16643 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016644
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016645 static void
16646setwinvar(argvars, rettv, off)
16647 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016648 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016649 int off;
16650{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 win_T *win;
16652#ifdef FEAT_WINDOWS
16653 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016654 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655#endif
16656 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016657 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016658 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016659 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660
16661 if (check_restricted() || check_secure())
16662 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016663
16664#ifdef FEAT_WINDOWS
16665 if (off == 1)
16666 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16667 else
16668 tp = curtab;
16669#endif
16670 win = find_win_by_nr(&argvars[off], tp);
16671 varname = get_tv_string_chk(&argvars[off + 1]);
16672 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673
16674 if (win != NULL && varname != NULL && varp != NULL)
16675 {
16676#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016677 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016678 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679#endif
16680
16681 if (*varname == '&')
16682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016683 long numval;
16684 char_u *strval;
16685 int error = FALSE;
16686
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016688 numval = get_tv_number_chk(varp, &error);
16689 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016690 if (!error && strval != NULL)
16691 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016692 }
16693 else
16694 {
16695 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16696 if (winvarname != NULL)
16697 {
16698 STRCPY(winvarname, "w:");
16699 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016700 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016701 vim_free(winvarname);
16702 }
16703 }
16704
16705#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016706 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707#endif
16708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709}
16710
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016711#ifdef FEAT_CRYPT
16712/*
16713 * "sha256({string})" function
16714 */
16715 static void
16716f_sha256(argvars, rettv)
16717 typval_T *argvars;
16718 typval_T *rettv;
16719{
16720 char_u *p;
16721
16722 p = get_tv_string(&argvars[0]);
16723 rettv->vval.v_string = vim_strsave(
16724 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16725 rettv->v_type = VAR_STRING;
16726}
16727#endif /* FEAT_CRYPT */
16728
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016730 * "shellescape({string})" function
16731 */
16732 static void
16733f_shellescape(argvars, rettv)
16734 typval_T *argvars;
16735 typval_T *rettv;
16736{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016737 rettv->vval.v_string = vim_strsave_shellescape(
16738 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016739 rettv->v_type = VAR_STRING;
16740}
16741
16742/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016743 * shiftwidth() function
16744 */
16745 static void
16746f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016747 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016748 typval_T *rettv;
16749{
16750 rettv->vval.v_number = get_sw_value();
16751}
16752
16753/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016755 */
16756 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016757f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 typval_T *argvars;
16759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016761 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762
Bram Moolenaar0d660222005-01-07 21:51:51 +000016763 p = get_tv_string(&argvars[0]);
16764 rettv->vval.v_string = vim_strsave(p);
16765 simplify_filename(rettv->vval.v_string); /* simplify in place */
16766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767}
16768
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016769#ifdef FEAT_FLOAT
16770/*
16771 * "sin()" function
16772 */
16773 static void
16774f_sin(argvars, rettv)
16775 typval_T *argvars;
16776 typval_T *rettv;
16777{
16778 float_T f;
16779
16780 rettv->v_type = VAR_FLOAT;
16781 if (get_float_arg(argvars, &f) == OK)
16782 rettv->vval.v_float = sin(f);
16783 else
16784 rettv->vval.v_float = 0.0;
16785}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016786
16787/*
16788 * "sinh()" function
16789 */
16790 static void
16791f_sinh(argvars, rettv)
16792 typval_T *argvars;
16793 typval_T *rettv;
16794{
16795 float_T f;
16796
16797 rettv->v_type = VAR_FLOAT;
16798 if (get_float_arg(argvars, &f) == OK)
16799 rettv->vval.v_float = sinh(f);
16800 else
16801 rettv->vval.v_float = 0.0;
16802}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016803#endif
16804
Bram Moolenaar0d660222005-01-07 21:51:51 +000016805static int
16806#ifdef __BORLANDC__
16807 _RTLENTRYF
16808#endif
16809 item_compare __ARGS((const void *s1, const void *s2));
16810static int
16811#ifdef __BORLANDC__
16812 _RTLENTRYF
16813#endif
16814 item_compare2 __ARGS((const void *s1, const void *s2));
16815
16816static int item_compare_ic;
16817static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016818static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016819static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016820#define ITEM_COMPARE_FAIL 999
16821
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016823 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825 static int
16826#ifdef __BORLANDC__
16827_RTLENTRYF
16828#endif
16829item_compare(s1, s2)
16830 const void *s1;
16831 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833 char_u *p1, *p2;
16834 char_u *tofree1, *tofree2;
16835 int res;
16836 char_u numbuf1[NUMBUFLEN];
16837 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016839 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16840 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016841 if (p1 == NULL)
16842 p1 = (char_u *)"";
16843 if (p2 == NULL)
16844 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016845 if (item_compare_ic)
16846 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016848 res = STRCMP(p1, p2);
16849 vim_free(tofree1);
16850 vim_free(tofree2);
16851 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852}
16853
16854 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855#ifdef __BORLANDC__
16856_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016858item_compare2(s1, s2)
16859 const void *s1;
16860 const void *s2;
16861{
16862 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016863 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016864 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 /* shortcut after failure in previous call; compare all items equal */
16868 if (item_compare_func_err)
16869 return 0;
16870
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016871 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16872 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016873 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16874 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875
16876 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016877 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016878 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16879 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880 clear_tv(&argv[0]);
16881 clear_tv(&argv[1]);
16882
16883 if (res == FAIL)
16884 res = ITEM_COMPARE_FAIL;
16885 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016886 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16887 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016888 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 clear_tv(&rettv);
16890 return res;
16891}
16892
16893/*
16894 * "sort({list})" function
16895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016898 typval_T *argvars;
16899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900{
Bram Moolenaar33570922005-01-25 22:26:29 +000016901 list_T *l;
16902 listitem_T *li;
16903 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016904 long len;
16905 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 if (argvars[0].v_type != VAR_LIST)
16908 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016909 else
16910 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016912 if (l == NULL || tv_check_lock(l->lv_lock,
16913 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 return;
16915 rettv->vval.v_list = l;
16916 rettv->v_type = VAR_LIST;
16917 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919 len = list_len(l);
16920 if (len <= 1)
16921 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 item_compare_ic = FALSE;
16924 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016925 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016926 if (argvars[1].v_type != VAR_UNKNOWN)
16927 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016928 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 else
16932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 int error = FALSE;
16934
16935 i = get_tv_number_chk(&argvars[1], &error);
16936 if (error)
16937 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938 if (i == 1)
16939 item_compare_ic = TRUE;
16940 else
16941 item_compare_func = get_tv_string(&argvars[1]);
16942 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016943
16944 if (argvars[2].v_type != VAR_UNKNOWN)
16945 {
16946 /* optional third argument: {dict} */
16947 if (argvars[2].v_type != VAR_DICT)
16948 {
16949 EMSG(_(e_dictreq));
16950 return;
16951 }
16952 item_compare_selfdict = argvars[2].vval.v_dict;
16953 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 if (ptrs == NULL)
16959 return;
16960 i = 0;
16961 for (li = l->lv_first; li != NULL; li = li->li_next)
16962 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 /* test the compare function */
16966 if (item_compare_func != NULL
16967 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16968 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016969 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 {
16972 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 item_compare_func == NULL ? item_compare : item_compare2);
16975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016976 if (!item_compare_func_err)
16977 {
16978 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016979 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980 l->lv_len = 0;
16981 for (i = 0; i < len; ++i)
16982 list_append(l, ptrs[i]);
16983 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 }
16985
16986 vim_free(ptrs);
16987 }
16988}
16989
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016990/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016991 * "soundfold({word})" function
16992 */
16993 static void
16994f_soundfold(argvars, rettv)
16995 typval_T *argvars;
16996 typval_T *rettv;
16997{
16998 char_u *s;
16999
17000 rettv->v_type = VAR_STRING;
17001 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017002#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017003 rettv->vval.v_string = eval_soundfold(s);
17004#else
17005 rettv->vval.v_string = vim_strsave(s);
17006#endif
17007}
17008
17009/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017010 * "spellbadword()" function
17011 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017012 static void
17013f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017014 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017015 typval_T *rettv;
17016{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017017 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017018 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017019 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017020
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017021 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017022 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017023
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017024#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017025 if (argvars[0].v_type == VAR_UNKNOWN)
17026 {
17027 /* Find the start and length of the badly spelled word. */
17028 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17029 if (len != 0)
17030 word = ml_get_cursor();
17031 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017032 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017033 {
17034 char_u *str = get_tv_string_chk(&argvars[0]);
17035 int capcol = -1;
17036
17037 if (str != NULL)
17038 {
17039 /* Check the argument for spelling. */
17040 while (*str != NUL)
17041 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017042 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017043 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017044 {
17045 word = str;
17046 break;
17047 }
17048 str += len;
17049 }
17050 }
17051 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017052#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017053
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017054 list_append_string(rettv->vval.v_list, word, len);
17055 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017056 attr == HLF_SPB ? "bad" :
17057 attr == HLF_SPR ? "rare" :
17058 attr == HLF_SPL ? "local" :
17059 attr == HLF_SPC ? "caps" :
17060 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017061}
17062
17063/*
17064 * "spellsuggest()" function
17065 */
17066 static void
17067f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017068 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017069 typval_T *rettv;
17070{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017071#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017072 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017073 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017074 int maxcount;
17075 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017076 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017077 listitem_T *li;
17078 int need_capital = FALSE;
17079#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017080
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017081 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017082 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017083
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017084#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017085 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017086 {
17087 str = get_tv_string(&argvars[0]);
17088 if (argvars[1].v_type != VAR_UNKNOWN)
17089 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017090 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017091 if (maxcount <= 0)
17092 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017093 if (argvars[2].v_type != VAR_UNKNOWN)
17094 {
17095 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17096 if (typeerr)
17097 return;
17098 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017099 }
17100 else
17101 maxcount = 25;
17102
Bram Moolenaar4770d092006-01-12 23:22:24 +000017103 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017104
17105 for (i = 0; i < ga.ga_len; ++i)
17106 {
17107 str = ((char_u **)ga.ga_data)[i];
17108
17109 li = listitem_alloc();
17110 if (li == NULL)
17111 vim_free(str);
17112 else
17113 {
17114 li->li_tv.v_type = VAR_STRING;
17115 li->li_tv.v_lock = 0;
17116 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017117 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017118 }
17119 }
17120 ga_clear(&ga);
17121 }
17122#endif
17123}
17124
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017126f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017127 typval_T *argvars;
17128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129{
17130 char_u *str;
17131 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017132 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133 regmatch_T regmatch;
17134 char_u patbuf[NUMBUFLEN];
17135 char_u *save_cpo;
17136 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017138 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017139 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140
17141 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17142 save_cpo = p_cpo;
17143 p_cpo = (char_u *)"";
17144
17145 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017146 if (argvars[1].v_type != VAR_UNKNOWN)
17147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17149 if (pat == NULL)
17150 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017151 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017153 }
17154 if (pat == NULL || *pat == NUL)
17155 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017157 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017159 if (typeerr)
17160 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17163 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017166 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017167 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017168 if (*str == NUL)
17169 match = FALSE; /* empty item at the end */
17170 else
17171 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172 if (match)
17173 end = regmatch.startp[0];
17174 else
17175 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017176 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17177 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017179 if (list_append_string(rettv->vval.v_list, str,
17180 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182 }
17183 if (!match)
17184 break;
17185 /* Advance to just after the match. */
17186 if (regmatch.endp[0] > str)
17187 col = 0;
17188 else
17189 {
17190 /* Don't get stuck at the same match. */
17191#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017192 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193#else
17194 col = 1;
17195#endif
17196 }
17197 str = regmatch.endp[0];
17198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
Bram Moolenaar0d660222005-01-07 21:51:51 +000017203 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204}
17205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017206#ifdef FEAT_FLOAT
17207/*
17208 * "sqrt()" function
17209 */
17210 static void
17211f_sqrt(argvars, rettv)
17212 typval_T *argvars;
17213 typval_T *rettv;
17214{
17215 float_T f;
17216
17217 rettv->v_type = VAR_FLOAT;
17218 if (get_float_arg(argvars, &f) == OK)
17219 rettv->vval.v_float = sqrt(f);
17220 else
17221 rettv->vval.v_float = 0.0;
17222}
17223
17224/*
17225 * "str2float()" function
17226 */
17227 static void
17228f_str2float(argvars, rettv)
17229 typval_T *argvars;
17230 typval_T *rettv;
17231{
17232 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17233
17234 if (*p == '+')
17235 p = skipwhite(p + 1);
17236 (void)string2float(p, &rettv->vval.v_float);
17237 rettv->v_type = VAR_FLOAT;
17238}
17239#endif
17240
Bram Moolenaar2c932302006-03-18 21:42:09 +000017241/*
17242 * "str2nr()" function
17243 */
17244 static void
17245f_str2nr(argvars, rettv)
17246 typval_T *argvars;
17247 typval_T *rettv;
17248{
17249 int base = 10;
17250 char_u *p;
17251 long n;
17252
17253 if (argvars[1].v_type != VAR_UNKNOWN)
17254 {
17255 base = get_tv_number(&argvars[1]);
17256 if (base != 8 && base != 10 && base != 16)
17257 {
17258 EMSG(_(e_invarg));
17259 return;
17260 }
17261 }
17262
17263 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017264 if (*p == '+')
17265 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017266 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17267 rettv->vval.v_number = n;
17268}
17269
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270#ifdef HAVE_STRFTIME
17271/*
17272 * "strftime({format}[, {time}])" function
17273 */
17274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017275f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 typval_T *argvars;
17277 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278{
17279 char_u result_buf[256];
17280 struct tm *curtime;
17281 time_t seconds;
17282 char_u *p;
17283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017284 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017286 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017287 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 seconds = time(NULL);
17289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017290 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 curtime = localtime(&seconds);
17292 /* MSVC returns NULL for an invalid value of seconds. */
17293 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017294 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 else
17296 {
17297# ifdef FEAT_MBYTE
17298 vimconv_T conv;
17299 char_u *enc;
17300
17301 conv.vc_type = CONV_NONE;
17302 enc = enc_locale();
17303 convert_setup(&conv, p_enc, enc);
17304 if (conv.vc_type != CONV_NONE)
17305 p = string_convert(&conv, p, NULL);
17306# endif
17307 if (p != NULL)
17308 (void)strftime((char *)result_buf, sizeof(result_buf),
17309 (char *)p, curtime);
17310 else
17311 result_buf[0] = NUL;
17312
17313# ifdef FEAT_MBYTE
17314 if (conv.vc_type != CONV_NONE)
17315 vim_free(p);
17316 convert_setup(&conv, enc, p_enc);
17317 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017318 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 else
17320# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322
17323# ifdef FEAT_MBYTE
17324 /* Release conversion descriptors */
17325 convert_setup(&conv, NULL, NULL);
17326 vim_free(enc);
17327# endif
17328 }
17329}
17330#endif
17331
17332/*
17333 * "stridx()" function
17334 */
17335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017337 typval_T *argvars;
17338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339{
17340 char_u buf[NUMBUFLEN];
17341 char_u *needle;
17342 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017343 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017345 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017347 needle = get_tv_string_chk(&argvars[1]);
17348 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017350 if (needle == NULL || haystack == NULL)
17351 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352
Bram Moolenaar33570922005-01-25 22:26:29 +000017353 if (argvars[2].v_type != VAR_UNKNOWN)
17354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017355 int error = FALSE;
17356
17357 start_idx = get_tv_number_chk(&argvars[2], &error);
17358 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017359 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017360 if (start_idx >= 0)
17361 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 }
17363
17364 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17365 if (pos != NULL)
17366 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367}
17368
17369/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017370 * "string()" function
17371 */
17372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017374 typval_T *argvars;
17375 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017376{
17377 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017378 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017381 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017382 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017383 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017384 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385}
17386
17387/*
17388 * "strlen()" function
17389 */
17390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017391f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017392 typval_T *argvars;
17393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017395 rettv->vval.v_number = (varnumber_T)(STRLEN(
17396 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397}
17398
17399/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017400 * "strchars()" function
17401 */
17402 static void
17403f_strchars(argvars, rettv)
17404 typval_T *argvars;
17405 typval_T *rettv;
17406{
17407 char_u *s = get_tv_string(&argvars[0]);
17408#ifdef FEAT_MBYTE
17409 varnumber_T len = 0;
17410
17411 while (*s != NUL)
17412 {
17413 mb_cptr2char_adv(&s);
17414 ++len;
17415 }
17416 rettv->vval.v_number = len;
17417#else
17418 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17419#endif
17420}
17421
17422/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017423 * "strdisplaywidth()" function
17424 */
17425 static void
17426f_strdisplaywidth(argvars, rettv)
17427 typval_T *argvars;
17428 typval_T *rettv;
17429{
17430 char_u *s = get_tv_string(&argvars[0]);
17431 int col = 0;
17432
17433 if (argvars[1].v_type != VAR_UNKNOWN)
17434 col = get_tv_number(&argvars[1]);
17435
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017436 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017437}
17438
17439/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017440 * "strwidth()" function
17441 */
17442 static void
17443f_strwidth(argvars, rettv)
17444 typval_T *argvars;
17445 typval_T *rettv;
17446{
17447 char_u *s = get_tv_string(&argvars[0]);
17448
17449 rettv->vval.v_number = (varnumber_T)(
17450#ifdef FEAT_MBYTE
17451 mb_string2cells(s, -1)
17452#else
17453 STRLEN(s)
17454#endif
17455 );
17456}
17457
17458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 * "strpart()" function
17460 */
17461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017462f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 typval_T *argvars;
17464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465{
17466 char_u *p;
17467 int n;
17468 int len;
17469 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017470 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017472 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 slen = (int)STRLEN(p);
17474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017475 n = get_tv_number_chk(&argvars[1], &error);
17476 if (error)
17477 len = 0;
17478 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 else
17481 len = slen - n; /* default len: all bytes that are available. */
17482
17483 /*
17484 * Only return the overlap between the specified part and the actual
17485 * string.
17486 */
17487 if (n < 0)
17488 {
17489 len += n;
17490 n = 0;
17491 }
17492 else if (n > slen)
17493 n = slen;
17494 if (len < 0)
17495 len = 0;
17496 else if (n + len > slen)
17497 len = slen - n;
17498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499 rettv->v_type = VAR_STRING;
17500 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
17503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017504 * "strridx()" function
17505 */
17506 static void
17507f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017508 typval_T *argvars;
17509 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510{
17511 char_u buf[NUMBUFLEN];
17512 char_u *needle;
17513 char_u *haystack;
17514 char_u *rest;
17515 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017516 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017518 needle = get_tv_string_chk(&argvars[1]);
17519 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017520
17521 rettv->vval.v_number = -1;
17522 if (needle == NULL || haystack == NULL)
17523 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017524
17525 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017526 if (argvars[2].v_type != VAR_UNKNOWN)
17527 {
17528 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017529 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017530 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017531 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017532 }
17533 else
17534 end_idx = haystack_len;
17535
Bram Moolenaar0d660222005-01-07 21:51:51 +000017536 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017537 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017539 lastmatch = haystack + end_idx;
17540 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017541 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017542 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017543 for (rest = haystack; *rest != '\0'; ++rest)
17544 {
17545 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017546 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017547 break;
17548 lastmatch = rest;
17549 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017550 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551
17552 if (lastmatch == NULL)
17553 rettv->vval.v_number = -1;
17554 else
17555 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17556}
17557
17558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 * "strtrans()" function
17560 */
17561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017562f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017563 typval_T *argvars;
17564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017566 rettv->v_type = VAR_STRING;
17567 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568}
17569
17570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017571 * "submatch()" function
17572 */
17573 static void
17574f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017575 typval_T *argvars;
17576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017577{
17578 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017579 rettv->vval.v_string =
17580 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017581}
17582
17583/*
17584 * "substitute()" function
17585 */
17586 static void
17587f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017588 typval_T *argvars;
17589 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017590{
17591 char_u patbuf[NUMBUFLEN];
17592 char_u subbuf[NUMBUFLEN];
17593 char_u flagsbuf[NUMBUFLEN];
17594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017595 char_u *str = get_tv_string_chk(&argvars[0]);
17596 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17597 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17598 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17599
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017601 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17602 rettv->vval.v_string = NULL;
17603 else
17604 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017605}
17606
17607/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017608 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017611f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017612 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
17615 int id = 0;
17616#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017617 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 long col;
17619 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017620 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017622 lnum = get_tv_lnum(argvars); /* -1 on type error */
17623 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17624 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017626 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017627 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017628 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629#endif
17630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017631 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632}
17633
17634/*
17635 * "synIDattr(id, what [, mode])" function
17636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017638f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641{
17642 char_u *p = NULL;
17643#ifdef FEAT_SYN_HL
17644 int id;
17645 char_u *what;
17646 char_u *mode;
17647 char_u modebuf[NUMBUFLEN];
17648 int modec;
17649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 id = get_tv_number(&argvars[0]);
17651 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017652 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017656 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 modec = 0; /* replace invalid with current */
17658 }
17659 else
17660 {
17661#ifdef FEAT_GUI
17662 if (gui.in_use)
17663 modec = 'g';
17664 else
17665#endif
17666 if (t_colors > 1)
17667 modec = 'c';
17668 else
17669 modec = 't';
17670 }
17671
17672
17673 switch (TOLOWER_ASC(what[0]))
17674 {
17675 case 'b':
17676 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17677 p = highlight_color(id, what, modec);
17678 else /* bold */
17679 p = highlight_has_attr(id, HL_BOLD, modec);
17680 break;
17681
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017682 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 p = highlight_color(id, what, modec);
17684 break;
17685
17686 case 'i':
17687 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17688 p = highlight_has_attr(id, HL_INVERSE, modec);
17689 else /* italic */
17690 p = highlight_has_attr(id, HL_ITALIC, modec);
17691 break;
17692
17693 case 'n': /* name */
17694 p = get_highlight_name(NULL, id - 1);
17695 break;
17696
17697 case 'r': /* reverse */
17698 p = highlight_has_attr(id, HL_INVERSE, modec);
17699 break;
17700
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017701 case 's':
17702 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17703 p = highlight_color(id, what, modec);
17704 else /* standout */
17705 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 break;
17707
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017708 case 'u':
17709 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17710 /* underline */
17711 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17712 else
17713 /* undercurl */
17714 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 break;
17716 }
17717
17718 if (p != NULL)
17719 p = vim_strsave(p);
17720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017721 rettv->v_type = VAR_STRING;
17722 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723}
17724
17725/*
17726 * "synIDtrans(id)" function
17727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017729f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732{
17733 int id;
17734
17735#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017736 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
17738 if (id > 0)
17739 id = syn_get_final_id(id);
17740 else
17741#endif
17742 id = 0;
17743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017744 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745}
17746
17747/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017748 * "synconcealed(lnum, col)" function
17749 */
17750 static void
17751f_synconcealed(argvars, rettv)
17752 typval_T *argvars UNUSED;
17753 typval_T *rettv;
17754{
17755#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17756 long lnum;
17757 long col;
17758 int syntax_flags = 0;
17759 int cchar;
17760 int matchid = 0;
17761 char_u str[NUMBUFLEN];
17762#endif
17763
17764 rettv->v_type = VAR_LIST;
17765 rettv->vval.v_list = NULL;
17766
17767#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17768 lnum = get_tv_lnum(argvars); /* -1 on type error */
17769 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17770
17771 vim_memset(str, NUL, sizeof(str));
17772
17773 if (rettv_list_alloc(rettv) != FAIL)
17774 {
17775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17776 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17777 && curwin->w_p_cole > 0)
17778 {
17779 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17780 syntax_flags = get_syntax_info(&matchid);
17781
17782 /* get the conceal character */
17783 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17784 {
17785 cchar = syn_get_sub_char();
17786 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17787 cchar = lcs_conceal;
17788 if (cchar != NUL)
17789 {
17790# ifdef FEAT_MBYTE
17791 if (has_mbyte)
17792 (*mb_char2bytes)(cchar, str);
17793 else
17794# endif
17795 str[0] = cchar;
17796 }
17797 }
17798 }
17799
17800 list_append_number(rettv->vval.v_list,
17801 (syntax_flags & HL_CONCEAL) != 0);
17802 /* -1 to auto-determine strlen */
17803 list_append_string(rettv->vval.v_list, str, -1);
17804 list_append_number(rettv->vval.v_list, matchid);
17805 }
17806#endif
17807}
17808
17809/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017810 * "synstack(lnum, col)" function
17811 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017812 static void
17813f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017814 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017815 typval_T *rettv;
17816{
17817#ifdef FEAT_SYN_HL
17818 long lnum;
17819 long col;
17820 int i;
17821 int id;
17822#endif
17823
17824 rettv->v_type = VAR_LIST;
17825 rettv->vval.v_list = NULL;
17826
17827#ifdef FEAT_SYN_HL
17828 lnum = get_tv_lnum(argvars); /* -1 on type error */
17829 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17830
17831 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017832 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017833 && rettv_list_alloc(rettv) != FAIL)
17834 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017835 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017836 for (i = 0; ; ++i)
17837 {
17838 id = syn_get_stack_item(i);
17839 if (id < 0)
17840 break;
17841 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17842 break;
17843 }
17844 }
17845#endif
17846}
17847
17848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 * "system()" function
17850 */
17851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017853 typval_T *argvars;
17854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017856 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017858 char_u *infile = NULL;
17859 char_u buf[NUMBUFLEN];
17860 int err = FALSE;
17861 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017863 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017864 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017865
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017866 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017867 {
17868 /*
17869 * Write the string to a temp file, to be used for input of the shell
17870 * command.
17871 */
17872 if ((infile = vim_tempname('i')) == NULL)
17873 {
17874 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017875 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017876 }
17877
17878 fd = mch_fopen((char *)infile, WRITEBIN);
17879 if (fd == NULL)
17880 {
17881 EMSG2(_(e_notopen), infile);
17882 goto done;
17883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017884 p = get_tv_string_buf_chk(&argvars[1], buf);
17885 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017886 {
17887 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017888 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017889 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017890 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17891 err = TRUE;
17892 if (fclose(fd) != 0)
17893 err = TRUE;
17894 if (err)
17895 {
17896 EMSG(_("E677: Error writing temp file"));
17897 goto done;
17898 }
17899 }
17900
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017901 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17902 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017903
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904#ifdef USE_CR
17905 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017906 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 {
17908 char_u *s;
17909
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017910 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 {
17912 if (*s == CAR)
17913 *s = NL;
17914 }
17915 }
17916#else
17917# ifdef USE_CRNL
17918 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017919 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920 {
17921 char_u *s, *d;
17922
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017923 d = res;
17924 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 {
17926 if (s[0] == CAR && s[1] == NL)
17927 ++s;
17928 *d++ = *s;
17929 }
17930 *d = NUL;
17931 }
17932# endif
17933#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017934
17935done:
17936 if (infile != NULL)
17937 {
17938 mch_remove(infile);
17939 vim_free(infile);
17940 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017941 rettv->v_type = VAR_STRING;
17942 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943}
17944
17945/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017946 * "tabpagebuflist()" function
17947 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017948 static void
17949f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017950 typval_T *argvars UNUSED;
17951 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017952{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017953#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017954 tabpage_T *tp;
17955 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017956
17957 if (argvars[0].v_type == VAR_UNKNOWN)
17958 wp = firstwin;
17959 else
17960 {
17961 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17962 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017963 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017964 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017965 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017966 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017967 for (; wp != NULL; wp = wp->w_next)
17968 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017969 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017970 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017971 }
17972#endif
17973}
17974
17975
17976/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017977 * "tabpagenr()" function
17978 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017979 static void
17980f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017981 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017982 typval_T *rettv;
17983{
17984 int nr = 1;
17985#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017986 char_u *arg;
17987
17988 if (argvars[0].v_type != VAR_UNKNOWN)
17989 {
17990 arg = get_tv_string_chk(&argvars[0]);
17991 nr = 0;
17992 if (arg != NULL)
17993 {
17994 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017995 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017996 else
17997 EMSG2(_(e_invexpr2), arg);
17998 }
17999 }
18000 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018001 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018002#endif
18003 rettv->vval.v_number = nr;
18004}
18005
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018006
18007#ifdef FEAT_WINDOWS
18008static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18009
18010/*
18011 * Common code for tabpagewinnr() and winnr().
18012 */
18013 static int
18014get_winnr(tp, argvar)
18015 tabpage_T *tp;
18016 typval_T *argvar;
18017{
18018 win_T *twin;
18019 int nr = 1;
18020 win_T *wp;
18021 char_u *arg;
18022
18023 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18024 if (argvar->v_type != VAR_UNKNOWN)
18025 {
18026 arg = get_tv_string_chk(argvar);
18027 if (arg == NULL)
18028 nr = 0; /* type error; errmsg already given */
18029 else if (STRCMP(arg, "$") == 0)
18030 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18031 else if (STRCMP(arg, "#") == 0)
18032 {
18033 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18034 if (twin == NULL)
18035 nr = 0;
18036 }
18037 else
18038 {
18039 EMSG2(_(e_invexpr2), arg);
18040 nr = 0;
18041 }
18042 }
18043
18044 if (nr > 0)
18045 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18046 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018047 {
18048 if (wp == NULL)
18049 {
18050 /* didn't find it in this tabpage */
18051 nr = 0;
18052 break;
18053 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018054 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018055 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018056 return nr;
18057}
18058#endif
18059
18060/*
18061 * "tabpagewinnr()" function
18062 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018063 static void
18064f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018065 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018066 typval_T *rettv;
18067{
18068 int nr = 1;
18069#ifdef FEAT_WINDOWS
18070 tabpage_T *tp;
18071
18072 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18073 if (tp == NULL)
18074 nr = 0;
18075 else
18076 nr = get_winnr(tp, &argvars[1]);
18077#endif
18078 rettv->vval.v_number = nr;
18079}
18080
18081
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018082/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018083 * "tagfiles()" function
18084 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018085 static void
18086f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018087 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018088 typval_T *rettv;
18089{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018090 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018091 tagname_T tn;
18092 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018094 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018095 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018096 fname = alloc(MAXPATHL);
18097 if (fname == NULL)
18098 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018099
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018100 for (first = TRUE; ; first = FALSE)
18101 if (get_tagfname(&tn, first, fname) == FAIL
18102 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018103 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018104 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018105 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018106}
18107
18108/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018109 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018110 */
18111 static void
18112f_taglist(argvars, rettv)
18113 typval_T *argvars;
18114 typval_T *rettv;
18115{
18116 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018117
18118 tag_pattern = get_tv_string(&argvars[0]);
18119
18120 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018121 if (*tag_pattern == NUL)
18122 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018124 if (rettv_list_alloc(rettv) == OK)
18125 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018126}
18127
18128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 * "tempname()" function
18130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018133 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135{
18136 static int x = 'A';
18137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018138 rettv->v_type = VAR_STRING;
18139 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
18141 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18142 * names. Skip 'I' and 'O', they are used for shell redirection. */
18143 do
18144 {
18145 if (x == 'Z')
18146 x = '0';
18147 else if (x == '9')
18148 x = 'A';
18149 else
18150 {
18151#ifdef EBCDIC
18152 if (x == 'I')
18153 x = 'J';
18154 else if (x == 'R')
18155 x = 'S';
18156 else
18157#endif
18158 ++x;
18159 }
18160 } while (x == 'I' || x == 'O');
18161}
18162
18163/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018164 * "test(list)" function: Just checking the walls...
18165 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018166 static void
18167f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018168 typval_T *argvars UNUSED;
18169 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018170{
18171 /* Used for unit testing. Change the code below to your liking. */
18172#if 0
18173 listitem_T *li;
18174 list_T *l;
18175 char_u *bad, *good;
18176
18177 if (argvars[0].v_type != VAR_LIST)
18178 return;
18179 l = argvars[0].vval.v_list;
18180 if (l == NULL)
18181 return;
18182 li = l->lv_first;
18183 if (li == NULL)
18184 return;
18185 bad = get_tv_string(&li->li_tv);
18186 li = li->li_next;
18187 if (li == NULL)
18188 return;
18189 good = get_tv_string(&li->li_tv);
18190 rettv->vval.v_number = test_edit_score(bad, good);
18191#endif
18192}
18193
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018194#ifdef FEAT_FLOAT
18195/*
18196 * "tan()" function
18197 */
18198 static void
18199f_tan(argvars, rettv)
18200 typval_T *argvars;
18201 typval_T *rettv;
18202{
18203 float_T f;
18204
18205 rettv->v_type = VAR_FLOAT;
18206 if (get_float_arg(argvars, &f) == OK)
18207 rettv->vval.v_float = tan(f);
18208 else
18209 rettv->vval.v_float = 0.0;
18210}
18211
18212/*
18213 * "tanh()" function
18214 */
18215 static void
18216f_tanh(argvars, rettv)
18217 typval_T *argvars;
18218 typval_T *rettv;
18219{
18220 float_T f;
18221
18222 rettv->v_type = VAR_FLOAT;
18223 if (get_float_arg(argvars, &f) == OK)
18224 rettv->vval.v_float = tanh(f);
18225 else
18226 rettv->vval.v_float = 0.0;
18227}
18228#endif
18229
Bram Moolenaard52d9742005-08-21 22:20:28 +000018230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 * "tolower(string)" function
18232 */
18233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018234f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018235 typval_T *argvars;
18236 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237{
18238 char_u *p;
18239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240 p = vim_strsave(get_tv_string(&argvars[0]));
18241 rettv->v_type = VAR_STRING;
18242 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243
18244 if (p != NULL)
18245 while (*p != NUL)
18246 {
18247#ifdef FEAT_MBYTE
18248 int l;
18249
18250 if (enc_utf8)
18251 {
18252 int c, lc;
18253
18254 c = utf_ptr2char(p);
18255 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018256 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 /* TODO: reallocate string when byte count changes. */
18258 if (utf_char2len(lc) == l)
18259 utf_char2bytes(lc, p);
18260 p += l;
18261 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018262 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 p += l; /* skip multi-byte character */
18264 else
18265#endif
18266 {
18267 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18268 ++p;
18269 }
18270 }
18271}
18272
18273/*
18274 * "toupper(string)" function
18275 */
18276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018277f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018278 typval_T *argvars;
18279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018281 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018282 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283}
18284
18285/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018286 * "tr(string, fromstr, tostr)" function
18287 */
18288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018289f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018290 typval_T *argvars;
18291 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018292{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018293 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018294 char_u *fromstr;
18295 char_u *tostr;
18296 char_u *p;
18297#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018298 int inlen;
18299 int fromlen;
18300 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018301 int idx;
18302 char_u *cpstr;
18303 int cplen;
18304 int first = TRUE;
18305#endif
18306 char_u buf[NUMBUFLEN];
18307 char_u buf2[NUMBUFLEN];
18308 garray_T ga;
18309
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018310 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18312 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018313
18314 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315 rettv->v_type = VAR_STRING;
18316 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 if (fromstr == NULL || tostr == NULL)
18318 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018319 ga_init2(&ga, (int)sizeof(char), 80);
18320
18321#ifdef FEAT_MBYTE
18322 if (!has_mbyte)
18323#endif
18324 /* not multi-byte: fromstr and tostr must be the same length */
18325 if (STRLEN(fromstr) != STRLEN(tostr))
18326 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018327#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018328error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018329#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018330 EMSG2(_(e_invarg2), fromstr);
18331 ga_clear(&ga);
18332 return;
18333 }
18334
18335 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018336 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018337 {
18338#ifdef FEAT_MBYTE
18339 if (has_mbyte)
18340 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018341 inlen = (*mb_ptr2len)(in_str);
18342 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018343 cplen = inlen;
18344 idx = 0;
18345 for (p = fromstr; *p != NUL; p += fromlen)
18346 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018347 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018348 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018349 {
18350 for (p = tostr; *p != NUL; p += tolen)
18351 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018352 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018353 if (idx-- == 0)
18354 {
18355 cplen = tolen;
18356 cpstr = p;
18357 break;
18358 }
18359 }
18360 if (*p == NUL) /* tostr is shorter than fromstr */
18361 goto error;
18362 break;
18363 }
18364 ++idx;
18365 }
18366
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018367 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018368 {
18369 /* Check that fromstr and tostr have the same number of
18370 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018371 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018372 first = FALSE;
18373 for (p = tostr; *p != NUL; p += tolen)
18374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018375 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018376 --idx;
18377 }
18378 if (idx != 0)
18379 goto error;
18380 }
18381
18382 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018383 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018384 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018385
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018386 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018387 }
18388 else
18389#endif
18390 {
18391 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018392 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018393 if (p != NULL)
18394 ga_append(&ga, tostr[p - fromstr]);
18395 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018396 ga_append(&ga, *in_str);
18397 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018398 }
18399 }
18400
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018401 /* add a terminating NUL */
18402 ga_grow(&ga, 1);
18403 ga_append(&ga, NUL);
18404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018405 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018406}
18407
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018408#ifdef FEAT_FLOAT
18409/*
18410 * "trunc({float})" function
18411 */
18412 static void
18413f_trunc(argvars, rettv)
18414 typval_T *argvars;
18415 typval_T *rettv;
18416{
18417 float_T f;
18418
18419 rettv->v_type = VAR_FLOAT;
18420 if (get_float_arg(argvars, &f) == OK)
18421 /* trunc() is not in C90, use floor() or ceil() instead. */
18422 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18423 else
18424 rettv->vval.v_float = 0.0;
18425}
18426#endif
18427
Bram Moolenaar8299df92004-07-10 09:47:34 +000018428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 * "type(expr)" function
18430 */
18431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018433 typval_T *argvars;
18434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018436 int n;
18437
18438 switch (argvars[0].v_type)
18439 {
18440 case VAR_NUMBER: n = 0; break;
18441 case VAR_STRING: n = 1; break;
18442 case VAR_FUNC: n = 2; break;
18443 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018444 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018445#ifdef FEAT_FLOAT
18446 case VAR_FLOAT: n = 5; break;
18447#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018448 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18449 }
18450 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451}
18452
18453/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018454 * "undofile(name)" function
18455 */
18456 static void
18457f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018458 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018459 typval_T *rettv;
18460{
18461 rettv->v_type = VAR_STRING;
18462#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018463 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018464 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018465
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018466 if (*fname == NUL)
18467 {
18468 /* If there is no file name there will be no undo file. */
18469 rettv->vval.v_string = NULL;
18470 }
18471 else
18472 {
18473 char_u *ffname = FullName_save(fname, FALSE);
18474
18475 if (ffname != NULL)
18476 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18477 vim_free(ffname);
18478 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018479 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018480#else
18481 rettv->vval.v_string = NULL;
18482#endif
18483}
18484
18485/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018486 * "undotree()" function
18487 */
18488 static void
18489f_undotree(argvars, rettv)
18490 typval_T *argvars UNUSED;
18491 typval_T *rettv;
18492{
18493 if (rettv_dict_alloc(rettv) == OK)
18494 {
18495 dict_T *dict = rettv->vval.v_dict;
18496 list_T *list;
18497
Bram Moolenaar730cde92010-06-27 05:18:54 +020018498 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018499 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018500 dict_add_nr_str(dict, "save_last",
18501 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018502 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18503 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018504 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018505
18506 list = list_alloc();
18507 if (list != NULL)
18508 {
18509 u_eval_tree(curbuf->b_u_oldhead, list);
18510 dict_add_list(dict, "entries", list);
18511 }
18512 }
18513}
18514
18515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018516 * "values(dict)" function
18517 */
18518 static void
18519f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018520 typval_T *argvars;
18521 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018522{
18523 dict_list(argvars, rettv, 1);
18524}
18525
18526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 * "virtcol(string)" function
18528 */
18529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018530f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018531 typval_T *argvars;
18532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018533{
18534 colnr_T vcol = 0;
18535 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018536 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018538 fp = var2fpos(&argvars[0], FALSE, &fnum);
18539 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18540 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 {
18542 getvvcol(curwin, fp, NULL, NULL, &vcol);
18543 ++vcol;
18544 }
18545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018546 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547}
18548
18549/*
18550 * "visualmode()" function
18551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018554 typval_T *argvars UNUSED;
18555 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556{
18557#ifdef FEAT_VISUAL
18558 char_u str[2];
18559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 str[0] = curbuf->b_visual_mode_eval;
18562 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564
18565 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018566 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568#endif
18569}
18570
18571/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018572 * "wildmenumode()" function
18573 */
18574 static void
18575f_wildmenumode(argvars, rettv)
18576 typval_T *argvars UNUSED;
18577 typval_T *rettv UNUSED;
18578{
18579#ifdef FEAT_WILDMENU
18580 if (wild_menu_showing)
18581 rettv->vval.v_number = 1;
18582#endif
18583}
18584
18585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 * "winbufnr(nr)" function
18587 */
18588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018590 typval_T *argvars;
18591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592{
18593 win_T *wp;
18594
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018595 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600}
18601
18602/*
18603 * "wincol()" function
18604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609{
18610 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612}
18613
18614/*
18615 * "winheight(nr)" function
18616 */
18617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018619 typval_T *argvars;
18620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621{
18622 win_T *wp;
18623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018624 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018628 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629}
18630
18631/*
18632 * "winline()" function
18633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638{
18639 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018640 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641}
18642
18643/*
18644 * "winnr()" function
18645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018647f_winnr(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 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018652
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018654 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657}
18658
18659/*
18660 * "winrestcmd()" function
18661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018663f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018664 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666{
18667#ifdef FEAT_WINDOWS
18668 win_T *wp;
18669 int winnr = 1;
18670 garray_T ga;
18671 char_u buf[50];
18672
18673 ga_init2(&ga, (int)sizeof(char), 70);
18674 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18675 {
18676 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18677 ga_concat(&ga, buf);
18678# ifdef FEAT_VERTSPLIT
18679 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18680 ga_concat(&ga, buf);
18681# endif
18682 ++winnr;
18683 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018684 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018686 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018688 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691}
18692
18693/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018694 * "winrestview()" function
18695 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018696 static void
18697f_winrestview(argvars, rettv)
18698 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018699 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018700{
18701 dict_T *dict;
18702
18703 if (argvars[0].v_type != VAR_DICT
18704 || (dict = argvars[0].vval.v_dict) == NULL)
18705 EMSG(_(e_invarg));
18706 else
18707 {
18708 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18709 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18710#ifdef FEAT_VIRTUALEDIT
18711 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18712#endif
18713 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018714 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018715
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018716 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018717#ifdef FEAT_DIFF
18718 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18719#endif
18720 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18721 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18722
18723 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018724 win_new_height(curwin, curwin->w_height);
18725# ifdef FEAT_VERTSPLIT
18726 win_new_width(curwin, W_WIDTH(curwin));
18727# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018728 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018729
18730 if (curwin->w_topline == 0)
18731 curwin->w_topline = 1;
18732 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18733 curwin->w_topline = curbuf->b_ml.ml_line_count;
18734#ifdef FEAT_DIFF
18735 check_topfill(curwin, TRUE);
18736#endif
18737 }
18738}
18739
18740/*
18741 * "winsaveview()" function
18742 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018743 static void
18744f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018745 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018746 typval_T *rettv;
18747{
18748 dict_T *dict;
18749
Bram Moolenaara800b422010-06-27 01:15:55 +020018750 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018751 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018752 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018753
18754 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18755 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18756#ifdef FEAT_VIRTUALEDIT
18757 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18758#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018759 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018760 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18761
18762 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18763#ifdef FEAT_DIFF
18764 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18765#endif
18766 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18767 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18768}
18769
18770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 * "winwidth(nr)" function
18772 */
18773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018775 typval_T *argvars;
18776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777{
18778 win_T *wp;
18779
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018780 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018782 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 else
18784#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018785 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018787 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788#endif
18789}
18790
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018792 * "writefile()" function
18793 */
18794 static void
18795f_writefile(argvars, rettv)
18796 typval_T *argvars;
18797 typval_T *rettv;
18798{
18799 int binary = FALSE;
18800 char_u *fname;
18801 FILE *fd;
18802 listitem_T *li;
18803 char_u *s;
18804 int ret = 0;
18805 int c;
18806
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018807 if (check_restricted() || check_secure())
18808 return;
18809
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018810 if (argvars[0].v_type != VAR_LIST)
18811 {
18812 EMSG2(_(e_listarg), "writefile()");
18813 return;
18814 }
18815 if (argvars[0].vval.v_list == NULL)
18816 return;
18817
18818 if (argvars[2].v_type != VAR_UNKNOWN
18819 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18820 binary = TRUE;
18821
18822 /* Always open the file in binary mode, library functions have a mind of
18823 * their own about CR-LF conversion. */
18824 fname = get_tv_string(&argvars[1]);
18825 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18826 {
18827 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18828 ret = -1;
18829 }
18830 else
18831 {
18832 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18833 li = li->li_next)
18834 {
18835 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18836 {
18837 if (*s == '\n')
18838 c = putc(NUL, fd);
18839 else
18840 c = putc(*s, fd);
18841 if (c == EOF)
18842 {
18843 ret = -1;
18844 break;
18845 }
18846 }
18847 if (!binary || li->li_next != NULL)
18848 if (putc('\n', fd) == EOF)
18849 {
18850 ret = -1;
18851 break;
18852 }
18853 if (ret < 0)
18854 {
18855 EMSG(_(e_write));
18856 break;
18857 }
18858 }
18859 fclose(fd);
18860 }
18861
18862 rettv->vval.v_number = ret;
18863}
18864
18865/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018866 * "xor(expr, expr)" function
18867 */
18868 static void
18869f_xor(argvars, rettv)
18870 typval_T *argvars;
18871 typval_T *rettv;
18872{
18873 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18874 ^ get_tv_number_chk(&argvars[1], NULL);
18875}
18876
18877
18878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018880 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 */
18882 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018883var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018885 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018886 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018888 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018890 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891
Bram Moolenaara5525202006-03-02 22:52:09 +000018892 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018893 if (varp->v_type == VAR_LIST)
18894 {
18895 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018896 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018897 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018898 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018899
18900 l = varp->vval.v_list;
18901 if (l == NULL)
18902 return NULL;
18903
18904 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018905 pos.lnum = list_find_nr(l, 0L, &error);
18906 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018907 return NULL; /* invalid line number */
18908
18909 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018910 pos.col = list_find_nr(l, 1L, &error);
18911 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018912 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018913 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018914
18915 /* We accept "$" for the column number: last column. */
18916 li = list_find(l, 1L);
18917 if (li != NULL && li->li_tv.v_type == VAR_STRING
18918 && li->li_tv.vval.v_string != NULL
18919 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18920 pos.col = len + 1;
18921
Bram Moolenaara5525202006-03-02 22:52:09 +000018922 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018923 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018924 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018925 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018926
Bram Moolenaara5525202006-03-02 22:52:09 +000018927#ifdef FEAT_VIRTUALEDIT
18928 /* Get the virtual offset. Defaults to zero. */
18929 pos.coladd = list_find_nr(l, 2L, &error);
18930 if (error)
18931 pos.coladd = 0;
18932#endif
18933
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018934 return &pos;
18935 }
18936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018937 name = get_tv_string_chk(varp);
18938 if (name == NULL)
18939 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018940 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018942#ifdef FEAT_VISUAL
18943 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18944 {
18945 if (VIsual_active)
18946 return &VIsual;
18947 return &curwin->w_cursor;
18948 }
18949#endif
18950 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018952 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18954 return NULL;
18955 return pp;
18956 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018957
18958#ifdef FEAT_VIRTUALEDIT
18959 pos.coladd = 0;
18960#endif
18961
Bram Moolenaar477933c2007-07-17 14:32:23 +000018962 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018963 {
18964 pos.col = 0;
18965 if (name[1] == '0') /* "w0": first visible line */
18966 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018967 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018968 pos.lnum = curwin->w_topline;
18969 return &pos;
18970 }
18971 else if (name[1] == '$') /* "w$": last visible line */
18972 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018973 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018974 pos.lnum = curwin->w_botline - 1;
18975 return &pos;
18976 }
18977 }
18978 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018980 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 {
18982 pos.lnum = curbuf->b_ml.ml_line_count;
18983 pos.col = 0;
18984 }
18985 else
18986 {
18987 pos.lnum = curwin->w_cursor.lnum;
18988 pos.col = (colnr_T)STRLEN(ml_get_curline());
18989 }
18990 return &pos;
18991 }
18992 return NULL;
18993}
18994
18995/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018996 * Convert list in "arg" into a position and optional file number.
18997 * When "fnump" is NULL there is no file number, only 3 items.
18998 * Note that the column is passed on as-is, the caller may want to decrement
18999 * it to use 1 for the first column.
19000 * Return FAIL when conversion is not possible, doesn't check the position for
19001 * validity.
19002 */
19003 static int
19004list2fpos(arg, posp, fnump)
19005 typval_T *arg;
19006 pos_T *posp;
19007 int *fnump;
19008{
19009 list_T *l = arg->vval.v_list;
19010 long i = 0;
19011 long n;
19012
Bram Moolenaarbde35262006-07-23 20:12:24 +000019013 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19014 * when "fnump" isn't NULL and "coladd" is optional. */
19015 if (arg->v_type != VAR_LIST
19016 || l == NULL
19017 || l->lv_len < (fnump == NULL ? 2 : 3)
19018 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019019 return FAIL;
19020
19021 if (fnump != NULL)
19022 {
19023 n = list_find_nr(l, i++, NULL); /* fnum */
19024 if (n < 0)
19025 return FAIL;
19026 if (n == 0)
19027 n = curbuf->b_fnum; /* current buffer */
19028 *fnump = n;
19029 }
19030
19031 n = list_find_nr(l, i++, NULL); /* lnum */
19032 if (n < 0)
19033 return FAIL;
19034 posp->lnum = n;
19035
19036 n = list_find_nr(l, i++, NULL); /* col */
19037 if (n < 0)
19038 return FAIL;
19039 posp->col = n;
19040
19041#ifdef FEAT_VIRTUALEDIT
19042 n = list_find_nr(l, i, NULL);
19043 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019044 posp->coladd = 0;
19045 else
19046 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019047#endif
19048
19049 return OK;
19050}
19051
19052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 * Get the length of an environment variable name.
19054 * Advance "arg" to the first character after the name.
19055 * Return 0 for error.
19056 */
19057 static int
19058get_env_len(arg)
19059 char_u **arg;
19060{
19061 char_u *p;
19062 int len;
19063
19064 for (p = *arg; vim_isIDc(*p); ++p)
19065 ;
19066 if (p == *arg) /* no name found */
19067 return 0;
19068
19069 len = (int)(p - *arg);
19070 *arg = p;
19071 return len;
19072}
19073
19074/*
19075 * Get the length of the name of a function or internal variable.
19076 * "arg" is advanced to the first non-white character after the name.
19077 * Return 0 if something is wrong.
19078 */
19079 static int
19080get_id_len(arg)
19081 char_u **arg;
19082{
19083 char_u *p;
19084 int len;
19085
19086 /* Find the end of the name. */
19087 for (p = *arg; eval_isnamec(*p); ++p)
19088 ;
19089 if (p == *arg) /* no name found */
19090 return 0;
19091
19092 len = (int)(p - *arg);
19093 *arg = skipwhite(p);
19094
19095 return len;
19096}
19097
19098/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019099 * Get the length of the name of a variable or function.
19100 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019102 * Return -1 if curly braces expansion failed.
19103 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 * If the name contains 'magic' {}'s, expand them and return the
19105 * expanded name in an allocated string via 'alias' - caller must free.
19106 */
19107 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019108get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 char_u **arg;
19110 char_u **alias;
19111 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019112 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113{
19114 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 char_u *p;
19116 char_u *expr_start;
19117 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118
19119 *alias = NULL; /* default to no alias */
19120
19121 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19122 && (*arg)[2] == (int)KE_SNR)
19123 {
19124 /* hard coded <SNR>, already translated */
19125 *arg += 3;
19126 return get_id_len(arg) + 3;
19127 }
19128 len = eval_fname_script(*arg);
19129 if (len > 0)
19130 {
19131 /* literal "<SID>", "s:" or "<SNR>" */
19132 *arg += len;
19133 }
19134
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019138 p = find_name_end(*arg, &expr_start, &expr_end,
19139 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 if (expr_start != NULL)
19141 {
19142 char_u *temp_string;
19143
19144 if (!evaluate)
19145 {
19146 len += (int)(p - *arg);
19147 *arg = skipwhite(p);
19148 return len;
19149 }
19150
19151 /*
19152 * Include any <SID> etc in the expanded string:
19153 * Thus the -len here.
19154 */
19155 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19156 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019157 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 *alias = temp_string;
19159 *arg = skipwhite(p);
19160 return (int)STRLEN(temp_string);
19161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162
19163 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019164 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 EMSG2(_(e_invexpr2), *arg);
19166
19167 return len;
19168}
19169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170/*
19171 * Find the end of a variable or function name, taking care of magic braces.
19172 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19173 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019174 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019175 * Return a pointer to just after the name. Equal to "arg" if there is no
19176 * valid name.
19177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019179find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 char_u *arg;
19181 char_u **expr_start;
19182 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019183 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019185 int mb_nest = 0;
19186 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 char_u *p;
19188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019189 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019191 *expr_start = NULL;
19192 *expr_end = NULL;
19193 }
19194
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019195 /* Quick check for valid starting character. */
19196 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19197 return arg;
19198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 for (p = arg; *p != NUL
19200 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019201 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019202 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019204 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019206 if (*p == '\'')
19207 {
19208 /* skip over 'string' to avoid counting [ and ] inside it. */
19209 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19210 ;
19211 if (*p == NUL)
19212 break;
19213 }
19214 else if (*p == '"')
19215 {
19216 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19217 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19218 if (*p == '\\' && p[1] != NUL)
19219 ++p;
19220 if (*p == NUL)
19221 break;
19222 }
19223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019226 if (*p == '[')
19227 ++br_nest;
19228 else if (*p == ']')
19229 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019234 if (*p == '{')
19235 {
19236 mb_nest++;
19237 if (expr_start != NULL && *expr_start == NULL)
19238 *expr_start = p;
19239 }
19240 else if (*p == '}')
19241 {
19242 mb_nest--;
19243 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19244 *expr_end = p;
19245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
19248
19249 return p;
19250}
19251
19252/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019253 * Expands out the 'magic' {}'s in a variable/function name.
19254 * Note that this can call itself recursively, to deal with
19255 * constructs like foo{bar}{baz}{bam}
19256 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19257 * "in_start" ^
19258 * "expr_start" ^
19259 * "expr_end" ^
19260 * "in_end" ^
19261 *
19262 * Returns a new allocated string, which the caller must free.
19263 * Returns NULL for failure.
19264 */
19265 static char_u *
19266make_expanded_name(in_start, expr_start, expr_end, in_end)
19267 char_u *in_start;
19268 char_u *expr_start;
19269 char_u *expr_end;
19270 char_u *in_end;
19271{
19272 char_u c1;
19273 char_u *retval = NULL;
19274 char_u *temp_result;
19275 char_u *nextcmd = NULL;
19276
19277 if (expr_end == NULL || in_end == NULL)
19278 return NULL;
19279 *expr_start = NUL;
19280 *expr_end = NUL;
19281 c1 = *in_end;
19282 *in_end = NUL;
19283
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019284 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019285 if (temp_result != NULL && nextcmd == NULL)
19286 {
19287 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19288 + (in_end - expr_end) + 1));
19289 if (retval != NULL)
19290 {
19291 STRCPY(retval, in_start);
19292 STRCAT(retval, temp_result);
19293 STRCAT(retval, expr_end + 1);
19294 }
19295 }
19296 vim_free(temp_result);
19297
19298 *in_end = c1; /* put char back for error messages */
19299 *expr_start = '{';
19300 *expr_end = '}';
19301
19302 if (retval != NULL)
19303 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019304 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019305 if (expr_start != NULL)
19306 {
19307 /* Further expansion! */
19308 temp_result = make_expanded_name(retval, expr_start,
19309 expr_end, temp_result);
19310 vim_free(retval);
19311 retval = temp_result;
19312 }
19313 }
19314
19315 return retval;
19316}
19317
19318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019320 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 */
19322 static int
19323eval_isnamec(c)
19324 int c;
19325{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019326 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19327}
19328
19329/*
19330 * Return TRUE if character "c" can be used as the first character in a
19331 * variable or function name (excluding '{' and '}').
19332 */
19333 static int
19334eval_isnamec1(c)
19335 int c;
19336{
19337 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338}
19339
19340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 * Set number v: variable to "val".
19342 */
19343 void
19344set_vim_var_nr(idx, val)
19345 int idx;
19346 long val;
19347{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019348 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349}
19350
19351/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019352 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353 */
19354 long
19355get_vim_var_nr(idx)
19356 int idx;
19357{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019358 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359}
19360
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019361/*
19362 * Get string v: variable value. Uses a static buffer, can only be used once.
19363 */
19364 char_u *
19365get_vim_var_str(idx)
19366 int idx;
19367{
19368 return get_tv_string(&vimvars[idx].vv_tv);
19369}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019370
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019372 * Get List v: variable value. Caller must take care of reference count when
19373 * needed.
19374 */
19375 list_T *
19376get_vim_var_list(idx)
19377 int idx;
19378{
19379 return vimvars[idx].vv_list;
19380}
19381
19382/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019383 * Set v:char to character "c".
19384 */
19385 void
19386set_vim_var_char(c)
19387 int c;
19388{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019389 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019390
19391#ifdef FEAT_MBYTE
19392 if (has_mbyte)
19393 buf[(*mb_char2bytes)(c, buf)] = NUL;
19394 else
19395#endif
19396 {
19397 buf[0] = c;
19398 buf[1] = NUL;
19399 }
19400 set_vim_var_string(VV_CHAR, buf, -1);
19401}
19402
19403/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019404 * Set v:count to "count" and v:count1 to "count1".
19405 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 */
19407 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019408set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409 long count;
19410 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019411 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019413 if (set_prevcount)
19414 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019415 vimvars[VV_COUNT].vv_nr = count;
19416 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417}
19418
19419/*
19420 * Set string v: variable to a copy of "val".
19421 */
19422 void
19423set_vim_var_string(idx, val, len)
19424 int idx;
19425 char_u *val;
19426 int len; /* length of "val" to use or -1 (whole string) */
19427{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019428 /* Need to do this (at least) once, since we can't initialize a union.
19429 * Will always be invoked when "v:progname" is set. */
19430 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19431
Bram Moolenaare9a41262005-01-15 22:18:47 +000019432 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019434 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019436 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019438 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439}
19440
19441/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019442 * Set List v: variable to "val".
19443 */
19444 void
19445set_vim_var_list(idx, val)
19446 int idx;
19447 list_T *val;
19448{
19449 list_unref(vimvars[idx].vv_list);
19450 vimvars[idx].vv_list = val;
19451 if (val != NULL)
19452 ++val->lv_refcount;
19453}
19454
19455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456 * Set v:register if needed.
19457 */
19458 void
19459set_reg_var(c)
19460 int c;
19461{
19462 char_u regname;
19463
19464 if (c == 0 || c == ' ')
19465 regname = '"';
19466 else
19467 regname = c;
19468 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019469 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 set_vim_var_string(VV_REG, &regname, 1);
19471}
19472
19473/*
19474 * Get or set v:exception. If "oldval" == NULL, return the current value.
19475 * Otherwise, restore the value to "oldval" and return NULL.
19476 * Must always be called in pairs to save and restore v:exception! Does not
19477 * take care of memory allocations.
19478 */
19479 char_u *
19480v_exception(oldval)
19481 char_u *oldval;
19482{
19483 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019484 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485
Bram Moolenaare9a41262005-01-15 22:18:47 +000019486 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 return NULL;
19488}
19489
19490/*
19491 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19492 * Otherwise, restore the value to "oldval" and return NULL.
19493 * Must always be called in pairs to save and restore v:throwpoint! Does not
19494 * take care of memory allocations.
19495 */
19496 char_u *
19497v_throwpoint(oldval)
19498 char_u *oldval;
19499{
19500 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019501 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502
Bram Moolenaare9a41262005-01-15 22:18:47 +000019503 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 return NULL;
19505}
19506
19507#if defined(FEAT_AUTOCMD) || defined(PROTO)
19508/*
19509 * Set v:cmdarg.
19510 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19511 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19512 * Must always be called in pairs!
19513 */
19514 char_u *
19515set_cmdarg(eap, oldarg)
19516 exarg_T *eap;
19517 char_u *oldarg;
19518{
19519 char_u *oldval;
19520 char_u *newval;
19521 unsigned len;
19522
Bram Moolenaare9a41262005-01-15 22:18:47 +000019523 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019524 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019526 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019527 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019528 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 }
19530
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019531 if (eap->force_bin == FORCE_BIN)
19532 len = 6;
19533 else if (eap->force_bin == FORCE_NOBIN)
19534 len = 8;
19535 else
19536 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019537
19538 if (eap->read_edit)
19539 len += 7;
19540
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019541 if (eap->force_ff != 0)
19542 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19543# ifdef FEAT_MBYTE
19544 if (eap->force_enc != 0)
19545 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019546 if (eap->bad_char != 0)
19547 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019548# endif
19549
19550 newval = alloc(len + 1);
19551 if (newval == NULL)
19552 return NULL;
19553
19554 if (eap->force_bin == FORCE_BIN)
19555 sprintf((char *)newval, " ++bin");
19556 else if (eap->force_bin == FORCE_NOBIN)
19557 sprintf((char *)newval, " ++nobin");
19558 else
19559 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019560
19561 if (eap->read_edit)
19562 STRCAT(newval, " ++edit");
19563
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019564 if (eap->force_ff != 0)
19565 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19566 eap->cmd + eap->force_ff);
19567# ifdef FEAT_MBYTE
19568 if (eap->force_enc != 0)
19569 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19570 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019571 if (eap->bad_char == BAD_KEEP)
19572 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19573 else if (eap->bad_char == BAD_DROP)
19574 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19575 else if (eap->bad_char != 0)
19576 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019577# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019578 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019579 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019580}
19581#endif
19582
19583/*
19584 * Get the value of internal variable "name".
19585 * Return OK or FAIL.
19586 */
19587 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019588get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 char_u *name;
19590 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019591 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019592 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593{
19594 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 typval_T *tv = NULL;
19596 typval_T atv;
19597 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599
19600 /* truncate the name, so that we can use strcmp() */
19601 cc = name[len];
19602 name[len] = NUL;
19603
19604 /*
19605 * Check for "b:changedtick".
19606 */
19607 if (STRCMP(name, "b:changedtick") == 0)
19608 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019609 atv.v_type = VAR_NUMBER;
19610 atv.vval.v_number = curbuf->b_changedtick;
19611 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 }
19613
19614 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 * Check for user-defined variables.
19616 */
19617 else
19618 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019619 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019621 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 }
19623
Bram Moolenaare9a41262005-01-15 22:18:47 +000019624 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019626 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019627 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 ret = FAIL;
19629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019631 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632
19633 name[len] = cc;
19634
19635 return ret;
19636}
19637
19638/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019639 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19640 * Also handle function call with Funcref variable: func(expr)
19641 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19642 */
19643 static int
19644handle_subscript(arg, rettv, evaluate, verbose)
19645 char_u **arg;
19646 typval_T *rettv;
19647 int evaluate; /* do more than finding the end */
19648 int verbose; /* give error messages */
19649{
19650 int ret = OK;
19651 dict_T *selfdict = NULL;
19652 char_u *s;
19653 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019654 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019655
19656 while (ret == OK
19657 && (**arg == '['
19658 || (**arg == '.' && rettv->v_type == VAR_DICT)
19659 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19660 && !vim_iswhite(*(*arg - 1)))
19661 {
19662 if (**arg == '(')
19663 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019664 /* need to copy the funcref so that we can clear rettv */
19665 functv = *rettv;
19666 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019667
19668 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019669 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019670 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019671 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19672 &len, evaluate, selfdict);
19673
19674 /* Clear the funcref afterwards, so that deleting it while
19675 * evaluating the arguments is possible (see test55). */
19676 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019677
19678 /* Stop the expression evaluation when immediately aborting on
19679 * error, or when an interrupt occurred or an exception was thrown
19680 * but not caught. */
19681 if (aborting())
19682 {
19683 if (ret == OK)
19684 clear_tv(rettv);
19685 ret = FAIL;
19686 }
19687 dict_unref(selfdict);
19688 selfdict = NULL;
19689 }
19690 else /* **arg == '[' || **arg == '.' */
19691 {
19692 dict_unref(selfdict);
19693 if (rettv->v_type == VAR_DICT)
19694 {
19695 selfdict = rettv->vval.v_dict;
19696 if (selfdict != NULL)
19697 ++selfdict->dv_refcount;
19698 }
19699 else
19700 selfdict = NULL;
19701 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19702 {
19703 clear_tv(rettv);
19704 ret = FAIL;
19705 }
19706 }
19707 }
19708 dict_unref(selfdict);
19709 return ret;
19710}
19711
19712/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019713 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019714 * value).
19715 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019718{
Bram Moolenaar33570922005-01-25 22:26:29 +000019719 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019720}
19721
19722/*
19723 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 * The string "s" must have been allocated, it is consumed.
19725 * Return NULL for out of memory, the variable otherwise.
19726 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729 char_u *s;
19730{
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733 rettv = alloc_tv();
19734 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 rettv->v_type = VAR_STRING;
19737 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 }
19739 else
19740 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742}
19743
19744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019745 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019747 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019748free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019749 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750{
19751 if (varp != NULL)
19752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019753 switch (varp->v_type)
19754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019755 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019756 func_unref(varp->vval.v_string);
19757 /*FALLTHROUGH*/
19758 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019759 vim_free(varp->vval.v_string);
19760 break;
19761 case VAR_LIST:
19762 list_unref(varp->vval.v_list);
19763 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019764 case VAR_DICT:
19765 dict_unref(varp->vval.v_dict);
19766 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019767 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019768#ifdef FEAT_FLOAT
19769 case VAR_FLOAT:
19770#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019771 case VAR_UNKNOWN:
19772 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019773 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019774 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019775 break;
19776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 vim_free(varp);
19778 }
19779}
19780
19781/*
19782 * Free the memory for a variable value and set the value to NULL or 0.
19783 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019784 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019786 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787{
19788 if (varp != NULL)
19789 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019792 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019793 func_unref(varp->vval.v_string);
19794 /*FALLTHROUGH*/
19795 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 vim_free(varp->vval.v_string);
19797 varp->vval.v_string = NULL;
19798 break;
19799 case VAR_LIST:
19800 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019801 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019803 case VAR_DICT:
19804 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019805 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019806 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019807 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808 varp->vval.v_number = 0;
19809 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019810#ifdef FEAT_FLOAT
19811 case VAR_FLOAT:
19812 varp->vval.v_float = 0.0;
19813 break;
19814#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 case VAR_UNKNOWN:
19816 break;
19817 default:
19818 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019820 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 }
19822}
19823
19824/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 * Set the value of a variable to NULL without freeing items.
19826 */
19827 static void
19828init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830{
19831 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833}
19834
19835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 * Get the number value of a variable.
19837 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019838 * For incompatible types, return 0.
19839 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19840 * caller of incompatible types: it sets *denote to TRUE if "denote"
19841 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 */
19843 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019845 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019847 int error = FALSE;
19848
19849 return get_tv_number_chk(varp, &error); /* return 0L on error */
19850}
19851
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019852 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019853get_tv_number_chk(varp, denote)
19854 typval_T *varp;
19855 int *denote;
19856{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019857 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019859 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019861 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019862 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019863#ifdef FEAT_FLOAT
19864 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019865 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019866 break;
19867#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019868 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019869 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019870 break;
19871 case VAR_STRING:
19872 if (varp->vval.v_string != NULL)
19873 vim_str2nr(varp->vval.v_string, NULL, NULL,
19874 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019875 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019876 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019877 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019878 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019879 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019880 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019881 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019882 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019883 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019884 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019886 if (denote == NULL) /* useful for values that must be unsigned */
19887 n = -1;
19888 else
19889 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891}
19892
19893/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019894 * Get the lnum from the first argument.
19895 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019896 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 */
19898 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901{
Bram Moolenaar33570922005-01-25 22:26:29 +000019902 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 linenr_T lnum;
19904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019905 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906 if (lnum == 0) /* no valid number, try using line() */
19907 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908 rettv.v_type = VAR_NUMBER;
19909 f_line(argvars, &rettv);
19910 lnum = rettv.vval.v_number;
19911 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912 }
19913 return lnum;
19914}
19915
19916/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019917 * Get the lnum from the first argument.
19918 * Also accepts "$", then "buf" is used.
19919 * Returns 0 on error.
19920 */
19921 static linenr_T
19922get_tv_lnum_buf(argvars, buf)
19923 typval_T *argvars;
19924 buf_T *buf;
19925{
19926 if (argvars[0].v_type == VAR_STRING
19927 && argvars[0].vval.v_string != NULL
19928 && argvars[0].vval.v_string[0] == '$'
19929 && buf != NULL)
19930 return buf->b_ml.ml_line_count;
19931 return get_tv_number_chk(&argvars[0], NULL);
19932}
19933
19934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 * Get the string value of a variable.
19936 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019937 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19938 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 * If the String variable has never been set, return an empty string.
19940 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019941 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19942 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 */
19944 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019945get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019946 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019947{
19948 static char_u mybuf[NUMBUFLEN];
19949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019950 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951}
19952
19953 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019954get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019955 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958 char_u *res = get_tv_string_buf_chk(varp, buf);
19959
19960 return res != NULL ? res : (char_u *)"";
19961}
19962
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019963 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019964get_tv_string_chk(varp)
19965 typval_T *varp;
19966{
19967 static char_u mybuf[NUMBUFLEN];
19968
19969 return get_tv_string_buf_chk(varp, mybuf);
19970}
19971
19972 static char_u *
19973get_tv_string_buf_chk(varp, buf)
19974 typval_T *varp;
19975 char_u *buf;
19976{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019977 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019979 case VAR_NUMBER:
19980 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19981 return buf;
19982 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019983 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 break;
19985 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019986 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019987 break;
19988 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019989 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019990 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019991#ifdef FEAT_FLOAT
19992 case VAR_FLOAT:
19993 EMSG(_("E806: using Float as a String"));
19994 break;
19995#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019996 case VAR_STRING:
19997 if (varp->vval.v_string != NULL)
19998 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020000 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020001 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020004 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005}
20006
20007/*
20008 * Find variable "name" in the list of variables.
20009 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020010 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020011 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020012 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020014 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020015find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020017 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020020 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021
Bram Moolenaara7043832005-01-21 11:56:39 +000020022 ht = find_var_ht(name, &varname);
20023 if (htp != NULL)
20024 *htp = ht;
20025 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020027 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028}
20029
20030/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020031 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020032 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020035find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020037 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020038 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020039 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020040{
Bram Moolenaar33570922005-01-25 22:26:29 +000020041 hashitem_T *hi;
20042
20043 if (*varname == NUL)
20044 {
20045 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020046 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020047 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020048 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020049 case 'g': return &globvars_var;
20050 case 'v': return &vimvars_var;
20051 case 'b': return &curbuf->b_bufvar;
20052 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020053#ifdef FEAT_WINDOWS
20054 case 't': return &curtab->tp_winvar;
20055#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020056 case 'l': return current_funccal == NULL
20057 ? NULL : &current_funccal->l_vars_var;
20058 case 'a': return current_funccal == NULL
20059 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 }
20061 return NULL;
20062 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020063
20064 hi = hash_find(ht, varname);
20065 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020066 {
20067 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020068 * worked find the variable again. Don't auto-load a script if it was
20069 * loaded already, otherwise it would be loaded every time when
20070 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020071 if (ht == &globvarht && !writing)
20072 {
20073 /* Note: script_autoload() may make "hi" invalid. It must either
20074 * be obtained again or not used. */
20075 if (!script_autoload(varname, FALSE) || aborting())
20076 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020077 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020078 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020079 if (HASHITEM_EMPTY(hi))
20080 return NULL;
20081 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020082 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020083}
20084
20085/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020086 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020087 * Set "varname" to the start of name without ':'.
20088 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020090find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 char_u *name;
20092 char_u **varname;
20093{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020094 hashitem_T *hi;
20095
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 if (name[1] != ':')
20097 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020098 /* The name must not start with a colon or #. */
20099 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100 return NULL;
20101 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020102
20103 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020104 hi = hash_find(&compat_hashtab, name);
20105 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020106 return &compat_hashtab;
20107
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020109 return &globvarht; /* global variable */
20110 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 }
20112 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020113 if (*name == 'g') /* global variable */
20114 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020115 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20116 */
20117 if (vim_strchr(name + 2, ':') != NULL
20118 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020119 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020121 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020123 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020124#ifdef FEAT_WINDOWS
20125 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020126 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020127#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 if (*name == 'v') /* v: variable */
20129 return &vimvarht;
20130 if (*name == 'a' && current_funccal != NULL) /* function argument */
20131 return &current_funccal->l_avars.dv_hashtab;
20132 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20133 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 if (*name == 's' /* script variable */
20135 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20136 return &SCRIPT_VARS(current_SID);
20137 return NULL;
20138}
20139
20140/*
20141 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020142 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 * Returns NULL when it doesn't exist.
20144 */
20145 char_u *
20146get_var_value(name)
20147 char_u *name;
20148{
Bram Moolenaar33570922005-01-25 22:26:29 +000020149 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150
Bram Moolenaara7043832005-01-21 11:56:39 +000020151 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 if (v == NULL)
20153 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020154 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155}
20156
20157/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020158 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159 * sourcing this script and when executing functions defined in the script.
20160 */
20161 void
20162new_script_vars(id)
20163 scid_T id;
20164{
Bram Moolenaara7043832005-01-21 11:56:39 +000020165 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 hashtab_T *ht;
20167 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020168
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20170 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020171 /* Re-allocating ga_data means that an ht_array pointing to
20172 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020173 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020174 for (i = 1; i <= ga_scripts.ga_len; ++i)
20175 {
20176 ht = &SCRIPT_VARS(i);
20177 if (ht->ht_mask == HT_INIT_SIZE - 1)
20178 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020179 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020180 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020181 }
20182
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183 while (ga_scripts.ga_len < id)
20184 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020185 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020186 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020187 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 }
20190 }
20191}
20192
20193/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20195 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 */
20197 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020198init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 dict_T *dict;
20200 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020201 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202{
Bram Moolenaar33570922005-01-25 22:26:29 +000020203 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020204 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020205 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020206 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020207 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020208 dict_var->di_tv.vval.v_dict = dict;
20209 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020210 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20212 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213}
20214
20215/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020216 * Unreference a dictionary initialized by init_var_dict().
20217 */
20218 void
20219unref_var_dict(dict)
20220 dict_T *dict;
20221{
20222 /* Now the dict needs to be freed if no one else is using it, go back to
20223 * normal reference counting. */
20224 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20225 dict_unref(dict);
20226}
20227
20228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020230 * Frees all allocated variables and the value they contain.
20231 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020232 */
20233 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020234vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020235 hashtab_T *ht;
20236{
20237 vars_clear_ext(ht, TRUE);
20238}
20239
20240/*
20241 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20242 */
20243 static void
20244vars_clear_ext(ht, free_val)
20245 hashtab_T *ht;
20246 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247{
Bram Moolenaara7043832005-01-21 11:56:39 +000020248 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020249 hashitem_T *hi;
20250 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251
Bram Moolenaar33570922005-01-25 22:26:29 +000020252 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020253 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020254 for (hi = ht->ht_array; todo > 0; ++hi)
20255 {
20256 if (!HASHITEM_EMPTY(hi))
20257 {
20258 --todo;
20259
Bram Moolenaar33570922005-01-25 22:26:29 +000020260 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020261 * ht_array might change then. hash_clear() takes care of it
20262 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020263 v = HI2DI(hi);
20264 if (free_val)
20265 clear_tv(&v->di_tv);
20266 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20267 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020268 }
20269 }
20270 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020271 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272}
20273
Bram Moolenaara7043832005-01-21 11:56:39 +000020274/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020275 * Delete a variable from hashtab "ht" at item "hi".
20276 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020279delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 hashtab_T *ht;
20281 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282{
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020284
20285 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020286 clear_tv(&di->di_tv);
20287 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288}
20289
20290/*
20291 * List the value of one internal variable.
20292 */
20293 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020294list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020297 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020299 char_u *tofree;
20300 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020301 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020302
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020303 current_copyID += COPYID_INC;
20304 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020306 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020307 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308}
20309
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020311list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 char_u *prefix;
20313 char_u *name;
20314 int type;
20315 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020316 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317{
Bram Moolenaar31859182007-08-14 20:41:13 +000020318 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20319 msg_start();
20320 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 if (name != NULL) /* "a:" vars don't have a name stored */
20322 msg_puts(name);
20323 msg_putchar(' ');
20324 msg_advance(22);
20325 if (type == VAR_NUMBER)
20326 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020327 else if (type == VAR_FUNC)
20328 msg_putchar('*');
20329 else if (type == VAR_LIST)
20330 {
20331 msg_putchar('[');
20332 if (*string == '[')
20333 ++string;
20334 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020335 else if (type == VAR_DICT)
20336 {
20337 msg_putchar('{');
20338 if (*string == '{')
20339 ++string;
20340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 else
20342 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020343
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020345
20346 if (type == VAR_FUNC)
20347 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020348 if (*first)
20349 {
20350 msg_clr_eos();
20351 *first = FALSE;
20352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353}
20354
20355/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357 * If the variable already exists, the value is updated.
20358 * Otherwise the variable is created.
20359 */
20360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020361set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020363 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020364 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365{
Bram Moolenaar33570922005-01-25 22:26:29 +000020366 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020370 ht = find_var_ht(name, &varname);
20371 if (ht == NULL || *varname == NUL)
20372 {
20373 EMSG2(_(e_illvar), name);
20374 return;
20375 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020376 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020377
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020378 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20379 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020380
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020384 if (var_check_ro(v->di_flags, name)
20385 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 return;
20387 if (v->di_tv.v_type != tv->v_type
20388 && !((v->di_tv.v_type == VAR_STRING
20389 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020391 || tv->v_type == VAR_NUMBER))
20392#ifdef FEAT_FLOAT
20393 && !((v->di_tv.v_type == VAR_NUMBER
20394 || v->di_tv.v_type == VAR_FLOAT)
20395 && (tv->v_type == VAR_NUMBER
20396 || tv->v_type == VAR_FLOAT))
20397#endif
20398 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020399 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020400 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020401 return;
20402 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020403
20404 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020405 * Handle setting internal v: variables separately: we don't change
20406 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020407 */
20408 if (ht == &vimvarht)
20409 {
20410 if (v->di_tv.v_type == VAR_STRING)
20411 {
20412 vim_free(v->di_tv.vval.v_string);
20413 if (copy || tv->v_type != VAR_STRING)
20414 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20415 else
20416 {
20417 /* Take over the string to avoid an extra alloc/free. */
20418 v->di_tv.vval.v_string = tv->vval.v_string;
20419 tv->vval.v_string = NULL;
20420 }
20421 }
20422 else if (v->di_tv.v_type != VAR_NUMBER)
20423 EMSG2(_(e_intern2), "set_var()");
20424 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020425 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020427 if (STRCMP(varname, "searchforward") == 0)
20428 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20429 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020430 return;
20431 }
20432
20433 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 }
20435 else /* add a new variable */
20436 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020437 /* Can't add "v:" variable. */
20438 if (ht == &vimvarht)
20439 {
20440 EMSG2(_(e_illvar), name);
20441 return;
20442 }
20443
Bram Moolenaar92124a32005-06-17 22:03:40 +000020444 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020445 if (!valid_varname(varname))
20446 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020447
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020448 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20449 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020450 if (v == NULL)
20451 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020452 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020453 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020455 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 return;
20457 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020458 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020460
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020461 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020463 else
20464 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020465 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020466 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020467 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469}
20470
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020471/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020472 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 * Also give an error message.
20474 */
20475 static int
20476var_check_ro(flags, name)
20477 int flags;
20478 char_u *name;
20479{
20480 if (flags & DI_FLAGS_RO)
20481 {
20482 EMSG2(_(e_readonlyvar), name);
20483 return TRUE;
20484 }
20485 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20486 {
20487 EMSG2(_(e_readonlysbx), name);
20488 return TRUE;
20489 }
20490 return FALSE;
20491}
20492
20493/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020494 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20495 * Also give an error message.
20496 */
20497 static int
20498var_check_fixed(flags, name)
20499 int flags;
20500 char_u *name;
20501{
20502 if (flags & DI_FLAGS_FIX)
20503 {
20504 EMSG2(_("E795: Cannot delete variable %s"), name);
20505 return TRUE;
20506 }
20507 return FALSE;
20508}
20509
20510/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020511 * Check if a funcref is assigned to a valid variable name.
20512 * Return TRUE and give an error if not.
20513 */
20514 static int
20515var_check_func_name(name, new_var)
20516 char_u *name; /* points to start of variable name */
20517 int new_var; /* TRUE when creating the variable */
20518{
20519 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20520 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20521 ? name[2] : name[0]))
20522 {
20523 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20524 name);
20525 return TRUE;
20526 }
20527 /* Don't allow hiding a function. When "v" is not NULL we might be
20528 * assigning another function to the same var, the type is checked
20529 * below. */
20530 if (new_var && function_exists(name))
20531 {
20532 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20533 name);
20534 return TRUE;
20535 }
20536 return FALSE;
20537}
20538
20539/*
20540 * Check if a variable name is valid.
20541 * Return FALSE and give an error if not.
20542 */
20543 static int
20544valid_varname(varname)
20545 char_u *varname;
20546{
20547 char_u *p;
20548
20549 for (p = varname; *p != NUL; ++p)
20550 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20551 && *p != AUTOLOAD_CHAR)
20552 {
20553 EMSG2(_(e_illvar), varname);
20554 return FALSE;
20555 }
20556 return TRUE;
20557}
20558
20559/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020560 * Return TRUE if typeval "tv" is set to be locked (immutable).
20561 * Also give an error message, using "name".
20562 */
20563 static int
20564tv_check_lock(lock, name)
20565 int lock;
20566 char_u *name;
20567{
20568 if (lock & VAR_LOCKED)
20569 {
20570 EMSG2(_("E741: Value is locked: %s"),
20571 name == NULL ? (char_u *)_("Unknown") : name);
20572 return TRUE;
20573 }
20574 if (lock & VAR_FIXED)
20575 {
20576 EMSG2(_("E742: Cannot change value of %s"),
20577 name == NULL ? (char_u *)_("Unknown") : name);
20578 return TRUE;
20579 }
20580 return FALSE;
20581}
20582
20583/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020584 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020585 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020586 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020587 * It is OK for "from" and "to" to point to the same item. This is used to
20588 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020589 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020590 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020591copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020592 typval_T *from;
20593 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020595 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020596 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020597 switch (from->v_type)
20598 {
20599 case VAR_NUMBER:
20600 to->vval.v_number = from->vval.v_number;
20601 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020602#ifdef FEAT_FLOAT
20603 case VAR_FLOAT:
20604 to->vval.v_float = from->vval.v_float;
20605 break;
20606#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607 case VAR_STRING:
20608 case VAR_FUNC:
20609 if (from->vval.v_string == NULL)
20610 to->vval.v_string = NULL;
20611 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020613 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020614 if (from->v_type == VAR_FUNC)
20615 func_ref(to->vval.v_string);
20616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020617 break;
20618 case VAR_LIST:
20619 if (from->vval.v_list == NULL)
20620 to->vval.v_list = NULL;
20621 else
20622 {
20623 to->vval.v_list = from->vval.v_list;
20624 ++to->vval.v_list->lv_refcount;
20625 }
20626 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020627 case VAR_DICT:
20628 if (from->vval.v_dict == NULL)
20629 to->vval.v_dict = NULL;
20630 else
20631 {
20632 to->vval.v_dict = from->vval.v_dict;
20633 ++to->vval.v_dict->dv_refcount;
20634 }
20635 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020636 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020638 break;
20639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640}
20641
20642/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020643 * Make a copy of an item.
20644 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020645 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20646 * reference to an already copied list/dict can be used.
20647 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020648 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020649 static int
20650item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020651 typval_T *from;
20652 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020653 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020654 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020655{
20656 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020657 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020658
Bram Moolenaar33570922005-01-25 22:26:29 +000020659 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020660 {
20661 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020662 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020663 }
20664 ++recurse;
20665
20666 switch (from->v_type)
20667 {
20668 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020669#ifdef FEAT_FLOAT
20670 case VAR_FLOAT:
20671#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672 case VAR_STRING:
20673 case VAR_FUNC:
20674 copy_tv(from, to);
20675 break;
20676 case VAR_LIST:
20677 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020678 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020679 if (from->vval.v_list == NULL)
20680 to->vval.v_list = NULL;
20681 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20682 {
20683 /* use the copy made earlier */
20684 to->vval.v_list = from->vval.v_list->lv_copylist;
20685 ++to->vval.v_list->lv_refcount;
20686 }
20687 else
20688 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20689 if (to->vval.v_list == NULL)
20690 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020691 break;
20692 case VAR_DICT:
20693 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020694 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020695 if (from->vval.v_dict == NULL)
20696 to->vval.v_dict = NULL;
20697 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20698 {
20699 /* use the copy made earlier */
20700 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20701 ++to->vval.v_dict->dv_refcount;
20702 }
20703 else
20704 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20705 if (to->vval.v_dict == NULL)
20706 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020707 break;
20708 default:
20709 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020710 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020711 }
20712 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020713 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020714}
20715
20716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 * ":echo expr1 ..." print each argument separated with a space, add a
20718 * newline at the end.
20719 * ":echon expr1 ..." print each argument plain.
20720 */
20721 void
20722ex_echo(eap)
20723 exarg_T *eap;
20724{
20725 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020726 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020727 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 char_u *p;
20729 int needclr = TRUE;
20730 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020731 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732
20733 if (eap->skip)
20734 ++emsg_skip;
20735 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20736 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020737 /* If eval1() causes an error message the text from the command may
20738 * still need to be cleared. E.g., "echo 22,44". */
20739 need_clr_eos = needclr;
20740
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 {
20744 /*
20745 * Report the invalid expression unless the expression evaluation
20746 * has been cancelled due to an aborting error, an interrupt, or an
20747 * exception.
20748 */
20749 if (!aborting())
20750 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020751 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 break;
20753 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020754 need_clr_eos = FALSE;
20755
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 if (!eap->skip)
20757 {
20758 if (atstart)
20759 {
20760 atstart = FALSE;
20761 /* Call msg_start() after eval1(), evaluating the expression
20762 * may cause a message to appear. */
20763 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020764 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020765 /* Mark the saved text as finishing the line, so that what
20766 * follows is displayed on a new line when scrolling back
20767 * at the more prompt. */
20768 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 }
20772 else if (eap->cmdidx == CMD_echo)
20773 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020774 current_copyID += COPYID_INC;
20775 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020776 if (p != NULL)
20777 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020779 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020781 if (*p != TAB && needclr)
20782 {
20783 /* remove any text still there from the command */
20784 msg_clr_eos();
20785 needclr = FALSE;
20786 }
20787 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 }
20789 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020790 {
20791#ifdef FEAT_MBYTE
20792 if (has_mbyte)
20793 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020794 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020795
20796 (void)msg_outtrans_len_attr(p, i, echo_attr);
20797 p += i - 1;
20798 }
20799 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020801 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020804 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020806 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 arg = skipwhite(arg);
20808 }
20809 eap->nextcmd = check_nextcmd(arg);
20810
20811 if (eap->skip)
20812 --emsg_skip;
20813 else
20814 {
20815 /* remove text that may still be there from the command */
20816 if (needclr)
20817 msg_clr_eos();
20818 if (eap->cmdidx == CMD_echo)
20819 msg_end();
20820 }
20821}
20822
20823/*
20824 * ":echohl {name}".
20825 */
20826 void
20827ex_echohl(eap)
20828 exarg_T *eap;
20829{
20830 int id;
20831
20832 id = syn_name2id(eap->arg);
20833 if (id == 0)
20834 echo_attr = 0;
20835 else
20836 echo_attr = syn_id2attr(id);
20837}
20838
20839/*
20840 * ":execute expr1 ..." execute the result of an expression.
20841 * ":echomsg expr1 ..." Print a message
20842 * ":echoerr expr1 ..." Print an error
20843 * Each gets spaces around each argument and a newline at the end for
20844 * echo commands
20845 */
20846 void
20847ex_execute(eap)
20848 exarg_T *eap;
20849{
20850 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020851 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 int ret = OK;
20853 char_u *p;
20854 garray_T ga;
20855 int len;
20856 int save_did_emsg;
20857
20858 ga_init2(&ga, 1, 80);
20859
20860 if (eap->skip)
20861 ++emsg_skip;
20862 while (*arg != NUL && *arg != '|' && *arg != '\n')
20863 {
20864 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 {
20867 /*
20868 * Report the invalid expression unless the expression evaluation
20869 * has been cancelled due to an aborting error, an interrupt, or an
20870 * exception.
20871 */
20872 if (!aborting())
20873 EMSG2(_(e_invexpr2), p);
20874 ret = FAIL;
20875 break;
20876 }
20877
20878 if (!eap->skip)
20879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020880 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881 len = (int)STRLEN(p);
20882 if (ga_grow(&ga, len + 2) == FAIL)
20883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020884 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 ret = FAIL;
20886 break;
20887 }
20888 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891 ga.ga_len += len;
20892 }
20893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 arg = skipwhite(arg);
20896 }
20897
20898 if (ret != FAIL && ga.ga_data != NULL)
20899 {
20900 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020903 out_flush();
20904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 else if (eap->cmdidx == CMD_echoerr)
20906 {
20907 /* We don't want to abort following commands, restore did_emsg. */
20908 save_did_emsg = did_emsg;
20909 EMSG((char_u *)ga.ga_data);
20910 if (!force_abort)
20911 did_emsg = save_did_emsg;
20912 }
20913 else if (eap->cmdidx == CMD_execute)
20914 do_cmdline((char_u *)ga.ga_data,
20915 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20916 }
20917
20918 ga_clear(&ga);
20919
20920 if (eap->skip)
20921 --emsg_skip;
20922
20923 eap->nextcmd = check_nextcmd(arg);
20924}
20925
20926/*
20927 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20928 * "arg" points to the "&" or '+' when called, to "option" when returning.
20929 * Returns NULL when no option name found. Otherwise pointer to the char
20930 * after the option name.
20931 */
20932 static char_u *
20933find_option_end(arg, opt_flags)
20934 char_u **arg;
20935 int *opt_flags;
20936{
20937 char_u *p = *arg;
20938
20939 ++p;
20940 if (*p == 'g' && p[1] == ':')
20941 {
20942 *opt_flags = OPT_GLOBAL;
20943 p += 2;
20944 }
20945 else if (*p == 'l' && p[1] == ':')
20946 {
20947 *opt_flags = OPT_LOCAL;
20948 p += 2;
20949 }
20950 else
20951 *opt_flags = 0;
20952
20953 if (!ASCII_ISALPHA(*p))
20954 return NULL;
20955 *arg = p;
20956
20957 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20958 p += 4; /* termcap option */
20959 else
20960 while (ASCII_ISALPHA(*p))
20961 ++p;
20962 return p;
20963}
20964
20965/*
20966 * ":function"
20967 */
20968 void
20969ex_function(eap)
20970 exarg_T *eap;
20971{
20972 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020973 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 int j;
20975 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 char_u *name = NULL;
20978 char_u *p;
20979 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020980 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 garray_T newargs;
20982 garray_T newlines;
20983 int varargs = FALSE;
20984 int mustend = FALSE;
20985 int flags = 0;
20986 ufunc_T *fp;
20987 int indent;
20988 int nesting;
20989 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020990 dictitem_T *v;
20991 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020992 static int func_nr = 0; /* number for nameless function */
20993 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020994 hashtab_T *ht;
20995 int todo;
20996 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020997 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998
20999 /*
21000 * ":function" without argument: list functions.
21001 */
21002 if (ends_excmd(*eap->arg))
21003 {
21004 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021005 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021006 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021007 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021008 {
21009 if (!HASHITEM_EMPTY(hi))
21010 {
21011 --todo;
21012 fp = HI2UF(hi);
21013 if (!isdigit(*fp->uf_name))
21014 list_func_head(fp, FALSE);
21015 }
21016 }
21017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 eap->nextcmd = check_nextcmd(eap->arg);
21019 return;
21020 }
21021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021022 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021023 * ":function /pat": list functions matching pattern.
21024 */
21025 if (*eap->arg == '/')
21026 {
21027 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21028 if (!eap->skip)
21029 {
21030 regmatch_T regmatch;
21031
21032 c = *p;
21033 *p = NUL;
21034 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21035 *p = c;
21036 if (regmatch.regprog != NULL)
21037 {
21038 regmatch.rm_ic = p_ic;
21039
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021040 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021041 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21042 {
21043 if (!HASHITEM_EMPTY(hi))
21044 {
21045 --todo;
21046 fp = HI2UF(hi);
21047 if (!isdigit(*fp->uf_name)
21048 && vim_regexec(&regmatch, fp->uf_name, 0))
21049 list_func_head(fp, FALSE);
21050 }
21051 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021052 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021053 }
21054 }
21055 if (*p == '/')
21056 ++p;
21057 eap->nextcmd = check_nextcmd(p);
21058 return;
21059 }
21060
21061 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021062 * Get the function name. There are these situations:
21063 * func normal function name
21064 * "name" == func, "fudi.fd_dict" == NULL
21065 * dict.func new dictionary entry
21066 * "name" == NULL, "fudi.fd_dict" set,
21067 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21068 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021069 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021070 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21071 * dict.func existing dict entry that's not a Funcref
21072 * "name" == NULL, "fudi.fd_dict" set,
21073 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021076 name = trans_function_name(&p, eap->skip, 0, &fudi);
21077 paren = (vim_strchr(p, '(') != NULL);
21078 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079 {
21080 /*
21081 * Return on an invalid expression in braces, unless the expression
21082 * evaluation has been cancelled due to an aborting error, an
21083 * interrupt, or an exception.
21084 */
21085 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021086 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021087 if (!eap->skip && fudi.fd_newkey != NULL)
21088 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021089 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 else
21093 eap->skip = TRUE;
21094 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021095
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 /* An error in a function call during evaluation of an expression in magic
21097 * braces should not cause the function not to be defined. */
21098 saved_did_emsg = did_emsg;
21099 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100
21101 /*
21102 * ":function func" with only function name: list function.
21103 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021104 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 {
21106 if (!ends_excmd(*skipwhite(p)))
21107 {
21108 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021109 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 }
21111 eap->nextcmd = check_nextcmd(p);
21112 if (eap->nextcmd != NULL)
21113 *p = NUL;
21114 if (!eap->skip && !got_int)
21115 {
21116 fp = find_func(name);
21117 if (fp != NULL)
21118 {
21119 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021120 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021122 if (FUNCLINE(fp, j) == NULL)
21123 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 msg_putchar('\n');
21125 msg_outnum((long)(j + 1));
21126 if (j < 9)
21127 msg_putchar(' ');
21128 if (j < 99)
21129 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021130 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 out_flush(); /* show a line at a time */
21132 ui_breakcheck();
21133 }
21134 if (!got_int)
21135 {
21136 msg_putchar('\n');
21137 msg_puts((char_u *)" endfunction");
21138 }
21139 }
21140 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021141 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021143 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 }
21145
21146 /*
21147 * ":function name(arg1, arg2)" Define function.
21148 */
21149 p = skipwhite(p);
21150 if (*p != '(')
21151 {
21152 if (!eap->skip)
21153 {
21154 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021155 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 }
21157 /* attempt to continue by skipping some text */
21158 if (vim_strchr(p, '(') != NULL)
21159 p = vim_strchr(p, '(');
21160 }
21161 p = skipwhite(p + 1);
21162
21163 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21164 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21165
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021166 if (!eap->skip)
21167 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021168 /* Check the name of the function. Unless it's a dictionary function
21169 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021170 if (name != NULL)
21171 arg = name;
21172 else
21173 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021174 if (arg != NULL && (fudi.fd_di == NULL
21175 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021176 {
21177 if (*arg == K_SPECIAL)
21178 j = 3;
21179 else
21180 j = 0;
21181 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21182 : eval_isnamec(arg[j])))
21183 ++j;
21184 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021185 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021186 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021187 /* Disallow using the g: dict. */
21188 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21189 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021190 }
21191
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 /*
21193 * Isolate the arguments: "arg1, arg2, ...)"
21194 */
21195 while (*p != ')')
21196 {
21197 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21198 {
21199 varargs = TRUE;
21200 p += 3;
21201 mustend = TRUE;
21202 }
21203 else
21204 {
21205 arg = p;
21206 while (ASCII_ISALNUM(*p) || *p == '_')
21207 ++p;
21208 if (arg == p || isdigit(*arg)
21209 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21210 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21211 {
21212 if (!eap->skip)
21213 EMSG2(_("E125: Illegal argument: %s"), arg);
21214 break;
21215 }
21216 if (ga_grow(&newargs, 1) == FAIL)
21217 goto erret;
21218 c = *p;
21219 *p = NUL;
21220 arg = vim_strsave(arg);
21221 if (arg == NULL)
21222 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021223
21224 /* Check for duplicate argument name. */
21225 for (i = 0; i < newargs.ga_len; ++i)
21226 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21227 {
21228 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21229 goto erret;
21230 }
21231
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21233 *p = c;
21234 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 if (*p == ',')
21236 ++p;
21237 else
21238 mustend = TRUE;
21239 }
21240 p = skipwhite(p);
21241 if (mustend && *p != ')')
21242 {
21243 if (!eap->skip)
21244 EMSG2(_(e_invarg2), eap->arg);
21245 break;
21246 }
21247 }
21248 ++p; /* skip the ')' */
21249
Bram Moolenaare9a41262005-01-15 22:18:47 +000021250 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 for (;;)
21252 {
21253 p = skipwhite(p);
21254 if (STRNCMP(p, "range", 5) == 0)
21255 {
21256 flags |= FC_RANGE;
21257 p += 5;
21258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021259 else if (STRNCMP(p, "dict", 4) == 0)
21260 {
21261 flags |= FC_DICT;
21262 p += 4;
21263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 else if (STRNCMP(p, "abort", 5) == 0)
21265 {
21266 flags |= FC_ABORT;
21267 p += 5;
21268 }
21269 else
21270 break;
21271 }
21272
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021273 /* When there is a line break use what follows for the function body.
21274 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21275 if (*p == '\n')
21276 line_arg = p + 1;
21277 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 EMSG(_(e_trailing));
21279
21280 /*
21281 * Read the body of the function, until ":endfunction" is found.
21282 */
21283 if (KeyTyped)
21284 {
21285 /* Check if the function already exists, don't let the user type the
21286 * whole function before telling him it doesn't work! For a script we
21287 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021288 if (!eap->skip && !eap->forceit)
21289 {
21290 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21291 EMSG(_(e_funcdict));
21292 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021293 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021296 if (!eap->skip && did_emsg)
21297 goto erret;
21298
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 msg_putchar('\n'); /* don't overwrite the function name */
21300 cmdline_row = msg_row;
21301 }
21302
21303 indent = 2;
21304 nesting = 0;
21305 for (;;)
21306 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021307 if (KeyTyped)
21308 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021310 sourcing_lnum_off = sourcing_lnum;
21311
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021312 if (line_arg != NULL)
21313 {
21314 /* Use eap->arg, split up in parts by line breaks. */
21315 theline = line_arg;
21316 p = vim_strchr(theline, '\n');
21317 if (p == NULL)
21318 line_arg += STRLEN(line_arg);
21319 else
21320 {
21321 *p = NUL;
21322 line_arg = p + 1;
21323 }
21324 }
21325 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 theline = getcmdline(':', 0L, indent);
21327 else
21328 theline = eap->getline(':', eap->cookie, indent);
21329 if (KeyTyped)
21330 lines_left = Rows - 1;
21331 if (theline == NULL)
21332 {
21333 EMSG(_("E126: Missing :endfunction"));
21334 goto erret;
21335 }
21336
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021337 /* Detect line continuation: sourcing_lnum increased more than one. */
21338 if (sourcing_lnum > sourcing_lnum_off + 1)
21339 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21340 else
21341 sourcing_lnum_off = 0;
21342
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 if (skip_until != NULL)
21344 {
21345 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21346 * don't check for ":endfunc". */
21347 if (STRCMP(theline, skip_until) == 0)
21348 {
21349 vim_free(skip_until);
21350 skip_until = NULL;
21351 }
21352 }
21353 else
21354 {
21355 /* skip ':' and blanks*/
21356 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21357 ;
21358
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021359 /* Check for "endfunction". */
21360 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021362 if (line_arg == NULL)
21363 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364 break;
21365 }
21366
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021367 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 * at "end". */
21369 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21370 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021371 else if (STRNCMP(p, "if", 2) == 0
21372 || STRNCMP(p, "wh", 2) == 0
21373 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 || STRNCMP(p, "try", 3) == 0)
21375 indent += 2;
21376
21377 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021378 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021380 if (*p == '!')
21381 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 p += eval_fname_script(p);
21383 if (ASCII_ISALPHA(*p))
21384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021385 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 if (*skipwhite(p) == '(')
21387 {
21388 ++nesting;
21389 indent += 2;
21390 }
21391 }
21392 }
21393
21394 /* Check for ":append" or ":insert". */
21395 p = skip_range(p, NULL);
21396 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21397 || (p[0] == 'i'
21398 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21399 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21400 skip_until = vim_strsave((char_u *)".");
21401
21402 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21403 arg = skipwhite(skiptowhite(p));
21404 if (arg[0] == '<' && arg[1] =='<'
21405 && ((p[0] == 'p' && p[1] == 'y'
21406 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21407 || (p[0] == 'p' && p[1] == 'e'
21408 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21409 || (p[0] == 't' && p[1] == 'c'
21410 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021411 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21412 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21414 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021415 || (p[0] == 'm' && p[1] == 'z'
21416 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 ))
21418 {
21419 /* ":python <<" continues until a dot, like ":append" */
21420 p = skipwhite(arg + 2);
21421 if (*p == NUL)
21422 skip_until = vim_strsave((char_u *)".");
21423 else
21424 skip_until = vim_strsave(p);
21425 }
21426 }
21427
21428 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021429 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021430 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021431 if (line_arg == NULL)
21432 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021434 }
21435
21436 /* Copy the line to newly allocated memory. get_one_sourceline()
21437 * allocates 250 bytes per line, this saves 80% on average. The cost
21438 * is an extra alloc/free. */
21439 p = vim_strsave(theline);
21440 if (p != NULL)
21441 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021442 if (line_arg == NULL)
21443 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021444 theline = p;
21445 }
21446
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021447 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21448
21449 /* Add NULL lines for continuation lines, so that the line count is
21450 * equal to the index in the growarray. */
21451 while (sourcing_lnum_off-- > 0)
21452 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021453
21454 /* Check for end of eap->arg. */
21455 if (line_arg != NULL && *line_arg == NUL)
21456 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 }
21458
21459 /* Don't define the function when skipping commands or when an error was
21460 * detected. */
21461 if (eap->skip || did_emsg)
21462 goto erret;
21463
21464 /*
21465 * If there are no errors, add the function
21466 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021467 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021468 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021469 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021470 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021471 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021472 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021473 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021474 goto erret;
21475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021476
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021477 fp = find_func(name);
21478 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021480 if (!eap->forceit)
21481 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021482 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021483 goto erret;
21484 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021485 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021486 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021487 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021488 name);
21489 goto erret;
21490 }
21491 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021492 ga_clear_strings(&(fp->uf_args));
21493 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021494 vim_free(name);
21495 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 }
21498 else
21499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 char numbuf[20];
21501
21502 fp = NULL;
21503 if (fudi.fd_newkey == NULL && !eap->forceit)
21504 {
21505 EMSG(_(e_funcdict));
21506 goto erret;
21507 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021508 if (fudi.fd_di == NULL)
21509 {
21510 /* Can't add a function to a locked dictionary */
21511 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21512 goto erret;
21513 }
21514 /* Can't change an existing function if it is locked */
21515 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21516 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021517
21518 /* Give the function a sequential number. Can only be used with a
21519 * Funcref! */
21520 vim_free(name);
21521 sprintf(numbuf, "%d", ++func_nr);
21522 name = vim_strsave((char_u *)numbuf);
21523 if (name == NULL)
21524 goto erret;
21525 }
21526
21527 if (fp == NULL)
21528 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021529 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021530 {
21531 int slen, plen;
21532 char_u *scriptname;
21533
21534 /* Check that the autoload name matches the script name. */
21535 j = FAIL;
21536 if (sourcing_name != NULL)
21537 {
21538 scriptname = autoload_name(name);
21539 if (scriptname != NULL)
21540 {
21541 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021542 plen = (int)STRLEN(p);
21543 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021544 if (slen > plen && fnamecmp(p,
21545 sourcing_name + slen - plen) == 0)
21546 j = OK;
21547 vim_free(scriptname);
21548 }
21549 }
21550 if (j == FAIL)
21551 {
21552 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21553 goto erret;
21554 }
21555 }
21556
21557 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 if (fp == NULL)
21559 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021560
21561 if (fudi.fd_dict != NULL)
21562 {
21563 if (fudi.fd_di == NULL)
21564 {
21565 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021566 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021567 if (fudi.fd_di == NULL)
21568 {
21569 vim_free(fp);
21570 goto erret;
21571 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021572 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21573 {
21574 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021575 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021576 goto erret;
21577 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021578 }
21579 else
21580 /* overwrite existing dict entry */
21581 clear_tv(&fudi.fd_di->di_tv);
21582 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021583 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021585 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021586
21587 /* behave like "dict" was used */
21588 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021589 }
21590
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021592 STRCPY(fp->uf_name, name);
21593 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021595 fp->uf_args = newargs;
21596 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021597#ifdef FEAT_PROFILE
21598 fp->uf_tml_count = NULL;
21599 fp->uf_tml_total = NULL;
21600 fp->uf_tml_self = NULL;
21601 fp->uf_profiling = FALSE;
21602 if (prof_def_func())
21603 func_do_profile(fp);
21604#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021605 fp->uf_varargs = varargs;
21606 fp->uf_flags = flags;
21607 fp->uf_calls = 0;
21608 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021609 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610
21611erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 ga_clear_strings(&newargs);
21613 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021614ret_free:
21615 vim_free(skip_until);
21616 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619}
21620
21621/*
21622 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021623 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021625 * flags:
21626 * TFN_INT: internal function name OK
21627 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 * Advances "pp" to just after the function name (if no error).
21629 */
21630 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021631trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 char_u **pp;
21633 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021635 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021637 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 char_u *start;
21639 char_u *end;
21640 int lead;
21641 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021643 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021644
21645 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021646 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021648
21649 /* Check for hard coded <SNR>: already translated function ID (from a user
21650 * command). */
21651 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21652 && (*pp)[2] == (int)KE_SNR)
21653 {
21654 *pp += 3;
21655 len = get_id_len(pp) + 3;
21656 return vim_strnsave(start, len);
21657 }
21658
21659 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21660 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021662 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021664
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021665 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21666 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021667 if (end == start)
21668 {
21669 if (!skip)
21670 EMSG(_("E129: Function name required"));
21671 goto theend;
21672 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021673 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021674 {
21675 /*
21676 * Report an invalid expression in braces, unless the expression
21677 * evaluation has been cancelled due to an aborting error, an
21678 * interrupt, or an exception.
21679 */
21680 if (!aborting())
21681 {
21682 if (end != NULL)
21683 EMSG2(_(e_invarg2), start);
21684 }
21685 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021686 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021687 goto theend;
21688 }
21689
21690 if (lv.ll_tv != NULL)
21691 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021692 if (fdp != NULL)
21693 {
21694 fdp->fd_dict = lv.ll_dict;
21695 fdp->fd_newkey = lv.ll_newkey;
21696 lv.ll_newkey = NULL;
21697 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021699 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21700 {
21701 name = vim_strsave(lv.ll_tv->vval.v_string);
21702 *pp = end;
21703 }
21704 else
21705 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021706 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21707 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 EMSG(_(e_funcref));
21709 else
21710 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021711 name = NULL;
21712 }
21713 goto theend;
21714 }
21715
21716 if (lv.ll_name == NULL)
21717 {
21718 /* Error found, but continue after the function name. */
21719 *pp = end;
21720 goto theend;
21721 }
21722
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021723 /* Check if the name is a Funcref. If so, use the value. */
21724 if (lv.ll_exp_name != NULL)
21725 {
21726 len = (int)STRLEN(lv.ll_exp_name);
21727 name = deref_func_name(lv.ll_exp_name, &len);
21728 if (name == lv.ll_exp_name)
21729 name = NULL;
21730 }
21731 else
21732 {
21733 len = (int)(end - *pp);
21734 name = deref_func_name(*pp, &len);
21735 if (name == *pp)
21736 name = NULL;
21737 }
21738 if (name != NULL)
21739 {
21740 name = vim_strsave(name);
21741 *pp = end;
21742 goto theend;
21743 }
21744
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021745 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021746 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021747 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021748 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21749 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21750 {
21751 /* When there was "s:" already or the name expanded to get a
21752 * leading "s:" then remove it. */
21753 lv.ll_name += 2;
21754 len -= 2;
21755 lead = 2;
21756 }
21757 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021758 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021759 {
21760 if (lead == 2) /* skip over "s:" */
21761 lv.ll_name += 2;
21762 len = (int)(end - lv.ll_name);
21763 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021764
21765 /*
21766 * Copy the function name to allocated memory.
21767 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21768 * Accept <SNR>123_name() outside a script.
21769 */
21770 if (skip)
21771 lead = 0; /* do nothing */
21772 else if (lead > 0)
21773 {
21774 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021775 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21776 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021777 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021778 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021779 if (current_SID <= 0)
21780 {
21781 EMSG(_(e_usingsid));
21782 goto theend;
21783 }
21784 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21785 lead += (int)STRLEN(sid_buf);
21786 }
21787 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021788 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021789 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021790 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021791 goto theend;
21792 }
21793 name = alloc((unsigned)(len + lead + 1));
21794 if (name != NULL)
21795 {
21796 if (lead > 0)
21797 {
21798 name[0] = K_SPECIAL;
21799 name[1] = KS_EXTRA;
21800 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021801 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021802 STRCPY(name + 3, sid_buf);
21803 }
21804 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21805 name[len + lead] = NUL;
21806 }
21807 *pp = end;
21808
21809theend:
21810 clear_lval(&lv);
21811 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812}
21813
21814/*
21815 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21816 * Return 2 if "p" starts with "s:".
21817 * Return 0 otherwise.
21818 */
21819 static int
21820eval_fname_script(p)
21821 char_u *p;
21822{
21823 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21824 || STRNICMP(p + 1, "SNR>", 4) == 0))
21825 return 5;
21826 if (p[0] == 's' && p[1] == ':')
21827 return 2;
21828 return 0;
21829}
21830
21831/*
21832 * Return TRUE if "p" starts with "<SID>" or "s:".
21833 * Only works if eval_fname_script() returned non-zero for "p"!
21834 */
21835 static int
21836eval_fname_sid(p)
21837 char_u *p;
21838{
21839 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21840}
21841
21842/*
21843 * List the head of the function: "name(arg1, arg2)".
21844 */
21845 static void
21846list_func_head(fp, indent)
21847 ufunc_T *fp;
21848 int indent;
21849{
21850 int j;
21851
21852 msg_start();
21853 if (indent)
21854 MSG_PUTS(" ");
21855 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021856 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 {
21858 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021859 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 }
21861 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021862 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021864 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 {
21866 if (j)
21867 MSG_PUTS(", ");
21868 msg_puts(FUNCARG(fp, j));
21869 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021870 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 {
21872 if (j)
21873 MSG_PUTS(", ");
21874 MSG_PUTS("...");
21875 }
21876 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021877 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021878 if (p_verbose > 0)
21879 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880}
21881
21882/*
21883 * Find a function by name, return pointer to it in ufuncs.
21884 * Return NULL for unknown function.
21885 */
21886 static ufunc_T *
21887find_func(name)
21888 char_u *name;
21889{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021892 hi = hash_find(&func_hashtab, name);
21893 if (!HASHITEM_EMPTY(hi))
21894 return HI2UF(hi);
21895 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896}
21897
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021898#if defined(EXITFREE) || defined(PROTO)
21899 void
21900free_all_functions()
21901{
21902 hashitem_T *hi;
21903
21904 /* Need to start all over every time, because func_free() may change the
21905 * hash table. */
21906 while (func_hashtab.ht_used > 0)
21907 for (hi = func_hashtab.ht_array; ; ++hi)
21908 if (!HASHITEM_EMPTY(hi))
21909 {
21910 func_free(HI2UF(hi));
21911 break;
21912 }
21913}
21914#endif
21915
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021916/*
21917 * Return TRUE if a function "name" exists.
21918 */
21919 static int
21920function_exists(name)
21921 char_u *name;
21922{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021923 char_u *nm = name;
21924 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 int n = FALSE;
21926
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021927 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021928 nm = skipwhite(nm);
21929
21930 /* Only accept "funcname", "funcname ", "funcname (..." and
21931 * "funcname(...", not "funcname!...". */
21932 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021933 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021934 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021935 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021936 else
21937 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021938 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021939 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021940 return n;
21941}
21942
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021943/*
21944 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021945 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021946 */
21947 static int
21948builtin_function(name)
21949 char_u *name;
21950{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021951 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21952 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021953}
21954
Bram Moolenaar05159a02005-02-26 23:04:13 +000021955#if defined(FEAT_PROFILE) || defined(PROTO)
21956/*
21957 * Start profiling function "fp".
21958 */
21959 static void
21960func_do_profile(fp)
21961 ufunc_T *fp;
21962{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021963 int len = fp->uf_lines.ga_len;
21964
21965 if (len == 0)
21966 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021967 fp->uf_tm_count = 0;
21968 profile_zero(&fp->uf_tm_self);
21969 profile_zero(&fp->uf_tm_total);
21970 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021971 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021972 if (fp->uf_tml_total == NULL)
21973 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021974 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021975 if (fp->uf_tml_self == NULL)
21976 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021977 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021978 fp->uf_tml_idx = -1;
21979 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21980 || fp->uf_tml_self == NULL)
21981 return; /* out of memory */
21982
21983 fp->uf_profiling = TRUE;
21984}
21985
21986/*
21987 * Dump the profiling results for all functions in file "fd".
21988 */
21989 void
21990func_dump_profile(fd)
21991 FILE *fd;
21992{
21993 hashitem_T *hi;
21994 int todo;
21995 ufunc_T *fp;
21996 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021997 ufunc_T **sorttab;
21998 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021999
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022000 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022001 if (todo == 0)
22002 return; /* nothing to dump */
22003
Bram Moolenaar73830342005-02-28 22:48:19 +000022004 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22005
Bram Moolenaar05159a02005-02-26 23:04:13 +000022006 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22007 {
22008 if (!HASHITEM_EMPTY(hi))
22009 {
22010 --todo;
22011 fp = HI2UF(hi);
22012 if (fp->uf_profiling)
22013 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022014 if (sorttab != NULL)
22015 sorttab[st_len++] = fp;
22016
Bram Moolenaar05159a02005-02-26 23:04:13 +000022017 if (fp->uf_name[0] == K_SPECIAL)
22018 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22019 else
22020 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22021 if (fp->uf_tm_count == 1)
22022 fprintf(fd, "Called 1 time\n");
22023 else
22024 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22025 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22026 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22027 fprintf(fd, "\n");
22028 fprintf(fd, "count total (s) self (s)\n");
22029
22030 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22031 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022032 if (FUNCLINE(fp, i) == NULL)
22033 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022034 prof_func_line(fd, fp->uf_tml_count[i],
22035 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022036 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22037 }
22038 fprintf(fd, "\n");
22039 }
22040 }
22041 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022042
22043 if (sorttab != NULL && st_len > 0)
22044 {
22045 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22046 prof_total_cmp);
22047 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22048 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22049 prof_self_cmp);
22050 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22051 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022052
22053 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022054}
Bram Moolenaar73830342005-02-28 22:48:19 +000022055
22056 static void
22057prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22058 FILE *fd;
22059 ufunc_T **sorttab;
22060 int st_len;
22061 char *title;
22062 int prefer_self; /* when equal print only self time */
22063{
22064 int i;
22065 ufunc_T *fp;
22066
22067 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22068 fprintf(fd, "count total (s) self (s) function\n");
22069 for (i = 0; i < 20 && i < st_len; ++i)
22070 {
22071 fp = sorttab[i];
22072 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22073 prefer_self);
22074 if (fp->uf_name[0] == K_SPECIAL)
22075 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22076 else
22077 fprintf(fd, " %s()\n", fp->uf_name);
22078 }
22079 fprintf(fd, "\n");
22080}
22081
22082/*
22083 * Print the count and times for one function or function line.
22084 */
22085 static void
22086prof_func_line(fd, count, total, self, prefer_self)
22087 FILE *fd;
22088 int count;
22089 proftime_T *total;
22090 proftime_T *self;
22091 int prefer_self; /* when equal print only self time */
22092{
22093 if (count > 0)
22094 {
22095 fprintf(fd, "%5d ", count);
22096 if (prefer_self && profile_equal(total, self))
22097 fprintf(fd, " ");
22098 else
22099 fprintf(fd, "%s ", profile_msg(total));
22100 if (!prefer_self && profile_equal(total, self))
22101 fprintf(fd, " ");
22102 else
22103 fprintf(fd, "%s ", profile_msg(self));
22104 }
22105 else
22106 fprintf(fd, " ");
22107}
22108
22109/*
22110 * Compare function for total time sorting.
22111 */
22112 static int
22113#ifdef __BORLANDC__
22114_RTLENTRYF
22115#endif
22116prof_total_cmp(s1, s2)
22117 const void *s1;
22118 const void *s2;
22119{
22120 ufunc_T *p1, *p2;
22121
22122 p1 = *(ufunc_T **)s1;
22123 p2 = *(ufunc_T **)s2;
22124 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22125}
22126
22127/*
22128 * Compare function for self time sorting.
22129 */
22130 static int
22131#ifdef __BORLANDC__
22132_RTLENTRYF
22133#endif
22134prof_self_cmp(s1, s2)
22135 const void *s1;
22136 const void *s2;
22137{
22138 ufunc_T *p1, *p2;
22139
22140 p1 = *(ufunc_T **)s1;
22141 p2 = *(ufunc_T **)s2;
22142 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22143}
22144
Bram Moolenaar05159a02005-02-26 23:04:13 +000022145#endif
22146
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022147/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022148 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022149 * Return TRUE if a package was loaded.
22150 */
22151 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022152script_autoload(name, reload)
22153 char_u *name;
22154 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022155{
22156 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022157 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022158 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022159 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022160
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022161 /* Return quickly when autoload disabled. */
22162 if (no_autoload)
22163 return FALSE;
22164
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022165 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022166 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022167 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022168 return FALSE;
22169
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022170 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022171
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022172 /* Find the name in the list of previously loaded package names. Skip
22173 * "autoload/", it's always the same. */
22174 for (i = 0; i < ga_loaded.ga_len; ++i)
22175 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22176 break;
22177 if (!reload && i < ga_loaded.ga_len)
22178 ret = FALSE; /* was loaded already */
22179 else
22180 {
22181 /* Remember the name if it wasn't loaded already. */
22182 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22183 {
22184 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22185 tofree = NULL;
22186 }
22187
22188 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022189 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022190 ret = TRUE;
22191 }
22192
22193 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022194 return ret;
22195}
22196
22197/*
22198 * Return the autoload script name for a function or variable name.
22199 * Returns NULL when out of memory.
22200 */
22201 static char_u *
22202autoload_name(name)
22203 char_u *name;
22204{
22205 char_u *p;
22206 char_u *scriptname;
22207
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022208 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022209 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22210 if (scriptname == NULL)
22211 return FALSE;
22212 STRCPY(scriptname, "autoload/");
22213 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022214 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022215 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022216 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022217 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022218 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022219}
22220
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22222
22223/*
22224 * Function given to ExpandGeneric() to obtain the list of user defined
22225 * function names.
22226 */
22227 char_u *
22228get_user_func_name(xp, idx)
22229 expand_T *xp;
22230 int idx;
22231{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022232 static long_u done;
22233 static hashitem_T *hi;
22234 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235
22236 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022238 done = 0;
22239 hi = func_hashtab.ht_array;
22240 }
22241 if (done < func_hashtab.ht_used)
22242 {
22243 if (done++ > 0)
22244 ++hi;
22245 while (HASHITEM_EMPTY(hi))
22246 ++hi;
22247 fp = HI2UF(hi);
22248
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022249 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022250 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022251
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022252 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22253 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254
22255 cat_func_name(IObuff, fp);
22256 if (xp->xp_context != EXPAND_USER_FUNC)
22257 {
22258 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022259 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 STRCAT(IObuff, ")");
22261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 return IObuff;
22263 }
22264 return NULL;
22265}
22266
22267#endif /* FEAT_CMDL_COMPL */
22268
22269/*
22270 * Copy the function name of "fp" to buffer "buf".
22271 * "buf" must be able to hold the function name plus three bytes.
22272 * Takes care of script-local function names.
22273 */
22274 static void
22275cat_func_name(buf, fp)
22276 char_u *buf;
22277 ufunc_T *fp;
22278{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022279 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 {
22281 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022282 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 }
22284 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022285 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286}
22287
22288/*
22289 * ":delfunction {name}"
22290 */
22291 void
22292ex_delfunction(eap)
22293 exarg_T *eap;
22294{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022295 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 char_u *p;
22297 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022298 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299
22300 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022301 name = trans_function_name(&p, eap->skip, 0, &fudi);
22302 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022304 {
22305 if (fudi.fd_dict != NULL && !eap->skip)
22306 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 if (!ends_excmd(*skipwhite(p)))
22310 {
22311 vim_free(name);
22312 EMSG(_(e_trailing));
22313 return;
22314 }
22315 eap->nextcmd = check_nextcmd(p);
22316 if (eap->nextcmd != NULL)
22317 *p = NUL;
22318
22319 if (!eap->skip)
22320 fp = find_func(name);
22321 vim_free(name);
22322
22323 if (!eap->skip)
22324 {
22325 if (fp == NULL)
22326 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022327 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 return;
22329 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022330 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331 {
22332 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22333 return;
22334 }
22335
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022336 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022338 /* Delete the dict item that refers to the function, it will
22339 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022340 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 else
22343 func_free(fp);
22344 }
22345}
22346
22347/*
22348 * Free a function and remove it from the list of functions.
22349 */
22350 static void
22351func_free(fp)
22352 ufunc_T *fp;
22353{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022354 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022355
22356 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022357 ga_clear_strings(&(fp->uf_args));
22358 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022359#ifdef FEAT_PROFILE
22360 vim_free(fp->uf_tml_count);
22361 vim_free(fp->uf_tml_total);
22362 vim_free(fp->uf_tml_self);
22363#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022364
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022365 /* remove the function from the function hashtable */
22366 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22367 if (HASHITEM_EMPTY(hi))
22368 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022369 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022370 hash_remove(&func_hashtab, hi);
22371
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022372 vim_free(fp);
22373}
22374
22375/*
22376 * Unreference a Function: decrement the reference count and free it when it
22377 * becomes zero. Only for numbered functions.
22378 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022379 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022380func_unref(name)
22381 char_u *name;
22382{
22383 ufunc_T *fp;
22384
22385 if (name != NULL && isdigit(*name))
22386 {
22387 fp = find_func(name);
22388 if (fp == NULL)
22389 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022390 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022391 {
22392 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022393 * when "uf_calls" becomes zero. */
22394 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022395 func_free(fp);
22396 }
22397 }
22398}
22399
22400/*
22401 * Count a reference to a Function.
22402 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022403 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022404func_ref(name)
22405 char_u *name;
22406{
22407 ufunc_T *fp;
22408
22409 if (name != NULL && isdigit(*name))
22410 {
22411 fp = find_func(name);
22412 if (fp == NULL)
22413 EMSG2(_(e_intern2), "func_ref()");
22414 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022415 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 }
22417}
22418
22419/*
22420 * Call a user function.
22421 */
22422 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022423call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 ufunc_T *fp; /* pointer to function */
22425 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 typval_T *argvars; /* arguments */
22427 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 linenr_T firstline; /* first line of range */
22429 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431{
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 char_u *save_sourcing_name;
22433 linenr_T save_sourcing_lnum;
22434 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022435 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022436 int save_did_emsg;
22437 static int depth = 0;
22438 dictitem_T *v;
22439 int fixvar_idx = 0; /* index in fixvar[] */
22440 int i;
22441 int ai;
22442 char_u numbuf[NUMBUFLEN];
22443 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022444#ifdef FEAT_PROFILE
22445 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022446 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448
22449 /* If depth of calling is getting too high, don't execute the function */
22450 if (depth >= p_mfd)
22451 {
22452 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022453 rettv->v_type = VAR_NUMBER;
22454 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 return;
22456 }
22457 ++depth;
22458
22459 line_breakcheck(); /* check for CTRL-C hit */
22460
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022461 fc = (funccall_T *)alloc(sizeof(funccall_T));
22462 fc->caller = current_funccal;
22463 current_funccal = fc;
22464 fc->func = fp;
22465 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022466 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022467 fc->linenr = 0;
22468 fc->returned = FALSE;
22469 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022471 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22472 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473
Bram Moolenaar33570922005-01-25 22:26:29 +000022474 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022475 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22477 * each argument variable and saves a lot of time.
22478 */
22479 /*
22480 * Init l: variables.
22481 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022482 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022483 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022484 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022485 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22486 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022487 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022488 name = v->di_key;
22489 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022491 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022493 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 v->di_tv.vval.v_dict = selfdict;
22495 ++selfdict->dv_refcount;
22496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022497
Bram Moolenaar33570922005-01-25 22:26:29 +000022498 /*
22499 * Init a: variables.
22500 * Set a:0 to "argcount".
22501 * Set a:000 to a list with room for the "..." arguments.
22502 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022503 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022504 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022505 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022506 /* Use "name" to avoid a warning from some compiler that checks the
22507 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022508 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022509 name = v->di_key;
22510 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022512 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022513 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022514 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022515 v->di_tv.vval.v_list = &fc->l_varlist;
22516 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22517 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22518 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022519
22520 /*
22521 * Set a:firstline to "firstline" and a:lastline to "lastline".
22522 * Set a:name to named arguments.
22523 * Set a:N to the "..." arguments.
22524 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022525 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022527 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022528 (varnumber_T)lastline);
22529 for (i = 0; i < argcount; ++i)
22530 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022531 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022532 if (ai < 0)
22533 /* named argument a:name */
22534 name = FUNCARG(fp, i);
22535 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 /* "..." argument a:1, a:2, etc. */
22538 sprintf((char *)numbuf, "%d", ai + 1);
22539 name = numbuf;
22540 }
22541 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22542 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022543 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22545 }
22546 else
22547 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022548 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22549 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 if (v == NULL)
22551 break;
22552 v->di_flags = DI_FLAGS_RO;
22553 }
22554 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022555 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022556
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022557 /* Note: the values are copied directly to avoid alloc/free.
22558 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022559 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022560 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022561
22562 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22563 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022564 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22565 fc->l_listitems[ai].li_tv = argvars[i];
22566 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022567 }
22568 }
22569
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 /* Don't redraw while executing the function. */
22571 ++RedrawingDisabled;
22572 save_sourcing_name = sourcing_name;
22573 save_sourcing_lnum = sourcing_lnum;
22574 sourcing_lnum = 1;
22575 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022576 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 if (sourcing_name != NULL)
22578 {
22579 if (save_sourcing_name != NULL
22580 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22581 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22582 else
22583 STRCPY(sourcing_name, "function ");
22584 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22585
22586 if (p_verbose >= 12)
22587 {
22588 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022589 verbose_enter_scroll();
22590
Bram Moolenaar555b2802005-05-19 21:08:39 +000022591 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 if (p_verbose >= 14)
22593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022595 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022596 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022597 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598
22599 msg_puts((char_u *)"(");
22600 for (i = 0; i < argcount; ++i)
22601 {
22602 if (i > 0)
22603 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022604 if (argvars[i].v_type == VAR_NUMBER)
22605 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 else
22607 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022608 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22609 if (s != NULL)
22610 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022611 if (vim_strsize(s) > MSG_BUF_CLEN)
22612 {
22613 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22614 s = buf;
22615 }
22616 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022617 vim_free(tofree);
22618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 }
22620 }
22621 msg_puts((char_u *)")");
22622 }
22623 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022624
22625 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 --no_wait_return;
22627 }
22628 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022629#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022630 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022631 {
22632 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22633 func_do_profile(fp);
22634 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022635 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022636 {
22637 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022638 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022639 profile_zero(&fp->uf_tm_children);
22640 }
22641 script_prof_save(&wait_start);
22642 }
22643#endif
22644
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022646 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647 save_did_emsg = did_emsg;
22648 did_emsg = FALSE;
22649
22650 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022651 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22653
22654 --RedrawingDisabled;
22655
22656 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022657 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022659 clear_tv(rettv);
22660 rettv->v_type = VAR_NUMBER;
22661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 }
22663
Bram Moolenaar05159a02005-02-26 23:04:13 +000022664#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022665 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022666 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022667 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022668 profile_end(&call_start);
22669 profile_sub_wait(&wait_start, &call_start);
22670 profile_add(&fp->uf_tm_total, &call_start);
22671 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022672 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022673 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022674 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22675 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022676 }
22677 }
22678#endif
22679
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 /* when being verbose, mention the return value */
22681 if (p_verbose >= 12)
22682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022684 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022687 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022688 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022689 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022690 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022693 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022694 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022695 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022696 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022697
Bram Moolenaar555b2802005-05-19 21:08:39 +000022698 /* The value may be very long. Skip the middle part, so that we
22699 * have some idea how it starts and ends. smsg() would always
22700 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022701 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022702 if (s != NULL)
22703 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022704 if (vim_strsize(s) > MSG_BUF_CLEN)
22705 {
22706 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22707 s = buf;
22708 }
22709 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022710 vim_free(tofree);
22711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
22713 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022714
22715 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 --no_wait_return;
22717 }
22718
22719 vim_free(sourcing_name);
22720 sourcing_name = save_sourcing_name;
22721 sourcing_lnum = save_sourcing_lnum;
22722 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022723#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022724 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022725 script_prof_restore(&wait_start);
22726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727
22728 if (p_verbose >= 12 && sourcing_name != NULL)
22729 {
22730 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022731 verbose_enter_scroll();
22732
Bram Moolenaar555b2802005-05-19 21:08:39 +000022733 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022735
22736 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 --no_wait_return;
22738 }
22739
22740 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022741 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022743
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022744 /* If the a:000 list and the l: and a: dicts are not referenced we can
22745 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022746 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22747 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22748 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22749 {
22750 free_funccal(fc, FALSE);
22751 }
22752 else
22753 {
22754 hashitem_T *hi;
22755 listitem_T *li;
22756 int todo;
22757
22758 /* "fc" is still in use. This can happen when returning "a:000" or
22759 * assigning "l:" to a global variable.
22760 * Link "fc" in the list for garbage collection later. */
22761 fc->caller = previous_funccal;
22762 previous_funccal = fc;
22763
22764 /* Make a copy of the a: variables, since we didn't do that above. */
22765 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22766 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22767 {
22768 if (!HASHITEM_EMPTY(hi))
22769 {
22770 --todo;
22771 v = HI2DI(hi);
22772 copy_tv(&v->di_tv, &v->di_tv);
22773 }
22774 }
22775
22776 /* Make a copy of the a:000 items, since we didn't do that above. */
22777 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22778 copy_tv(&li->li_tv, &li->li_tv);
22779 }
22780}
22781
22782/*
22783 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022784 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022785 */
22786 static int
22787can_free_funccal(fc, copyID)
22788 funccall_T *fc;
22789 int copyID;
22790{
22791 return (fc->l_varlist.lv_copyID != copyID
22792 && fc->l_vars.dv_copyID != copyID
22793 && fc->l_avars.dv_copyID != copyID);
22794}
22795
22796/*
22797 * Free "fc" and what it contains.
22798 */
22799 static void
22800free_funccal(fc, free_val)
22801 funccall_T *fc;
22802 int free_val; /* a: vars were allocated */
22803{
22804 listitem_T *li;
22805
22806 /* The a: variables typevals may not have been allocated, only free the
22807 * allocated variables. */
22808 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22809
22810 /* free all l: variables */
22811 vars_clear(&fc->l_vars.dv_hashtab);
22812
22813 /* Free the a:000 variables if they were allocated. */
22814 if (free_val)
22815 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22816 clear_tv(&li->li_tv);
22817
22818 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819}
22820
22821/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 * Add a number variable "name" to dict "dp" with value "nr".
22823 */
22824 static void
22825add_nr_var(dp, v, name, nr)
22826 dict_T *dp;
22827 dictitem_T *v;
22828 char *name;
22829 varnumber_T nr;
22830{
22831 STRCPY(v->di_key, name);
22832 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22833 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22834 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022835 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022836 v->di_tv.vval.v_number = nr;
22837}
22838
22839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 * ":return [expr]"
22841 */
22842 void
22843ex_return(eap)
22844 exarg_T *eap;
22845{
22846 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022847 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 int returning = FALSE;
22849
22850 if (current_funccal == NULL)
22851 {
22852 EMSG(_("E133: :return not inside a function"));
22853 return;
22854 }
22855
22856 if (eap->skip)
22857 ++emsg_skip;
22858
22859 eap->nextcmd = NULL;
22860 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022861 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 {
22863 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022864 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022866 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 }
22868 /* It's safer to return also on error. */
22869 else if (!eap->skip)
22870 {
22871 /*
22872 * Return unless the expression evaluation has been cancelled due to an
22873 * aborting error, an interrupt, or an exception.
22874 */
22875 if (!aborting())
22876 returning = do_return(eap, FALSE, TRUE, NULL);
22877 }
22878
22879 /* When skipping or the return gets pending, advance to the next command
22880 * in this line (!returning). Otherwise, ignore the rest of the line.
22881 * Following lines will be ignored by get_func_line(). */
22882 if (returning)
22883 eap->nextcmd = NULL;
22884 else if (eap->nextcmd == NULL) /* no argument */
22885 eap->nextcmd = check_nextcmd(arg);
22886
22887 if (eap->skip)
22888 --emsg_skip;
22889}
22890
22891/*
22892 * Return from a function. Possibly makes the return pending. Also called
22893 * for a pending return at the ":endtry" or after returning from an extra
22894 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022896 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 * FALSE when the return gets pending.
22898 */
22899 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022900do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 exarg_T *eap;
22902 int reanimate;
22903 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022904 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905{
22906 int idx;
22907 struct condstack *cstack = eap->cstack;
22908
22909 if (reanimate)
22910 /* Undo the return. */
22911 current_funccal->returned = FALSE;
22912
22913 /*
22914 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22915 * not in its finally clause (which then is to be executed next) is found.
22916 * In this case, make the ":return" pending for execution at the ":endtry".
22917 * Otherwise, return normally.
22918 */
22919 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22920 if (idx >= 0)
22921 {
22922 cstack->cs_pending[idx] = CSTP_RETURN;
22923
22924 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022925 /* A pending return again gets pending. "rettv" points to an
22926 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022928 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 else
22930 {
22931 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022932 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022934 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022936 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 {
22938 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022939 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022940 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 else
22942 EMSG(_(e_outofmem));
22943 }
22944 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022945 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946
22947 if (reanimate)
22948 {
22949 /* The pending return value could be overwritten by a ":return"
22950 * without argument in a finally clause; reset the default
22951 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022952 current_funccal->rettv->v_type = VAR_NUMBER;
22953 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 }
22955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022956 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 }
22958 else
22959 {
22960 current_funccal->returned = TRUE;
22961
22962 /* If the return is carried out now, store the return value. For
22963 * a return immediately after reanimation, the value is already
22964 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022965 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022967 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022968 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022970 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
22972 }
22973
22974 return idx < 0;
22975}
22976
22977/*
22978 * Free the variable with a pending return value.
22979 */
22980 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022981discard_pending_return(rettv)
22982 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983{
Bram Moolenaar33570922005-01-25 22:26:29 +000022984 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985}
22986
22987/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022988 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 * is an allocated string. Used by report_pending() for verbose messages.
22990 */
22991 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022992get_return_cmd(rettv)
22993 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022995 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022996 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022997 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022999 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023000 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023001 if (s == NULL)
23002 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023003
23004 STRCPY(IObuff, ":return ");
23005 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23006 if (STRLEN(s) + 8 >= IOSIZE)
23007 STRCPY(IObuff + IOSIZE - 4, "...");
23008 vim_free(tofree);
23009 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010}
23011
23012/*
23013 * Get next function line.
23014 * Called by do_cmdline() to get the next line.
23015 * Returns allocated string, or NULL for end of function.
23016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 char_u *
23018get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023019 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023021 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022{
Bram Moolenaar33570922005-01-25 22:26:29 +000023023 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023024 ufunc_T *fp = fcp->func;
23025 char_u *retval;
23026 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027
23028 /* If breakpoints have been added/deleted need to check for it. */
23029 if (fcp->dbg_tick != debug_tick)
23030 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023031 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 sourcing_lnum);
23033 fcp->dbg_tick = debug_tick;
23034 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023035#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023036 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023037 func_line_end(cookie);
23038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039
Bram Moolenaar05159a02005-02-26 23:04:13 +000023040 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023041 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23042 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 retval = NULL;
23044 else
23045 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023046 /* Skip NULL lines (continuation lines). */
23047 while (fcp->linenr < gap->ga_len
23048 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23049 ++fcp->linenr;
23050 if (fcp->linenr >= gap->ga_len)
23051 retval = NULL;
23052 else
23053 {
23054 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23055 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023056#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023057 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023058 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023059#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 }
23062
23063 /* Did we encounter a breakpoint? */
23064 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23065 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023066 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023068 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069 sourcing_lnum);
23070 fcp->dbg_tick = debug_tick;
23071 }
23072
23073 return retval;
23074}
23075
Bram Moolenaar05159a02005-02-26 23:04:13 +000023076#if defined(FEAT_PROFILE) || defined(PROTO)
23077/*
23078 * Called when starting to read a function line.
23079 * "sourcing_lnum" must be correct!
23080 * When skipping lines it may not actually be executed, but we won't find out
23081 * until later and we need to store the time now.
23082 */
23083 void
23084func_line_start(cookie)
23085 void *cookie;
23086{
23087 funccall_T *fcp = (funccall_T *)cookie;
23088 ufunc_T *fp = fcp->func;
23089
23090 if (fp->uf_profiling && sourcing_lnum >= 1
23091 && sourcing_lnum <= fp->uf_lines.ga_len)
23092 {
23093 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023094 /* Skip continuation lines. */
23095 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23096 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023097 fp->uf_tml_execed = FALSE;
23098 profile_start(&fp->uf_tml_start);
23099 profile_zero(&fp->uf_tml_children);
23100 profile_get_wait(&fp->uf_tml_wait);
23101 }
23102}
23103
23104/*
23105 * Called when actually executing a function line.
23106 */
23107 void
23108func_line_exec(cookie)
23109 void *cookie;
23110{
23111 funccall_T *fcp = (funccall_T *)cookie;
23112 ufunc_T *fp = fcp->func;
23113
23114 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23115 fp->uf_tml_execed = TRUE;
23116}
23117
23118/*
23119 * Called when done with a function line.
23120 */
23121 void
23122func_line_end(cookie)
23123 void *cookie;
23124{
23125 funccall_T *fcp = (funccall_T *)cookie;
23126 ufunc_T *fp = fcp->func;
23127
23128 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23129 {
23130 if (fp->uf_tml_execed)
23131 {
23132 ++fp->uf_tml_count[fp->uf_tml_idx];
23133 profile_end(&fp->uf_tml_start);
23134 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023135 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023136 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23137 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023138 }
23139 fp->uf_tml_idx = -1;
23140 }
23141}
23142#endif
23143
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144/*
23145 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023146 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 */
23148 int
23149func_has_ended(cookie)
23150 void *cookie;
23151{
Bram Moolenaar33570922005-01-25 22:26:29 +000023152 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153
23154 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23155 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023156 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 || fcp->returned);
23158}
23159
23160/*
23161 * return TRUE if cookie indicates a function which "abort"s on errors.
23162 */
23163 int
23164func_has_abort(cookie)
23165 void *cookie;
23166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023167 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168}
23169
23170#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23171typedef enum
23172{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023173 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23174 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23175 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176} var_flavour_T;
23177
23178static var_flavour_T var_flavour __ARGS((char_u *varname));
23179
23180 static var_flavour_T
23181var_flavour(varname)
23182 char_u *varname;
23183{
23184 char_u *p = varname;
23185
23186 if (ASCII_ISUPPER(*p))
23187 {
23188 while (*(++p))
23189 if (ASCII_ISLOWER(*p))
23190 return VAR_FLAVOUR_SESSION;
23191 return VAR_FLAVOUR_VIMINFO;
23192 }
23193 else
23194 return VAR_FLAVOUR_DEFAULT;
23195}
23196#endif
23197
23198#if defined(FEAT_VIMINFO) || defined(PROTO)
23199/*
23200 * Restore global vars that start with a capital from the viminfo file
23201 */
23202 int
23203read_viminfo_varlist(virp, writing)
23204 vir_T *virp;
23205 int writing;
23206{
23207 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023208 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023209 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210
23211 if (!writing && (find_viminfo_parameter('!') != NULL))
23212 {
23213 tab = vim_strchr(virp->vir_line + 1, '\t');
23214 if (tab != NULL)
23215 {
23216 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023217 switch (*tab)
23218 {
23219 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023220#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023221 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023222#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023223 case 'D': type = VAR_DICT; break;
23224 case 'L': type = VAR_LIST; break;
23225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226
23227 tab = vim_strchr(tab, '\t');
23228 if (tab != NULL)
23229 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023230 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023231 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023232 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023234#ifdef FEAT_FLOAT
23235 else if (type == VAR_FLOAT)
23236 (void)string2float(tab + 1, &tv.vval.v_float);
23237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023239 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023240 if (type == VAR_DICT || type == VAR_LIST)
23241 {
23242 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23243
23244 if (etv == NULL)
23245 /* Failed to parse back the dict or list, use it as a
23246 * string. */
23247 tv.v_type = VAR_STRING;
23248 else
23249 {
23250 vim_free(tv.vval.v_string);
23251 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023252 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023253 }
23254 }
23255
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023256 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023257
23258 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023259 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023260 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23261 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 }
23263 }
23264 }
23265
23266 return viminfo_readline(virp);
23267}
23268
23269/*
23270 * Write global vars that start with a capital to the viminfo file
23271 */
23272 void
23273write_viminfo_varlist(fp)
23274 FILE *fp;
23275{
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 hashitem_T *hi;
23277 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023278 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023279 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023280 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023281 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023282 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283
23284 if (find_viminfo_parameter('!') == NULL)
23285 return;
23286
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023287 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023289 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023290 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023292 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023294 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023295 this_var = HI2DI(hi);
23296 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023297 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023298 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023299 {
23300 case VAR_STRING: s = "STR"; break;
23301 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023302#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023303 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023304#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023305 case VAR_DICT: s = "DIC"; break;
23306 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023307 default: continue;
23308 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023309 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023310 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023311 if (p != NULL)
23312 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023313 vim_free(tofree);
23314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 }
23316 }
23317}
23318#endif
23319
23320#if defined(FEAT_SESSION) || defined(PROTO)
23321 int
23322store_session_globals(fd)
23323 FILE *fd;
23324{
Bram Moolenaar33570922005-01-25 22:26:29 +000023325 hashitem_T *hi;
23326 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023327 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 char_u *p, *t;
23329
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 ((this_var->di_tv.v_type == VAR_NUMBER
23338 || this_var->di_tv.v_type == VAR_STRING)
23339 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023340 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023341 /* Escape special characters with a backslash. Turn a LF and
23342 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023343 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023344 (char_u *)"\\\"\n\r");
23345 if (p == NULL) /* out of memory */
23346 break;
23347 for (t = p; *t != NUL; ++t)
23348 if (*t == '\n')
23349 *t = 'n';
23350 else if (*t == '\r')
23351 *t = 'r';
23352 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 this_var->di_key,
23354 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23355 : ' ',
23356 p,
23357 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23358 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023359 || put_eol(fd) == FAIL)
23360 {
23361 vim_free(p);
23362 return FAIL;
23363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 vim_free(p);
23365 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023366#ifdef FEAT_FLOAT
23367 else if (this_var->di_tv.v_type == VAR_FLOAT
23368 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23369 {
23370 float_T f = this_var->di_tv.vval.v_float;
23371 int sign = ' ';
23372
23373 if (f < 0)
23374 {
23375 f = -f;
23376 sign = '-';
23377 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023378 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023379 this_var->di_key, sign, f) < 0)
23380 || put_eol(fd) == FAIL)
23381 return FAIL;
23382 }
23383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 }
23385 }
23386 return OK;
23387}
23388#endif
23389
Bram Moolenaar661b1822005-07-28 22:36:45 +000023390/*
23391 * Display script name where an item was last set.
23392 * Should only be invoked when 'verbose' is non-zero.
23393 */
23394 void
23395last_set_msg(scriptID)
23396 scid_T scriptID;
23397{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023398 char_u *p;
23399
Bram Moolenaar661b1822005-07-28 22:36:45 +000023400 if (scriptID != 0)
23401 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023402 p = home_replace_save(NULL, get_scriptname(scriptID));
23403 if (p != NULL)
23404 {
23405 verbose_enter();
23406 MSG_PUTS(_("\n\tLast set from "));
23407 MSG_PUTS(p);
23408 vim_free(p);
23409 verbose_leave();
23410 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023411 }
23412}
23413
Bram Moolenaard812df62008-11-09 12:46:09 +000023414/*
23415 * List v:oldfiles in a nice way.
23416 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023417 void
23418ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023419 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023420{
23421 list_T *l = vimvars[VV_OLDFILES].vv_list;
23422 listitem_T *li;
23423 int nr = 0;
23424
23425 if (l == NULL)
23426 msg((char_u *)_("No old files"));
23427 else
23428 {
23429 msg_start();
23430 msg_scroll = TRUE;
23431 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23432 {
23433 msg_outnum((long)++nr);
23434 MSG_PUTS(": ");
23435 msg_outtrans(get_tv_string(&li->li_tv));
23436 msg_putchar('\n');
23437 out_flush(); /* output one line at a time */
23438 ui_breakcheck();
23439 }
23440 /* Assume "got_int" was set to truncate the listing. */
23441 got_int = FALSE;
23442
23443#ifdef FEAT_BROWSE_CMD
23444 if (cmdmod.browse)
23445 {
23446 quit_more = FALSE;
23447 nr = prompt_for_number(FALSE);
23448 msg_starthere();
23449 if (nr > 0)
23450 {
23451 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23452 (long)nr);
23453
23454 if (p != NULL)
23455 {
23456 p = expand_env_save(p);
23457 eap->arg = p;
23458 eap->cmdidx = CMD_edit;
23459 cmdmod.browse = FALSE;
23460 do_exedit(eap, NULL);
23461 vim_free(p);
23462 }
23463 }
23464 }
23465#endif
23466 }
23467}
23468
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469#endif /* FEAT_EVAL */
23470
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023472#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473
23474#ifdef WIN3264
23475/*
23476 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23477 */
23478static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23479static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23480static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23481
23482/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023483 * Get the short path (8.3) for the filename in "fnamep".
23484 * Only works for a valid file name.
23485 * When the path gets longer "fnamep" is changed and the allocated buffer
23486 * is put in "bufp".
23487 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23488 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 */
23490 static int
23491get_short_pathname(fnamep, bufp, fnamelen)
23492 char_u **fnamep;
23493 char_u **bufp;
23494 int *fnamelen;
23495{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023496 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023497 char_u *newbuf;
23498
23499 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 l = GetShortPathName(*fnamep, *fnamep, len);
23501 if (l > len - 1)
23502 {
23503 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023504 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 newbuf = vim_strnsave(*fnamep, l);
23506 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023507 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508
23509 vim_free(*bufp);
23510 *fnamep = *bufp = newbuf;
23511
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023512 /* Really should always succeed, as the buffer is big enough. */
23513 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 }
23515
23516 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023517 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518}
23519
23520/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023521 * Get the short path (8.3) for the filename in "fname". The converted
23522 * path is returned in "bufp".
23523 *
23524 * Some of the directories specified in "fname" may not exist. This function
23525 * will shorten the existing directories at the beginning of the path and then
23526 * append the remaining non-existing path.
23527 *
23528 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023529 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023530 * bufp - Pointer to an allocated buffer for the filename.
23531 * fnamelen - Length of the filename pointed to by fname
23532 *
23533 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 */
23535 static int
23536shortpath_for_invalid_fname(fname, bufp, fnamelen)
23537 char_u **fname;
23538 char_u **bufp;
23539 int *fnamelen;
23540{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023541 char_u *short_fname, *save_fname, *pbuf_unused;
23542 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023544 int old_len, len;
23545 int new_len, sfx_len;
23546 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547
23548 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023549 old_len = *fnamelen;
23550 save_fname = vim_strnsave(*fname, old_len);
23551 pbuf_unused = NULL;
23552 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023554 endp = save_fname + old_len - 1; /* Find the end of the copy */
23555 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023557 /*
23558 * Try shortening the supplied path till it succeeds by removing one
23559 * directory at a time from the tail of the path.
23560 */
23561 len = 0;
23562 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023564 /* go back one path-separator */
23565 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23566 --endp;
23567 if (endp <= save_fname)
23568 break; /* processed the complete path */
23569
23570 /*
23571 * Replace the path separator with a NUL and try to shorten the
23572 * resulting path.
23573 */
23574 ch = *endp;
23575 *endp = 0;
23576 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023577 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023578 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23579 {
23580 retval = FAIL;
23581 goto theend;
23582 }
23583 *endp = ch; /* preserve the string */
23584
23585 if (len > 0)
23586 break; /* successfully shortened the path */
23587
23588 /* failed to shorten the path. Skip the path separator */
23589 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 }
23591
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023592 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023594 /*
23595 * Succeeded in shortening the path. Now concatenate the shortened
23596 * path with the remaining path at the tail.
23597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023599 /* Compute the length of the new path. */
23600 sfx_len = (int)(save_endp - endp) + 1;
23601 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023603 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023605 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023607 /* There is not enough space in the currently allocated string,
23608 * copy it to a buffer big enough. */
23609 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023611 {
23612 retval = FAIL;
23613 goto theend;
23614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 }
23616 else
23617 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023618 /* Transfer short_fname to the main buffer (it's big enough),
23619 * unless get_short_pathname() did its work in-place. */
23620 *fname = *bufp = save_fname;
23621 if (short_fname != save_fname)
23622 vim_strncpy(save_fname, short_fname, len);
23623 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023625
23626 /* concat the not-shortened part of the path */
23627 vim_strncpy(*fname + len, endp, sfx_len);
23628 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023630
23631theend:
23632 vim_free(pbuf_unused);
23633 vim_free(save_fname);
23634
23635 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636}
23637
23638/*
23639 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023640 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 */
23642 static int
23643shortpath_for_partial(fnamep, bufp, fnamelen)
23644 char_u **fnamep;
23645 char_u **bufp;
23646 int *fnamelen;
23647{
23648 int sepcount, len, tflen;
23649 char_u *p;
23650 char_u *pbuf, *tfname;
23651 int hasTilde;
23652
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023653 /* Count up the path separators from the RHS.. so we know which part
23654 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023656 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 if (vim_ispathsep(*p))
23658 ++sepcount;
23659
23660 /* Need full path first (use expand_env() to remove a "~/") */
23661 hasTilde = (**fnamep == '~');
23662 if (hasTilde)
23663 pbuf = tfname = expand_env_save(*fnamep);
23664 else
23665 pbuf = tfname = FullName_save(*fnamep, FALSE);
23666
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023667 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023669 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23670 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671
23672 if (len == 0)
23673 {
23674 /* Don't have a valid filename, so shorten the rest of the
23675 * path if we can. This CAN give us invalid 8.3 filenames, but
23676 * there's not a lot of point in guessing what it might be.
23677 */
23678 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023679 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 }
23682
23683 /* Count the paths backward to find the beginning of the desired string. */
23684 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023685 {
23686#ifdef FEAT_MBYTE
23687 if (has_mbyte)
23688 p -= mb_head_off(tfname, p);
23689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 if (vim_ispathsep(*p))
23691 {
23692 if (sepcount == 0 || (hasTilde && sepcount == 1))
23693 break;
23694 else
23695 sepcount --;
23696 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 if (hasTilde)
23699 {
23700 --p;
23701 if (p >= tfname)
23702 *p = '~';
23703 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023704 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 }
23706 else
23707 ++p;
23708
23709 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23710 vim_free(*bufp);
23711 *fnamelen = (int)STRLEN(p);
23712 *bufp = pbuf;
23713 *fnamep = p;
23714
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023715 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023716}
23717#endif /* WIN3264 */
23718
23719/*
23720 * Adjust a filename, according to a string of modifiers.
23721 * *fnamep must be NUL terminated when called. When returning, the length is
23722 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023723 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 * When there is an error, *fnamep is set to NULL.
23725 */
23726 int
23727modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23728 char_u *src; /* string with modifiers */
23729 int *usedlen; /* characters after src that are used */
23730 char_u **fnamep; /* file name so far */
23731 char_u **bufp; /* buffer for allocated file name or NULL */
23732 int *fnamelen; /* length of fnamep */
23733{
23734 int valid = 0;
23735 char_u *tail;
23736 char_u *s, *p, *pbuf;
23737 char_u dirname[MAXPATHL];
23738 int c;
23739 int has_fullname = 0;
23740#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023741 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 int has_shortname = 0;
23743#endif
23744
23745repeat:
23746 /* ":p" - full path/file_name */
23747 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23748 {
23749 has_fullname = 1;
23750
23751 valid |= VALID_PATH;
23752 *usedlen += 2;
23753
23754 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23755 if ((*fnamep)[0] == '~'
23756#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23757 && ((*fnamep)[1] == '/'
23758# ifdef BACKSLASH_IN_FILENAME
23759 || (*fnamep)[1] == '\\'
23760# endif
23761 || (*fnamep)[1] == NUL)
23762
23763#endif
23764 )
23765 {
23766 *fnamep = expand_env_save(*fnamep);
23767 vim_free(*bufp); /* free any allocated file name */
23768 *bufp = *fnamep;
23769 if (*fnamep == NULL)
23770 return -1;
23771 }
23772
23773 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023774 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 {
23776 if (vim_ispathsep(*p)
23777 && p[1] == '.'
23778 && (p[2] == NUL
23779 || vim_ispathsep(p[2])
23780 || (p[2] == '.'
23781 && (p[3] == NUL || vim_ispathsep(p[3])))))
23782 break;
23783 }
23784
23785 /* FullName_save() is slow, don't use it when not needed. */
23786 if (*p != NUL || !vim_isAbsName(*fnamep))
23787 {
23788 *fnamep = FullName_save(*fnamep, *p != NUL);
23789 vim_free(*bufp); /* free any allocated file name */
23790 *bufp = *fnamep;
23791 if (*fnamep == NULL)
23792 return -1;
23793 }
23794
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023795#ifdef WIN3264
23796# if _WIN32_WINNT >= 0x0500
23797 if (vim_strchr(*fnamep, '~') != NULL)
23798 {
23799 /* Expand 8.3 filename to full path. Needed to make sure the same
23800 * file does not have two different names.
23801 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23802 p = alloc(_MAX_PATH + 1);
23803 if (p != NULL)
23804 {
23805 if (GetLongPathName(*fnamep, p, MAXPATHL))
23806 {
23807 vim_free(*bufp);
23808 *bufp = *fnamep = p;
23809 }
23810 else
23811 vim_free(p);
23812 }
23813 }
23814# endif
23815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 /* Append a path separator to a directory. */
23817 if (mch_isdir(*fnamep))
23818 {
23819 /* Make room for one or two extra characters. */
23820 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23821 vim_free(*bufp); /* free any allocated file name */
23822 *bufp = *fnamep;
23823 if (*fnamep == NULL)
23824 return -1;
23825 add_pathsep(*fnamep);
23826 }
23827 }
23828
23829 /* ":." - path relative to the current directory */
23830 /* ":~" - path relative to the home directory */
23831 /* ":8" - shortname path - postponed till after */
23832 while (src[*usedlen] == ':'
23833 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23834 {
23835 *usedlen += 2;
23836 if (c == '8')
23837 {
23838#ifdef WIN3264
23839 has_shortname = 1; /* Postpone this. */
23840#endif
23841 continue;
23842 }
23843 pbuf = NULL;
23844 /* Need full path first (use expand_env() to remove a "~/") */
23845 if (!has_fullname)
23846 {
23847 if (c == '.' && **fnamep == '~')
23848 p = pbuf = expand_env_save(*fnamep);
23849 else
23850 p = pbuf = FullName_save(*fnamep, FALSE);
23851 }
23852 else
23853 p = *fnamep;
23854
23855 has_fullname = 0;
23856
23857 if (p != NULL)
23858 {
23859 if (c == '.')
23860 {
23861 mch_dirname(dirname, MAXPATHL);
23862 s = shorten_fname(p, dirname);
23863 if (s != NULL)
23864 {
23865 *fnamep = s;
23866 if (pbuf != NULL)
23867 {
23868 vim_free(*bufp); /* free any allocated file name */
23869 *bufp = pbuf;
23870 pbuf = NULL;
23871 }
23872 }
23873 }
23874 else
23875 {
23876 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23877 /* Only replace it when it starts with '~' */
23878 if (*dirname == '~')
23879 {
23880 s = vim_strsave(dirname);
23881 if (s != NULL)
23882 {
23883 *fnamep = s;
23884 vim_free(*bufp);
23885 *bufp = s;
23886 }
23887 }
23888 }
23889 vim_free(pbuf);
23890 }
23891 }
23892
23893 tail = gettail(*fnamep);
23894 *fnamelen = (int)STRLEN(*fnamep);
23895
23896 /* ":h" - head, remove "/file_name", can be repeated */
23897 /* Don't remove the first "/" or "c:\" */
23898 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23899 {
23900 valid |= VALID_HEAD;
23901 *usedlen += 2;
23902 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023903 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023904 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 *fnamelen = (int)(tail - *fnamep);
23906#ifdef VMS
23907 if (*fnamelen > 0)
23908 *fnamelen += 1; /* the path separator is part of the path */
23909#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023910 if (*fnamelen == 0)
23911 {
23912 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23913 p = vim_strsave((char_u *)".");
23914 if (p == NULL)
23915 return -1;
23916 vim_free(*bufp);
23917 *bufp = *fnamep = tail = p;
23918 *fnamelen = 1;
23919 }
23920 else
23921 {
23922 while (tail > s && !after_pathsep(s, tail))
23923 mb_ptr_back(*fnamep, tail);
23924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 }
23926
23927 /* ":8" - shortname */
23928 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23929 {
23930 *usedlen += 2;
23931#ifdef WIN3264
23932 has_shortname = 1;
23933#endif
23934 }
23935
23936#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023937 /*
23938 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 */
23940 if (has_shortname)
23941 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023942 /* Copy the string if it is shortened by :h and when it wasn't copied
23943 * yet, because we are going to change it in place. Avoids changing
23944 * the buffer name for "%:8". */
23945 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 {
23947 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023948 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 return -1;
23950 vim_free(*bufp);
23951 *bufp = *fnamep = p;
23952 }
23953
23954 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023955 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 if (!has_fullname && !vim_isAbsName(*fnamep))
23957 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023958 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 return -1;
23960 }
23961 else
23962 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023963 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964
Bram Moolenaardc935552011-08-17 15:23:23 +020023965 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023967 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 return -1;
23969
23970 if (l == 0)
23971 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023972 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023974 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 return -1;
23976 }
23977 *fnamelen = l;
23978 }
23979 }
23980#endif /* WIN3264 */
23981
23982 /* ":t" - tail, just the basename */
23983 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23984 {
23985 *usedlen += 2;
23986 *fnamelen -= (int)(tail - *fnamep);
23987 *fnamep = tail;
23988 }
23989
23990 /* ":e" - extension, can be repeated */
23991 /* ":r" - root, without extension, can be repeated */
23992 while (src[*usedlen] == ':'
23993 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23994 {
23995 /* find a '.' in the tail:
23996 * - for second :e: before the current fname
23997 * - otherwise: The last '.'
23998 */
23999 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24000 s = *fnamep - 2;
24001 else
24002 s = *fnamep + *fnamelen - 1;
24003 for ( ; s > tail; --s)
24004 if (s[0] == '.')
24005 break;
24006 if (src[*usedlen + 1] == 'e') /* :e */
24007 {
24008 if (s > tail)
24009 {
24010 *fnamelen += (int)(*fnamep - (s + 1));
24011 *fnamep = s + 1;
24012#ifdef VMS
24013 /* cut version from the extension */
24014 s = *fnamep + *fnamelen - 1;
24015 for ( ; s > *fnamep; --s)
24016 if (s[0] == ';')
24017 break;
24018 if (s > *fnamep)
24019 *fnamelen = s - *fnamep;
24020#endif
24021 }
24022 else if (*fnamep <= tail)
24023 *fnamelen = 0;
24024 }
24025 else /* :r */
24026 {
24027 if (s > tail) /* remove one extension */
24028 *fnamelen = (int)(s - *fnamep);
24029 }
24030 *usedlen += 2;
24031 }
24032
24033 /* ":s?pat?foo?" - substitute */
24034 /* ":gs?pat?foo?" - global substitute */
24035 if (src[*usedlen] == ':'
24036 && (src[*usedlen + 1] == 's'
24037 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24038 {
24039 char_u *str;
24040 char_u *pat;
24041 char_u *sub;
24042 int sep;
24043 char_u *flags;
24044 int didit = FALSE;
24045
24046 flags = (char_u *)"";
24047 s = src + *usedlen + 2;
24048 if (src[*usedlen + 1] == 'g')
24049 {
24050 flags = (char_u *)"g";
24051 ++s;
24052 }
24053
24054 sep = *s++;
24055 if (sep)
24056 {
24057 /* find end of pattern */
24058 p = vim_strchr(s, sep);
24059 if (p != NULL)
24060 {
24061 pat = vim_strnsave(s, (int)(p - s));
24062 if (pat != NULL)
24063 {
24064 s = p + 1;
24065 /* find end of substitution */
24066 p = vim_strchr(s, sep);
24067 if (p != NULL)
24068 {
24069 sub = vim_strnsave(s, (int)(p - s));
24070 str = vim_strnsave(*fnamep, *fnamelen);
24071 if (sub != NULL && str != NULL)
24072 {
24073 *usedlen = (int)(p + 1 - src);
24074 s = do_string_sub(str, pat, sub, flags);
24075 if (s != NULL)
24076 {
24077 *fnamep = s;
24078 *fnamelen = (int)STRLEN(s);
24079 vim_free(*bufp);
24080 *bufp = s;
24081 didit = TRUE;
24082 }
24083 }
24084 vim_free(sub);
24085 vim_free(str);
24086 }
24087 vim_free(pat);
24088 }
24089 }
24090 /* after using ":s", repeat all the modifiers */
24091 if (didit)
24092 goto repeat;
24093 }
24094 }
24095
24096 return valid;
24097}
24098
24099/*
24100 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24101 * "flags" can be "g" to do a global substitute.
24102 * Returns an allocated string, NULL for error.
24103 */
24104 char_u *
24105do_string_sub(str, pat, sub, flags)
24106 char_u *str;
24107 char_u *pat;
24108 char_u *sub;
24109 char_u *flags;
24110{
24111 int sublen;
24112 regmatch_T regmatch;
24113 int i;
24114 int do_all;
24115 char_u *tail;
24116 garray_T ga;
24117 char_u *ret;
24118 char_u *save_cpo;
24119
24120 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24121 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024122 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123
24124 ga_init2(&ga, 1, 200);
24125
24126 do_all = (flags[0] == 'g');
24127
24128 regmatch.rm_ic = p_ic;
24129 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24130 if (regmatch.regprog != NULL)
24131 {
24132 tail = str;
24133 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24134 {
24135 /*
24136 * Get some space for a temporary buffer to do the substitution
24137 * into. It will contain:
24138 * - The text up to where the match is.
24139 * - The substituted text.
24140 * - The text after the match.
24141 */
24142 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24143 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24144 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24145 {
24146 ga_clear(&ga);
24147 break;
24148 }
24149
24150 /* copy the text up to where the match is */
24151 i = (int)(regmatch.startp[0] - tail);
24152 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24153 /* add the substituted text */
24154 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24155 + ga.ga_len + i, TRUE, TRUE, FALSE);
24156 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 /* avoid getting stuck on a match with an empty string */
24158 if (tail == regmatch.endp[0])
24159 {
24160 if (*tail == NUL)
24161 break;
24162 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24163 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 }
24165 else
24166 {
24167 tail = regmatch.endp[0];
24168 if (*tail == NUL)
24169 break;
24170 }
24171 if (!do_all)
24172 break;
24173 }
24174
24175 if (ga.ga_data != NULL)
24176 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24177
24178 vim_free(regmatch.regprog);
24179 }
24180
24181 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24182 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024183 if (p_cpo == empty_option)
24184 p_cpo = save_cpo;
24185 else
24186 /* Darn, evaluating {sub} expression changed the value. */
24187 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188
24189 return ret;
24190}
24191
24192#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */