blob: 83233c9daade8b4eafa04177c5e03265b5c1311e [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 long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100416static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
417static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
418static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000419static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000421static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
423static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000424static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000425static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100426static 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 +0000427static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000428static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200429static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
431static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000432static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000434static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000436static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
437static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000439#ifdef FEAT_FLOAT
440static int string2float __ARGS((char_u *text, float_T *value));
441#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
443static int find_internal_func __ARGS((char_u *name));
444static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
445static 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 +0200446static 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 +0000447static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000448static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000450#ifdef FEAT_FLOAT
451static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200452static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000454static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100455static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200461static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200463static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000465static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#ifdef FEAT_FLOAT
477static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
478#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000479static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000482static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000484#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000485static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000486static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000489static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#ifdef FEAT_FLOAT
492static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200493static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
498static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200509#ifdef FEAT_FLOAT
510static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
511#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000512static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000514static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#ifdef FEAT_FLOAT
521static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000524#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000525static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000534static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000535static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000536static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000542static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000550static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000551static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000552static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000553static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200556static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000557static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000565static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000579static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000580static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100584static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000586static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000587static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000598#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200599static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000600static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
601#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200602#ifdef FEAT_LUA
603static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000609static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000610static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000611static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000612static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000613static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000617#ifdef vim_mkdir
618static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
619#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100621#ifdef FEAT_MZSCHEME
622static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
623#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100626static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000628#ifdef FEAT_FLOAT
629static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
630#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000632static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000633static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200634#ifdef FEAT_PYTHON3
635static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
637#ifdef FEAT_PYTHON
638static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000640static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000641static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000642static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
643static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
645static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
647static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
655static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100657static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000660static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000662static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000669static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000670static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000671static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000672static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200674static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000675static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100677#ifdef FEAT_CRYPT
678static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
679#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000680static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200681static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000682static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000683#ifdef FEAT_FLOAT
684static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200685static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000686#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000688static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000689static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
690static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
693static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
694static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
695#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000696static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200697static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698#ifdef HAVE_STRFTIME
699static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
701static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
702static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
703static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
706static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200707static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000714static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200715static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000717static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000718static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000719static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000720static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000721static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000722static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000723static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200724#ifdef FEAT_FLOAT
725static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
726static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
727#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
732static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
733#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000734static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200735static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200736static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000737static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
738static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100740static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
742static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000747static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000749static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000750static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100751static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000752
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000753static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000754static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000755static int get_env_len __ARGS((char_u **arg));
756static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000758static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
759#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
760#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
761 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000762static 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 +0000763static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000764static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
766static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000767static typval_T *alloc_tv __ARGS((void));
768static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000769static void init_tv __ARGS((typval_T *varp));
770static long get_tv_number __ARGS((typval_T *varp));
771static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000772static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773static char_u *get_tv_string __ARGS((typval_T *varp));
774static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000775static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200777static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
779static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
780static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000781static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
782static 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 +0000783static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
784static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000785static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200786static int var_check_func_name __ARGS((char_u *name, int new_var));
787static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000788static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000789static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
791static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
792static int eval_fname_script __ARGS((char_u *p));
793static int eval_fname_sid __ARGS((char_u *p));
794static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static ufunc_T *find_func __ARGS((char_u *name));
796static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000797static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798#ifdef FEAT_PROFILE
799static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000800static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
801static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
802static int
803# ifdef __BORLANDC__
804 _RTLENTRYF
805# endif
806 prof_total_cmp __ARGS((const void *s1, const void *s2));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200813static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000814static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000815static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000817static 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 +0000818static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
819static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000820static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000821static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
822static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000823static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000824static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000825static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200827
828#ifdef EBCDIC
829static int compare_func_name __ARGS((const void *s1, const void *s2));
830static void sortFunctions __ARGS(());
831#endif
832
Bram Moolenaar33570922005-01-25 22:26:29 +0000833/*
834 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000835 */
836 void
837eval_init()
838{
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 int i;
840 struct vimvar *p;
841
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200842 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
843 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200844 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000845 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000846 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000847
848 for (i = 0; i < VV_LEN; ++i)
849 {
850 p = &vimvars[i];
851 STRCPY(p->vv_di.di_key, p->vv_name);
852 if (p->vv_flags & VV_RO)
853 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
854 else if (p->vv_flags & VV_RO_SBX)
855 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
856 else
857 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000858
859 /* add to v: scope dict, unless the value is not always available */
860 if (p->vv_type != VAR_UNKNOWN)
861 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000863 /* add to compat scope dict */
864 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000865 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000866 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200867 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200868
869#ifdef EBCDIC
870 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100871 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200872 */
873 sortFunctions();
874#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000875}
876
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000877#if defined(EXITFREE) || defined(PROTO)
878 void
879eval_clear()
880{
881 int i;
882 struct vimvar *p;
883
884 for (i = 0; i < VV_LEN; ++i)
885 {
886 p = &vimvars[i];
887 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000888 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000889 vim_free(p->vv_str);
890 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000891 }
892 else if (p->vv_di.di_tv.v_type == VAR_LIST)
893 {
894 list_unref(p->vv_list);
895 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000896 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000897 }
898 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000899 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000900 hash_clear(&compat_hashtab);
901
Bram Moolenaard9fba312005-06-26 22:34:35 +0000902 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100903# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200904 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100905# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000906
907 /* global variables */
908 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000910 /* autoloaded script names */
911 ga_clear_strings(&ga_loaded);
912
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200913 /* script-local variables */
914 for (i = 1; i <= ga_scripts.ga_len; ++i)
915 {
916 vars_clear(&SCRIPT_VARS(i));
917 vim_free(SCRIPT_SV(i));
918 }
919 ga_clear(&ga_scripts);
920
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000921 /* unreferenced lists and dicts */
922 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000923
924 /* functions */
925 free_all_functions();
926 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000927}
928#endif
929
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * Return the name of the executed function.
932 */
933 char_u *
934func_name(cookie)
935 void *cookie;
936{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000937 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938}
939
940/*
941 * Return the address holding the next breakpoint line for a funccall cookie.
942 */
943 linenr_T *
944func_breakpoint(cookie)
945 void *cookie;
946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948}
949
950/*
951 * Return the address holding the debug tick for a funccall cookie.
952 */
953 int *
954func_dbg_tick(cookie)
955 void *cookie;
956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958}
959
960/*
961 * Return the nesting level for a funccall cookie.
962 */
963 int
964func_level(cookie)
965 void *cookie;
966{
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000971funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000973/* pointer to list of previously used funccal, still around because some
974 * item in it is still being used. */
975funccall_T *previous_funccal = NULL;
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977/*
978 * Return TRUE when a function was ended by a ":return" command.
979 */
980 int
981current_func_returned()
982{
983 return current_funccal->returned;
984}
985
986
987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 * Set an internal variable to a string value. Creates the variable if it does
989 * not already exist.
990 */
991 void
992set_internal_string_var(name, value)
993 char_u *name;
994 char_u *value;
995{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000997 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
999 val = vim_strsave(value);
1000 if (val != NULL)
1001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001002 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001005 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001006 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 }
1009}
1010
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001011static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001012static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013static char_u *redir_endp = NULL;
1014static char_u *redir_varname = NULL;
1015
1016/*
1017 * Start recording command output to a variable
1018 * Returns OK if successfully completed the setup. FAIL otherwise.
1019 */
1020 int
1021var_redir_start(name, append)
1022 char_u *name;
1023 int append; /* append to an existing variable */
1024{
1025 int save_emsg;
1026 int err;
1027 typval_T tv;
1028
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001029 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001030 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 {
1032 EMSG(_(e_invarg));
1033 return FAIL;
1034 }
1035
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001036 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 redir_varname = vim_strsave(name);
1038 if (redir_varname == NULL)
1039 return FAIL;
1040
1041 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1042 if (redir_lval == NULL)
1043 {
1044 var_redir_stop();
1045 return FAIL;
1046 }
1047
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001048 /* The output is stored in growarray "redir_ga" until redirection ends. */
1049 ga_init2(&redir_ga, (int)sizeof(char), 500);
1050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001052 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1053 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1055 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001056 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (redir_endp != NULL && *redir_endp != NUL)
1058 /* Trailing characters are present after the variable name */
1059 EMSG(_(e_trailing));
1060 else
1061 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001062 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 var_redir_stop();
1064 return FAIL;
1065 }
1066
1067 /* check if we can write to the variable: set it to or append an empty
1068 * string */
1069 save_emsg = did_emsg;
1070 did_emsg = FALSE;
1071 tv.v_type = VAR_STRING;
1072 tv.vval.v_string = (char_u *)"";
1073 if (append)
1074 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1075 else
1076 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001077 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001079 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 if (err)
1081 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001082 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 var_redir_stop();
1084 return FAIL;
1085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086
1087 return OK;
1088}
1089
1090/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091 * Append "value[value_len]" to the variable set by var_redir_start().
1092 * The actual appending is postponed until redirection ends, because the value
1093 * appended may in fact be the string we write to, changing it may cause freed
1094 * memory to be used:
1095 * :redir => foo
1096 * :let foo
1097 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 */
1099 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001104 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105
1106 if (redir_lval == NULL)
1107 return;
1108
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001114 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 {
1116 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001118 }
1119 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121}
1122
1123/*
1124 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 */
1127 void
1128var_redir_stop()
1129{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 typval_T tv;
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_lval != NULL)
1133 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* If there was no error: assign the text to the variable. */
1135 if (redir_endp != NULL)
1136 {
1137 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1138 tv.v_type = VAR_STRING;
1139 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001140 /* Call get_lval() again, if it's inside a Dict or List it may
1141 * have changed. */
1142 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1143 FALSE, FALSE, FALSE, FNE_CHECK_START);
1144 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1145 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1146 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001148
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* free the collected output */
1150 vim_free(redir_ga.ga_data);
1151 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 vim_free(redir_lval);
1154 redir_lval = NULL;
1155 }
1156 vim_free(redir_varname);
1157 redir_varname = NULL;
1158}
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160# if defined(FEAT_MBYTE) || defined(PROTO)
1161 int
1162eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1163 char_u *enc_from;
1164 char_u *enc_to;
1165 char_u *fname_from;
1166 char_u *fname_to;
1167{
1168 int err = FALSE;
1169
1170 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1171 set_vim_var_string(VV_CC_TO, enc_to, -1);
1172 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1173 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1174 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1175 err = TRUE;
1176 set_vim_var_string(VV_CC_FROM, NULL, -1);
1177 set_vim_var_string(VV_CC_TO, NULL, -1);
1178 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1179 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1180
1181 if (err)
1182 return FAIL;
1183 return OK;
1184}
1185# endif
1186
1187# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1188 int
1189eval_printexpr(fname, args)
1190 char_u *fname;
1191 char_u *args;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_FNAME_IN, fname, -1);
1196 set_vim_var_string(VV_CMDARG, args, -1);
1197 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1198 err = TRUE;
1199 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1200 set_vim_var_string(VV_CMDARG, NULL, -1);
1201
1202 if (err)
1203 {
1204 mch_remove(fname);
1205 return FAIL;
1206 }
1207 return OK;
1208}
1209# endif
1210
1211# if defined(FEAT_DIFF) || defined(PROTO)
1212 void
1213eval_diff(origfile, newfile, outfile)
1214 char_u *origfile;
1215 char_u *newfile;
1216 char_u *outfile;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1221 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1222 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1223 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1226 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1227}
1228
1229 void
1230eval_patch(origfile, difffile, outfile)
1231 char_u *origfile;
1232 char_u *difffile;
1233 char_u *outfile;
1234{
1235 int err;
1236
1237 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1238 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1239 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1240 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1241 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1242 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1243 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1244}
1245# endif
1246
1247/*
1248 * Top level evaluation function, returning a boolean.
1249 * Sets "error" to TRUE if there was an error.
1250 * Return TRUE or FALSE.
1251 */
1252 int
1253eval_to_bool(arg, error, nextcmd, skip)
1254 char_u *arg;
1255 int *error;
1256 char_u **nextcmd;
1257 int skip; /* only parse, don't execute */
1258{
Bram Moolenaar33570922005-01-25 22:26:29 +00001259 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 int retval = FALSE;
1261
1262 if (skip)
1263 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001264 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 else
1267 {
1268 *error = FALSE;
1269 if (!skip)
1270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001271 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274 }
1275 if (skip)
1276 --emsg_skip;
1277
1278 return retval;
1279}
1280
1281/*
1282 * Top level evaluation function, returning a string. If "skip" is TRUE,
1283 * only parsing to "nextcmd" is done, without reporting errors. Return
1284 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1285 */
1286 char_u *
1287eval_to_string_skip(arg, nextcmd, skip)
1288 char_u *arg;
1289 char_u **nextcmd;
1290 int skip; /* only parse, don't execute */
1291{
Bram Moolenaar33570922005-01-25 22:26:29 +00001292 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 char_u *retval;
1294
1295 if (skip)
1296 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 retval = NULL;
1299 else
1300 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 retval = vim_strsave(get_tv_string(&tv));
1302 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 if (skip)
1305 --emsg_skip;
1306
1307 return retval;
1308}
1309
1310/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001311 * Skip over an expression at "*pp".
1312 * Return FAIL for an error, OK otherwise.
1313 */
1314 int
1315skip_expr(pp)
1316 char_u **pp;
1317{
Bram Moolenaar33570922005-01-25 22:26:29 +00001318 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319
1320 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001321 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001322}
1323
1324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001326 * When "convert" is TRUE convert a List into a sequence of lines and convert
1327 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 * Return pointer to allocated memory, or NULL for failure.
1329 */
1330 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 char_u *arg;
1333 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001338 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001339#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 retval = NULL;
1345 else
1346 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001348 {
1349 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001350 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001351 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001352 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001353 if (tv.vval.v_list->lv_len > 0)
1354 ga_append(&ga, NL);
1355 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 ga_append(&ga, NUL);
1357 retval = (char_u *)ga.ga_data;
1358 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359#ifdef FEAT_FLOAT
1360 else if (convert && tv.v_type == VAR_FLOAT)
1361 {
1362 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1363 retval = vim_strsave(numbuf);
1364 }
1365#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001366 else
1367 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370
1371 return retval;
1372}
1373
1374/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001375 * Call eval_to_string() without using current local variables and using
1376 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 */
1378 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 char_u *arg;
1381 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
1384 char_u *retval;
1385 void *save_funccalp;
1386
1387 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 if (use_sandbox)
1389 ++sandbox;
1390 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 --sandbox;
1394 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 restore_funccal(save_funccalp);
1396 return retval;
1397}
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399/*
1400 * Top level evaluation function, returning a number.
1401 * Evaluates "expr" silently.
1402 * Returns -1 for an error.
1403 */
1404 int
1405eval_to_number(expr)
1406 char_u *expr;
1407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001410 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 ++emsg_off;
1413
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = -1;
1416 else
1417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001418 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 --emsg_off;
1422
1423 return retval;
1424}
1425
Bram Moolenaara40058a2005-07-11 22:42:07 +00001426/*
1427 * Prepare v: variable "idx" to be used.
1428 * Save the current typeval in "save_tv".
1429 * When not used yet add the variable to the v: hashtable.
1430 */
1431 static void
1432prepare_vimvar(idx, save_tv)
1433 int idx;
1434 typval_T *save_tv;
1435{
1436 *save_tv = vimvars[idx].vv_tv;
1437 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1438 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1439}
1440
1441/*
1442 * Restore v: variable "idx" to typeval "save_tv".
1443 * When no longer defined, remove the variable from the v: hashtable.
1444 */
1445 static void
1446restore_vimvar(idx, save_tv)
1447 int idx;
1448 typval_T *save_tv;
1449{
1450 hashitem_T *hi;
1451
Bram Moolenaara40058a2005-07-11 22:42:07 +00001452 vimvars[idx].vv_tv = *save_tv;
1453 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1454 {
1455 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1456 if (HASHITEM_EMPTY(hi))
1457 EMSG2(_(e_intern2), "restore_vimvar()");
1458 else
1459 hash_remove(&vimvarht, hi);
1460 }
1461}
1462
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001463#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001464/*
1465 * Evaluate an expression to a list with suggestions.
1466 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001467 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468 */
1469 list_T *
1470eval_spell_expr(badword, expr)
1471 char_u *badword;
1472 char_u *expr;
1473{
1474 typval_T save_val;
1475 typval_T rettv;
1476 list_T *list = NULL;
1477 char_u *p = skipwhite(expr);
1478
1479 /* Set "v:val" to the bad word. */
1480 prepare_vimvar(VV_VAL, &save_val);
1481 vimvars[VV_VAL].vv_type = VAR_STRING;
1482 vimvars[VV_VAL].vv_str = badword;
1483 if (p_verbose == 0)
1484 ++emsg_off;
1485
1486 if (eval1(&p, &rettv, TRUE) == OK)
1487 {
1488 if (rettv.v_type != VAR_LIST)
1489 clear_tv(&rettv);
1490 else
1491 list = rettv.vval.v_list;
1492 }
1493
1494 if (p_verbose == 0)
1495 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001496 restore_vimvar(VV_VAL, &save_val);
1497
1498 return list;
1499}
1500
1501/*
1502 * "list" is supposed to contain two items: a word and a number. Return the
1503 * word in "pp" and the number as the return value.
1504 * Return -1 if anything isn't right.
1505 * Used to get the good word and score from the eval_spell_expr() result.
1506 */
1507 int
1508get_spellword(list, pp)
1509 list_T *list;
1510 char_u **pp;
1511{
1512 listitem_T *li;
1513
1514 li = list->lv_first;
1515 if (li == NULL)
1516 return -1;
1517 *pp = get_tv_string(&li->li_tv);
1518
1519 li = li->li_next;
1520 if (li == NULL)
1521 return -1;
1522 return get_tv_number(&li->li_tv);
1523}
1524#endif
1525
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001526/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001527 * Top level evaluation function.
1528 * Returns an allocated typval_T with the result.
1529 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530 */
1531 typval_T *
1532eval_expr(arg, nextcmd)
1533 char_u *arg;
1534 char_u **nextcmd;
1535{
1536 typval_T *tv;
1537
1538 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001540 {
1541 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 }
1544
1545 return tv;
1546}
1547
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001550 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001551 * Uses argv[argc] for the function arguments. Only Number and String
1552 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001553 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001555 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001556call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 char_u *func;
1558 int argc;
1559 char_u **argv;
1560 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563{
Bram Moolenaar33570922005-01-25 22:26:29 +00001564 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 long n;
1566 int len;
1567 int i;
1568 int doesrange;
1569 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001572 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 for (i = 0; i < argc; i++)
1577 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001578 /* Pass a NULL or empty argument as an empty string */
1579 if (argv[i] == NULL || *argv[i] == NUL)
1580 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001581 argvars[i].v_type = VAR_STRING;
1582 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 continue;
1584 }
1585
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 if (str_arg_only)
1587 len = 0;
1588 else
1589 /* Recognize a number argument, the others must be strings. */
1590 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (len != 0 && len == (int)STRLEN(argv[i]))
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_NUMBER;
1594 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_STRING;
1599 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602
1603 if (safe)
1604 {
1605 save_funccalp = save_funccal();
1606 ++sandbox;
1607 }
1608
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001609 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1610 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (safe)
1614 {
1615 --sandbox;
1616 restore_funccal(save_funccalp);
1617 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 vim_free(argvars);
1619
1620 if (ret == FAIL)
1621 clear_tv(rettv);
1622
1623 return ret;
1624}
1625
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001626/*
1627 * Call vimL function "func" and return the result as a number.
1628 * Returns -1 when calling the function fails.
1629 * Uses argv[argc] for the function arguments.
1630 */
1631 long
1632call_func_retnr(func, argc, argv, safe)
1633 char_u *func;
1634 int argc;
1635 char_u **argv;
1636 int safe; /* use the sandbox */
1637{
1638 typval_T rettv;
1639 long retval;
1640
1641 /* All arguments are passed as strings, no conversion to number. */
1642 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1643 return -1;
1644
1645 retval = get_tv_number_chk(&rettv, NULL);
1646 clear_tv(&rettv);
1647 return retval;
1648}
1649
1650#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1651 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a string.
1656 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 * Uses argv[argc] for the function arguments.
1658 */
1659 void *
1660call_func_retstr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001669 /* All arguments are passed as strings, no conversion to number. */
1670 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 return NULL;
1672
1673 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 return retval;
1676}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001677# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001682 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 */
1684 void *
1685call_func_retlist(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
1692
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001693 /* All arguments are passed as strings, no conversion to number. */
1694 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 return NULL;
1696
1697 if (rettv.v_type != VAR_LIST)
1698 {
1699 clear_tv(&rettv);
1700 return NULL;
1701 }
1702
1703 return rettv.vval.v_list;
1704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#endif
1706
1707/*
1708 * Save the current function call pointer, and set it to NULL.
1709 * Used when executing autocommands and for ":source".
1710 */
1711 void *
1712save_funccal()
1713{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001714 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 current_funccal = NULL;
1717 return (void *)fc;
1718}
1719
1720 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721restore_funccal(vfc)
1722 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001724 funccall_T *fc = (funccall_T *)vfc;
1725
1726 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727}
1728
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729#if defined(FEAT_PROFILE) || defined(PROTO)
1730/*
1731 * Prepare profiling for entering a child or something else that is not
1732 * counted for the script/function itself.
1733 * Should always be called in pair with prof_child_exit().
1734 */
1735 void
1736prof_child_enter(tm)
1737 proftime_T *tm; /* place to store waittime */
1738{
1739 funccall_T *fc = current_funccal;
1740
1741 if (fc != NULL && fc->func->uf_profiling)
1742 profile_start(&fc->prof_child);
1743 script_prof_save(tm);
1744}
1745
1746/*
1747 * Take care of time spent in a child.
1748 * Should always be called after prof_child_enter().
1749 */
1750 void
1751prof_child_exit(tm)
1752 proftime_T *tm; /* where waittime was stored */
1753{
1754 funccall_T *fc = current_funccal;
1755
1756 if (fc != NULL && fc->func->uf_profiling)
1757 {
1758 profile_end(&fc->prof_child);
1759 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1760 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1761 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1762 }
1763 script_prof_restore(tm);
1764}
1765#endif
1766
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifdef FEAT_FOLDING
1769/*
1770 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1771 * it in "*cp". Doesn't give error messages.
1772 */
1773 int
1774eval_foldexpr(arg, cp)
1775 char_u *arg;
1776 int *cp;
1777{
Bram Moolenaar33570922005-01-25 22:26:29 +00001778 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 int retval;
1780 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001781 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1782 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001785 if (use_sandbox)
1786 ++sandbox;
1787 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 retval = 0;
1791 else
1792 {
1793 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (tv.v_type == VAR_NUMBER)
1795 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001796 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 retval = 0;
1798 else
1799 {
1800 /* If the result is a string, check if there is a non-digit before
1801 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 if (!VIM_ISDIGIT(*s) && *s != '-')
1804 *cp = *s++;
1805 retval = atol((char *)s);
1806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 --sandbox;
1812 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814 return retval;
1815}
1816#endif
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 * ":let" list all variable values
1820 * ":let var1 var2" list variable values
1821 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001822 * ":let var += expr" assignment command.
1823 * ":let var -= expr" assignment command.
1824 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 */
1827 void
1828ex_let(eap)
1829 exarg_T *eap;
1830{
1831 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001833 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001835 int var_count = 0;
1836 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001837 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001839 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaardb552d602006-03-23 22:59:57 +00001841 argend = skip_var_list(arg, &var_count, &semicolon);
1842 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001843 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001844 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1845 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001847 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001849 /*
1850 * ":let" without "=": list variables
1851 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (*arg == '[')
1853 EMSG(_(e_invarg));
1854 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001856 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001860 list_glob_vars(&first);
1861 list_buf_vars(&first);
1862 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001865#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_script_vars(&first);
1867 list_func_vars(&first);
1868 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 eap->nextcmd = check_nextcmd(arg);
1871 }
1872 else
1873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 op[0] = '=';
1875 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001876 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001877 {
1878 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1879 op[0] = expr[-1]; /* +=, -= or .= */
1880 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001881 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (eap->skip)
1884 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001885 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (eap->skip)
1887 {
1888 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 --emsg_skip;
1891 }
1892 else if (i != FAIL)
1893 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 }
1899}
1900
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901/*
1902 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1903 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 * When "nextchars" is not NULL it points to a string with characters that
1905 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1906 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 * Returns OK or FAIL;
1908 */
1909 static int
1910ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1911 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 int copy; /* copy values from "tv", don't move */
1914 int semicolon; /* from skip_var_list() */
1915 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917{
1918 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001921 listitem_T *item;
1922 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923
1924 if (*arg != '[')
1925 {
1926 /*
1927 * ":let var = expr" or ":for var in list"
1928 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001929 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001930 return FAIL;
1931 return OK;
1932 }
1933
1934 /*
1935 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1936 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 {
1939 EMSG(_(e_listreq));
1940 return FAIL;
1941 }
1942
1943 i = list_len(l);
1944 if (semicolon == 0 && var_count < i)
1945 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001946 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001947 return FAIL;
1948 }
1949 if (var_count - semicolon > i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954
1955 item = l->lv_first;
1956 while (*arg != ']')
1957 {
1958 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 item = item->li_next;
1961 if (arg == NULL)
1962 return FAIL;
1963
1964 arg = skipwhite(arg);
1965 if (*arg == ';')
1966 {
1967 /* Put the rest of the list (may be empty) in the var after ';'.
1968 * Create a new list for this. */
1969 l = list_alloc();
1970 if (l == NULL)
1971 return FAIL;
1972 while (item != NULL)
1973 {
1974 list_append_tv(l, &item->li_tv);
1975 item = item->li_next;
1976 }
1977
1978 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001979 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 ltv.vval.v_list = l;
1981 l->lv_refcount = 1;
1982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1984 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 clear_tv(&ltv);
1986 if (arg == NULL)
1987 return FAIL;
1988 break;
1989 }
1990 else if (*arg != ',' && *arg != ']')
1991 {
1992 EMSG2(_(e_intern2), "ex_let_vars()");
1993 return FAIL;
1994 }
1995 }
1996
1997 return OK;
1998}
1999
2000/*
2001 * Skip over assignable variable "var" or list of variables "[var, var]".
2002 * Used for ":let varvar = expr" and ":for varvar in expr".
2003 * For "[var, var]" increment "*var_count" for each variable.
2004 * for "[var, var; var]" set "semicolon".
2005 * Return NULL for an error.
2006 */
2007 static char_u *
2008skip_var_list(arg, var_count, semicolon)
2009 char_u *arg;
2010 int *var_count;
2011 int *semicolon;
2012{
2013 char_u *p, *s;
2014
2015 if (*arg == '[')
2016 {
2017 /* "[var, var]": find the matching ']'. */
2018 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002019 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 {
2021 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2022 s = skip_var_one(p);
2023 if (s == p)
2024 {
2025 EMSG2(_(e_invarg2), p);
2026 return NULL;
2027 }
2028 ++*var_count;
2029
2030 p = skipwhite(s);
2031 if (*p == ']')
2032 break;
2033 else if (*p == ';')
2034 {
2035 if (*semicolon == 1)
2036 {
2037 EMSG(_("Double ; in list of variables"));
2038 return NULL;
2039 }
2040 *semicolon = 1;
2041 }
2042 else if (*p != ',')
2043 {
2044 EMSG2(_(e_invarg2), p);
2045 return NULL;
2046 }
2047 }
2048 return p + 1;
2049 }
2050 else
2051 return skip_var_one(arg);
2052}
2053
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002054/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002055 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002056 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002057 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 static char_u *
2059skip_var_one(arg)
2060 char_u *arg;
2061{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 if (*arg == '@' && arg[1] != NUL)
2063 return arg + 2;
2064 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2065 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066}
2067
Bram Moolenaara7043832005-01-21 11:56:39 +00002068/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002069 * List variables for hashtab "ht" with prefix "prefix".
2070 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002072 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002073list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002075 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002076 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002077 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002078{
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashitem_T *hi;
2080 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 int todo;
2082
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2085 {
2086 if (!HASHITEM_EMPTY(hi))
2087 {
2088 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002089 di = HI2DI(hi);
2090 if (empty || di->di_tv.v_type != VAR_STRING
2091 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002092 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 }
2094 }
2095}
2096
2097/*
2098 * List global variables.
2099 */
2100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_glob_vars(first)
2102 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105}
2106
2107/*
2108 * List buffer variables.
2109 */
2110 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002111list_buf_vars(first)
2112 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002113{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002114 char_u numbuf[NUMBUFLEN];
2115
Bram Moolenaar429fa852013-04-15 12:27:36 +02002116 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118
2119 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2121 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002122}
2123
2124/*
2125 * List window variables.
2126 */
2127 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128list_win_vars(first)
2129 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002130{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002131 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002135#ifdef FEAT_WINDOWS
2136/*
2137 * List tab page variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_tab_vars(first)
2141 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002145}
2146#endif
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
2149 * List Vim variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_vim_vars(first)
2153 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002156}
2157
2158/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002159 * List script-local variables, if there is a script.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_script_vars(first)
2163 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164{
2165 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2167 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002168}
2169
2170/*
2171 * List function variables, if there is a function.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_func_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_funccal != NULL)
2178 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002183 * List variables in "arg".
2184 */
2185 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002187 exarg_T *eap;
2188 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002190{
2191 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194 char_u *name_start;
2195 char_u *arg_subsc;
2196 char_u *tofree;
2197 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198
2199 while (!ends_excmd(*arg) && !got_int)
2200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002203 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2205 {
2206 emsg_severe = TRUE;
2207 EMSG(_(e_trailing));
2208 break;
2209 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 /* get_name_len() takes care of expanding curly braces */
2214 name_start = name = arg;
2215 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2216 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* This is mainly to keep test 49 working: when expanding
2219 * curly braces fails overrule the exception error message. */
2220 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 emsg_severe = TRUE;
2223 EMSG2(_(e_invarg2), arg);
2224 break;
2225 }
2226 error = TRUE;
2227 }
2228 else
2229 {
2230 if (tofree != NULL)
2231 name = tofree;
2232 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 else
2235 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002236 /* handle d.key, l[idx], f(expr) */
2237 arg_subsc = arg;
2238 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 case 'g': list_glob_vars(first); break;
2247 case 'b': list_buf_vars(first); break;
2248 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'v': list_vim_vars(first); break;
2253 case 's': list_script_vars(first); break;
2254 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 default:
2256 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002258 }
2259 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 {
2261 char_u numbuf[NUMBUFLEN];
2262 char_u *tf;
2263 int c;
2264 char_u *s;
2265
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002266 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 c = *arg;
2268 *arg = NUL;
2269 list_one_var_a((char_u *)"",
2270 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002271 tv.v_type,
2272 s == NULL ? (char_u *)"" : s,
2273 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 *arg = c;
2275 vim_free(tf);
2276 }
2277 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002278 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 }
2280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281
2282 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284
2285 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 }
2287
2288 return arg;
2289}
2290
2291/*
2292 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2293 * Returns a pointer to the char just after the var name.
2294 * Returns NULL if there is an error.
2295 */
2296 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002297ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002299 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002301 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303{
2304 int c1;
2305 char_u *name;
2306 char_u *p;
2307 char_u *arg_end = NULL;
2308 int len;
2309 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311
2312 /*
2313 * ":let $VAR = expr": Set environment variable.
2314 */
2315 if (*arg == '$')
2316 {
2317 /* Find the end of the name. */
2318 ++arg;
2319 name = arg;
2320 len = get_env_len(&arg);
2321 if (len == 0)
2322 EMSG2(_(e_invarg2), name - 1);
2323 else
2324 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325 if (op != NULL && (*op == '+' || *op == '-'))
2326 EMSG2(_(e_letwrong), op);
2327 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002328 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002330 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 {
2332 c1 = name[len];
2333 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334 p = get_tv_string_chk(tv);
2335 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002336 {
2337 int mustfree = FALSE;
2338 char_u *s = vim_getenv(name, &mustfree);
2339
2340 if (s != NULL)
2341 {
2342 p = tofree = concat_str(s, p);
2343 if (mustfree)
2344 vim_free(s);
2345 }
2346 }
2347 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002349 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002350 if (STRICMP(name, "HOME") == 0)
2351 init_homedir();
2352 else if (didset_vim && STRICMP(name, "VIM") == 0)
2353 didset_vim = FALSE;
2354 else if (didset_vimruntime
2355 && STRICMP(name, "VIMRUNTIME") == 0)
2356 didset_vimruntime = FALSE;
2357 arg_end = arg;
2358 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362 }
2363 }
2364
2365 /*
2366 * ":let &option = expr": Set option value.
2367 * ":let &l:option = expr": Set local option value.
2368 * ":let &g:option = expr": Set global option value.
2369 */
2370 else if (*arg == '&')
2371 {
2372 /* Find the end of the name. */
2373 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002374 if (p == NULL || (endchars != NULL
2375 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 EMSG(_(e_letunexp));
2377 else
2378 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 long n;
2380 int opt_type;
2381 long numval;
2382 char_u *stringval = NULL;
2383 char_u *s;
2384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 c1 = *p;
2386 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387
2388 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 s = get_tv_string_chk(tv); /* != NULL if number or string */
2390 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 opt_type = get_option_value(arg, &numval,
2393 &stringval, opt_flags);
2394 if ((opt_type == 1 && *op == '.')
2395 || (opt_type == 0 && *op != '.'))
2396 EMSG2(_(e_letwrong), op);
2397 else
2398 {
2399 if (opt_type == 1) /* number */
2400 {
2401 if (*op == '+')
2402 n = numval + n;
2403 else
2404 n = numval - n;
2405 }
2406 else if (opt_type == 0 && stringval != NULL) /* string */
2407 {
2408 s = concat_str(stringval, s);
2409 vim_free(stringval);
2410 stringval = s;
2411 }
2412 }
2413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002414 if (s != NULL)
2415 {
2416 set_option_value(arg, n, s, opt_flags);
2417 arg_end = p;
2418 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002420 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 }
2422 }
2423
2424 /*
2425 * ":let @r = expr": Set register contents.
2426 */
2427 else if (*arg == '@')
2428 {
2429 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 if (op != NULL && (*op == '+' || *op == '-'))
2431 EMSG2(_(e_letwrong), op);
2432 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002437 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 char_u *s;
2439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 p = get_tv_string_chk(tv);
2441 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002443 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 if (s != NULL)
2445 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 arg_end = arg + 1;
2454 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002456 }
2457 }
2458
2459 /*
2460 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002463 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002465 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002467 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2471 EMSG(_(e_letunexp));
2472 else
2473 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002474 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480
2481 else
2482 EMSG2(_(e_invarg2), arg);
2483
2484 return arg_end;
2485}
2486
2487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002488 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2489 */
2490 static int
2491check_changedtick(arg)
2492 char_u *arg;
2493{
2494 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2495 {
2496 EMSG2(_(e_readonlyvar), arg);
2497 return TRUE;
2498 }
2499 return FALSE;
2500}
2501
2502/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 * Get an lval: variable, Dict item or List item that can be assigned a value
2504 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2505 * "name.key", "name.key[expr]" etc.
2506 * Indexing only works if "name" is an existing List or Dictionary.
2507 * "name" points to the start of the name.
2508 * If "rettv" is not NULL it points to the value to be assigned.
2509 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2510 * wrong; must end in space or cmd separator.
2511 *
2512 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002513 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
2516 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002517get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002519 typval_T *rettv;
2520 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 int unlet;
2522 int skip;
2523 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 char_u *expr_start, *expr_end;
2528 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 dictitem_T *v;
2530 typval_T var1;
2531 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002535 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002536 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540
2541 if (skip)
2542 {
2543 /* When skipping just find the end of the name. */
2544 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002545 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 }
2547
2548 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002549 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 if (expr_start != NULL)
2551 {
2552 /* Don't expand the name when we already know there is an error. */
2553 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2554 && *p != '[' && *p != '.')
2555 {
2556 EMSG(_(e_trailing));
2557 return NULL;
2558 }
2559
2560 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2561 if (lp->ll_exp_name == NULL)
2562 {
2563 /* Report an invalid expression in braces, unless the
2564 * expression evaluation has been cancelled due to an
2565 * aborting error, an interrupt, or an exception. */
2566 if (!aborting() && !quiet)
2567 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002568 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 EMSG2(_(e_invarg2), name);
2570 return NULL;
2571 }
2572 }
2573 lp->ll_name = lp->ll_exp_name;
2574 }
2575 else
2576 lp->ll_name = name;
2577
2578 /* Without [idx] or .key we are done. */
2579 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2580 return p;
2581
2582 cc = *p;
2583 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 if (v == NULL && !quiet)
2586 EMSG2(_(e_undefvar), lp->ll_name);
2587 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 if (v == NULL)
2589 return NULL;
2590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 /*
2592 * Loop until no more [idx] or .key is following.
2593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2598 && !(lp->ll_tv->v_type == VAR_DICT
2599 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 if (!quiet)
2602 EMSG(_("E689: Can only index a List or Dictionary"));
2603 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E708: [:] must come last"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002611
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 len = -1;
2613 if (*p == '.')
2614 {
2615 key = p + 1;
2616 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2617 ;
2618 if (len == 0)
2619 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 if (!quiet)
2621 EMSG(_(e_emptykey));
2622 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002623 }
2624 p = key + len;
2625 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002626 else
2627 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002629 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 if (*p == ':')
2631 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 empty1 = FALSE;
2635 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 if (get_tv_string_chk(&var1) == NULL)
2638 {
2639 /* not a number or string */
2640 clear_tv(&var1);
2641 return NULL;
2642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002643 }
2644
2645 /* Optionally get the second index [ :expr]. */
2646 if (*p == ':')
2647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002651 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002652 if (!empty1)
2653 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002655 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (rettv != NULL && (rettv->v_type != VAR_LIST
2657 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!quiet)
2660 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 if (!empty1)
2662 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 }
2665 p = skipwhite(p + 1);
2666 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 else
2669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 if (!empty1)
2674 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002677 if (get_tv_string_chk(&var2) == NULL)
2678 {
2679 /* not a number or string */
2680 if (!empty1)
2681 clear_tv(&var1);
2682 clear_tv(&var2);
2683 return NULL;
2684 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!quiet)
2694 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (!empty1)
2696 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Skip to past ']'. */
2703 ++p;
2704 }
2705
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 {
2708 if (len == -1)
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (*key == NUL)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002720 lp->ll_list = NULL;
2721 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002722 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002723
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002724 /* When assigning to a scope dictionary check that a function and
2725 * variable name is valid (only variable name unless it is l: or
2726 * g: dictionary). Disallow overwriting a builtin function. */
2727 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 int prevval;
2730 int wrong;
2731
2732 if (len != -1)
2733 {
2734 prevval = key[len];
2735 key[len] = NUL;
2736 }
2737 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2738 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002739 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002740 || !valid_varname(key);
2741 if (len != -1)
2742 key[len] = prevval;
2743 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002744 return NULL;
2745 }
2746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749 /* Can't add "v:" variable. */
2750 if (lp->ll_dict == &vimvardict)
2751 {
2752 EMSG2(_(e_illvar), name);
2753 return NULL;
2754 }
2755
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002756 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002760 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (len == -1)
2762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
2765 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (len == -1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 p = NULL;
2773 break;
2774 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002775 /* existing variable, need to check if it can be changed */
2776 else if (var_check_ro(lp->ll_di->di_flags, name))
2777 return NULL;
2778
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (len == -1)
2780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
2783 else
2784 {
2785 /*
2786 * Get the number and item for the only or first index of the List.
2787 */
2788 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 else
2791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002792 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 clear_tv(&var1);
2794 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_list = lp->ll_tv->vval.v_list;
2797 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2798 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002800 if (lp->ll_n1 < 0)
2801 {
2802 lp->ll_n1 = 0;
2803 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2804 }
2805 }
2806 if (lp->ll_li == NULL)
2807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002810 if (!quiet)
2811 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 }
2814
2815 /*
2816 * May need to find the item or absolute index for the second
2817 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 * When no index given: "lp->ll_empty2" is TRUE.
2819 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002823 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 {
2830 if (!quiet)
2831 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002833 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 }
2836
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2838 if (lp->ll_n1 < 0)
2839 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2840 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 {
2842 if (!quiet)
2843 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002846 }
2847
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002849 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002850 }
2851
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 return p;
2853}
2854
2855/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002856 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 */
2858 static void
2859clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002860 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861{
2862 vim_free(lp->ll_exp_name);
2863 vim_free(lp->ll_newkey);
2864}
2865
2866/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002867 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 */
2871 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002872set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878{
2879 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 listitem_T *ri;
2881 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882
2883 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 cc = *endp;
2888 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 if (op != NULL && *op != '=')
2890 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002892
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002893 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002894 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002895 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 {
2897 if (tv_op(&tv, rettv, op) == OK)
2898 set_var(lp->ll_name, &tv, FALSE);
2899 clear_tv(&tv);
2900 }
2901 }
2902 else
2903 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002907 else if (tv_check_lock(lp->ll_newkey == NULL
2908 ? lp->ll_tv->v_lock
2909 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2910 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 else if (lp->ll_range)
2912 {
2913 /*
2914 * Assign the List values to the list items.
2915 */
2916 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002918 if (op != NULL && *op != '=')
2919 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2920 else
2921 {
2922 clear_tv(&lp->ll_li->li_tv);
2923 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 ri = ri->li_next;
2926 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2927 break;
2928 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002929 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002931 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002932 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 ri = NULL;
2934 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002935 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 lp->ll_li = lp->ll_li->li_next;
2938 ++lp->ll_n1;
2939 }
2940 if (ri != NULL)
2941 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 else if (lp->ll_empty2
2943 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 : lp->ll_n1 != lp->ll_n2)
2945 EMSG(_("E711: List value has not enough items"));
2946 }
2947 else
2948 {
2949 /*
2950 * Assign to a List or Dictionary item.
2951 */
2952 if (lp->ll_newkey != NULL)
2953 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 if (op != NULL && *op != '=')
2955 {
2956 EMSG2(_(e_letwrong), op);
2957 return;
2958 }
2959
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002961 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 if (di == NULL)
2963 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002964 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2965 {
2966 vim_free(di);
2967 return;
2968 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 else if (op != NULL && *op != '=')
2972 {
2973 tv_op(lp->ll_tv, rettv, op);
2974 return;
2975 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /*
2980 * Assign the value to the variable or list item.
2981 */
2982 if (copy)
2983 copy_tv(rettv, lp->ll_tv);
2984 else
2985 {
2986 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002987 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002989 }
2990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002991}
2992
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002993/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2995 * Returns OK or FAIL.
2996 */
2997 static int
2998tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 typval_T *tv1;
3000 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 char_u *op;
3002{
3003 long n;
3004 char_u numbuf[NUMBUFLEN];
3005 char_u *s;
3006
3007 /* Can't do anything with a Funcref or a Dict on the right. */
3008 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3009 {
3010 switch (tv1->v_type)
3011 {
3012 case VAR_DICT:
3013 case VAR_FUNC:
3014 break;
3015
3016 case VAR_LIST:
3017 if (*op != '+' || tv2->v_type != VAR_LIST)
3018 break;
3019 /* List += List */
3020 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3021 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3022 return OK;
3023
3024 case VAR_NUMBER:
3025 case VAR_STRING:
3026 if (tv2->v_type == VAR_LIST)
3027 break;
3028 if (*op == '+' || *op == '-')
3029 {
3030 /* nr += nr or nr -= nr*/
3031 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003032#ifdef FEAT_FLOAT
3033 if (tv2->v_type == VAR_FLOAT)
3034 {
3035 float_T f = n;
3036
3037 if (*op == '+')
3038 f += tv2->vval.v_float;
3039 else
3040 f -= tv2->vval.v_float;
3041 clear_tv(tv1);
3042 tv1->v_type = VAR_FLOAT;
3043 tv1->vval.v_float = f;
3044 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003046#endif
3047 {
3048 if (*op == '+')
3049 n += get_tv_number(tv2);
3050 else
3051 n -= get_tv_number(tv2);
3052 clear_tv(tv1);
3053 tv1->v_type = VAR_NUMBER;
3054 tv1->vval.v_number = n;
3055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 }
3057 else
3058 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003059 if (tv2->v_type == VAR_FLOAT)
3060 break;
3061
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003062 /* str .= str */
3063 s = get_tv_string(tv1);
3064 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3065 clear_tv(tv1);
3066 tv1->v_type = VAR_STRING;
3067 tv1->vval.v_string = s;
3068 }
3069 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003070
3071#ifdef FEAT_FLOAT
3072 case VAR_FLOAT:
3073 {
3074 float_T f;
3075
3076 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3077 && tv2->v_type != VAR_NUMBER
3078 && tv2->v_type != VAR_STRING))
3079 break;
3080 if (tv2->v_type == VAR_FLOAT)
3081 f = tv2->vval.v_float;
3082 else
3083 f = get_tv_number(tv2);
3084 if (*op == '+')
3085 tv1->vval.v_float += f;
3086 else
3087 tv1->vval.v_float -= f;
3088 }
3089 return OK;
3090#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 }
3092 }
3093
3094 EMSG2(_(e_letwrong), op);
3095 return FAIL;
3096}
3097
3098/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099 * Add a watcher to a list.
3100 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003101 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003102list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003103 list_T *l;
3104 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003105{
3106 lw->lw_next = l->lv_watch;
3107 l->lv_watch = lw;
3108}
3109
3110/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003111 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112 * No warning when it isn't found...
3113 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003114 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 list_T *l;
3117 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118{
Bram Moolenaar33570922005-01-25 22:26:29 +00003119 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120
3121 lwp = &l->lv_watch;
3122 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3123 {
3124 if (lw == lwrem)
3125 {
3126 *lwp = lw->lw_next;
3127 break;
3128 }
3129 lwp = &lw->lw_next;
3130 }
3131}
3132
3133/*
3134 * Just before removing an item from a list: advance watchers to the next
3135 * item.
3136 */
3137 static void
3138list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003139 list_T *l;
3140 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003141{
Bram Moolenaar33570922005-01-25 22:26:29 +00003142 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003143
3144 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3145 if (lw->lw_item == item)
3146 lw->lw_item = item->li_next;
3147}
3148
3149/*
3150 * Evaluate the expression used in a ":for var in expr" command.
3151 * "arg" points to "var".
3152 * Set "*errp" to TRUE for an error, FALSE otherwise;
3153 * Return a pointer that holds the info. Null when there is an error.
3154 */
3155 void *
3156eval_for_line(arg, errp, nextcmdp, skip)
3157 char_u *arg;
3158 int *errp;
3159 char_u **nextcmdp;
3160 int skip;
3161{
Bram Moolenaar33570922005-01-25 22:26:29 +00003162 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003163 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003164 typval_T tv;
3165 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003166
3167 *errp = TRUE; /* default: there is an error */
3168
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 if (fi == NULL)
3171 return NULL;
3172
3173 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3174 if (expr == NULL)
3175 return fi;
3176
3177 expr = skipwhite(expr);
3178 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3179 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003180 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 return fi;
3182 }
3183
3184 if (skip)
3185 ++emsg_skip;
3186 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3187 {
3188 *errp = FALSE;
3189 if (!skip)
3190 {
3191 l = tv.vval.v_list;
3192 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003193 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 clear_tv(&tv);
3196 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 else
3198 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003199 /* No need to increment the refcount, it's already set for the
3200 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 fi->fi_list = l;
3202 list_add_watch(l, &fi->fi_lw);
3203 fi->fi_lw.lw_item = l->lv_first;
3204 }
3205 }
3206 }
3207 if (skip)
3208 --emsg_skip;
3209
3210 return fi;
3211}
3212
3213/*
3214 * Use the first item in a ":for" list. Advance to the next.
3215 * Assign the values to the variable (list). "arg" points to the first one.
3216 * Return TRUE when a valid item was found, FALSE when at end of list or
3217 * something wrong.
3218 */
3219 int
3220next_for_item(fi_void, arg)
3221 void *fi_void;
3222 char_u *arg;
3223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227
3228 item = fi->fi_lw.lw_item;
3229 if (item == NULL)
3230 result = FALSE;
3231 else
3232 {
3233 fi->fi_lw.lw_item = item->li_next;
3234 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3235 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3236 }
3237 return result;
3238}
3239
3240/*
3241 * Free the structure used to store info used by ":for".
3242 */
3243 void
3244free_for_info(fi_void)
3245 void *fi_void;
3246{
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003249 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003250 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 list_unref(fi->fi_list);
3253 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 vim_free(fi);
3255}
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3258
3259 void
3260set_context_for_expression(xp, arg, cmdidx)
3261 expand_T *xp;
3262 char_u *arg;
3263 cmdidx_T cmdidx;
3264{
3265 int got_eq = FALSE;
3266 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 if (cmdidx == CMD_let)
3270 {
3271 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003272 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 {
3274 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003275 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 {
3277 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 if (vim_iswhite(*p))
3280 break;
3281 }
3282 return;
3283 }
3284 }
3285 else
3286 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3287 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 while ((xp->xp_pattern = vim_strpbrk(arg,
3289 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3290 {
3291 c = *xp->xp_pattern;
3292 if (c == '&')
3293 {
3294 c = xp->xp_pattern[1];
3295 if (c == '&')
3296 {
3297 ++xp->xp_pattern;
3298 xp->xp_context = cmdidx != CMD_let || got_eq
3299 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3300 }
3301 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003304 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3305 xp->xp_pattern += 2;
3306
3307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 else if (c == '$')
3310 {
3311 /* environment variable */
3312 xp->xp_context = EXPAND_ENV_VARS;
3313 }
3314 else if (c == '=')
3315 {
3316 got_eq = TRUE;
3317 xp->xp_context = EXPAND_EXPRESSION;
3318 }
3319 else if (c == '<'
3320 && xp->xp_context == EXPAND_FUNCTIONS
3321 && vim_strchr(xp->xp_pattern, '(') == NULL)
3322 {
3323 /* Function name can start with "<SNR>" */
3324 break;
3325 }
3326 else if (cmdidx != CMD_let || got_eq)
3327 {
3328 if (c == '"') /* string */
3329 {
3330 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3331 if (c == '\\' && xp->xp_pattern[1] != NUL)
3332 ++xp->xp_pattern;
3333 xp->xp_context = EXPAND_NOTHING;
3334 }
3335 else if (c == '\'') /* literal string */
3336 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003337 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3339 /* skip */ ;
3340 xp->xp_context = EXPAND_NOTHING;
3341 }
3342 else if (c == '|')
3343 {
3344 if (xp->xp_pattern[1] == '|')
3345 {
3346 ++xp->xp_pattern;
3347 xp->xp_context = EXPAND_EXPRESSION;
3348 }
3349 else
3350 xp->xp_context = EXPAND_COMMANDS;
3351 }
3352 else
3353 xp->xp_context = EXPAND_EXPRESSION;
3354 }
3355 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 /* Doesn't look like something valid, expand as an expression
3357 * anyway. */
3358 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 arg = xp->xp_pattern;
3360 if (*arg != NUL)
3361 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3362 /* skip */ ;
3363 }
3364 xp->xp_pattern = arg;
3365}
3366
3367#endif /* FEAT_CMDL_COMPL */
3368
3369/*
3370 * ":1,25call func(arg1, arg2)" function call.
3371 */
3372 void
3373ex_call(eap)
3374 exarg_T *eap;
3375{
3376 char_u *arg = eap->arg;
3377 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003379 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003381 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 linenr_T lnum;
3383 int doesrange;
3384 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003385 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003387 if (eap->skip)
3388 {
3389 /* trans_function_name() doesn't work well when skipping, use eval0()
3390 * instead to skip to any following command, e.g. for:
3391 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003392 ++emsg_skip;
3393 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3394 clear_tv(&rettv);
3395 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003396 return;
3397 }
3398
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003399 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003400 if (fudi.fd_newkey != NULL)
3401 {
3402 /* Still need to give an error message for missing key. */
3403 EMSG2(_(e_dictkey), fudi.fd_newkey);
3404 vim_free(fudi.fd_newkey);
3405 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003406 if (tofree == NULL)
3407 return;
3408
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003409 /* Increase refcount on dictionary, it could get deleted when evaluating
3410 * the arguments. */
3411 if (fudi.fd_dict != NULL)
3412 ++fudi.fd_dict->dv_refcount;
3413
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003414 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003415 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003416 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
Bram Moolenaar532c7802005-01-27 14:44:31 +00003418 /* Skip white space to allow ":call func ()". Not good, but required for
3419 * backward compatibility. */
3420 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 if (*startarg != '(')
3424 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003425 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 goto end;
3427 }
3428
3429 /*
3430 * When skipping, evaluate the function once, to find the end of the
3431 * arguments.
3432 * When the function takes a range, this is discovered after the first
3433 * call, and the loop is broken.
3434 */
3435 if (eap->skip)
3436 {
3437 ++emsg_skip;
3438 lnum = eap->line2; /* do it once, also with an invalid range */
3439 }
3440 else
3441 lnum = eap->line1;
3442 for ( ; lnum <= eap->line2; ++lnum)
3443 {
3444 if (!eap->skip && eap->addr_count > 0)
3445 {
3446 curwin->w_cursor.lnum = lnum;
3447 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003448#ifdef FEAT_VIRTUALEDIT
3449 curwin->w_cursor.coladd = 0;
3450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003453 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003454 eap->line1, eap->line2, &doesrange,
3455 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
3457 failed = TRUE;
3458 break;
3459 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003460
3461 /* Handle a function returning a Funcref, Dictionary or List. */
3462 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3463 {
3464 failed = TRUE;
3465 break;
3466 }
3467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003468 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (doesrange || eap->skip)
3470 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003473 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003474 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003475 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (aborting())
3477 break;
3478 }
3479 if (eap->skip)
3480 --emsg_skip;
3481
3482 if (!failed)
3483 {
3484 /* Check for trailing illegal characters and a following command. */
3485 if (!ends_excmd(*arg))
3486 {
3487 emsg_severe = TRUE;
3488 EMSG(_(e_trailing));
3489 }
3490 else
3491 eap->nextcmd = check_nextcmd(arg);
3492 }
3493
3494end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003495 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003496 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497}
3498
3499/*
3500 * ":unlet[!] var1 ... " command.
3501 */
3502 void
3503ex_unlet(eap)
3504 exarg_T *eap;
3505{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003506 ex_unletlock(eap, eap->arg, 0);
3507}
3508
3509/*
3510 * ":lockvar" and ":unlockvar" commands
3511 */
3512 void
3513ex_lockvar(eap)
3514 exarg_T *eap;
3515{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003517 int deep = 2;
3518
3519 if (eap->forceit)
3520 deep = -1;
3521 else if (vim_isdigit(*arg))
3522 {
3523 deep = getdigits(&arg);
3524 arg = skipwhite(arg);
3525 }
3526
3527 ex_unletlock(eap, arg, deep);
3528}
3529
3530/*
3531 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3532 */
3533 static void
3534ex_unletlock(eap, argstart, deep)
3535 exarg_T *eap;
3536 char_u *argstart;
3537 int deep;
3538{
3539 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003542 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543
3544 do
3545 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003547 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3548 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003549 if (lv.ll_name == NULL)
3550 error = TRUE; /* error but continue parsing */
3551 if (name_end == NULL || (!vim_iswhite(*name_end)
3552 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 if (name_end != NULL)
3555 {
3556 emsg_severe = TRUE;
3557 EMSG(_(e_trailing));
3558 }
3559 if (!(eap->skip || error))
3560 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 break;
3562 }
3563
3564 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003565 {
3566 if (eap->cmdidx == CMD_unlet)
3567 {
3568 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3569 error = TRUE;
3570 }
3571 else
3572 {
3573 if (do_lock_var(&lv, name_end, deep,
3574 eap->cmdidx == CMD_lockvar) == FAIL)
3575 error = TRUE;
3576 }
3577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003579 if (!eap->skip)
3580 clear_lval(&lv);
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 arg = skipwhite(name_end);
3583 } while (!ends_excmd(*arg));
3584
3585 eap->nextcmd = check_nextcmd(arg);
3586}
3587
Bram Moolenaar8c711452005-01-14 21:53:12 +00003588 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003589do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003591 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 int forceit;
3593{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003594 int ret = OK;
3595 int cc;
3596
3597 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 cc = *name_end;
3600 *name_end = NUL;
3601
3602 /* Normal name or expanded name. */
3603 if (check_changedtick(lp->ll_name))
3604 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3610 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003611 else if (lp->ll_range)
3612 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003613 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614
3615 /* Delete a range of List items. */
3616 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3617 {
3618 li = lp->ll_li->li_next;
3619 listitem_remove(lp->ll_list, lp->ll_li);
3620 lp->ll_li = li;
3621 ++lp->ll_n1;
3622 }
3623 }
3624 else
3625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 /* unlet a List item. */
3628 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003631 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 }
3633
3634 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003635}
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637/*
3638 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 */
3641 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003644 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
Bram Moolenaar33570922005-01-25 22:26:29 +00003646 hashtab_T *ht;
3647 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003648 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003649 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar33570922005-01-25 22:26:29 +00003651 ht = find_var_ht(name, &varname);
3652 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 hi = hash_find(ht, varname);
3655 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003657 di = HI2DI(hi);
3658 if (var_check_fixed(di->di_flags, name)
3659 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660 return FAIL;
3661 delete_var(ht, hi);
3662 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 if (forceit)
3666 return OK;
3667 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 return FAIL;
3669}
3670
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003671/*
3672 * Lock or unlock variable indicated by "lp".
3673 * "deep" is the levels to go (-1 for unlimited);
3674 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3675 */
3676 static int
3677do_lock_var(lp, name_end, deep, lock)
3678 lval_T *lp;
3679 char_u *name_end;
3680 int deep;
3681 int lock;
3682{
3683 int ret = OK;
3684 int cc;
3685 dictitem_T *di;
3686
3687 if (deep == 0) /* nothing to do */
3688 return OK;
3689
3690 if (lp->ll_tv == NULL)
3691 {
3692 cc = *name_end;
3693 *name_end = NUL;
3694
3695 /* Normal name or expanded name. */
3696 if (check_changedtick(lp->ll_name))
3697 ret = FAIL;
3698 else
3699 {
3700 di = find_var(lp->ll_name, NULL);
3701 if (di == NULL)
3702 ret = FAIL;
3703 else
3704 {
3705 if (lock)
3706 di->di_flags |= DI_FLAGS_LOCK;
3707 else
3708 di->di_flags &= ~DI_FLAGS_LOCK;
3709 item_lock(&di->di_tv, deep, lock);
3710 }
3711 }
3712 *name_end = cc;
3713 }
3714 else if (lp->ll_range)
3715 {
3716 listitem_T *li = lp->ll_li;
3717
3718 /* (un)lock a range of List items. */
3719 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3720 {
3721 item_lock(&li->li_tv, deep, lock);
3722 li = li->li_next;
3723 ++lp->ll_n1;
3724 }
3725 }
3726 else if (lp->ll_list != NULL)
3727 /* (un)lock a List item. */
3728 item_lock(&lp->ll_li->li_tv, deep, lock);
3729 else
3730 /* un(lock) a Dictionary item. */
3731 item_lock(&lp->ll_di->di_tv, deep, lock);
3732
3733 return ret;
3734}
3735
3736/*
3737 * Lock or unlock an item. "deep" is nr of levels to go.
3738 */
3739 static void
3740item_lock(tv, deep, lock)
3741 typval_T *tv;
3742 int deep;
3743 int lock;
3744{
3745 static int recurse = 0;
3746 list_T *l;
3747 listitem_T *li;
3748 dict_T *d;
3749 hashitem_T *hi;
3750 int todo;
3751
3752 if (recurse >= DICT_MAXNEST)
3753 {
3754 EMSG(_("E743: variable nested too deep for (un)lock"));
3755 return;
3756 }
3757 if (deep == 0)
3758 return;
3759 ++recurse;
3760
3761 /* lock/unlock the item itself */
3762 if (lock)
3763 tv->v_lock |= VAR_LOCKED;
3764 else
3765 tv->v_lock &= ~VAR_LOCKED;
3766
3767 switch (tv->v_type)
3768 {
3769 case VAR_LIST:
3770 if ((l = tv->vval.v_list) != NULL)
3771 {
3772 if (lock)
3773 l->lv_lock |= VAR_LOCKED;
3774 else
3775 l->lv_lock &= ~VAR_LOCKED;
3776 if (deep < 0 || deep > 1)
3777 /* recursive: lock/unlock the items the List contains */
3778 for (li = l->lv_first; li != NULL; li = li->li_next)
3779 item_lock(&li->li_tv, deep - 1, lock);
3780 }
3781 break;
3782 case VAR_DICT:
3783 if ((d = tv->vval.v_dict) != NULL)
3784 {
3785 if (lock)
3786 d->dv_lock |= VAR_LOCKED;
3787 else
3788 d->dv_lock &= ~VAR_LOCKED;
3789 if (deep < 0 || deep > 1)
3790 {
3791 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003792 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3794 {
3795 if (!HASHITEM_EMPTY(hi))
3796 {
3797 --todo;
3798 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3799 }
3800 }
3801 }
3802 }
3803 }
3804 --recurse;
3805}
3806
Bram Moolenaara40058a2005-07-11 22:42:07 +00003807/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003808 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3809 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003810 */
3811 static int
3812tv_islocked(tv)
3813 typval_T *tv;
3814{
3815 return (tv->v_lock & VAR_LOCKED)
3816 || (tv->v_type == VAR_LIST
3817 && tv->vval.v_list != NULL
3818 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3819 || (tv->v_type == VAR_DICT
3820 && tv->vval.v_dict != NULL
3821 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3822}
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3825/*
3826 * Delete all "menutrans_" variables.
3827 */
3828 void
3829del_menutrans_vars()
3830{
Bram Moolenaar33570922005-01-25 22:26:29 +00003831 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003832 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
Bram Moolenaar33570922005-01-25 22:26:29 +00003834 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003835 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003836 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003837 {
3838 if (!HASHITEM_EMPTY(hi))
3839 {
3840 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3842 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003843 }
3844 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003845 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846}
3847#endif
3848
3849#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3850
3851/*
3852 * Local string buffer for the next two functions to store a variable name
3853 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3854 * get_user_var_name().
3855 */
3856
3857static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3858
3859static char_u *varnamebuf = NULL;
3860static int varnamebuflen = 0;
3861
3862/*
3863 * Function to concatenate a prefix and a variable name.
3864 */
3865 static char_u *
3866cat_prefix_varname(prefix, name)
3867 int prefix;
3868 char_u *name;
3869{
3870 int len;
3871
3872 len = (int)STRLEN(name) + 3;
3873 if (len > varnamebuflen)
3874 {
3875 vim_free(varnamebuf);
3876 len += 10; /* some additional space */
3877 varnamebuf = alloc(len);
3878 if (varnamebuf == NULL)
3879 {
3880 varnamebuflen = 0;
3881 return NULL;
3882 }
3883 varnamebuflen = len;
3884 }
3885 *varnamebuf = prefix;
3886 varnamebuf[1] = ':';
3887 STRCPY(varnamebuf + 2, name);
3888 return varnamebuf;
3889}
3890
3891/*
3892 * Function given to ExpandGeneric() to obtain the list of user defined
3893 * (global/buffer/window/built-in) variable names.
3894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 char_u *
3896get_user_var_name(xp, idx)
3897 expand_T *xp;
3898 int idx;
3899{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003900 static long_u gdone;
3901 static long_u bdone;
3902 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003903#ifdef FEAT_WINDOWS
3904 static long_u tdone;
3905#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003906 static int vidx;
3907 static hashitem_T *hi;
3908 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909
3910 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003911 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003912 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003913#ifdef FEAT_WINDOWS
3914 tdone = 0;
3915#endif
3916 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003917
3918 /* Global variables */
3919 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003921 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003922 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003923 else
3924 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003925 while (HASHITEM_EMPTY(hi))
3926 ++hi;
3927 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3928 return cat_prefix_varname('g', hi->hi_key);
3929 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003931
3932 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003933 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003934 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003936 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003937 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003938 else
3939 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 while (HASHITEM_EMPTY(hi))
3941 ++hi;
3942 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 return (char_u *)"b:changedtick";
3948 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003949
3950 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003951 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003954 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003956 else
3957 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 while (HASHITEM_EMPTY(hi))
3959 ++hi;
3960 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003962
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003963#ifdef FEAT_WINDOWS
3964 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003965 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003966 if (tdone < ht->ht_used)
3967 {
3968 if (tdone++ == 0)
3969 hi = ht->ht_array;
3970 else
3971 ++hi;
3972 while (HASHITEM_EMPTY(hi))
3973 ++hi;
3974 return cat_prefix_varname('t', hi->hi_key);
3975 }
3976#endif
3977
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 /* v: variables */
3979 if (vidx < VV_LEN)
3980 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982 vim_free(varnamebuf);
3983 varnamebuf = NULL;
3984 varnamebuflen = 0;
3985 return NULL;
3986}
3987
3988#endif /* FEAT_CMDL_COMPL */
3989
3990/*
3991 * types for expressions.
3992 */
3993typedef enum
3994{
3995 TYPE_UNKNOWN = 0
3996 , TYPE_EQUAL /* == */
3997 , TYPE_NEQUAL /* != */
3998 , TYPE_GREATER /* > */
3999 , TYPE_GEQUAL /* >= */
4000 , TYPE_SMALLER /* < */
4001 , TYPE_SEQUAL /* <= */
4002 , TYPE_MATCH /* =~ */
4003 , TYPE_NOMATCH /* !~ */
4004} exptype_T;
4005
4006/*
4007 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4010 */
4011
4012/*
4013 * Handle zero level expression.
4014 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004016 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 * Return OK or FAIL.
4018 */
4019 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004020eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 char_u **nextcmd;
4024 int evaluate;
4025{
4026 int ret;
4027 char_u *p;
4028
4029 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 if (ret == FAIL || !ends_excmd(*p))
4032 {
4033 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 /*
4036 * Report the invalid expression unless the expression evaluation has
4037 * been cancelled due to an aborting error, an interrupt, or an
4038 * exception.
4039 */
4040 if (!aborting())
4041 EMSG2(_(e_invexpr2), arg);
4042 ret = FAIL;
4043 }
4044 if (nextcmd != NULL)
4045 *nextcmd = check_nextcmd(p);
4046
4047 return ret;
4048}
4049
4050/*
4051 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004052 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 *
4054 * "arg" must point to the first non-white of the expression.
4055 * "arg" is advanced to the next non-white after the recognized expression.
4056 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004057 * Note: "rettv.v_lock" is not set.
4058 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 * Return OK or FAIL.
4060 */
4061 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004062eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 int evaluate;
4066{
4067 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 /*
4071 * Get the first variable.
4072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 return FAIL;
4075
4076 if ((*arg)[0] == '?')
4077 {
4078 result = FALSE;
4079 if (evaluate)
4080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 int error = FALSE;
4082
4083 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004086 if (error)
4087 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089
4090 /*
4091 * Get the second variable.
4092 */
4093 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 return FAIL;
4096
4097 /*
4098 * Check for the ":".
4099 */
4100 if ((*arg)[0] != ':')
4101 {
4102 EMSG(_("E109: Missing ':' after '?'"));
4103 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 return FAIL;
4106 }
4107
4108 /*
4109 * Get the third variable.
4110 */
4111 *arg = skipwhite(*arg + 1);
4112 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4113 {
4114 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 return FAIL;
4117 }
4118 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
4122 return OK;
4123}
4124
4125/*
4126 * Handle first level expression:
4127 * expr2 || expr2 || expr2 logical OR
4128 *
4129 * "arg" must point to the first non-white of the expression.
4130 * "arg" is advanced to the next non-white after the recognized expression.
4131 *
4132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 int evaluate;
4139{
Bram Moolenaar33570922005-01-25 22:26:29 +00004140 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 long result;
4142 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004143 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
4145 /*
4146 * Get the first variable.
4147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 return FAIL;
4150
4151 /*
4152 * Repeat until there is no following "||".
4153 */
4154 first = TRUE;
4155 result = FALSE;
4156 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4157 {
4158 if (evaluate && first)
4159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004160 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004162 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004163 if (error)
4164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 first = FALSE;
4166 }
4167
4168 /*
4169 * Get the second variable.
4170 */
4171 *arg = skipwhite(*arg + 2);
4172 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4173 return FAIL;
4174
4175 /*
4176 * Compute the result.
4177 */
4178 if (evaluate && !result)
4179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004180 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004183 if (error)
4184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 if (evaluate)
4187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 rettv->v_type = VAR_NUMBER;
4189 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191 }
4192
4193 return OK;
4194}
4195
4196/*
4197 * Handle second level expression:
4198 * expr3 && expr3 && expr3 logical AND
4199 *
4200 * "arg" must point to the first non-white of the expression.
4201 * "arg" is advanced to the next non-white after the recognized expression.
4202 *
4203 * Return OK or FAIL.
4204 */
4205 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 int evaluate;
4210{
Bram Moolenaar33570922005-01-25 22:26:29 +00004211 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 long result;
4213 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004214 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 /*
4217 * Get the first variable.
4218 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return FAIL;
4221
4222 /*
4223 * Repeat until there is no following "&&".
4224 */
4225 first = TRUE;
4226 result = TRUE;
4227 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4228 {
4229 if (evaluate && first)
4230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004231 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 if (error)
4235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 first = FALSE;
4237 }
4238
4239 /*
4240 * Get the second variable.
4241 */
4242 *arg = skipwhite(*arg + 2);
4243 if (eval4(arg, &var2, evaluate && result) == FAIL)
4244 return FAIL;
4245
4246 /*
4247 * Compute the result.
4248 */
4249 if (evaluate && result)
4250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004254 if (error)
4255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 if (evaluate)
4258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 rettv->v_type = VAR_NUMBER;
4260 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 }
4263
4264 return OK;
4265}
4266
4267/*
4268 * Handle third level expression:
4269 * var1 == var2
4270 * var1 =~ var2
4271 * var1 != var2
4272 * var1 !~ var2
4273 * var1 > var2
4274 * var1 >= var2
4275 * var1 < var2
4276 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004277 * var1 is var2
4278 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 *
4280 * "arg" must point to the first non-white of the expression.
4281 * "arg" is advanced to the next non-white after the recognized expression.
4282 *
4283 * Return OK or FAIL.
4284 */
4285 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004288 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int evaluate;
4290{
Bram Moolenaar33570922005-01-25 22:26:29 +00004291 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 char_u *p;
4293 int i;
4294 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004295 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 int len = 2;
4297 long n1, n2;
4298 char_u *s1, *s2;
4299 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4300 regmatch_T regmatch;
4301 int ic;
4302 char_u *save_cpo;
4303
4304 /*
4305 * Get the first variable.
4306 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 return FAIL;
4309
4310 p = *arg;
4311 switch (p[0])
4312 {
4313 case '=': if (p[1] == '=')
4314 type = TYPE_EQUAL;
4315 else if (p[1] == '~')
4316 type = TYPE_MATCH;
4317 break;
4318 case '!': if (p[1] == '=')
4319 type = TYPE_NEQUAL;
4320 else if (p[1] == '~')
4321 type = TYPE_NOMATCH;
4322 break;
4323 case '>': if (p[1] != '=')
4324 {
4325 type = TYPE_GREATER;
4326 len = 1;
4327 }
4328 else
4329 type = TYPE_GEQUAL;
4330 break;
4331 case '<': if (p[1] != '=')
4332 {
4333 type = TYPE_SMALLER;
4334 len = 1;
4335 }
4336 else
4337 type = TYPE_SEQUAL;
4338 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004339 case 'i': if (p[1] == 's')
4340 {
4341 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4342 len = 5;
4343 if (!vim_isIDc(p[len]))
4344 {
4345 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4346 type_is = TRUE;
4347 }
4348 }
4349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351
4352 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004353 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 */
4355 if (type != TYPE_UNKNOWN)
4356 {
4357 /* extra question mark appended: ignore case */
4358 if (p[len] == '?')
4359 {
4360 ic = TRUE;
4361 ++len;
4362 }
4363 /* extra '#' appended: match case */
4364 else if (p[len] == '#')
4365 {
4366 ic = FALSE;
4367 ++len;
4368 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004369 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 else
4371 ic = p_ic;
4372
4373 /*
4374 * Get the second variable.
4375 */
4376 *arg = skipwhite(p + len);
4377 if (eval5(arg, &var2, evaluate) == FAIL)
4378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 return FAIL;
4381 }
4382
4383 if (evaluate)
4384 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004385 if (type_is && rettv->v_type != var2.v_type)
4386 {
4387 /* For "is" a different type always means FALSE, for "notis"
4388 * it means TRUE. */
4389 n1 = (type == TYPE_NEQUAL);
4390 }
4391 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4392 {
4393 if (type_is)
4394 {
4395 n1 = (rettv->v_type == var2.v_type
4396 && rettv->vval.v_list == var2.vval.v_list);
4397 if (type == TYPE_NEQUAL)
4398 n1 = !n1;
4399 }
4400 else if (rettv->v_type != var2.v_type
4401 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4402 {
4403 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004404 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004405 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004406 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004407 clear_tv(rettv);
4408 clear_tv(&var2);
4409 return FAIL;
4410 }
4411 else
4412 {
4413 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004414 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4415 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 }
4420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004421 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4422 {
4423 if (type_is)
4424 {
4425 n1 = (rettv->v_type == var2.v_type
4426 && rettv->vval.v_dict == var2.vval.v_dict);
4427 if (type == TYPE_NEQUAL)
4428 n1 = !n1;
4429 }
4430 else if (rettv->v_type != var2.v_type
4431 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4432 {
4433 if (rettv->v_type != var2.v_type)
4434 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4435 else
4436 EMSG(_("E736: Invalid operation for Dictionary"));
4437 clear_tv(rettv);
4438 clear_tv(&var2);
4439 return FAIL;
4440 }
4441 else
4442 {
4443 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004444 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4445 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004446 if (type == TYPE_NEQUAL)
4447 n1 = !n1;
4448 }
4449 }
4450
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4452 {
4453 if (rettv->v_type != var2.v_type
4454 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4455 {
4456 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004457 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004458 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004459 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004460 clear_tv(rettv);
4461 clear_tv(&var2);
4462 return FAIL;
4463 }
4464 else
4465 {
4466 /* Compare two Funcrefs for being equal or unequal. */
4467 if (rettv->vval.v_string == NULL
4468 || var2.vval.v_string == NULL)
4469 n1 = FALSE;
4470 else
4471 n1 = STRCMP(rettv->vval.v_string,
4472 var2.vval.v_string) == 0;
4473 if (type == TYPE_NEQUAL)
4474 n1 = !n1;
4475 }
4476 }
4477
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004478#ifdef FEAT_FLOAT
4479 /*
4480 * If one of the two variables is a float, compare as a float.
4481 * When using "=~" or "!~", always compare as string.
4482 */
4483 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4484 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4485 {
4486 float_T f1, f2;
4487
4488 if (rettv->v_type == VAR_FLOAT)
4489 f1 = rettv->vval.v_float;
4490 else
4491 f1 = get_tv_number(rettv);
4492 if (var2.v_type == VAR_FLOAT)
4493 f2 = var2.vval.v_float;
4494 else
4495 f2 = get_tv_number(&var2);
4496 n1 = FALSE;
4497 switch (type)
4498 {
4499 case TYPE_EQUAL: n1 = (f1 == f2); break;
4500 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4501 case TYPE_GREATER: n1 = (f1 > f2); break;
4502 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4503 case TYPE_SMALLER: n1 = (f1 < f2); break;
4504 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4505 case TYPE_UNKNOWN:
4506 case TYPE_MATCH:
4507 case TYPE_NOMATCH: break; /* avoid gcc warning */
4508 }
4509 }
4510#endif
4511
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /*
4513 * If one of the two variables is a number, compare as a number.
4514 * When using "=~" or "!~", always compare as string.
4515 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004519 n1 = get_tv_number(rettv);
4520 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 switch (type)
4522 {
4523 case TYPE_EQUAL: n1 = (n1 == n2); break;
4524 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4525 case TYPE_GREATER: n1 = (n1 > n2); break;
4526 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4527 case TYPE_SMALLER: n1 = (n1 < n2); break;
4528 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4529 case TYPE_UNKNOWN:
4530 case TYPE_MATCH:
4531 case TYPE_NOMATCH: break; /* avoid gcc warning */
4532 }
4533 }
4534 else
4535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004536 s1 = get_tv_string_buf(rettv, buf1);
4537 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4539 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4540 else
4541 i = 0;
4542 n1 = FALSE;
4543 switch (type)
4544 {
4545 case TYPE_EQUAL: n1 = (i == 0); break;
4546 case TYPE_NEQUAL: n1 = (i != 0); break;
4547 case TYPE_GREATER: n1 = (i > 0); break;
4548 case TYPE_GEQUAL: n1 = (i >= 0); break;
4549 case TYPE_SMALLER: n1 = (i < 0); break;
4550 case TYPE_SEQUAL: n1 = (i <= 0); break;
4551
4552 case TYPE_MATCH:
4553 case TYPE_NOMATCH:
4554 /* avoid 'l' flag in 'cpoptions' */
4555 save_cpo = p_cpo;
4556 p_cpo = (char_u *)"";
4557 regmatch.regprog = vim_regcomp(s2,
4558 RE_MAGIC + RE_STRING);
4559 regmatch.rm_ic = ic;
4560 if (regmatch.regprog != NULL)
4561 {
4562 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4563 vim_free(regmatch.regprog);
4564 if (type == TYPE_NOMATCH)
4565 n1 = !n1;
4566 }
4567 p_cpo = save_cpo;
4568 break;
4569
4570 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4571 }
4572 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 clear_tv(rettv);
4574 clear_tv(&var2);
4575 rettv->v_type = VAR_NUMBER;
4576 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 }
4579
4580 return OK;
4581}
4582
4583/*
4584 * Handle fourth level expression:
4585 * + number addition
4586 * - number subtraction
4587 * . string concatenation
4588 *
4589 * "arg" must point to the first non-white of the expression.
4590 * "arg" is advanced to the next non-white after the recognized expression.
4591 *
4592 * Return OK or FAIL.
4593 */
4594 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004595eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004597 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 int evaluate;
4599{
Bram Moolenaar33570922005-01-25 22:26:29 +00004600 typval_T var2;
4601 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 int op;
4603 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004604#ifdef FEAT_FLOAT
4605 float_T f1 = 0, f2 = 0;
4606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 char_u *s1, *s2;
4608 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4609 char_u *p;
4610
4611 /*
4612 * Get the first variable.
4613 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004614 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 return FAIL;
4616
4617 /*
4618 * Repeat computing, until no '+', '-' or '.' is following.
4619 */
4620 for (;;)
4621 {
4622 op = **arg;
4623 if (op != '+' && op != '-' && op != '.')
4624 break;
4625
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004626 if ((op != '+' || rettv->v_type != VAR_LIST)
4627#ifdef FEAT_FLOAT
4628 && (op == '.' || rettv->v_type != VAR_FLOAT)
4629#endif
4630 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 {
4632 /* For "list + ...", an illegal use of the first operand as
4633 * a number cannot be determined before evaluating the 2nd
4634 * operand: if this is also a list, all is ok.
4635 * For "something . ...", "something - ..." or "non-list + ...",
4636 * we know that the first operand needs to be a string or number
4637 * without evaluating the 2nd operand. So check before to avoid
4638 * side effects after an error. */
4639 if (evaluate && get_tv_string_chk(rettv) == NULL)
4640 {
4641 clear_tv(rettv);
4642 return FAIL;
4643 }
4644 }
4645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 /*
4647 * Get the second variable.
4648 */
4649 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004650 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 return FAIL;
4654 }
4655
4656 if (evaluate)
4657 {
4658 /*
4659 * Compute the result.
4660 */
4661 if (op == '.')
4662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004663 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4664 s2 = get_tv_string_buf_chk(&var2, buf2);
4665 if (s2 == NULL) /* type error ? */
4666 {
4667 clear_tv(rettv);
4668 clear_tv(&var2);
4669 return FAIL;
4670 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004671 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004672 clear_tv(rettv);
4673 rettv->v_type = VAR_STRING;
4674 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004676 else if (op == '+' && rettv->v_type == VAR_LIST
4677 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004678 {
4679 /* concatenate Lists */
4680 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4681 &var3) == FAIL)
4682 {
4683 clear_tv(rettv);
4684 clear_tv(&var2);
4685 return FAIL;
4686 }
4687 clear_tv(rettv);
4688 *rettv = var3;
4689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else
4691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 int error = FALSE;
4693
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694#ifdef FEAT_FLOAT
4695 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697 f1 = rettv->vval.v_float;
4698 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004700 else
4701#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004703 n1 = get_tv_number_chk(rettv, &error);
4704 if (error)
4705 {
4706 /* This can only happen for "list + non-list". For
4707 * "non-list + ..." or "something - ...", we returned
4708 * before evaluating the 2nd operand. */
4709 clear_tv(rettv);
4710 return FAIL;
4711 }
4712#ifdef FEAT_FLOAT
4713 if (var2.v_type == VAR_FLOAT)
4714 f1 = n1;
4715#endif
4716 }
4717#ifdef FEAT_FLOAT
4718 if (var2.v_type == VAR_FLOAT)
4719 {
4720 f2 = var2.vval.v_float;
4721 n2 = 0;
4722 }
4723 else
4724#endif
4725 {
4726 n2 = get_tv_number_chk(&var2, &error);
4727 if (error)
4728 {
4729 clear_tv(rettv);
4730 clear_tv(&var2);
4731 return FAIL;
4732 }
4733#ifdef FEAT_FLOAT
4734 if (rettv->v_type == VAR_FLOAT)
4735 f2 = n2;
4736#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004739
4740#ifdef FEAT_FLOAT
4741 /* If there is a float on either side the result is a float. */
4742 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4743 {
4744 if (op == '+')
4745 f1 = f1 + f2;
4746 else
4747 f1 = f1 - f2;
4748 rettv->v_type = VAR_FLOAT;
4749 rettv->vval.v_float = f1;
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004752#endif
4753 {
4754 if (op == '+')
4755 n1 = n1 + n2;
4756 else
4757 n1 = n1 - n2;
4758 rettv->v_type = VAR_NUMBER;
4759 rettv->vval.v_number = n1;
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 }
4765 return OK;
4766}
4767
4768/*
4769 * Handle fifth level expression:
4770 * * number multiplication
4771 * / number division
4772 * % number modulo
4773 *
4774 * "arg" must point to the first non-white of the expression.
4775 * "arg" is advanced to the next non-white after the recognized expression.
4776 *
4777 * Return OK or FAIL.
4778 */
4779 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004780eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004784 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
Bram Moolenaar33570922005-01-25 22:26:29 +00004786 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 int op;
4788 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789#ifdef FEAT_FLOAT
4790 int use_float = FALSE;
4791 float_T f1 = 0, f2;
4792#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795 /*
4796 * Get the first variable.
4797 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004798 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 return FAIL;
4800
4801 /*
4802 * Repeat computing, until no '*', '/' or '%' is following.
4803 */
4804 for (;;)
4805 {
4806 op = **arg;
4807 if (op != '*' && op != '/' && op != '%')
4808 break;
4809
4810 if (evaluate)
4811 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004812#ifdef FEAT_FLOAT
4813 if (rettv->v_type == VAR_FLOAT)
4814 {
4815 f1 = rettv->vval.v_float;
4816 use_float = TRUE;
4817 n1 = 0;
4818 }
4819 else
4820#endif
4821 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004822 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 if (error)
4824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 else
4827 n1 = 0;
4828
4829 /*
4830 * Get the second variable.
4831 */
4832 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004833 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return FAIL;
4835
4836 if (evaluate)
4837 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838#ifdef FEAT_FLOAT
4839 if (var2.v_type == VAR_FLOAT)
4840 {
4841 if (!use_float)
4842 {
4843 f1 = n1;
4844 use_float = TRUE;
4845 }
4846 f2 = var2.vval.v_float;
4847 n2 = 0;
4848 }
4849 else
4850#endif
4851 {
4852 n2 = get_tv_number_chk(&var2, &error);
4853 clear_tv(&var2);
4854 if (error)
4855 return FAIL;
4856#ifdef FEAT_FLOAT
4857 if (use_float)
4858 f2 = n2;
4859#endif
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 /*
4863 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#ifdef FEAT_FLOAT
4867 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869 if (op == '*')
4870 f1 = f1 * f2;
4871 else if (op == '/')
4872 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004873# ifdef VMS
4874 /* VMS crashes on divide by zero, work around it */
4875 if (f2 == 0.0)
4876 {
4877 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004878 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004879 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004880 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004881 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004882 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883 }
4884 else
4885 f1 = f1 / f2;
4886# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887 /* We rely on the floating point library to handle divide
4888 * by zero to result in "inf" and not a crash. */
4889 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004893 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004894 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 return FAIL;
4896 }
4897 rettv->v_type = VAR_FLOAT;
4898 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 if (op == '*')
4904 n1 = n1 * n2;
4905 else if (op == '/')
4906 {
4907 if (n2 == 0) /* give an error message? */
4908 {
4909 if (n1 == 0)
4910 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4911 else if (n1 < 0)
4912 n1 = -0x7fffffffL;
4913 else
4914 n1 = 0x7fffffffL;
4915 }
4916 else
4917 n1 = n1 / n2;
4918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920 {
4921 if (n2 == 0) /* give an error message? */
4922 n1 = 0;
4923 else
4924 n1 = n1 % n2;
4925 }
4926 rettv->v_type = VAR_NUMBER;
4927 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 }
4931
4932 return OK;
4933}
4934
4935/*
4936 * Handle sixth level expression:
4937 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004938 * "string" string constant
4939 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 * &option-name option value
4941 * @r register contents
4942 * identifier variable value
4943 * function() function call
4944 * $VAR environment variable
4945 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004946 * [expr, expr] List
4947 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 *
4949 * Also handle:
4950 * ! in front logical NOT
4951 * - in front unary minus
4952 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004953 * trailing [] subscript in String or List
4954 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 *
4956 * "arg" must point to the first non-white of the expression.
4957 * "arg" is advanced to the next non-white after the recognized expression.
4958 *
4959 * Return OK or FAIL.
4960 */
4961 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004962eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004966 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 long n;
4969 int len;
4970 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 char_u *start_leader, *end_leader;
4972 int ret = OK;
4973 char_u *alias;
4974
4975 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981 /*
4982 * Skip '!' and '-' characters. They are handled later.
4983 */
4984 start_leader = *arg;
4985 while (**arg == '!' || **arg == '-' || **arg == '+')
4986 *arg = skipwhite(*arg + 1);
4987 end_leader = *arg;
4988
4989 switch (**arg)
4990 {
4991 /*
4992 * Number constant.
4993 */
4994 case '0':
4995 case '1':
4996 case '2':
4997 case '3':
4998 case '4':
4999 case '5':
5000 case '6':
5001 case '7':
5002 case '8':
5003 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 {
5005#ifdef FEAT_FLOAT
5006 char_u *p = skipdigits(*arg + 1);
5007 int get_float = FALSE;
5008
5009 /* We accept a float when the format matches
5010 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005011 * strict to avoid backwards compatibility problems.
5012 * Don't look for a float after the "." operator, so that
5013 * ":let vers = 1.2.3" doesn't fail. */
5014 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005016 get_float = TRUE;
5017 p = skipdigits(p + 2);
5018 if (*p == 'e' || *p == 'E')
5019 {
5020 ++p;
5021 if (*p == '-' || *p == '+')
5022 ++p;
5023 if (!vim_isdigit(*p))
5024 get_float = FALSE;
5025 else
5026 p = skipdigits(p + 1);
5027 }
5028 if (ASCII_ISALPHA(*p) || *p == '.')
5029 get_float = FALSE;
5030 }
5031 if (get_float)
5032 {
5033 float_T f;
5034
5035 *arg += string2float(*arg, &f);
5036 if (evaluate)
5037 {
5038 rettv->v_type = VAR_FLOAT;
5039 rettv->vval.v_float = f;
5040 }
5041 }
5042 else
5043#endif
5044 {
5045 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5046 *arg += len;
5047 if (evaluate)
5048 {
5049 rettv->v_type = VAR_NUMBER;
5050 rettv->vval.v_number = n;
5051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 /*
5057 * String constant: "string".
5058 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061
5062 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005063 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 break;
5067
5068 /*
5069 * List: [expr, expr]
5070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 break;
5073
5074 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 * Dictionary: {key: val, key: val}
5076 */
5077 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5078 break;
5079
5080 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005081 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005083 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085
5086 /*
5087 * Environment variable: $VAR.
5088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 break;
5091
5092 /*
5093 * Register contents: @r.
5094 */
5095 case '@': ++*arg;
5096 if (evaluate)
5097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005099 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101 if (**arg != NUL)
5102 ++*arg;
5103 break;
5104
5105 /*
5106 * nested expression: (expression).
5107 */
5108 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if (**arg == ')')
5111 ++*arg;
5112 else if (ret == OK)
5113 {
5114 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 ret = FAIL;
5117 }
5118 break;
5119
Bram Moolenaar8c711452005-01-14 21:53:12 +00005120 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 break;
5122 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005123
5124 if (ret == NOTDONE)
5125 {
5126 /*
5127 * Must be a variable or function name.
5128 * Can also be a curly-braces kind of name: {expr}.
5129 */
5130 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005131 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 if (alias != NULL)
5133 s = alias;
5134
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005135 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005136 ret = FAIL;
5137 else
5138 {
5139 if (**arg == '(') /* recursive! */
5140 {
5141 /* If "s" is the name of a variable of type VAR_FUNC
5142 * use its contents. */
5143 s = deref_func_name(s, &len);
5144
5145 /* Invoke the function. */
5146 ret = get_func_tv(s, len, rettv, arg,
5147 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005148 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005149
5150 /* If evaluate is FALSE rettv->v_type was not set in
5151 * get_func_tv, but it's needed in handle_subscript() to parse
5152 * what follows. So set it here. */
5153 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5154 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005155 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005156 rettv->v_type = VAR_FUNC;
5157 }
5158
Bram Moolenaar8c711452005-01-14 21:53:12 +00005159 /* Stop the expression evaluation when immediately
5160 * aborting on error, or when an interrupt occurred or
5161 * an exception was thrown but not caught. */
5162 if (aborting())
5163 {
5164 if (ret == OK)
5165 clear_tv(rettv);
5166 ret = FAIL;
5167 }
5168 }
5169 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005170 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005171 else
5172 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005174 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 }
5176
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 *arg = skipwhite(*arg);
5178
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005179 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5180 * expr(expr). */
5181 if (ret == OK)
5182 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
5184 /*
5185 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5186 */
5187 if (ret == OK && evaluate && end_leader > start_leader)
5188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005189 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005190 int val = 0;
5191#ifdef FEAT_FLOAT
5192 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005193
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005194 if (rettv->v_type == VAR_FLOAT)
5195 f = rettv->vval.v_float;
5196 else
5197#endif
5198 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005199 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 clear_tv(rettv);
5202 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005204 else
5205 {
5206 while (end_leader > start_leader)
5207 {
5208 --end_leader;
5209 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005210 {
5211#ifdef FEAT_FLOAT
5212 if (rettv->v_type == VAR_FLOAT)
5213 f = !f;
5214 else
5215#endif
5216 val = !val;
5217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 {
5220#ifdef FEAT_FLOAT
5221 if (rettv->v_type == VAR_FLOAT)
5222 f = -f;
5223 else
5224#endif
5225 val = -val;
5226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005228#ifdef FEAT_FLOAT
5229 if (rettv->v_type == VAR_FLOAT)
5230 {
5231 clear_tv(rettv);
5232 rettv->vval.v_float = f;
5233 }
5234 else
5235#endif
5236 {
5237 clear_tv(rettv);
5238 rettv->v_type = VAR_NUMBER;
5239 rettv->vval.v_number = val;
5240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243
5244 return ret;
5245}
5246
5247/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005248 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5249 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5251 */
5252 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005253eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005254 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005255 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005257 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258{
5259 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005262 long len = -1;
5263 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005267 if (rettv->v_type == VAR_FUNC
5268#ifdef FEAT_FLOAT
5269 || rettv->v_type == VAR_FLOAT
5270#endif
5271 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273 if (verbose)
5274 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 return FAIL;
5276 }
5277
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 /*
5281 * dict.name
5282 */
5283 key = *arg + 1;
5284 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5285 ;
5286 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289 }
5290 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 /*
5293 * something[idx]
5294 *
5295 * Get the (first) variable from inside the [].
5296 */
5297 *arg = skipwhite(*arg + 1);
5298 if (**arg == ':')
5299 empty1 = TRUE;
5300 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5301 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5303 {
5304 /* not a number or string */
5305 clear_tv(&var1);
5306 return FAIL;
5307 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308
5309 /*
5310 * Get the second variable from inside the [:].
5311 */
5312 if (**arg == ':')
5313 {
5314 range = TRUE;
5315 *arg = skipwhite(*arg + 1);
5316 if (**arg == ']')
5317 empty2 = TRUE;
5318 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 if (!empty1)
5321 clear_tv(&var1);
5322 return FAIL;
5323 }
5324 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5325 {
5326 /* not a number or string */
5327 if (!empty1)
5328 clear_tv(&var1);
5329 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005330 return FAIL;
5331 }
5332 }
5333
5334 /* Check for the ']'. */
5335 if (**arg != ']')
5336 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005337 if (verbose)
5338 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 clear_tv(&var1);
5340 if (range)
5341 clear_tv(&var2);
5342 return FAIL;
5343 }
5344 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 }
5346
5347 if (evaluate)
5348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005349 n1 = 0;
5350 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 n1 = get_tv_number(&var1);
5353 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 }
5355 if (range)
5356 {
5357 if (empty2)
5358 n2 = -1;
5359 else
5360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005361 n2 = get_tv_number(&var2);
5362 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 }
5364 }
5365
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005366 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
5368 case VAR_NUMBER:
5369 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005370 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 len = (long)STRLEN(s);
5372 if (range)
5373 {
5374 /* The resulting variable is a substring. If the indexes
5375 * are out of range the result is empty. */
5376 if (n1 < 0)
5377 {
5378 n1 = len + n1;
5379 if (n1 < 0)
5380 n1 = 0;
5381 }
5382 if (n2 < 0)
5383 n2 = len + n2;
5384 else if (n2 >= len)
5385 n2 = len;
5386 if (n1 >= len || n2 < 0 || n1 > n2)
5387 s = NULL;
5388 else
5389 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5390 }
5391 else
5392 {
5393 /* The resulting variable is a string of a single
5394 * character. If the index is too big or negative the
5395 * result is empty. */
5396 if (n1 >= len || n1 < 0)
5397 s = NULL;
5398 else
5399 s = vim_strnsave(s + n1, 1);
5400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005401 clear_tv(rettv);
5402 rettv->v_type = VAR_STRING;
5403 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 break;
5405
5406 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005407 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 if (n1 < 0)
5409 n1 = len + n1;
5410 if (!empty1 && (n1 < 0 || n1 >= len))
5411 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005412 /* For a range we allow invalid values and return an empty
5413 * list. A list index out of range is an error. */
5414 if (!range)
5415 {
5416 if (verbose)
5417 EMSGN(_(e_listidx), n1);
5418 return FAIL;
5419 }
5420 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 }
5422 if (range)
5423 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005424 list_T *l;
5425 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426
5427 if (n2 < 0)
5428 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005429 else if (n2 >= len)
5430 n2 = len - 1;
5431 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005432 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 l = list_alloc();
5434 if (l == NULL)
5435 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005436 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 n1 <= n2; ++n1)
5438 {
5439 if (list_append_tv(l, &item->li_tv) == FAIL)
5440 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005441 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 return FAIL;
5443 }
5444 item = item->li_next;
5445 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 clear_tv(rettv);
5447 rettv->v_type = VAR_LIST;
5448 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005449 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450 }
5451 else
5452 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005453 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005454 clear_tv(rettv);
5455 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458
5459 case VAR_DICT:
5460 if (range)
5461 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005462 if (verbose)
5463 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 if (len == -1)
5465 clear_tv(&var1);
5466 return FAIL;
5467 }
5468 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005469 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470
5471 if (len == -1)
5472 {
5473 key = get_tv_string(&var1);
5474 if (*key == NUL)
5475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005476 if (verbose)
5477 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 clear_tv(&var1);
5479 return FAIL;
5480 }
5481 }
5482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005483 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005486 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 if (len == -1)
5488 clear_tv(&var1);
5489 if (item == NULL)
5490 return FAIL;
5491
5492 copy_tv(&item->di_tv, &var1);
5493 clear_tv(rettv);
5494 *rettv = var1;
5495 }
5496 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 }
5498 }
5499
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 return OK;
5501}
5502
5503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 * Get an option value.
5505 * "arg" points to the '&' or '+' before the option name.
5506 * "arg" is advanced to character after the option name.
5507 * Return OK or FAIL.
5508 */
5509 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 int evaluate;
5514{
5515 char_u *option_end;
5516 long numval;
5517 char_u *stringval;
5518 int opt_type;
5519 int c;
5520 int working = (**arg == '+'); /* has("+option") */
5521 int ret = OK;
5522 int opt_flags;
5523
5524 /*
5525 * Isolate the option name and find its value.
5526 */
5527 option_end = find_option_end(arg, &opt_flags);
5528 if (option_end == NULL)
5529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 EMSG2(_("E112: Option name missing: %s"), *arg);
5532 return FAIL;
5533 }
5534
5535 if (!evaluate)
5536 {
5537 *arg = option_end;
5538 return OK;
5539 }
5540
5541 c = *option_end;
5542 *option_end = NUL;
5543 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546 if (opt_type == -3) /* invalid name */
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 EMSG2(_("E113: Unknown option: %s"), *arg);
5550 ret = FAIL;
5551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
5554 if (opt_type == -2) /* hidden string option */
5555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 rettv->v_type = VAR_STRING;
5557 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
5559 else if (opt_type == -1) /* hidden number option */
5560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 rettv->v_type = VAR_NUMBER;
5562 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564 else if (opt_type == 1) /* number option */
5565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 rettv->v_type = VAR_NUMBER;
5567 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 else /* string option */
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 rettv->v_type = VAR_STRING;
5572 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574 }
5575 else if (working && (opt_type == -2 || opt_type == -1))
5576 ret = FAIL;
5577
5578 *option_end = c; /* put back for error messages */
5579 *arg = option_end;
5580
5581 return ret;
5582}
5583
5584/*
5585 * Allocate a variable for a string constant.
5586 * Return OK or FAIL.
5587 */
5588 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 int evaluate;
5593{
5594 char_u *p;
5595 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 int extra = 0;
5597
5598 /*
5599 * Find the end of the string, skipping backslashed characters.
5600 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005601 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 {
5603 if (*p == '\\' && p[1] != NUL)
5604 {
5605 ++p;
5606 /* A "\<x>" form occupies at least 4 characters, and produces up
5607 * to 6 characters: reserve space for 2 extra */
5608 if (*p == '<')
5609 extra += 2;
5610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612
5613 if (*p != '"')
5614 {
5615 EMSG2(_("E114: Missing quote: %s"), *arg);
5616 return FAIL;
5617 }
5618
5619 /* If only parsing, set *arg and return here */
5620 if (!evaluate)
5621 {
5622 *arg = p + 1;
5623 return OK;
5624 }
5625
5626 /*
5627 * Copy the string into allocated memory, handling backslashed
5628 * characters.
5629 */
5630 name = alloc((unsigned)(p - *arg + extra));
5631 if (name == NULL)
5632 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 rettv->v_type = VAR_STRING;
5634 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
5638 if (*p == '\\')
5639 {
5640 switch (*++p)
5641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 case 'b': *name++ = BS; ++p; break;
5643 case 'e': *name++ = ESC; ++p; break;
5644 case 'f': *name++ = FF; ++p; break;
5645 case 'n': *name++ = NL; ++p; break;
5646 case 'r': *name++ = CAR; ++p; break;
5647 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 case 'X': /* hex: "\x1", "\x12" */
5650 case 'x':
5651 case 'u': /* Unicode: "\u0023" */
5652 case 'U':
5653 if (vim_isxdigit(p[1]))
5654 {
5655 int n, nr;
5656 int c = toupper(*p);
5657
5658 if (c == 'X')
5659 n = 2;
5660 else
5661 n = 4;
5662 nr = 0;
5663 while (--n >= 0 && vim_isxdigit(p[1]))
5664 {
5665 ++p;
5666 nr = (nr << 4) + hex2nr(*p);
5667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669#ifdef FEAT_MBYTE
5670 /* For "\u" store the number according to
5671 * 'encoding'. */
5672 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 else
5675#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005676 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 break;
5679
5680 /* octal: "\1", "\12", "\123" */
5681 case '0':
5682 case '1':
5683 case '2':
5684 case '3':
5685 case '4':
5686 case '5':
5687 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 case '7': *name = *p++ - '0';
5689 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 *name = (*name << 3) + *p++ - '0';
5692 if (*p >= '0' && *p <= '7')
5693 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 break;
5697
5698 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (extra != 0)
5701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 break;
5704 }
5705 /* FALLTHROUGH */
5706
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 break;
5709 }
5710 }
5711 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005715 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 *arg = p + 1;
5717
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 return OK;
5719}
5720
5721/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 * Return OK or FAIL.
5724 */
5725 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 int evaluate;
5730{
5731 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005732 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005733 int reduce = 0;
5734
5735 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005737 */
5738 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005740 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005741 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005742 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 break;
5744 ++reduce;
5745 ++p;
5746 }
5747 }
5748
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 return FAIL;
5753 }
5754
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 if (!evaluate)
5757 {
5758 *arg = p + 1;
5759 return OK;
5760 }
5761
5762 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 */
5765 str = alloc((unsigned)((p - *arg) - reduce));
5766 if (str == NULL)
5767 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005768 rettv->v_type = VAR_STRING;
5769 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005773 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 break;
5777 ++p;
5778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 *arg = p + 1;
5783
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 return OK;
5785}
5786
5787/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 * Allocate a variable for a List and fill it from "*arg".
5789 * Return OK or FAIL.
5790 */
5791 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005792get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005793 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005794 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 int evaluate;
5796{
Bram Moolenaar33570922005-01-25 22:26:29 +00005797 list_T *l = NULL;
5798 typval_T tv;
5799 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800
5801 if (evaluate)
5802 {
5803 l = list_alloc();
5804 if (l == NULL)
5805 return FAIL;
5806 }
5807
5808 *arg = skipwhite(*arg + 1);
5809 while (**arg != ']' && **arg != NUL)
5810 {
5811 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5812 goto failret;
5813 if (evaluate)
5814 {
5815 item = listitem_alloc();
5816 if (item != NULL)
5817 {
5818 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005819 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005820 list_append(l, item);
5821 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005822 else
5823 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005824 }
5825
5826 if (**arg == ']')
5827 break;
5828 if (**arg != ',')
5829 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 goto failret;
5832 }
5833 *arg = skipwhite(*arg + 1);
5834 }
5835
5836 if (**arg != ']')
5837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839failret:
5840 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005841 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 return FAIL;
5843 }
5844
5845 *arg = skipwhite(*arg + 1);
5846 if (evaluate)
5847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005848 rettv->v_type = VAR_LIST;
5849 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850 ++l->lv_refcount;
5851 }
5852
5853 return OK;
5854}
5855
5856/*
5857 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005858 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005859 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005860 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861list_alloc()
5862{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005863 list_T *l;
5864
5865 l = (list_T *)alloc_clear(sizeof(list_T));
5866 if (l != NULL)
5867 {
5868 /* Prepend the list to the list of lists for garbage collection. */
5869 if (first_list != NULL)
5870 first_list->lv_used_prev = l;
5871 l->lv_used_prev = NULL;
5872 l->lv_used_next = first_list;
5873 first_list = l;
5874 }
5875 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876}
5877
5878/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005879 * Allocate an empty list for a return value.
5880 * Returns OK or FAIL.
5881 */
5882 static int
5883rettv_list_alloc(rettv)
5884 typval_T *rettv;
5885{
5886 list_T *l = list_alloc();
5887
5888 if (l == NULL)
5889 return FAIL;
5890
5891 rettv->vval.v_list = l;
5892 rettv->v_type = VAR_LIST;
5893 ++l->lv_refcount;
5894 return OK;
5895}
5896
5897/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005898 * Unreference a list: decrement the reference count and free it when it
5899 * becomes zero.
5900 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005901 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005903 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005905 if (l != NULL && --l->lv_refcount <= 0)
5906 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907}
5908
5909/*
5910 * Free a list, including all items it points to.
5911 * Ignores the reference count.
5912 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005913 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005914list_free(l, recurse)
5915 list_T *l;
5916 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917{
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005920 /* Remove the list from the list of lists for garbage collection. */
5921 if (l->lv_used_prev == NULL)
5922 first_list = l->lv_used_next;
5923 else
5924 l->lv_used_prev->lv_used_next = l->lv_used_next;
5925 if (l->lv_used_next != NULL)
5926 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5927
Bram Moolenaard9fba312005-06-26 22:34:35 +00005928 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005930 /* Remove the item before deleting it. */
5931 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005932 if (recurse || (item->li_tv.v_type != VAR_LIST
5933 && item->li_tv.v_type != VAR_DICT))
5934 clear_tv(&item->li_tv);
5935 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 }
5937 vim_free(l);
5938}
5939
5940/*
5941 * Allocate a list item.
5942 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005943 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944listitem_alloc()
5945{
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947}
5948
5949/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005950 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005952 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005953listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005954 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 vim_free(item);
5958}
5959
5960/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005961 * Remove a list item from a List and free it. Also clears the value.
5962 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005963 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005964listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 list_T *l;
5966 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005967{
5968 list_remove(l, item, item);
5969 listitem_free(item);
5970}
5971
5972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 * Get the number of items in a list.
5974 */
5975 static long
5976list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979 if (l == NULL)
5980 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005981 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982}
5983
5984/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005985 * Return TRUE when two lists have exactly the same values.
5986 */
5987 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005988list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l1;
5990 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005991 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005993{
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005996 if (l1 == NULL || l2 == NULL)
5997 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005998 if (l1 == l2)
5999 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006000 if (list_len(l1) != list_len(l2))
6001 return FALSE;
6002
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003 for (item1 = l1->lv_first, item2 = l2->lv_first;
6004 item1 != NULL && item2 != NULL;
6005 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007 return FALSE;
6008 return item1 == NULL && item2 == NULL;
6009}
6010
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006011#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6012 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006013/*
6014 * Return the dictitem that an entry in a hashtable points to.
6015 */
6016 dictitem_T *
6017dict_lookup(hi)
6018 hashitem_T *hi;
6019{
6020 return HI2DI(hi);
6021}
6022#endif
6023
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006024/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006025 * Return TRUE when two dictionaries have exactly the same key/values.
6026 */
6027 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006028dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 dict_T *d1;
6030 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006031 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006032 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006033{
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 hashitem_T *hi;
6035 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006038 if (d1 == NULL || d2 == NULL)
6039 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006040 if (d1 == d2)
6041 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042 if (dict_len(d1) != dict_len(d2))
6043 return FALSE;
6044
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006045 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006047 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 if (!HASHITEM_EMPTY(hi))
6049 {
6050 item2 = dict_find(d2, hi->hi_key, -1);
6051 if (item2 == NULL)
6052 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006053 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006054 return FALSE;
6055 --todo;
6056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006057 }
6058 return TRUE;
6059}
6060
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061static int tv_equal_recurse_limit;
6062
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006063/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006064 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006065 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006066 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006067 */
6068 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006069tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 typval_T *tv1;
6071 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072 int ic; /* ignore case */
6073 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006074{
6075 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006076 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006077 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006078 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006079
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006080 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006081 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006082
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006083 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084 * recursiveness to a limit. We guess they are equal then.
6085 * A fixed limit has the problem of still taking an awful long time.
6086 * Reduce the limit every time running into it. That should work fine for
6087 * deeply linked structures that are not recursively linked and catch
6088 * recursiveness quickly. */
6089 if (!recursive)
6090 tv_equal_recurse_limit = 1000;
6091 if (recursive_cnt >= tv_equal_recurse_limit)
6092 {
6093 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006096
6097 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006099 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 ++recursive_cnt;
6101 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6102 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006103 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104
6105 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 ++recursive_cnt;
6107 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, 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_FUNC:
6112 return (tv1->vval.v_string != NULL
6113 && tv2->vval.v_string != NULL
6114 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6115
6116 case VAR_NUMBER:
6117 return tv1->vval.v_number == tv2->vval.v_number;
6118
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006119#ifdef FEAT_FLOAT
6120 case VAR_FLOAT:
6121 return tv1->vval.v_float == tv2->vval.v_float;
6122#endif
6123
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006124 case VAR_STRING:
6125 s1 = get_tv_string_buf(tv1, buf1);
6126 s2 = get_tv_string_buf(tv2, buf2);
6127 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006128 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006129
6130 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131 return TRUE;
6132}
6133
6134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006135 * Locate item with index "n" in list "l" and return it.
6136 * A negative index is counted from the end; -1 is the last item.
6137 * Returns NULL when "n" is out of range.
6138 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006139 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142 long n;
6143{
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 long idx;
6146
6147 if (l == NULL)
6148 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006149
6150 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006152 n = l->lv_len + n;
6153
6154 /* Check for index out of range. */
6155 if (n < 0 || n >= l->lv_len)
6156 return NULL;
6157
6158 /* When there is a cached index may start search from there. */
6159 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006161 if (n < l->lv_idx / 2)
6162 {
6163 /* closest to the start of the list */
6164 item = l->lv_first;
6165 idx = 0;
6166 }
6167 else if (n > (l->lv_idx + l->lv_len) / 2)
6168 {
6169 /* closest to the end of the list */
6170 item = l->lv_last;
6171 idx = l->lv_len - 1;
6172 }
6173 else
6174 {
6175 /* closest to the cached index */
6176 item = l->lv_idx_item;
6177 idx = l->lv_idx;
6178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 }
6180 else
6181 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006182 if (n < l->lv_len / 2)
6183 {
6184 /* closest to the start of the list */
6185 item = l->lv_first;
6186 idx = 0;
6187 }
6188 else
6189 {
6190 /* closest to the end of the list */
6191 item = l->lv_last;
6192 idx = l->lv_len - 1;
6193 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006195
6196 while (n > idx)
6197 {
6198 /* search forward */
6199 item = item->li_next;
6200 ++idx;
6201 }
6202 while (n < idx)
6203 {
6204 /* search backward */
6205 item = item->li_prev;
6206 --idx;
6207 }
6208
6209 /* cache the used index */
6210 l->lv_idx = idx;
6211 l->lv_idx_item = item;
6212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 return item;
6214}
6215
6216/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006217 * Get list item "l[idx]" as a number.
6218 */
6219 static long
6220list_find_nr(l, idx, errorp)
6221 list_T *l;
6222 long idx;
6223 int *errorp; /* set to TRUE when something wrong */
6224{
6225 listitem_T *li;
6226
6227 li = list_find(l, idx);
6228 if (li == NULL)
6229 {
6230 if (errorp != NULL)
6231 *errorp = TRUE;
6232 return -1L;
6233 }
6234 return get_tv_number_chk(&li->li_tv, errorp);
6235}
6236
6237/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006238 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6239 */
6240 char_u *
6241list_find_str(l, idx)
6242 list_T *l;
6243 long idx;
6244{
6245 listitem_T *li;
6246
6247 li = list_find(l, idx - 1);
6248 if (li == NULL)
6249 {
6250 EMSGN(_(e_listidx), idx);
6251 return NULL;
6252 }
6253 return get_tv_string(&li->li_tv);
6254}
6255
6256/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006257 * Locate "item" list "l" and return its index.
6258 * Returns -1 when "item" is not in the list.
6259 */
6260 static long
6261list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 list_T *l;
6263 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006264{
6265 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006267
6268 if (l == NULL)
6269 return -1;
6270 idx = 0;
6271 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6272 ++idx;
6273 if (li == NULL)
6274 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006275 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006276}
6277
6278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 * Append item "item" to the end of list "l".
6280 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006281 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006283 list_T *l;
6284 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285{
6286 if (l->lv_last == NULL)
6287 {
6288 /* empty list */
6289 l->lv_first = item;
6290 l->lv_last = item;
6291 item->li_prev = NULL;
6292 }
6293 else
6294 {
6295 l->lv_last->li_next = item;
6296 item->li_prev = l->lv_last;
6297 l->lv_last = item;
6298 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006299 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006300 item->li_next = NULL;
6301}
6302
6303/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006305 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006307 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 list_T *l;
6310 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006312 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313
Bram Moolenaar05159a02005-02-26 23:04:13 +00006314 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006315 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006316 copy_tv(tv, &li->li_tv);
6317 list_append(l, li);
6318 return OK;
6319}
6320
6321/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006322 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 * Return FAIL when out of memory.
6324 */
6325 int
6326list_append_dict(list, dict)
6327 list_T *list;
6328 dict_T *dict;
6329{
6330 listitem_T *li = listitem_alloc();
6331
6332 if (li == NULL)
6333 return FAIL;
6334 li->li_tv.v_type = VAR_DICT;
6335 li->li_tv.v_lock = 0;
6336 li->li_tv.vval.v_dict = dict;
6337 list_append(list, li);
6338 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 return OK;
6340}
6341
6342/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006343 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006344 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006345 * Returns FAIL when out of memory.
6346 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006347 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006348list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006349 list_T *l;
6350 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006351 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006352{
6353 listitem_T *li = listitem_alloc();
6354
6355 if (li == NULL)
6356 return FAIL;
6357 list_append(l, li);
6358 li->li_tv.v_type = VAR_STRING;
6359 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006360 if (str == NULL)
6361 li->li_tv.vval.v_string = NULL;
6362 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006363 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006364 return FAIL;
6365 return OK;
6366}
6367
6368/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006369 * Append "n" to list "l".
6370 * Returns FAIL when out of memory.
6371 */
6372 static int
6373list_append_number(l, n)
6374 list_T *l;
6375 varnumber_T n;
6376{
6377 listitem_T *li;
6378
6379 li = listitem_alloc();
6380 if (li == NULL)
6381 return FAIL;
6382 li->li_tv.v_type = VAR_NUMBER;
6383 li->li_tv.v_lock = 0;
6384 li->li_tv.vval.v_number = n;
6385 list_append(l, li);
6386 return OK;
6387}
6388
6389/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006390 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006391 * If "item" is NULL append at the end.
6392 * Return FAIL when out of memory.
6393 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006394 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006395list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 list_T *l;
6397 typval_T *tv;
6398 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399{
Bram Moolenaar33570922005-01-25 22:26:29 +00006400 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006401
6402 if (ni == NULL)
6403 return FAIL;
6404 copy_tv(tv, &ni->li_tv);
6405 if (item == NULL)
6406 /* Append new item at end of list. */
6407 list_append(l, ni);
6408 else
6409 {
6410 /* Insert new item before existing item. */
6411 ni->li_prev = item->li_prev;
6412 ni->li_next = item;
6413 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006414 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006416 ++l->lv_idx;
6417 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006419 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006421 l->lv_idx_item = NULL;
6422 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006424 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 }
6426 return OK;
6427}
6428
6429/*
6430 * Extend "l1" with "l2".
6431 * If "bef" is NULL append at the end, otherwise insert before this item.
6432 * Returns FAIL when out of memory.
6433 */
6434 static int
6435list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 list_T *l1;
6437 list_T *l2;
6438 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006441 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006442
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006443 /* We also quit the loop when we have inserted the original item count of
6444 * the list, avoid a hang when we extend a list with itself. */
6445 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6447 return FAIL;
6448 return OK;
6449}
6450
6451/*
6452 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6453 * Return FAIL when out of memory.
6454 */
6455 static int
6456list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006457 list_T *l1;
6458 list_T *l2;
6459 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460{
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006462
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006463 if (l1 == NULL || l2 == NULL)
6464 return FAIL;
6465
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006466 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006467 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468 if (l == NULL)
6469 return FAIL;
6470 tv->v_type = VAR_LIST;
6471 tv->vval.v_list = l;
6472
6473 /* append all items from the second list */
6474 return list_extend(l, l2, NULL);
6475}
6476
6477/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006478 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006480 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 * Returns NULL when out of memory.
6482 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006483 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006484list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006487 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488{
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *copy;
6490 listitem_T *item;
6491 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492
6493 if (orig == NULL)
6494 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495
6496 copy = list_alloc();
6497 if (copy != NULL)
6498 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 if (copyID != 0)
6500 {
6501 /* Do this before adding the items, because one of the items may
6502 * refer back to this list. */
6503 orig->lv_copyID = copyID;
6504 orig->lv_copylist = copy;
6505 }
6506 for (item = orig->lv_first; item != NULL && !got_int;
6507 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 {
6509 ni = listitem_alloc();
6510 if (ni == NULL)
6511 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006512 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006513 {
6514 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6515 {
6516 vim_free(ni);
6517 break;
6518 }
6519 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006521 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 list_append(copy, ni);
6523 }
6524 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 if (item != NULL)
6526 {
6527 list_unref(copy);
6528 copy = NULL;
6529 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006530 }
6531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 return copy;
6533}
6534
6535/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006537 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006539 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006540list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 list_T *l;
6542 listitem_T *item;
6543 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544{
Bram Moolenaar33570922005-01-25 22:26:29 +00006545 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 /* notify watchers */
6548 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006550 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 list_fix_watch(l, ip);
6552 if (ip == item2)
6553 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555
6556 if (item2->li_next == NULL)
6557 l->lv_last = item->li_prev;
6558 else
6559 item2->li_next->li_prev = item->li_prev;
6560 if (item->li_prev == NULL)
6561 l->lv_first = item2->li_next;
6562 else
6563 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006564 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565}
6566
6567/*
6568 * Return an allocated string with the string representation of a list.
6569 * May return NULL.
6570 */
6571 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006572list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006574 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575{
6576 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577
6578 if (tv->vval.v_list == NULL)
6579 return NULL;
6580 ga_init2(&ga, (int)sizeof(char), 80);
6581 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006582 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 {
6584 vim_free(ga.ga_data);
6585 return NULL;
6586 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 ga_append(&ga, ']');
6588 ga_append(&ga, NUL);
6589 return (char_u *)ga.ga_data;
6590}
6591
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006592typedef struct join_S {
6593 char_u *s;
6594 char_u *tofree;
6595} join_T;
6596
6597 static int
6598list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6599 garray_T *gap; /* to store the result in */
6600 list_T *l;
6601 char_u *sep;
6602 int echo_style;
6603 int copyID;
6604 garray_T *join_gap; /* to keep each list item string */
6605{
6606 int i;
6607 join_T *p;
6608 int len;
6609 int sumlen = 0;
6610 int first = TRUE;
6611 char_u *tofree;
6612 char_u numbuf[NUMBUFLEN];
6613 listitem_T *item;
6614 char_u *s;
6615
6616 /* Stringify each item in the list. */
6617 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6618 {
6619 if (echo_style)
6620 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6621 else
6622 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6623 if (s == NULL)
6624 return FAIL;
6625
6626 len = (int)STRLEN(s);
6627 sumlen += len;
6628
6629 ga_grow(join_gap, 1);
6630 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6631 if (tofree != NULL || s != numbuf)
6632 {
6633 p->s = s;
6634 p->tofree = tofree;
6635 }
6636 else
6637 {
6638 p->s = vim_strnsave(s, len);
6639 p->tofree = p->s;
6640 }
6641
6642 line_breakcheck();
6643 }
6644
6645 /* Allocate result buffer with its total size, avoid re-allocation and
6646 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6647 if (join_gap->ga_len >= 2)
6648 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6649 if (ga_grow(gap, sumlen + 2) == FAIL)
6650 return FAIL;
6651
6652 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6653 {
6654 if (first)
6655 first = FALSE;
6656 else
6657 ga_concat(gap, sep);
6658 p = ((join_T *)join_gap->ga_data) + i;
6659
6660 if (p->s != NULL)
6661 ga_concat(gap, p->s);
6662 line_breakcheck();
6663 }
6664
6665 return OK;
6666}
6667
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006669 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006670 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006671 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006672 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006673 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006674list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006675 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006677 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006678 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006679 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006681 garray_T join_ga;
6682 int retval;
6683 join_T *p;
6684 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006685
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6687 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6688
6689 /* Dispose each item in join_ga. */
6690 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692 p = (join_T *)join_ga.ga_data;
6693 for (i = 0; i < join_ga.ga_len; ++i)
6694 {
6695 vim_free(p->tofree);
6696 ++p;
6697 }
6698 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700
6701 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702}
6703
6704/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006705 * Garbage collection for lists and dictionaries.
6706 *
6707 * We use reference counts to be able to free most items right away when they
6708 * are no longer used. But for composite items it's possible that it becomes
6709 * unused while the reference count is > 0: When there is a recursive
6710 * reference. Example:
6711 * :let l = [1, 2, 3]
6712 * :let d = {9: l}
6713 * :let l[1] = d
6714 *
6715 * Since this is quite unusual we handle this with garbage collection: every
6716 * once in a while find out which lists and dicts are not referenced from any
6717 * variable.
6718 *
6719 * Here is a good reference text about garbage collection (refers to Python
6720 * but it applies to all reference-counting mechanisms):
6721 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006722 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006723
6724/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006725 * Do garbage collection for lists and dicts.
6726 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006727 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006728 int
6729garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006730{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006731 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 buf_T *buf;
6733 win_T *wp;
6734 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006735 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006736 int did_free;
6737 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006738#ifdef FEAT_WINDOWS
6739 tabpage_T *tp;
6740#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006741
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006742 /* Only do this once. */
6743 want_garbage_collect = FALSE;
6744 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006745 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006746
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 /* We advance by two because we add one for items referenced through
6748 * previous_funccal. */
6749 current_copyID += COPYID_INC;
6750 copyID = current_copyID;
6751
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752 /*
6753 * 1. Go through all accessible variables and mark all lists and dicts
6754 * with copyID.
6755 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006756
6757 /* Don't free variables in the previous_funccal list unless they are only
6758 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006759 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006760 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6761 {
6762 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6763 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6764 }
6765
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 /* script-local variables */
6767 for (i = 1; i <= ga_scripts.ga_len; ++i)
6768 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6769
6770 /* buffer-local variables */
6771 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006772 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773
6774 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006775 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006776 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006777#ifdef FEAT_AUTOCMD
6778 if (aucmd_win != NULL)
6779 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782#ifdef FEAT_WINDOWS
6783 /* tabpage-local variables */
6784 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006785 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786#endif
6787
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788 /* global variables */
6789 set_ref_in_ht(&globvarht, copyID);
6790
6791 /* function-local variables */
6792 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6796 }
6797
Bram Moolenaard812df62008-11-09 12:46:09 +00006798 /* v: vars */
6799 set_ref_in_ht(&vimvarht, copyID);
6800
Bram Moolenaar1dced572012-04-05 16:54:08 +02006801#ifdef FEAT_LUA
6802 set_ref_in_lua(copyID);
6803#endif
6804
Bram Moolenaardb913952012-06-29 12:54:53 +02006805#ifdef FEAT_PYTHON
6806 set_ref_in_python(copyID);
6807#endif
6808
6809#ifdef FEAT_PYTHON3
6810 set_ref_in_python3(copyID);
6811#endif
6812
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006813 /*
6814 * 2. Free lists and dictionaries that are not referenced.
6815 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006816 did_free = free_unref_items(copyID);
6817
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006818 /*
6819 * 3. Check if any funccal can be freed now.
6820 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006821 for (pfc = &previous_funccal; *pfc != NULL; )
6822 {
6823 if (can_free_funccal(*pfc, copyID))
6824 {
6825 fc = *pfc;
6826 *pfc = fc->caller;
6827 free_funccal(fc, TRUE);
6828 did_free = TRUE;
6829 did_free_funccal = TRUE;
6830 }
6831 else
6832 pfc = &(*pfc)->caller;
6833 }
6834 if (did_free_funccal)
6835 /* When a funccal was freed some more items might be garbage
6836 * collected, so run again. */
6837 (void)garbage_collect();
6838
6839 return did_free;
6840}
6841
6842/*
6843 * Free lists and dictionaries that are no longer referenced.
6844 */
6845 static int
6846free_unref_items(copyID)
6847 int copyID;
6848{
6849 dict_T *dd;
6850 list_T *ll;
6851 int did_free = FALSE;
6852
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006853 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855 */
6856 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006859 /* Free the Dictionary and ordinary items it contains, but don't
6860 * recurse into Lists and Dictionaries, they will be in the list
6861 * of dicts or list of lists. */
6862 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863 did_free = TRUE;
6864
6865 /* restart, next dict may also have been freed */
6866 dd = first_dict;
6867 }
6868 else
6869 dd = dd->dv_used_next;
6870
6871 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 * Go through the list of lists and free items without the copyID.
6873 * But don't free a list that has a watcher (used in a for loop), these
6874 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 */
6876 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6878 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006880 /* Free the List and ordinary items it contains, but don't recurse
6881 * into Lists and Dictionaries, they will be in the list of dicts
6882 * or list of lists. */
6883 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 did_free = TRUE;
6885
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006886 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 ll = first_list;
6888 }
6889 else
6890 ll = ll->lv_used_next;
6891
6892 return did_free;
6893}
6894
6895/*
6896 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6897 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006898 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899set_ref_in_ht(ht, copyID)
6900 hashtab_T *ht;
6901 int copyID;
6902{
6903 int todo;
6904 hashitem_T *hi;
6905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006906 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 for (hi = ht->ht_array; todo > 0; ++hi)
6908 if (!HASHITEM_EMPTY(hi))
6909 {
6910 --todo;
6911 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6912 }
6913}
6914
6915/*
6916 * Mark all lists and dicts referenced through list "l" with "copyID".
6917 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006918 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919set_ref_in_list(l, copyID)
6920 list_T *l;
6921 int copyID;
6922{
6923 listitem_T *li;
6924
6925 for (li = l->lv_first; li != NULL; li = li->li_next)
6926 set_ref_in_item(&li->li_tv, copyID);
6927}
6928
6929/*
6930 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6931 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006932 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933set_ref_in_item(tv, copyID)
6934 typval_T *tv;
6935 int copyID;
6936{
6937 dict_T *dd;
6938 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939
6940 switch (tv->v_type)
6941 {
6942 case VAR_DICT:
6943 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006944 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006945 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006946 /* Didn't see this dict yet. */
6947 dd->dv_copyID = copyID;
6948 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006949 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951
6952 case VAR_LIST:
6953 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006954 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006955 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 /* Didn't see this list yet. */
6957 ll->lv_copyID = copyID;
6958 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963}
6964
6965/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006966 * Allocate an empty header for a dictionary.
6967 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006968 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969dict_alloc()
6970{
Bram Moolenaar33570922005-01-25 22:26:29 +00006971 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006972
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006974 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006975 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006976 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 if (first_dict != NULL)
6978 first_dict->dv_used_prev = d;
6979 d->dv_used_next = first_dict;
6980 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006984 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006985 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006987 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006988 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006989 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006990}
6991
6992/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006993 * Allocate an empty dict for a return value.
6994 * Returns OK or FAIL.
6995 */
6996 static int
6997rettv_dict_alloc(rettv)
6998 typval_T *rettv;
6999{
7000 dict_T *d = dict_alloc();
7001
7002 if (d == NULL)
7003 return FAIL;
7004
7005 rettv->vval.v_dict = d;
7006 rettv->v_type = VAR_DICT;
7007 ++d->dv_refcount;
7008 return OK;
7009}
7010
7011
7012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007013 * Unreference a Dictionary: decrement the reference count and free it when it
7014 * becomes zero.
7015 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007016 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007017dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007018 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007019{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 if (d != NULL && --d->dv_refcount <= 0)
7021 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
7025 * Free a Dictionary, including all items it contains.
7026 * Ignores the reference count.
7027 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007028 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007029dict_free(d, recurse)
7030 dict_T *d;
7031 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007033 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007034 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007035 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 /* Remove the dict from the list of dicts for garbage collection. */
7038 if (d->dv_used_prev == NULL)
7039 first_dict = d->dv_used_next;
7040 else
7041 d->dv_used_prev->dv_used_next = d->dv_used_next;
7042 if (d->dv_used_next != NULL)
7043 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7044
7045 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007047 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007050 if (!HASHITEM_EMPTY(hi))
7051 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007052 /* Remove the item before deleting it, just in case there is
7053 * something recursive causing trouble. */
7054 di = HI2DI(hi);
7055 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007056 if (recurse || (di->di_tv.v_type != VAR_LIST
7057 && di->di_tv.v_type != VAR_DICT))
7058 clear_tv(&di->di_tv);
7059 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007060 --todo;
7061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064 vim_free(d);
7065}
7066
7067/*
7068 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007069 * The "key" is copied to the new item.
7070 * Note that the value of the item "di_tv" still needs to be initialized!
7071 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007073 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074dictitem_alloc(key)
7075 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076{
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007078
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007083 di->di_flags = 0;
7084 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086}
7087
7088/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007089 * Make a copy of a Dictionary item.
7090 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007093 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094{
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007097 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7098 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007099 if (di != NULL)
7100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007102 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 copy_tv(&org->di_tv, &di->di_tv);
7104 }
7105 return di;
7106}
7107
7108/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007109 * Remove item "item" from Dictionary "dict" and free it.
7110 */
7111 static void
7112dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007113 dict_T *dict;
7114 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115{
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117
Bram Moolenaar33570922005-01-25 22:26:29 +00007118 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007119 if (HASHITEM_EMPTY(hi))
7120 EMSG2(_(e_intern2), "dictitem_remove()");
7121 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 dictitem_free(item);
7124}
7125
7126/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007127 * Free a dict item. Also clears the value.
7128 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007129 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133 clear_tv(&item->di_tv);
7134 vim_free(item);
7135}
7136
7137/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007138 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7139 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007140 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007141 * Returns NULL when out of memory.
7142 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007144dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007146 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007147 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007148{
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 dict_T *copy;
7150 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007152 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153
7154 if (orig == NULL)
7155 return NULL;
7156
7157 copy = dict_alloc();
7158 if (copy != NULL)
7159 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007160 if (copyID != 0)
7161 {
7162 orig->dv_copyID = copyID;
7163 orig->dv_copydict = copy;
7164 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007165 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007166 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007168 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007169 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007170 --todo;
7171
7172 di = dictitem_alloc(hi->hi_key);
7173 if (di == NULL)
7174 break;
7175 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176 {
7177 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7178 copyID) == FAIL)
7179 {
7180 vim_free(di);
7181 break;
7182 }
7183 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 else
7185 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7186 if (dict_add(copy, di) == FAIL)
7187 {
7188 dictitem_free(di);
7189 break;
7190 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007191 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007192 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007193
Bram Moolenaare9a41262005-01-15 22:18:47 +00007194 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195 if (todo > 0)
7196 {
7197 dict_unref(copy);
7198 copy = NULL;
7199 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007200 }
7201
7202 return copy;
7203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007207 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007209 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
7212 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213{
Bram Moolenaar33570922005-01-25 22:26:29 +00007214 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215}
7216
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007218 * Add a number or string entry to dictionary "d".
7219 * When "str" is NULL use number "nr", otherwise use "str".
7220 * Returns FAIL when out of memory and when key already exists.
7221 */
7222 int
7223dict_add_nr_str(d, key, nr, str)
7224 dict_T *d;
7225 char *key;
7226 long nr;
7227 char_u *str;
7228{
7229 dictitem_T *item;
7230
7231 item = dictitem_alloc((char_u *)key);
7232 if (item == NULL)
7233 return FAIL;
7234 item->di_tv.v_lock = 0;
7235 if (str == NULL)
7236 {
7237 item->di_tv.v_type = VAR_NUMBER;
7238 item->di_tv.vval.v_number = nr;
7239 }
7240 else
7241 {
7242 item->di_tv.v_type = VAR_STRING;
7243 item->di_tv.vval.v_string = vim_strsave(str);
7244 }
7245 if (dict_add(d, item) == FAIL)
7246 {
7247 dictitem_free(item);
7248 return FAIL;
7249 }
7250 return OK;
7251}
7252
7253/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007254 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007255 * Returns FAIL when out of memory and when key already exists.
7256 */
7257 int
7258dict_add_list(d, key, list)
7259 dict_T *d;
7260 char *key;
7261 list_T *list;
7262{
7263 dictitem_T *item;
7264
7265 item = dictitem_alloc((char_u *)key);
7266 if (item == NULL)
7267 return FAIL;
7268 item->di_tv.v_lock = 0;
7269 item->di_tv.v_type = VAR_LIST;
7270 item->di_tv.vval.v_list = list;
7271 if (dict_add(d, item) == FAIL)
7272 {
7273 dictitem_free(item);
7274 return FAIL;
7275 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007276 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007277 return OK;
7278}
7279
7280/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 * Get the number of items in a Dictionary.
7282 */
7283 static long
7284dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007286{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007287 if (d == NULL)
7288 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007289 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290}
7291
7292/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293 * Find item "key[len]" in Dictionary "d".
7294 * If "len" is negative use strlen(key).
7295 * Returns NULL when not found.
7296 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007297 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 char_u *key;
7301 int len;
7302{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303#define AKEYLEN 200
7304 char_u buf[AKEYLEN];
7305 char_u *akey;
7306 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309 if (len < 0)
7310 akey = key;
7311 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 tofree = akey = vim_strnsave(key, len);
7314 if (akey == NULL)
7315 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007316 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 else
7318 {
7319 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007320 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 akey = buf;
7322 }
7323
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 vim_free(tofree);
7326 if (HASHITEM_EMPTY(hi))
7327 return NULL;
7328 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329}
7330
7331/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007332 * Get a string item from a dictionary.
7333 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007334 * Returns NULL if the entry doesn't exist or out of memory.
7335 */
7336 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007337get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007338 dict_T *d;
7339 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007340 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007341{
7342 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007344
7345 di = dict_find(d, key, -1);
7346 if (di == NULL)
7347 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348 s = get_tv_string(&di->di_tv);
7349 if (save && s != NULL)
7350 s = vim_strsave(s);
7351 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352}
7353
7354/*
7355 * Get a number item from a dictionary.
7356 * Returns 0 if the entry doesn't exist or out of memory.
7357 */
7358 long
7359get_dict_number(d, key)
7360 dict_T *d;
7361 char_u *key;
7362{
7363 dictitem_T *di;
7364
7365 di = dict_find(d, key, -1);
7366 if (di == NULL)
7367 return 0;
7368 return get_tv_number(&di->di_tv);
7369}
7370
7371/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007372 * Return an allocated string with the string representation of a Dictionary.
7373 * May return NULL.
7374 */
7375 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007376dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007378 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379{
7380 garray_T ga;
7381 int first = TRUE;
7382 char_u *tofree;
7383 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007385 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007387 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007388
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390 return NULL;
7391 ga_init2(&ga, (int)sizeof(char), 80);
7392 ga_append(&ga, '{');
7393
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007394 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007395 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 --todo;
7400
7401 if (first)
7402 first = FALSE;
7403 else
7404 ga_concat(&ga, (char_u *)", ");
7405
7406 tofree = string_quote(hi->hi_key, FALSE);
7407 if (tofree != NULL)
7408 {
7409 ga_concat(&ga, tofree);
7410 vim_free(tofree);
7411 }
7412 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007414 if (s != NULL)
7415 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (s == NULL)
7418 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 if (todo > 0)
7422 {
7423 vim_free(ga.ga_data);
7424 return NULL;
7425 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426
7427 ga_append(&ga, '}');
7428 ga_append(&ga, NUL);
7429 return (char_u *)ga.ga_data;
7430}
7431
7432/*
7433 * Allocate a variable for a Dictionary and fill it from "*arg".
7434 * Return OK or FAIL. Returns NOTDONE for {expr}.
7435 */
7436 static int
7437get_dict_tv(arg, rettv, evaluate)
7438 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440 int evaluate;
7441{
Bram Moolenaar33570922005-01-25 22:26:29 +00007442 dict_T *d = NULL;
7443 typval_T tvkey;
7444 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007445 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007447 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449
7450 /*
7451 * First check if it's not a curly-braces thing: {expr}.
7452 * Must do this without evaluating, otherwise a function may be called
7453 * twice. Unfortunately this means we need to call eval1() twice for the
7454 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 if (*start != '}')
7458 {
7459 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7460 return FAIL;
7461 if (*start == '}')
7462 return NOTDONE;
7463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464
7465 if (evaluate)
7466 {
7467 d = dict_alloc();
7468 if (d == NULL)
7469 return FAIL;
7470 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007471 tvkey.v_type = VAR_UNKNOWN;
7472 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473
7474 *arg = skipwhite(*arg + 1);
7475 while (**arg != '}' && **arg != NUL)
7476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 goto failret;
7479 if (**arg != ':')
7480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007481 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 goto failret;
7484 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007485 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007487 key = get_tv_string_buf_chk(&tvkey, buf);
7488 if (key == NULL || *key == NUL)
7489 {
7490 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7491 if (key != NULL)
7492 EMSG(_(e_emptykey));
7493 clear_tv(&tvkey);
7494 goto failret;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
7498 *arg = skipwhite(*arg + 1);
7499 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7500 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 if (evaluate)
7502 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 goto failret;
7504 }
7505 if (evaluate)
7506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 if (item != NULL)
7509 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007510 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 clear_tv(&tv);
7513 goto failret;
7514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 item = dictitem_alloc(key);
7516 clear_tv(&tvkey);
7517 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007520 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 if (dict_add(d, item) == FAIL)
7522 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 }
7524 }
7525
7526 if (**arg == '}')
7527 break;
7528 if (**arg != ',')
7529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007530 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 goto failret;
7532 }
7533 *arg = skipwhite(*arg + 1);
7534 }
7535
7536 if (**arg != '}')
7537 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007538 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539failret:
7540 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007541 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 return FAIL;
7543 }
7544
7545 *arg = skipwhite(*arg + 1);
7546 if (evaluate)
7547 {
7548 rettv->v_type = VAR_DICT;
7549 rettv->vval.v_dict = d;
7550 ++d->dv_refcount;
7551 }
7552
7553 return OK;
7554}
7555
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007557 * Return a string with the string representation of a variable.
7558 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007559 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007560 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007561 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007562 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007563 */
7564 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007565echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007567 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007568 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007569 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007570{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 static int recurse = 0;
7572 char_u *r = NULL;
7573
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007576 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007577 *tofree = NULL;
7578 return NULL;
7579 }
7580 ++recurse;
7581
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582 switch (tv->v_type)
7583 {
7584 case VAR_FUNC:
7585 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 r = tv->vval.v_string;
7587 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007588
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007590 if (tv->vval.v_list == NULL)
7591 {
7592 *tofree = NULL;
7593 r = NULL;
7594 }
7595 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7596 {
7597 *tofree = NULL;
7598 r = (char_u *)"[...]";
7599 }
7600 else
7601 {
7602 tv->vval.v_list->lv_copyID = copyID;
7603 *tofree = list2string(tv, copyID);
7604 r = *tofree;
7605 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007606 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007607
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007609 if (tv->vval.v_dict == NULL)
7610 {
7611 *tofree = NULL;
7612 r = NULL;
7613 }
7614 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7615 {
7616 *tofree = NULL;
7617 r = (char_u *)"{...}";
7618 }
7619 else
7620 {
7621 tv->vval.v_dict->dv_copyID = copyID;
7622 *tofree = dict2string(tv, copyID);
7623 r = *tofree;
7624 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007625 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 case VAR_STRING:
7628 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007629 *tofree = NULL;
7630 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007631 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007633#ifdef FEAT_FLOAT
7634 case VAR_FLOAT:
7635 *tofree = NULL;
7636 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7637 r = numbuf;
7638 break;
7639#endif
7640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007642 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645
7646 --recurse;
7647 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007648}
7649
7650/*
7651 * Return a string with the string representation of a variable.
7652 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7653 * "numbuf" is used for a number.
7654 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007655 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 */
7657 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007659 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660 char_u **tofree;
7661 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007662 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007663{
7664 switch (tv->v_type)
7665 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007666 case VAR_FUNC:
7667 *tofree = string_quote(tv->vval.v_string, TRUE);
7668 return *tofree;
7669 case VAR_STRING:
7670 *tofree = string_quote(tv->vval.v_string, FALSE);
7671 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007672#ifdef FEAT_FLOAT
7673 case VAR_FLOAT:
7674 *tofree = NULL;
7675 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7676 return numbuf;
7677#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007678 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007679 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007682 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007684 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007685 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007686}
7687
7688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007689 * Return string "str" in ' quotes, doubling ' characters.
7690 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 */
7693 static char_u *
7694string_quote(str, function)
7695 char_u *str;
7696 int function;
7697{
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007699 char_u *p, *r, *s;
7700
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 len = (function ? 13 : 3);
7702 if (str != NULL)
7703 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007704 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 for (p = str; *p != NUL; mb_ptr_adv(p))
7706 if (*p == '\'')
7707 ++len;
7708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007709 s = r = alloc(len);
7710 if (r != NULL)
7711 {
7712 if (function)
7713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007715 r += 10;
7716 }
7717 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 if (str != NULL)
7720 for (p = str; *p != NUL; )
7721 {
7722 if (*p == '\'')
7723 *r++ = '\'';
7724 MB_COPY_CHAR(p, r);
7725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 if (function)
7728 *r++ = ')';
7729 *r++ = NUL;
7730 }
7731 return s;
7732}
7733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007734#ifdef FEAT_FLOAT
7735/*
7736 * Convert the string "text" to a floating point number.
7737 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7738 * this always uses a decimal point.
7739 * Returns the length of the text that was consumed.
7740 */
7741 static int
7742string2float(text, value)
7743 char_u *text;
7744 float_T *value; /* result stored here */
7745{
7746 char *s = (char *)text;
7747 float_T f;
7748
7749 f = strtod(s, &s);
7750 *value = f;
7751 return (int)((char_u *)s - text);
7752}
7753#endif
7754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * Get the value of an environment variable.
7757 * "arg" is pointing to the '$'. It is advanced to after the name.
7758 * If the environment variable was not set, silently assume it is empty.
7759 * Always return OK.
7760 */
7761 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007762get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 int evaluate;
7766{
7767 char_u *string = NULL;
7768 int len;
7769 int cc;
7770 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007771 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772
7773 ++*arg;
7774 name = *arg;
7775 len = get_env_len(arg);
7776 if (evaluate)
7777 {
7778 if (len != 0)
7779 {
7780 cc = name[len];
7781 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 /* first try vim_getenv(), fast for normal environment vars */
7783 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 {
7786 if (!mustfree)
7787 string = vim_strsave(string);
7788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 else
7790 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007791 if (mustfree)
7792 vim_free(string);
7793
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 /* next try expanding things like $VIM and ${HOME} */
7795 string = expand_env_save(name - 1);
7796 if (string != NULL && *string == '$')
7797 {
7798 vim_free(string);
7799 string = NULL;
7800 }
7801 }
7802 name[len] = cc;
7803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007804 rettv->v_type = VAR_STRING;
7805 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
7807
7808 return OK;
7809}
7810
7811/*
7812 * Array with names and number of arguments of all internal functions
7813 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7814 */
7815static struct fst
7816{
7817 char *f_name; /* function name */
7818 char f_min_argc; /* minimal number of arguments */
7819 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007820 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007821 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822} functions[] =
7823{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007824#ifdef FEAT_FLOAT
7825 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007826 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007828 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007829 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"append", 2, 2, f_append},
7831 {"argc", 0, 0, f_argc},
7832 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007833 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007834#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007835 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007840 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"bufexists", 1, 1, f_bufexists},
7842 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7843 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7844 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7845 {"buflisted", 1, 1, f_buflisted},
7846 {"bufloaded", 1, 1, f_bufloaded},
7847 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007848 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {"bufwinnr", 1, 1, f_bufwinnr},
7850 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007851 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007852 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007853#ifdef FEAT_FLOAT
7854 {"ceil", 1, 1, f_ceil},
7855#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007856 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007857 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007861#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007862 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007863 {"complete_add", 1, 1, f_complete_add},
7864 {"complete_check", 0, 0, f_complete_check},
7865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#ifdef FEAT_FLOAT
7869 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007870 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007872 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007874 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007875 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {"delete", 1, 1, f_delete},
7877 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007878 {"diff_filler", 1, 1, f_diff_filler},
7879 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007880 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007882 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {"eventhandler", 0, 0, f_eventhandler},
7884 {"executable", 1, 1, f_executable},
7885 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007886#ifdef FEAT_FLOAT
7887 {"exp", 1, 1, f_exp},
7888#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007889 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007891 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7893 {"filereadable", 1, 1, f_filereadable},
7894 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007895 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007896 {"finddir", 1, 3, f_finddir},
7897 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007898#ifdef FEAT_FLOAT
7899 {"float2nr", 1, 1, f_float2nr},
7900 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007901 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007902#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007903 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"fnamemodify", 2, 2, f_fnamemodify},
7905 {"foldclosed", 1, 1, f_foldclosed},
7906 {"foldclosedend", 1, 1, f_foldclosedend},
7907 {"foldlevel", 1, 1, f_foldlevel},
7908 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007909 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007911 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007912 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007913 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007914 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007915 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"getchar", 0, 1, f_getchar},
7917 {"getcharmod", 0, 0, f_getcharmod},
7918 {"getcmdline", 0, 0, f_getcmdline},
7919 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007920 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007922 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007923 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"getfsize", 1, 1, f_getfsize},
7925 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007926 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007928 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007929 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007930 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007931 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007932 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007933 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007935 {"gettabvar", 2, 3, f_gettabvar},
7936 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"getwinposx", 0, 0, f_getwinposx},
7938 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007939 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007940 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007941 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007944 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007945 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7947 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7948 {"histadd", 2, 2, f_histadd},
7949 {"histdel", 1, 2, f_histdel},
7950 {"histget", 1, 2, f_histget},
7951 {"histnr", 1, 1, f_histnr},
7952 {"hlID", 1, 1, f_hlID},
7953 {"hlexists", 1, 1, f_hlexists},
7954 {"hostname", 0, 0, f_hostname},
7955 {"iconv", 3, 3, f_iconv},
7956 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007958 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007960 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"inputrestore", 0, 0, f_inputrestore},
7962 {"inputsave", 0, 0, f_inputsave},
7963 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007964 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007965 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007967 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"libcall", 3, 3, f_libcall},
7974 {"libcallnr", 3, 3, f_libcallnr},
7975 {"line", 1, 1, f_line},
7976 {"line2byte", 1, 1, f_line2byte},
7977 {"lispindent", 1, 1, f_lispindent},
7978 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007979#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007980 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007981 {"log10", 1, 1, f_log10},
7982#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007983#ifdef FEAT_LUA
7984 {"luaeval", 1, 2, f_luaeval},
7985#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007987 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007988 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007989 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007990 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007991 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007992 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007993 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007994 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007995 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007996 {"max", 1, 1, f_max},
7997 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007998#ifdef vim_mkdir
7999 {"mkdir", 1, 3, f_mkdir},
8000#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008001 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008002#ifdef FEAT_MZSCHEME
8003 {"mzeval", 1, 1, f_mzeval},
8004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008006 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008007 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008008 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008009#ifdef FEAT_FLOAT
8010 {"pow", 2, 2, f_pow},
8011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008013 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008014 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008015#ifdef FEAT_PYTHON3
8016 {"py3eval", 1, 1, f_py3eval},
8017#endif
8018#ifdef FEAT_PYTHON
8019 {"pyeval", 1, 1, f_pyeval},
8020#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008022 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008023 {"reltime", 0, 2, f_reltime},
8024 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {"remote_expr", 2, 3, f_remote_expr},
8026 {"remote_foreground", 1, 1, f_remote_foreground},
8027 {"remote_peek", 1, 2, f_remote_peek},
8028 {"remote_read", 1, 1, f_remote_read},
8029 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008030 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008032 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008034 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008035#ifdef FEAT_FLOAT
8036 {"round", 1, 1, f_round},
8037#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008038 {"screencol", 0, 0, f_screencol},
8039 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008040 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008041 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008042 {"searchpair", 3, 7, f_searchpair},
8043 {"searchpairpos", 3, 7, f_searchpairpos},
8044 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"server2client", 2, 2, f_server2client},
8046 {"serverlist", 0, 0, f_serverlist},
8047 {"setbufvar", 3, 3, f_setbufvar},
8048 {"setcmdpos", 1, 1, f_setcmdpos},
8049 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008050 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008051 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008052 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008053 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008055 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008056 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008058#ifdef FEAT_CRYPT
8059 {"sha256", 1, 1, f_sha256},
8060#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008061 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008062 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064#ifdef FEAT_FLOAT
8065 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008066 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008067#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008068 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008069 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008070 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008071 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008072 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073#ifdef FEAT_FLOAT
8074 {"sqrt", 1, 1, f_sqrt},
8075 {"str2float", 1, 1, f_str2float},
8076#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008077 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008078 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008079 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080#ifdef HAVE_STRFTIME
8081 {"strftime", 1, 2, f_strftime},
8082#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {"strlen", 1, 1, f_strlen},
8086 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008087 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008089 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {"submatch", 1, 1, f_submatch},
8091 {"substitute", 4, 4, f_substitute},
8092 {"synID", 3, 3, f_synID},
8093 {"synIDattr", 2, 3, f_synIDattr},
8094 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008095 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008096 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008097 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008098 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008099 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008100 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008101 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008102 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008103#ifdef FEAT_FLOAT
8104 {"tan", 1, 1, f_tan},
8105 {"tanh", 1, 1, f_tanh},
8106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008108 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"tolower", 1, 1, f_tolower},
8110 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008111 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008112#ifdef FEAT_FLOAT
8113 {"trunc", 1, 1, f_trunc},
8114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008116 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008117 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008118 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"virtcol", 1, 1, f_virtcol},
8120 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008121 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"winbufnr", 1, 1, f_winbufnr},
8123 {"wincol", 0, 0, f_wincol},
8124 {"winheight", 1, 1, f_winheight},
8125 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008126 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008128 {"winrestview", 1, 1, f_winrestview},
8129 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008131 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008132 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133};
8134
8135#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8136
8137/*
8138 * Function given to ExpandGeneric() to obtain the list of internal
8139 * or user defined function names.
8140 */
8141 char_u *
8142get_function_name(xp, idx)
8143 expand_T *xp;
8144 int idx;
8145{
8146 static int intidx = -1;
8147 char_u *name;
8148
8149 if (idx == 0)
8150 intidx = -1;
8151 if (intidx < 0)
8152 {
8153 name = get_user_func_name(xp, idx);
8154 if (name != NULL)
8155 return name;
8156 }
8157 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8158 {
8159 STRCPY(IObuff, functions[intidx].f_name);
8160 STRCAT(IObuff, "(");
8161 if (functions[intidx].f_max_argc == 0)
8162 STRCAT(IObuff, ")");
8163 return IObuff;
8164 }
8165
8166 return NULL;
8167}
8168
8169/*
8170 * Function given to ExpandGeneric() to obtain the list of internal or
8171 * user defined variable or function names.
8172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 char_u *
8174get_expr_name(xp, idx)
8175 expand_T *xp;
8176 int idx;
8177{
8178 static int intidx = -1;
8179 char_u *name;
8180
8181 if (idx == 0)
8182 intidx = -1;
8183 if (intidx < 0)
8184 {
8185 name = get_function_name(xp, idx);
8186 if (name != NULL)
8187 return name;
8188 }
8189 return get_user_var_name(xp, ++intidx);
8190}
8191
8192#endif /* FEAT_CMDL_COMPL */
8193
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008194#if defined(EBCDIC) || defined(PROTO)
8195/*
8196 * Compare struct fst by function name.
8197 */
8198 static int
8199compare_func_name(s1, s2)
8200 const void *s1;
8201 const void *s2;
8202{
8203 struct fst *p1 = (struct fst *)s1;
8204 struct fst *p2 = (struct fst *)s2;
8205
8206 return STRCMP(p1->f_name, p2->f_name);
8207}
8208
8209/*
8210 * Sort the function table by function name.
8211 * The sorting of the table above is ASCII dependant.
8212 * On machines using EBCDIC we have to sort it.
8213 */
8214 static void
8215sortFunctions()
8216{
8217 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8218
8219 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8220}
8221#endif
8222
8223
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224/*
8225 * Find internal function in table above.
8226 * Return index, or -1 if not found
8227 */
8228 static int
8229find_internal_func(name)
8230 char_u *name; /* name of the function */
8231{
8232 int first = 0;
8233 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8234 int cmp;
8235 int x;
8236
8237 /*
8238 * Find the function name in the table. Binary search.
8239 */
8240 while (first <= last)
8241 {
8242 x = first + ((unsigned)(last - first) >> 1);
8243 cmp = STRCMP(name, functions[x].f_name);
8244 if (cmp < 0)
8245 last = x - 1;
8246 else if (cmp > 0)
8247 first = x + 1;
8248 else
8249 return x;
8250 }
8251 return -1;
8252}
8253
8254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008255 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8256 * name it contains, otherwise return "name".
8257 */
8258 static char_u *
8259deref_func_name(name, lenp)
8260 char_u *name;
8261 int *lenp;
8262{
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008264 int cc;
8265
8266 cc = name[*lenp];
8267 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008268 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008271 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008272 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008273 {
8274 *lenp = 0;
8275 return (char_u *)""; /* just in case */
8276 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008277 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008278 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008279 }
8280
8281 return name;
8282}
8283
8284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 * Allocate a variable for the result of a function.
8286 * Return OK or FAIL.
8287 */
8288 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008289get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8290 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 char_u *name; /* name of the function */
8292 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 char_u **arg; /* argument, pointing to the '(' */
8295 linenr_T firstline; /* first line of range */
8296 linenr_T lastline; /* last line of range */
8297 int *doesrange; /* return: function handled range */
8298 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 char_u *argp;
8302 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008303 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 int argcount = 0; /* number of arguments found */
8305
8306 /*
8307 * Get the arguments.
8308 */
8309 argp = *arg;
8310 while (argcount < MAX_FUNC_ARGS)
8311 {
8312 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8313 if (*argp == ')' || *argp == ',' || *argp == NUL)
8314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8316 {
8317 ret = FAIL;
8318 break;
8319 }
8320 ++argcount;
8321 if (*argp != ',')
8322 break;
8323 }
8324 if (*argp == ')')
8325 ++argp;
8326 else
8327 ret = FAIL;
8328
8329 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008330 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008331 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008333 {
8334 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008335 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008337 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339
8340 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
8343 *arg = skipwhite(argp);
8344 return ret;
8345}
8346
8347
8348/*
8349 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008350 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008351 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 */
8353 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008354call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008355 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008356 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008360 typval_T *argvars; /* vars for arguments, must have "argcount"
8361 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 linenr_T firstline; /* first line of range */
8363 linenr_T lastline; /* last line of range */
8364 int *doesrange; /* return: function handled range */
8365 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008366 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369#define ERROR_UNKNOWN 0
8370#define ERROR_TOOMANY 1
8371#define ERROR_TOOFEW 2
8372#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008373#define ERROR_DICT 4
8374#define ERROR_NONE 5
8375#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 int error = ERROR_NONE;
8377 int i;
8378 int llen;
8379 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380#define FLEN_FIXED 40
8381 char_u fname_buf[FLEN_FIXED + 1];
8382 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008383 char_u *name;
8384
8385 /* Make a copy of the name, if it comes from a funcref variable it could
8386 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008387 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008388 if (name == NULL)
8389 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390
8391 /*
8392 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8393 * Change <SNR>123_name() to K_SNR 123_name().
8394 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 llen = eval_fname_script(name);
8397 if (llen > 0)
8398 {
8399 fname_buf[0] = K_SPECIAL;
8400 fname_buf[1] = KS_EXTRA;
8401 fname_buf[2] = (int)KE_SNR;
8402 i = 3;
8403 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8404 {
8405 if (current_SID <= 0)
8406 error = ERROR_SCRIPT;
8407 else
8408 {
8409 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8410 i = (int)STRLEN(fname_buf);
8411 }
8412 }
8413 if (i + STRLEN(name + llen) < FLEN_FIXED)
8414 {
8415 STRCPY(fname_buf + i, name + llen);
8416 fname = fname_buf;
8417 }
8418 else
8419 {
8420 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8421 if (fname == NULL)
8422 error = ERROR_OTHER;
8423 else
8424 {
8425 mch_memmove(fname, fname_buf, (size_t)i);
8426 STRCPY(fname + i, name + llen);
8427 }
8428 }
8429 }
8430 else
8431 fname = name;
8432
8433 *doesrange = FALSE;
8434
8435
8436 /* execute the function if no errors detected and executing */
8437 if (evaluate && error == ERROR_NONE)
8438 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008439 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8440 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 error = ERROR_UNKNOWN;
8442
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008443 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
8445 /*
8446 * User defined function.
8447 */
8448 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008449
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008451 /* Trigger FuncUndefined event, may load the function. */
8452 if (fp == NULL
8453 && apply_autocmds(EVENT_FUNCUNDEFINED,
8454 fname, fname, TRUE, NULL)
8455 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 fp = find_func(fname);
8459 }
8460#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008461 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008462 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463 {
8464 /* loaded a package, search for the function again */
8465 fp = find_func(fname);
8466 }
8467
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 if (fp != NULL)
8469 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008470 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008472 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008474 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008476 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008477 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 else
8479 {
8480 /*
8481 * Call the user function.
8482 * Save and restore search patterns, script variables and
8483 * redo buffer.
8484 */
8485 save_search_patterns();
8486 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8491 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8492 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008493 /* Function was unreferenced while being used, free it
8494 * now. */
8495 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 restoreRedobuff();
8497 restore_search_patterns();
8498 error = ERROR_NONE;
8499 }
8500 }
8501 }
8502 else
8503 {
8504 /*
8505 * Find the function name in the table, call its implementation.
8506 */
8507 i = find_internal_func(fname);
8508 if (i >= 0)
8509 {
8510 if (argcount < functions[i].f_min_argc)
8511 error = ERROR_TOOFEW;
8512 else if (argcount > functions[i].f_max_argc)
8513 error = ERROR_TOOMANY;
8514 else
8515 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008517 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 error = ERROR_NONE;
8519 }
8520 }
8521 }
8522 /*
8523 * The function call (or "FuncUndefined" autocommand sequence) might
8524 * have been aborted by an error, an interrupt, or an explicitly thrown
8525 * exception that has not been caught so far. This situation can be
8526 * tested for by calling aborting(). For an error in an internal
8527 * function or for the "E132" error in call_user_func(), however, the
8528 * throw point at which the "force_abort" flag (temporarily reset by
8529 * emsg()) is normally updated has not been reached yet. We need to
8530 * update that flag first to make aborting() reliable.
8531 */
8532 update_force_abort();
8533 }
8534 if (error == ERROR_NONE)
8535 ret = OK;
8536
8537 /*
8538 * Report an error unless the argument evaluation or function call has been
8539 * cancelled due to an aborting error, an interrupt, or an exception.
8540 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008541 if (!aborting())
8542 {
8543 switch (error)
8544 {
8545 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008546 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008547 break;
8548 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008549 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008550 break;
8551 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008552 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008553 name);
8554 break;
8555 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008556 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008557 name);
8558 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 name);
8562 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 }
8564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (fname != name && fname != fname_buf)
8567 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008568 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 return ret;
8571}
8572
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008573/*
8574 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008575 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008576 */
8577 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008578emsg_funcname(ermsg, name)
8579 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008580 char_u *name;
8581{
8582 char_u *p;
8583
8584 if (*name == K_SPECIAL)
8585 p = concat_str((char_u *)"<SNR>", name + 3);
8586 else
8587 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008588 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 if (p != name)
8590 vim_free(p);
8591}
8592
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008593/*
8594 * Return TRUE for a non-zero Number and a non-empty String.
8595 */
8596 static int
8597non_zero_arg(argvars)
8598 typval_T *argvars;
8599{
8600 return ((argvars[0].v_type == VAR_NUMBER
8601 && argvars[0].vval.v_number != 0)
8602 || (argvars[0].v_type == VAR_STRING
8603 && argvars[0].vval.v_string != NULL
8604 && *argvars[0].vval.v_string != NUL));
8605}
8606
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607/*********************************************
8608 * Implementation of the built-in functions
8609 */
8610
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008611#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008612static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8613
8614/*
8615 * Get the float value of "argvars[0]" into "f".
8616 * Returns FAIL when the argument is not a Number or Float.
8617 */
8618 static int
8619get_float_arg(argvars, f)
8620 typval_T *argvars;
8621 float_T *f;
8622{
8623 if (argvars[0].v_type == VAR_FLOAT)
8624 {
8625 *f = argvars[0].vval.v_float;
8626 return OK;
8627 }
8628 if (argvars[0].v_type == VAR_NUMBER)
8629 {
8630 *f = (float_T)argvars[0].vval.v_number;
8631 return OK;
8632 }
8633 EMSG(_("E808: Number or Float required"));
8634 return FAIL;
8635}
8636
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637/*
8638 * "abs(expr)" function
8639 */
8640 static void
8641f_abs(argvars, rettv)
8642 typval_T *argvars;
8643 typval_T *rettv;
8644{
8645 if (argvars[0].v_type == VAR_FLOAT)
8646 {
8647 rettv->v_type = VAR_FLOAT;
8648 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8649 }
8650 else
8651 {
8652 varnumber_T n;
8653 int error = FALSE;
8654
8655 n = get_tv_number_chk(&argvars[0], &error);
8656 if (error)
8657 rettv->vval.v_number = -1;
8658 else if (n > 0)
8659 rettv->vval.v_number = n;
8660 else
8661 rettv->vval.v_number = -n;
8662 }
8663}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008664
8665/*
8666 * "acos()" function
8667 */
8668 static void
8669f_acos(argvars, rettv)
8670 typval_T *argvars;
8671 typval_T *rettv;
8672{
8673 float_T f;
8674
8675 rettv->v_type = VAR_FLOAT;
8676 if (get_float_arg(argvars, &f) == OK)
8677 rettv->vval.v_float = acos(f);
8678 else
8679 rettv->vval.v_float = 0.0;
8680}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008681#endif
8682
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008684 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 */
8686 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008687f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008688 typval_T *argvars;
8689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
Bram Moolenaar33570922005-01-25 22:26:29 +00008691 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008693 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008694 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008696 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008697 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008698 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008699 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008700 }
8701 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008702 EMSG(_(e_listreq));
8703}
8704
8705/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008706 * "and(expr, expr)" function
8707 */
8708 static void
8709f_and(argvars, rettv)
8710 typval_T *argvars;
8711 typval_T *rettv;
8712{
8713 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8714 & get_tv_number_chk(&argvars[1], NULL);
8715}
8716
8717/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008718 * "append(lnum, string/list)" function
8719 */
8720 static void
8721f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008722 typval_T *argvars;
8723 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008724{
8725 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008726 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 list_T *l = NULL;
8728 listitem_T *li = NULL;
8729 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008730 long added = 0;
8731
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 lnum = get_tv_lnum(argvars);
8733 if (lnum >= 0
8734 && lnum <= curbuf->b_ml.ml_line_count
8735 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008736 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008739 l = argvars[1].vval.v_list;
8740 if (l == NULL)
8741 return;
8742 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008743 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 for (;;)
8745 {
8746 if (l == NULL)
8747 tv = &argvars[1]; /* append a string */
8748 else if (li == NULL)
8749 break; /* end of list */
8750 else
8751 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008752 line = get_tv_string_chk(tv);
8753 if (line == NULL) /* type error */
8754 {
8755 rettv->vval.v_number = 1; /* Failed */
8756 break;
8757 }
8758 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008759 ++added;
8760 if (l == NULL)
8761 break;
8762 li = li->li_next;
8763 }
8764
8765 appended_lines_mark(lnum, added);
8766 if (curwin->w_cursor.lnum > lnum)
8767 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769 else
8770 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771}
8772
8773/*
8774 * "argc()" function
8775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008778 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008781 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782}
8783
8784/*
8785 * "argidx()" function
8786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008788f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008789 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008792 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795/*
8796 * "argv(nr)" function
8797 */
8798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T *argvars;
8801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
8803 int idx;
8804
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008805 if (argvars[0].v_type != VAR_UNKNOWN)
8806 {
8807 idx = get_tv_number_chk(&argvars[0], NULL);
8808 if (idx >= 0 && idx < ARGCOUNT)
8809 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8810 else
8811 rettv->vval.v_string = NULL;
8812 rettv->v_type = VAR_STRING;
8813 }
8814 else if (rettv_list_alloc(rettv) == OK)
8815 for (idx = 0; idx < ARGCOUNT; ++idx)
8816 list_append_string(rettv->vval.v_list,
8817 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008820#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008821/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008822 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008824 static void
8825f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008826 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008827 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008828{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008829 float_T f;
8830
8831 rettv->v_type = VAR_FLOAT;
8832 if (get_float_arg(argvars, &f) == OK)
8833 rettv->vval.v_float = asin(f);
8834 else
8835 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008836}
8837
8838/*
8839 * "atan()" function
8840 */
8841 static void
8842f_atan(argvars, rettv)
8843 typval_T *argvars;
8844 typval_T *rettv;
8845{
8846 float_T f;
8847
8848 rettv->v_type = VAR_FLOAT;
8849 if (get_float_arg(argvars, &f) == OK)
8850 rettv->vval.v_float = atan(f);
8851 else
8852 rettv->vval.v_float = 0.0;
8853}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008854
8855/*
8856 * "atan2()" function
8857 */
8858 static void
8859f_atan2(argvars, rettv)
8860 typval_T *argvars;
8861 typval_T *rettv;
8862{
8863 float_T fx, fy;
8864
8865 rettv->v_type = VAR_FLOAT;
8866 if (get_float_arg(argvars, &fx) == OK
8867 && get_float_arg(&argvars[1], &fy) == OK)
8868 rettv->vval.v_float = atan2(fx, fy);
8869 else
8870 rettv->vval.v_float = 0.0;
8871}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008872#endif
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874/*
8875 * "browse(save, title, initdir, default)" function
8876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008879 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882#ifdef FEAT_BROWSE
8883 int save;
8884 char_u *title;
8885 char_u *initdir;
8886 char_u *defname;
8887 char_u buf[NUMBUFLEN];
8888 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008889 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008891 save = get_tv_number_chk(&argvars[0], &error);
8892 title = get_tv_string_chk(&argvars[1]);
8893 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8894 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008896 if (error || title == NULL || initdir == NULL || defname == NULL)
8897 rettv->vval.v_string = NULL;
8898 else
8899 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008900 do_browse(save ? BROWSE_SAVE : 0,
8901 title, defname, NULL, initdir, NULL, curbuf);
8902#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008904#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008906}
8907
8908/*
8909 * "browsedir(title, initdir)" function
8910 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008913 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008915{
8916#ifdef FEAT_BROWSE
8917 char_u *title;
8918 char_u *initdir;
8919 char_u buf[NUMBUFLEN];
8920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008921 title = get_tv_string_chk(&argvars[0]);
8922 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008924 if (title == NULL || initdir == NULL)
8925 rettv->vval.v_string = NULL;
8926 else
8927 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008928 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933}
8934
Bram Moolenaar33570922005-01-25 22:26:29 +00008935static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937/*
8938 * Find a buffer by number or exact name.
8939 */
8940 static buf_T *
8941find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008942 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008946 if (avar->v_type == VAR_NUMBER)
8947 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008948 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008950 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008951 if (buf == NULL)
8952 {
8953 /* No full path name match, try a match with a URL or a "nofile"
8954 * buffer, these don't use the full path. */
8955 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8956 if (buf->b_fname != NULL
8957 && (path_with_url(buf->b_fname)
8958#ifdef FEAT_QUICKFIX
8959 || bt_nofile(buf)
8960#endif
8961 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008962 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008963 break;
8964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 }
8966 return buf;
8967}
8968
8969/*
8970 * "bufexists(expr)" function
8971 */
8972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 typval_T *argvars;
8975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
8979
8980/*
8981 * "buflisted(expr)" function
8982 */
8983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 typval_T *argvars;
8986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988 buf_T *buf;
8989
8990 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992}
8993
8994/*
8995 * "bufloaded(expr)" function
8996 */
8997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 buf_T *buf;
9003
9004 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006}
9007
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009008static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009009
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010/*
9011 * Get buffer by number or pattern.
9012 */
9013 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009014get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009016 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 int save_magic;
9020 char_u *save_cpo;
9021 buf_T *buf;
9022
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023 if (tv->v_type == VAR_NUMBER)
9024 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009025 if (tv->v_type != VAR_STRING)
9026 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (name == NULL || *name == NUL)
9028 return curbuf;
9029 if (name[0] == '$' && name[1] == NUL)
9030 return lastbuf;
9031
9032 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9033 save_magic = p_magic;
9034 p_magic = TRUE;
9035 save_cpo = p_cpo;
9036 p_cpo = (char_u *)"";
9037
9038 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009039 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 p_magic = save_magic;
9042 p_cpo = save_cpo;
9043
9044 /* If not found, try expanding the name, like done for bufexists(). */
9045 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009046 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048 return buf;
9049}
9050
9051/*
9052 * "bufname(expr)" function
9053 */
9054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009056 typval_T *argvars;
9057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 buf_T *buf;
9060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009063 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 --emsg_off;
9070}
9071
9072/*
9073 * "bufnr(expr)" function
9074 */
9075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009081 int error = FALSE;
9082 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009086 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009087 --emsg_off;
9088
9089 /* If the buffer isn't found and the second argument is not zero create a
9090 * new buffer. */
9091 if (buf == NULL
9092 && argvars[1].v_type != VAR_UNKNOWN
9093 && get_tv_number_chk(&argvars[1], &error) != 0
9094 && !error
9095 && (name = get_tv_string_chk(&argvars[0])) != NULL
9096 && !error)
9097 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009102 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103}
9104
9105/*
9106 * "bufwinnr(nr)" function
9107 */
9108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 typval_T *argvars;
9111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112{
9113#ifdef FEAT_WINDOWS
9114 win_T *wp;
9115 int winnr = 0;
9116#endif
9117 buf_T *buf;
9118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009121 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122#ifdef FEAT_WINDOWS
9123 for (wp = firstwin; wp; wp = wp->w_next)
9124 {
9125 ++winnr;
9126 if (wp->w_buffer == buf)
9127 break;
9128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132#endif
9133 --emsg_off;
9134}
9135
9136/*
9137 * "byte2line(byte)" function
9138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146#else
9147 long boff = 0;
9148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 (linenr_T)0, &boff);
9155#endif
9156}
9157
9158/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009159 * "byteidx()" function
9160 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009163 typval_T *argvars;
9164 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009165{
9166#ifdef FEAT_MBYTE
9167 char_u *t;
9168#endif
9169 char_u *str;
9170 long idx;
9171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 str = get_tv_string_chk(&argvars[0]);
9173 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009176 return;
9177
9178#ifdef FEAT_MBYTE
9179 t = str;
9180 for ( ; idx > 0; idx--)
9181 {
9182 if (*t == NUL) /* EOL reached */
9183 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009184 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009185 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009186 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009187#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009188 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009190#endif
9191}
9192
Bram Moolenaardb913952012-06-29 12:54:53 +02009193 int
9194func_call(name, args, selfdict, rettv)
9195 char_u *name;
9196 typval_T *args;
9197 dict_T *selfdict;
9198 typval_T *rettv;
9199{
9200 listitem_T *item;
9201 typval_T argv[MAX_FUNC_ARGS + 1];
9202 int argc = 0;
9203 int dummy;
9204 int r = 0;
9205
9206 for (item = args->vval.v_list->lv_first; item != NULL;
9207 item = item->li_next)
9208 {
9209 if (argc == MAX_FUNC_ARGS)
9210 {
9211 EMSG(_("E699: Too many arguments"));
9212 break;
9213 }
9214 /* Make a copy of each argument. This is needed to be able to set
9215 * v_lock to VAR_FIXED in the copy without changing the original list.
9216 */
9217 copy_tv(&item->li_tv, &argv[argc++]);
9218 }
9219
9220 if (item == NULL)
9221 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9222 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9223 &dummy, TRUE, selfdict);
9224
9225 /* Free the arguments. */
9226 while (argc > 0)
9227 clear_tv(&argv[--argc]);
9228
9229 return r;
9230}
9231
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009232/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009233 * "call(func, arglist)" function
9234 */
9235 static void
9236f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009237 typval_T *argvars;
9238 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009239{
9240 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009241 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009242
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009243 if (argvars[1].v_type != VAR_LIST)
9244 {
9245 EMSG(_(e_listreq));
9246 return;
9247 }
9248 if (argvars[1].vval.v_list == NULL)
9249 return;
9250
9251 if (argvars[0].v_type == VAR_FUNC)
9252 func = argvars[0].vval.v_string;
9253 else
9254 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009255 if (*func == NUL)
9256 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009257
Bram Moolenaare9a41262005-01-15 22:18:47 +00009258 if (argvars[2].v_type != VAR_UNKNOWN)
9259 {
9260 if (argvars[2].v_type != VAR_DICT)
9261 {
9262 EMSG(_(e_dictreq));
9263 return;
9264 }
9265 selfdict = argvars[2].vval.v_dict;
9266 }
9267
Bram Moolenaardb913952012-06-29 12:54:53 +02009268 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009269}
9270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#ifdef FEAT_FLOAT
9272/*
9273 * "ceil({float})" function
9274 */
9275 static void
9276f_ceil(argvars, rettv)
9277 typval_T *argvars;
9278 typval_T *rettv;
9279{
9280 float_T f;
9281
9282 rettv->v_type = VAR_FLOAT;
9283 if (get_float_arg(argvars, &f) == OK)
9284 rettv->vval.v_float = ceil(f);
9285 else
9286 rettv->vval.v_float = 0.0;
9287}
9288#endif
9289
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009290/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009291 * "changenr()" function
9292 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009293 static void
9294f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009295 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009296 typval_T *rettv;
9297{
9298 rettv->vval.v_number = curbuf->b_u_seq_cur;
9299}
9300
9301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * "char2nr(string)" function
9303 */
9304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009306 typval_T *argvars;
9307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309#ifdef FEAT_MBYTE
9310 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009311 {
9312 int utf8 = 0;
9313
9314 if (argvars[1].v_type != VAR_UNKNOWN)
9315 utf8 = get_tv_number_chk(&argvars[1], NULL);
9316
9317 if (utf8)
9318 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9319 else
9320 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 else
9323#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325}
9326
9327/*
9328 * "cindent(lnum)" function
9329 */
9330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009331f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
9335#ifdef FEAT_CINDENT
9336 pos_T pos;
9337 linenr_T lnum;
9338
9339 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009340 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9342 {
9343 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 curwin->w_cursor = pos;
9346 }
9347 else
9348#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009349 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350}
9351
9352/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009353 * "clearmatches()" function
9354 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009355 static void
9356f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009357 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009358 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009359{
9360#ifdef FEAT_SEARCH_EXTRA
9361 clear_matches(curwin);
9362#endif
9363}
9364
9365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 * "col(string)" function
9367 */
9368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009369f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009370 typval_T *argvars;
9371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372{
9373 colnr_T col = 0;
9374 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009375 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009377 fp = var2fpos(&argvars[0], FALSE, &fnum);
9378 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 {
9380 if (fp->col == MAXCOL)
9381 {
9382 /* '> can be MAXCOL, get the length of the line then */
9383 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009384 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 else
9386 col = MAXCOL;
9387 }
9388 else
9389 {
9390 col = fp->col + 1;
9391#ifdef FEAT_VIRTUALEDIT
9392 /* col(".") when the cursor is on the NUL at the end of the line
9393 * because of "coladd" can be seen as an extra column. */
9394 if (virtual_active() && fp == &curwin->w_cursor)
9395 {
9396 char_u *p = ml_get_cursor();
9397
9398 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9399 curwin->w_virtcol - curwin->w_cursor.coladd))
9400 {
9401# ifdef FEAT_MBYTE
9402 int l;
9403
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009404 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 col += l;
9406# else
9407 if (*p != NUL && p[1] == NUL)
9408 ++col;
9409# endif
9410 }
9411 }
9412#endif
9413 }
9414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416}
9417
Bram Moolenaar572cb562005-08-05 21:35:02 +00009418#if defined(FEAT_INS_EXPAND)
9419/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009420 * "complete()" function
9421 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009422 static void
9423f_complete(argvars, rettv)
9424 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009425 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009426{
9427 int startcol;
9428
9429 if ((State & INSERT) == 0)
9430 {
9431 EMSG(_("E785: complete() can only be used in Insert mode"));
9432 return;
9433 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009434
9435 /* Check for undo allowed here, because if something was already inserted
9436 * the line was already saved for undo and this check isn't done. */
9437 if (!undo_allowed())
9438 return;
9439
Bram Moolenaarade00832006-03-10 21:46:58 +00009440 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9441 {
9442 EMSG(_(e_invarg));
9443 return;
9444 }
9445
9446 startcol = get_tv_number_chk(&argvars[0], NULL);
9447 if (startcol <= 0)
9448 return;
9449
9450 set_completion(startcol - 1, argvars[1].vval.v_list);
9451}
9452
9453/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009454 * "complete_add()" function
9455 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009456 static void
9457f_complete_add(argvars, rettv)
9458 typval_T *argvars;
9459 typval_T *rettv;
9460{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009461 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009462}
9463
9464/*
9465 * "complete_check()" function
9466 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009467 static void
9468f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009469 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009470 typval_T *rettv;
9471{
9472 int saved = RedrawingDisabled;
9473
9474 RedrawingDisabled = 0;
9475 ins_compl_check_keys(0);
9476 rettv->vval.v_number = compl_interrupted;
9477 RedrawingDisabled = saved;
9478}
9479#endif
9480
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481/*
9482 * "confirm(message, buttons[, default [, type]])" function
9483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009486 typval_T *argvars UNUSED;
9487 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9490 char_u *message;
9491 char_u *buttons = NULL;
9492 char_u buf[NUMBUFLEN];
9493 char_u buf2[NUMBUFLEN];
9494 int def = 1;
9495 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 char_u *typestr;
9497 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009499 message = get_tv_string_chk(&argvars[0]);
9500 if (message == NULL)
9501 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009502 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009504 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9505 if (buttons == NULL)
9506 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009507 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009510 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9513 if (typestr == NULL)
9514 error = TRUE;
9515 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 switch (TOUPPER_ASC(*typestr))
9518 {
9519 case 'E': type = VIM_ERROR; break;
9520 case 'Q': type = VIM_QUESTION; break;
9521 case 'I': type = VIM_INFO; break;
9522 case 'W': type = VIM_WARNING; break;
9523 case 'G': type = VIM_GENERIC; break;
9524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 }
9526 }
9527 }
9528 }
9529
9530 if (buttons == NULL || *buttons == NUL)
9531 buttons = (char_u *)_("&Ok");
9532
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009533 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009535 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536#endif
9537}
9538
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009539/*
9540 * "copy()" function
9541 */
9542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 typval_T *argvars;
9545 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009546{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009547 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009550#ifdef FEAT_FLOAT
9551/*
9552 * "cos()" function
9553 */
9554 static void
9555f_cos(argvars, rettv)
9556 typval_T *argvars;
9557 typval_T *rettv;
9558{
9559 float_T f;
9560
9561 rettv->v_type = VAR_FLOAT;
9562 if (get_float_arg(argvars, &f) == OK)
9563 rettv->vval.v_float = cos(f);
9564 else
9565 rettv->vval.v_float = 0.0;
9566}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009567
9568/*
9569 * "cosh()" function
9570 */
9571 static void
9572f_cosh(argvars, rettv)
9573 typval_T *argvars;
9574 typval_T *rettv;
9575{
9576 float_T f;
9577
9578 rettv->v_type = VAR_FLOAT;
9579 if (get_float_arg(argvars, &f) == OK)
9580 rettv->vval.v_float = cosh(f);
9581 else
9582 rettv->vval.v_float = 0.0;
9583}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009584#endif
9585
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009587 * "count()" function
9588 */
9589 static void
9590f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009591 typval_T *argvars;
9592 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009593{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009594 long n = 0;
9595 int ic = FALSE;
9596
Bram Moolenaare9a41262005-01-15 22:18:47 +00009597 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009598 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009599 listitem_T *li;
9600 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009601 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009602
Bram Moolenaare9a41262005-01-15 22:18:47 +00009603 if ((l = argvars[0].vval.v_list) != NULL)
9604 {
9605 li = l->lv_first;
9606 if (argvars[2].v_type != VAR_UNKNOWN)
9607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009608 int error = FALSE;
9609
9610 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009611 if (argvars[3].v_type != VAR_UNKNOWN)
9612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 idx = get_tv_number_chk(&argvars[3], &error);
9614 if (!error)
9615 {
9616 li = list_find(l, idx);
9617 if (li == NULL)
9618 EMSGN(_(e_listidx), idx);
9619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009620 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 if (error)
9622 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 }
9624
9625 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009626 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009627 ++n;
9628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009630 else if (argvars[0].v_type == VAR_DICT)
9631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009632 int todo;
9633 dict_T *d;
9634 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009635
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009636 if ((d = argvars[0].vval.v_dict) != NULL)
9637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638 int error = FALSE;
9639
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 if (argvars[2].v_type != VAR_UNKNOWN)
9641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 if (argvars[3].v_type != VAR_UNKNOWN)
9644 EMSG(_(e_invarg));
9645 }
9646
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009647 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009648 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009649 {
9650 if (!HASHITEM_EMPTY(hi))
9651 {
9652 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009653 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009654 ++n;
9655 }
9656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657 }
9658 }
9659 else
9660 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009661 rettv->vval.v_number = n;
9662}
9663
9664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9666 *
9667 * Checks the existence of a cscope connection.
9668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009671 typval_T *argvars UNUSED;
9672 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673{
9674#ifdef FEAT_CSCOPE
9675 int num = 0;
9676 char_u *dbpath = NULL;
9677 char_u *prepend = NULL;
9678 char_u buf[NUMBUFLEN];
9679
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009680 if (argvars[0].v_type != VAR_UNKNOWN
9681 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 num = (int)get_tv_number(&argvars[0]);
9684 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009685 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 }
9688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690#endif
9691}
9692
9693/*
9694 * "cursor(lnum, col)" function
9695 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009696 * Moves the cursor to the specified line and column.
9697 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009701 typval_T *argvars;
9702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703{
9704 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009705#ifdef FEAT_VIRTUALEDIT
9706 long coladd = 0;
9707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009709 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009710 if (argvars[1].v_type == VAR_UNKNOWN)
9711 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009712 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009713
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009715 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009716 line = pos.lnum;
9717 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009718#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009719 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009720#endif
9721 }
9722 else
9723 {
9724 line = get_tv_lnum(argvars);
9725 col = get_tv_number_chk(&argvars[1], NULL);
9726#ifdef FEAT_VIRTUALEDIT
9727 if (argvars[2].v_type != VAR_UNKNOWN)
9728 coladd = get_tv_number_chk(&argvars[2], NULL);
9729#endif
9730 }
9731 if (line < 0 || col < 0
9732#ifdef FEAT_VIRTUALEDIT
9733 || coladd < 0
9734#endif
9735 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009736 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (line > 0)
9738 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (col > 0)
9740 curwin->w_cursor.col = col - 1;
9741#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009742 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743#endif
9744
9745 /* Make sure the cursor is in a valid position. */
9746 check_cursor();
9747#ifdef FEAT_MBYTE
9748 /* Correct cursor for multi-byte character. */
9749 if (has_mbyte)
9750 mb_adjust_cursor();
9751#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009752
9753 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009754 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755}
9756
9757/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009758 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 */
9760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009762 typval_T *argvars;
9763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009765 int noref = 0;
9766
9767 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009768 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009769 if (noref < 0 || noref > 1)
9770 EMSG(_(e_invarg));
9771 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009772 {
9773 current_copyID += COPYID_INC;
9774 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776}
9777
9778/*
9779 * "delete()" function
9780 */
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790}
9791
9792/*
9793 * "did_filetype()" function
9794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009797 typval_T *argvars UNUSED;
9798 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#endif
9803}
9804
9805/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009806 * "diff_filler()" function
9807 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009810 typval_T *argvars UNUSED;
9811 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009812{
9813#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009815#endif
9816}
9817
9818/*
9819 * "diff_hlID()" function
9820 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009823 typval_T *argvars UNUSED;
9824 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009825{
9826#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828 static linenr_T prev_lnum = 0;
9829 static int changedtick = 0;
9830 static int fnum = 0;
9831 static int change_start = 0;
9832 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009833 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 int filler_lines;
9835 int col;
9836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009837 if (lnum < 0) /* ignore type error in {lnum} arg */
9838 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009839 if (lnum != prev_lnum
9840 || changedtick != curbuf->b_changedtick
9841 || fnum != curbuf->b_fnum)
9842 {
9843 /* New line, buffer, change: need to get the values. */
9844 filler_lines = diff_check(curwin, lnum);
9845 if (filler_lines < 0)
9846 {
9847 if (filler_lines == -1)
9848 {
9849 change_start = MAXCOL;
9850 change_end = -1;
9851 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9852 hlID = HLF_ADD; /* added line */
9853 else
9854 hlID = HLF_CHD; /* changed line */
9855 }
9856 else
9857 hlID = HLF_ADD; /* added line */
9858 }
9859 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009860 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009861 prev_lnum = lnum;
9862 changedtick = curbuf->b_changedtick;
9863 fnum = curbuf->b_fnum;
9864 }
9865
9866 if (hlID == HLF_CHD || hlID == HLF_TXD)
9867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009868 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009869 if (col >= change_start && col <= change_end)
9870 hlID = HLF_TXD; /* changed text */
9871 else
9872 hlID = HLF_CHD; /* changed line */
9873 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009874 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009875#endif
9876}
9877
9878/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009879 * "empty({expr})" function
9880 */
9881 static void
9882f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009883 typval_T *argvars;
9884 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009885{
9886 int n;
9887
9888 switch (argvars[0].v_type)
9889 {
9890 case VAR_STRING:
9891 case VAR_FUNC:
9892 n = argvars[0].vval.v_string == NULL
9893 || *argvars[0].vval.v_string == NUL;
9894 break;
9895 case VAR_NUMBER:
9896 n = argvars[0].vval.v_number == 0;
9897 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009898#ifdef FEAT_FLOAT
9899 case VAR_FLOAT:
9900 n = argvars[0].vval.v_float == 0.0;
9901 break;
9902#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009903 case VAR_LIST:
9904 n = argvars[0].vval.v_list == NULL
9905 || argvars[0].vval.v_list->lv_first == NULL;
9906 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 case VAR_DICT:
9908 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009911 default:
9912 EMSG2(_(e_intern2), "f_empty()");
9913 n = 0;
9914 }
9915
9916 rettv->vval.v_number = n;
9917}
9918
9919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 * "escape({string}, {chars})" function
9921 */
9922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009923f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009924 typval_T *argvars;
9925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
9927 char_u buf[NUMBUFLEN];
9928
Bram Moolenaar758711c2005-02-02 23:11:38 +00009929 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9930 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009931 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932}
9933
9934/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009935 * "eval()" function
9936 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009937 static void
9938f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009939 typval_T *argvars;
9940 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009941{
9942 char_u *s;
9943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 s = get_tv_string_chk(&argvars[0]);
9945 if (s != NULL)
9946 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9949 {
9950 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009951 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009953 else if (*s != NUL)
9954 EMSG(_(e_trailing));
9955}
9956
9957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 * "eventhandler()" function
9959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009962 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966}
9967
9968/*
9969 * "executable()" function
9970 */
9971 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009972f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009973 typval_T *argvars;
9974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979/*
9980 * "exists()" function
9981 */
9982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *argvars;
9985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
9987 char_u *p;
9988 char_u *name;
9989 int n = FALSE;
9990 int len = 0;
9991
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009992 no_autoload = TRUE;
9993
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 if (*p == '$') /* environment variable */
9996 {
9997 /* first try "normal" environment variables (fast) */
9998 if (mch_getenv(p + 1) != NULL)
9999 n = TRUE;
10000 else
10001 {
10002 /* try expanding things like $VIM and ${HOME} */
10003 p = expand_env_save(p);
10004 if (p != NULL && *p != '$')
10005 n = TRUE;
10006 vim_free(p);
10007 }
10008 }
10009 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010012 if (*skipwhite(p) != NUL)
10013 n = FALSE; /* trailing garbage */
10014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 else if (*p == '*') /* internal or user defined function */
10016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010017 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 }
10019 else if (*p == ':')
10020 {
10021 n = cmd_exists(p + 1);
10022 }
10023 else if (*p == '#')
10024 {
10025#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010026 if (p[1] == '#')
10027 n = autocmd_supported(p + 2);
10028 else
10029 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#endif
10031 }
10032 else /* internal variable */
10033 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010034 char_u *tofree;
10035 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010037 /* get_name_len() takes care of expanding curly braces */
10038 name = p;
10039 len = get_name_len(&p, &tofree, TRUE, FALSE);
10040 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010042 if (tofree != NULL)
10043 name = tofree;
10044 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10045 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010047 /* handle d.key, l[idx], f(expr) */
10048 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10049 if (n)
10050 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 }
10052 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010053 if (*p != NUL)
10054 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010056 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010060
10061 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062}
10063
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010064#ifdef FEAT_FLOAT
10065/*
10066 * "exp()" function
10067 */
10068 static void
10069f_exp(argvars, rettv)
10070 typval_T *argvars;
10071 typval_T *rettv;
10072{
10073 float_T f;
10074
10075 rettv->v_type = VAR_FLOAT;
10076 if (get_float_arg(argvars, &f) == OK)
10077 rettv->vval.v_float = exp(f);
10078 else
10079 rettv->vval.v_float = 0.0;
10080}
10081#endif
10082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083/*
10084 * "expand()" function
10085 */
10086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 typval_T *argvars;
10089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 char_u *s;
10092 int len;
10093 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010094 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010097 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010099 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010100 if (argvars[1].v_type != VAR_UNKNOWN
10101 && argvars[2].v_type != VAR_UNKNOWN
10102 && get_tv_number_chk(&argvars[2], &error)
10103 && !error)
10104 {
10105 rettv->v_type = VAR_LIST;
10106 rettv->vval.v_list = NULL;
10107 }
10108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (*s == '%' || *s == '#' || *s == '<')
10111 {
10112 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010113 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010115 if (rettv->v_type == VAR_LIST)
10116 {
10117 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10118 list_append_string(rettv->vval.v_list, result, -1);
10119 else
10120 vim_free(result);
10121 }
10122 else
10123 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 }
10125 else
10126 {
10127 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010128 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 if (argvars[1].v_type != VAR_UNKNOWN
10130 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010131 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 if (!error)
10133 {
10134 ExpandInit(&xpc);
10135 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010136 if (p_wic)
10137 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010138 if (rettv->v_type == VAR_STRING)
10139 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10140 options, WILD_ALL);
10141 else if (rettv_list_alloc(rettv) != FAIL)
10142 {
10143 int i;
10144
10145 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10146 for (i = 0; i < xpc.xp_numfiles; i++)
10147 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10148 ExpandCleanup(&xpc);
10149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 }
10151 else
10152 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 }
10154}
10155
10156/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010157 * Go over all entries in "d2" and add them to "d1".
10158 * When "action" is "error" then a duplicate key is an error.
10159 * When "action" is "force" then a duplicate key is overwritten.
10160 * Otherwise duplicate keys are ignored ("action" is "keep").
10161 */
10162 void
10163dict_extend(d1, d2, action)
10164 dict_T *d1;
10165 dict_T *d2;
10166 char_u *action;
10167{
10168 dictitem_T *di1;
10169 hashitem_T *hi2;
10170 int todo;
10171
10172 todo = (int)d2->dv_hashtab.ht_used;
10173 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10174 {
10175 if (!HASHITEM_EMPTY(hi2))
10176 {
10177 --todo;
10178 di1 = dict_find(d1, hi2->hi_key, -1);
10179 if (d1->dv_scope != 0)
10180 {
10181 /* Disallow replacing a builtin function in l: and g:.
10182 * Check the key to be valid when adding to any
10183 * scope. */
10184 if (d1->dv_scope == VAR_DEF_SCOPE
10185 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10186 && var_check_func_name(hi2->hi_key,
10187 di1 == NULL))
10188 break;
10189 if (!valid_varname(hi2->hi_key))
10190 break;
10191 }
10192 if (di1 == NULL)
10193 {
10194 di1 = dictitem_copy(HI2DI(hi2));
10195 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10196 dictitem_free(di1);
10197 }
10198 else if (*action == 'e')
10199 {
10200 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10201 break;
10202 }
10203 else if (*action == 'f' && HI2DI(hi2) != di1)
10204 {
10205 clear_tv(&di1->di_tv);
10206 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10207 }
10208 }
10209 }
10210}
10211
10212/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010213 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010214 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010215 */
10216 static void
10217f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010218 typval_T *argvars;
10219 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010220{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010221 char *arg_errmsg = N_("extend() argument");
10222
Bram Moolenaare9a41262005-01-15 22:18:47 +000010223 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010224 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010225 list_T *l1, *l2;
10226 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010229
Bram Moolenaare9a41262005-01-15 22:18:47 +000010230 l1 = argvars[0].vval.v_list;
10231 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010232 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010233 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010234 {
10235 if (argvars[2].v_type != VAR_UNKNOWN)
10236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 before = get_tv_number_chk(&argvars[2], &error);
10238 if (error)
10239 return; /* type error; errmsg already given */
10240
Bram Moolenaar758711c2005-02-02 23:11:38 +000010241 if (before == l1->lv_len)
10242 item = NULL;
10243 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010244 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010245 item = list_find(l1, before);
10246 if (item == NULL)
10247 {
10248 EMSGN(_(e_listidx), before);
10249 return;
10250 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 }
10252 }
10253 else
10254 item = NULL;
10255 list_extend(l1, l2, item);
10256
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 copy_tv(&argvars[0], rettv);
10258 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010260 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10261 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010262 dict_T *d1, *d2;
10263 char_u *action;
10264 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010265
10266 d1 = argvars[0].vval.v_dict;
10267 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010268 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010269 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 {
10271 /* Check the third argument. */
10272 if (argvars[2].v_type != VAR_UNKNOWN)
10273 {
10274 static char *(av[]) = {"keep", "force", "error"};
10275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 action = get_tv_string_chk(&argvars[2]);
10277 if (action == NULL)
10278 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 for (i = 0; i < 3; ++i)
10280 if (STRCMP(action, av[i]) == 0)
10281 break;
10282 if (i == 3)
10283 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010284 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 return;
10286 }
10287 }
10288 else
10289 action = (char_u *)"force";
10290
Bram Moolenaara9922d62013-05-30 13:01:18 +020010291 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 copy_tv(&argvars[0], rettv);
10294 }
10295 }
10296 else
10297 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010298}
10299
10300/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010301 * "feedkeys()" function
10302 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010303 static void
10304f_feedkeys(argvars, rettv)
10305 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010306 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010307{
10308 int remap = TRUE;
10309 char_u *keys, *flags;
10310 char_u nbuf[NUMBUFLEN];
10311 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010312 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010313
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010314 /* This is not allowed in the sandbox. If the commands would still be
10315 * executed in the sandbox it would be OK, but it probably happens later,
10316 * when "sandbox" is no longer set. */
10317 if (check_secure())
10318 return;
10319
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010320 keys = get_tv_string(&argvars[0]);
10321 if (*keys != NUL)
10322 {
10323 if (argvars[1].v_type != VAR_UNKNOWN)
10324 {
10325 flags = get_tv_string_buf(&argvars[1], nbuf);
10326 for ( ; *flags != NUL; ++flags)
10327 {
10328 switch (*flags)
10329 {
10330 case 'n': remap = FALSE; break;
10331 case 'm': remap = TRUE; break;
10332 case 't': typed = TRUE; break;
10333 }
10334 }
10335 }
10336
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010337 /* Need to escape K_SPECIAL and CSI before putting the string in the
10338 * typeahead buffer. */
10339 keys_esc = vim_strsave_escape_csi(keys);
10340 if (keys_esc != NULL)
10341 {
10342 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010343 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010344 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010345 if (vgetc_busy)
10346 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010347 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010348 }
10349}
10350
10351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 * "filereadable()" function
10353 */
10354 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010356 typval_T *argvars;
10357 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010359 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 char_u *p;
10361 int n;
10362
Bram Moolenaarc236c162008-07-13 17:41:49 +000010363#ifndef O_NONBLOCK
10364# define O_NONBLOCK 0
10365#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010366 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010367 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10368 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 {
10370 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010371 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 }
10373 else
10374 n = FALSE;
10375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377}
10378
10379/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010380 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 * rights to write into.
10382 */
10383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010384f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010385 typval_T *argvars;
10386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010388 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389}
10390
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010391static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010392
10393 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010394findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010395 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010396 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010397 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010398{
10399#ifdef FEAT_SEARCHPATH
10400 char_u *fname;
10401 char_u *fresult = NULL;
10402 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10403 char_u *p;
10404 char_u pathbuf[NUMBUFLEN];
10405 int count = 1;
10406 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010407 int error = FALSE;
10408#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010409
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010410 rettv->vval.v_string = NULL;
10411 rettv->v_type = VAR_STRING;
10412
10413#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010415
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010416 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10419 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010420 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 else
10422 {
10423 if (*p != NUL)
10424 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010427 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010428 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010429 }
10430
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010431 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10432 error = TRUE;
10433
10434 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010435 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 do
10437 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010438 if (rettv->v_type == VAR_STRING)
10439 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 fresult = find_file_in_path_option(first ? fname : NULL,
10441 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010442 0, first, path,
10443 find_what,
10444 curbuf->b_ffname,
10445 find_what == FINDFILE_DIR
10446 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010448
10449 if (fresult != NULL && rettv->v_type == VAR_LIST)
10450 list_append_string(rettv->vval.v_list, fresult, -1);
10451
10452 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010454
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010455 if (rettv->v_type == VAR_STRING)
10456 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010458}
10459
Bram Moolenaar33570922005-01-25 22:26:29 +000010460static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10461static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010462
10463/*
10464 * Implementation of map() and filter().
10465 */
10466 static void
10467filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010468 typval_T *argvars;
10469 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010470 int map;
10471{
10472 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010474 listitem_T *li, *nli;
10475 list_T *l = NULL;
10476 dictitem_T *di;
10477 hashtab_T *ht;
10478 hashitem_T *hi;
10479 dict_T *d = NULL;
10480 typval_T save_val;
10481 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010483 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010484 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10485 char *arg_errmsg = (map ? N_("map() argument")
10486 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010487 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010488 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010489
Bram Moolenaare9a41262005-01-15 22:18:47 +000010490 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010492 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010493 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 return;
10495 }
10496 else if (argvars[0].v_type == VAR_DICT)
10497 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010498 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010499 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 return;
10501 }
10502 else
10503 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010504 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 return;
10506 }
10507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 expr = get_tv_string_buf_chk(&argvars[1], buf);
10509 /* On type errors, the preceding call has already displayed an error
10510 * message. Avoid a misleading error message for an empty string that
10511 * was not passed as argument. */
10512 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 prepare_vimvar(VV_VAL, &save_val);
10515 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010516
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010517 /* We reset "did_emsg" to be able to detect whether an error
10518 * occurred during evaluation of the expression. */
10519 save_did_emsg = did_emsg;
10520 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010521
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010522 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010523 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 vimvars[VV_KEY].vv_type = VAR_STRING;
10526
10527 ht = &d->dv_hashtab;
10528 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010529 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 if (!HASHITEM_EMPTY(hi))
10533 {
10534 --todo;
10535 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010536 if (tv_check_lock(di->di_tv.v_lock,
10537 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 break;
10539 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010540 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010541 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 break;
10543 if (!map && rem)
10544 dictitem_remove(d, di);
10545 clear_tv(&vimvars[VV_KEY].vv_tv);
10546 }
10547 }
10548 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010549 }
10550 else
10551 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010552 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 for (li = l->lv_first; li != NULL; li = nli)
10555 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010556 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010557 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010558 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010559 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010560 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010561 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010562 break;
10563 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010565 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010566 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010567 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010568
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010569 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010571
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010572 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574
10575 copy_tv(&argvars[0], rettv);
10576}
10577
10578 static int
10579filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010580 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 char_u *expr;
10582 int map;
10583 int *remp;
10584{
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010587 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 s = expr;
10591 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010592 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 if (*s != NUL) /* check for trailing chars after expr */
10594 {
10595 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010596 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597 }
10598 if (map)
10599 {
10600 /* map(): replace the list item value */
10601 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010602 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 *tv = rettv;
10604 }
10605 else
10606 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607 int error = FALSE;
10608
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 /* On type error, nothing has been removed; return FAIL to stop the
10613 * loop. The error message was given by get_tv_number_chk(). */
10614 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010615 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010617 retval = OK;
10618theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010619 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010620 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621}
10622
10623/*
10624 * "filter()" function
10625 */
10626 static void
10627f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 typval_T *argvars;
10629 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010630{
10631 filter_map(argvars, rettv, FALSE);
10632}
10633
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010635 * "finddir({fname}[, {path}[, {count}]])" function
10636 */
10637 static void
10638f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010639 typval_T *argvars;
10640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010641{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010642 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010643}
10644
10645/*
10646 * "findfile({fname}[, {path}[, {count}]])" function
10647 */
10648 static void
10649f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010650 typval_T *argvars;
10651 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010652{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010653 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654}
10655
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010656#ifdef FEAT_FLOAT
10657/*
10658 * "float2nr({float})" function
10659 */
10660 static void
10661f_float2nr(argvars, rettv)
10662 typval_T *argvars;
10663 typval_T *rettv;
10664{
10665 float_T f;
10666
10667 if (get_float_arg(argvars, &f) == OK)
10668 {
10669 if (f < -0x7fffffff)
10670 rettv->vval.v_number = -0x7fffffff;
10671 else if (f > 0x7fffffff)
10672 rettv->vval.v_number = 0x7fffffff;
10673 else
10674 rettv->vval.v_number = (varnumber_T)f;
10675 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010676}
10677
10678/*
10679 * "floor({float})" function
10680 */
10681 static void
10682f_floor(argvars, rettv)
10683 typval_T *argvars;
10684 typval_T *rettv;
10685{
10686 float_T f;
10687
10688 rettv->v_type = VAR_FLOAT;
10689 if (get_float_arg(argvars, &f) == OK)
10690 rettv->vval.v_float = floor(f);
10691 else
10692 rettv->vval.v_float = 0.0;
10693}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010694
10695/*
10696 * "fmod()" function
10697 */
10698 static void
10699f_fmod(argvars, rettv)
10700 typval_T *argvars;
10701 typval_T *rettv;
10702{
10703 float_T fx, fy;
10704
10705 rettv->v_type = VAR_FLOAT;
10706 if (get_float_arg(argvars, &fx) == OK
10707 && get_float_arg(&argvars[1], &fy) == OK)
10708 rettv->vval.v_float = fmod(fx, fy);
10709 else
10710 rettv->vval.v_float = 0.0;
10711}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010712#endif
10713
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010715 * "fnameescape({string})" function
10716 */
10717 static void
10718f_fnameescape(argvars, rettv)
10719 typval_T *argvars;
10720 typval_T *rettv;
10721{
10722 rettv->vval.v_string = vim_strsave_fnameescape(
10723 get_tv_string(&argvars[0]), FALSE);
10724 rettv->v_type = VAR_STRING;
10725}
10726
10727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 * "fnamemodify({fname}, {mods})" function
10729 */
10730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010732 typval_T *argvars;
10733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
10735 char_u *fname;
10736 char_u *mods;
10737 int usedlen = 0;
10738 int len;
10739 char_u *fbuf = NULL;
10740 char_u buf[NUMBUFLEN];
10741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010742 fname = get_tv_string_chk(&argvars[0]);
10743 mods = get_tv_string_buf_chk(&argvars[1], buf);
10744 if (fname == NULL || mods == NULL)
10745 fname = NULL;
10746 else
10747 {
10748 len = (int)STRLEN(fname);
10749 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010756 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 vim_free(fbuf);
10758}
10759
Bram Moolenaar33570922005-01-25 22:26:29 +000010760static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761
10762/*
10763 * "foldclosed()" function
10764 */
10765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010766foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010767 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010769 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
10771#ifdef FEAT_FOLDING
10772 linenr_T lnum;
10773 linenr_T first, last;
10774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10777 {
10778 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10779 {
10780 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010781 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010783 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 return;
10785 }
10786 }
10787#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789}
10790
10791/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010792 * "foldclosed()" function
10793 */
10794 static void
10795f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010796 typval_T *argvars;
10797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010798{
10799 foldclosed_both(argvars, rettv, FALSE);
10800}
10801
10802/*
10803 * "foldclosedend()" function
10804 */
10805 static void
10806f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010807 typval_T *argvars;
10808 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010809{
10810 foldclosed_both(argvars, rettv, TRUE);
10811}
10812
10813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 * "foldlevel()" function
10815 */
10816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010818 typval_T *argvars UNUSED;
10819 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821#ifdef FEAT_FOLDING
10822 linenr_T lnum;
10823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010826 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * "foldtext()" function
10832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010835 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
10838#ifdef FEAT_FOLDING
10839 linenr_T lnum;
10840 char_u *s;
10841 char_u *r;
10842 int len;
10843 char *txt;
10844#endif
10845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 rettv->v_type = VAR_STRING;
10847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10850 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10851 <= curbuf->b_ml.ml_line_count
10852 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 {
10854 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10856 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 {
10858 if (!linewhite(lnum))
10859 break;
10860 ++lnum;
10861 }
10862
10863 /* Find interesting text in this line. */
10864 s = skipwhite(ml_get(lnum));
10865 /* skip C comment-start */
10866 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010869 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010871 {
10872 s = skipwhite(ml_get(lnum + 1));
10873 if (*s == '*')
10874 s = skipwhite(s + 1);
10875 }
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 txt = _("+-%s%3ld lines: ");
10878 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010879 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 + 20 /* for %3ld */
10881 + STRLEN(s))); /* concatenated */
10882 if (r != NULL)
10883 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010884 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10885 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10886 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 len = (int)STRLEN(r);
10888 STRCAT(r, s);
10889 /* remove 'foldmarker' and 'commentstring' */
10890 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010891 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 }
10893 }
10894#endif
10895}
10896
10897/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010898 * "foldtextresult(lnum)" function
10899 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010901f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010902 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010904{
10905#ifdef FEAT_FOLDING
10906 linenr_T lnum;
10907 char_u *text;
10908 char_u buf[51];
10909 foldinfo_T foldinfo;
10910 int fold_count;
10911#endif
10912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->v_type = VAR_STRING;
10914 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010915#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 /* treat illegal types and illegal string values for {lnum} the same */
10918 if (lnum < 0)
10919 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010920 fold_count = foldedCount(curwin, lnum, &foldinfo);
10921 if (fold_count > 0)
10922 {
10923 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10924 &foldinfo, buf);
10925 if (text == buf)
10926 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010928 }
10929#endif
10930}
10931
10932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 * "foreground()" function
10934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010937 typval_T *argvars UNUSED;
10938 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940#ifdef FEAT_GUI
10941 if (gui.in_use)
10942 gui_mch_set_foreground();
10943#else
10944# ifdef WIN32
10945 win32_set_foreground();
10946# endif
10947#endif
10948}
10949
10950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010951 * "function()" function
10952 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010954f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 typval_T *argvars;
10956 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957{
10958 char_u *s;
Bram Moolenaara1544c02013-05-30 12:35:52 +020010959 char_u *name = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010962 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010963 EMSG2(_(e_invarg2), s);
Bram Moolenaara1544c02013-05-30 12:35:52 +020010964 /* Don't check an autoload name for existence here, but still expand it
10965 * checking for validity */
10966 else if ((name = get_expanded_name(s, vim_strchr(s, AUTOLOAD_CHAR) == NULL))
10967 == NULL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010968 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010969 else
10970 {
Bram Moolenaara1544c02013-05-30 12:35:52 +020010971 if (name == NULL)
10972 /* Autoload function, need to copy string */
10973 rettv->vval.v_string = vim_strsave(s);
10974 else
10975 /* Function found by get_expanded_name, string allocated by
10976 * trans_function_name: no need to copy */
10977 rettv->vval.v_string = name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010979 }
10980}
10981
10982/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010983 * "garbagecollect()" function
10984 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010985 static void
10986f_garbagecollect(argvars, rettv)
10987 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010988 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010989{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010990 /* This is postponed until we are back at the toplevel, because we may be
10991 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10992 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010993
10994 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10995 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010996}
10997
10998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010999 * "get()" function
11000 */
11001 static void
11002f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *argvars;
11004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005{
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 listitem_T *li;
11007 list_T *l;
11008 dictitem_T *di;
11009 dict_T *d;
11010 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011011
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011013 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011016 int error = FALSE;
11017
11018 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11019 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011020 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011021 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011023 else if (argvars[0].v_type == VAR_DICT)
11024 {
11025 if ((d = argvars[0].vval.v_dict) != NULL)
11026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011027 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011028 if (di != NULL)
11029 tv = &di->di_tv;
11030 }
11031 }
11032 else
11033 EMSG2(_(e_listdictarg), "get()");
11034
11035 if (tv == NULL)
11036 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011037 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011038 copy_tv(&argvars[2], rettv);
11039 }
11040 else
11041 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011042}
11043
Bram Moolenaar342337a2005-07-21 21:11:17 +000011044static 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 +000011045
11046/*
11047 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011048 * Return a range (from start to end) of lines in rettv from the specified
11049 * buffer.
11050 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011051 */
11052 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011053get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011054 buf_T *buf;
11055 linenr_T start;
11056 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011057 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011058 typval_T *rettv;
11059{
11060 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011061
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011062 if (retlist && rettv_list_alloc(rettv) == FAIL)
11063 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011064
11065 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11066 return;
11067
11068 if (!retlist)
11069 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011070 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11071 p = ml_get_buf(buf, start, FALSE);
11072 else
11073 p = (char_u *)"";
11074
11075 rettv->v_type = VAR_STRING;
11076 rettv->vval.v_string = vim_strsave(p);
11077 }
11078 else
11079 {
11080 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011081 return;
11082
11083 if (start < 1)
11084 start = 1;
11085 if (end > buf->b_ml.ml_line_count)
11086 end = buf->b_ml.ml_line_count;
11087 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011088 if (list_append_string(rettv->vval.v_list,
11089 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011090 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011091 }
11092}
11093
11094/*
11095 * "getbufline()" function
11096 */
11097 static void
11098f_getbufline(argvars, rettv)
11099 typval_T *argvars;
11100 typval_T *rettv;
11101{
11102 linenr_T lnum;
11103 linenr_T end;
11104 buf_T *buf;
11105
11106 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11107 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011108 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011109 --emsg_off;
11110
Bram Moolenaar661b1822005-07-28 22:36:45 +000011111 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011112 if (argvars[2].v_type == VAR_UNKNOWN)
11113 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011114 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011115 end = get_tv_lnum_buf(&argvars[2], buf);
11116
Bram Moolenaar342337a2005-07-21 21:11:17 +000011117 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011118}
11119
Bram Moolenaar0d660222005-01-07 21:51:51 +000011120/*
11121 * "getbufvar()" function
11122 */
11123 static void
11124f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011125 typval_T *argvars;
11126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011127{
11128 buf_T *buf;
11129 buf_T *save_curbuf;
11130 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011131 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011132 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011134 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11135 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011136 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011137 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011138
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011139 rettv->v_type = VAR_STRING;
11140 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011141
11142 if (buf != NULL && varname != NULL)
11143 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011144 /* set curbuf to be our buf, temporarily */
11145 save_curbuf = curbuf;
11146 curbuf = buf;
11147
Bram Moolenaar0d660222005-01-07 21:51:51 +000011148 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011149 {
11150 if (get_option_tv(&varname, rettv, TRUE) == OK)
11151 done = TRUE;
11152 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011153 else if (STRCMP(varname, "changedtick") == 0)
11154 {
11155 rettv->v_type = VAR_NUMBER;
11156 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011157 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011158 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 else
11160 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011161 /* Look up the variable. */
11162 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11163 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11164 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011166 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011168 done = TRUE;
11169 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011170 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011171
11172 /* restore previous notion of curbuf */
11173 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011174 }
11175
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011176 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11177 /* use the default value */
11178 copy_tv(&argvars[2], rettv);
11179
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180 --emsg_off;
11181}
11182
11183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 * "getchar()" function
11185 */
11186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011188 typval_T *argvars;
11189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
11191 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011194 /* Position the cursor. Needed after a message that ends in a space. */
11195 windgoto(msg_row, msg_col);
11196
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 ++no_mapping;
11198 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011199 for (;;)
11200 {
11201 if (argvars[0].v_type == VAR_UNKNOWN)
11202 /* getchar(): blocking wait. */
11203 n = safe_vgetc();
11204 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11205 /* getchar(1): only check if char avail */
11206 n = vpeekc();
11207 else if (error || vpeekc() == NUL)
11208 /* illegal argument or getchar(0) and no char avail: return zero */
11209 n = 0;
11210 else
11211 /* getchar(0) and char avail: return char */
11212 n = safe_vgetc();
11213 if (n == K_IGNORE)
11214 continue;
11215 break;
11216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 --no_mapping;
11218 --allow_keys;
11219
Bram Moolenaar219b8702006-11-01 14:32:36 +000011220 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11221 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11222 vimvars[VV_MOUSE_COL].vv_nr = 0;
11223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 if (IS_SPECIAL(n) || mod_mask != 0)
11226 {
11227 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11228 int i = 0;
11229
11230 /* Turn a special key into three bytes, plus modifier. */
11231 if (mod_mask != 0)
11232 {
11233 temp[i++] = K_SPECIAL;
11234 temp[i++] = KS_MODIFIER;
11235 temp[i++] = mod_mask;
11236 }
11237 if (IS_SPECIAL(n))
11238 {
11239 temp[i++] = K_SPECIAL;
11240 temp[i++] = K_SECOND(n);
11241 temp[i++] = K_THIRD(n);
11242 }
11243#ifdef FEAT_MBYTE
11244 else if (has_mbyte)
11245 i += (*mb_char2bytes)(n, temp + i);
11246#endif
11247 else
11248 temp[i++] = n;
11249 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011250 rettv->v_type = VAR_STRING;
11251 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011252
11253#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011254 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011255 {
11256 int row = mouse_row;
11257 int col = mouse_col;
11258 win_T *win;
11259 linenr_T lnum;
11260# ifdef FEAT_WINDOWS
11261 win_T *wp;
11262# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011263 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011264
11265 if (row >= 0 && col >= 0)
11266 {
11267 /* Find the window at the mouse coordinates and compute the
11268 * text position. */
11269 win = mouse_find_win(&row, &col);
11270 (void)mouse_comp_pos(win, &row, &col, &lnum);
11271# ifdef FEAT_WINDOWS
11272 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011273 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011274# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011275 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011276 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11277 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11278 }
11279 }
11280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 }
11282}
11283
11284/*
11285 * "getcharmod()" function
11286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011288f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011289 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293}
11294
11295/*
11296 * "getcmdline()" function
11297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011300 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011301 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011303 rettv->v_type = VAR_STRING;
11304 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305}
11306
11307/*
11308 * "getcmdpos()" function
11309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316}
11317
11318/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011319 * "getcmdtype()" function
11320 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011321 static void
11322f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011324 typval_T *rettv;
11325{
11326 rettv->v_type = VAR_STRING;
11327 rettv->vval.v_string = alloc(2);
11328 if (rettv->vval.v_string != NULL)
11329 {
11330 rettv->vval.v_string[0] = get_cmdline_type();
11331 rettv->vval.v_string[1] = NUL;
11332 }
11333}
11334
11335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 * "getcwd()" function
11337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011339f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011340 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011343 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011346 rettv->vval.v_string = NULL;
11347 cwd = alloc(MAXPATHL);
11348 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011350 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11351 {
11352 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011354 if (rettv->vval.v_string != NULL)
11355 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011357 }
11358 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 }
11360}
11361
11362/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011363 * "getfontname()" function
11364 */
11365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011367 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011368 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011369{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370 rettv->v_type = VAR_STRING;
11371 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011372#ifdef FEAT_GUI
11373 if (gui.in_use)
11374 {
11375 GuiFont font;
11376 char_u *name = NULL;
11377
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011378 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011379 {
11380 /* Get the "Normal" font. Either the name saved by
11381 * hl_set_font_name() or from the font ID. */
11382 font = gui.norm_font;
11383 name = hl_get_font_name();
11384 }
11385 else
11386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011388 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11389 return;
11390 font = gui_mch_get_font(name, FALSE);
11391 if (font == NOFONT)
11392 return; /* Invalid font name, return empty string. */
11393 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011395 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011396 gui_mch_free_font(font);
11397 }
11398#endif
11399}
11400
11401/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011402 * "getfperm({fname})" function
11403 */
11404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011405f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011406 typval_T *argvars;
11407 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011408{
11409 char_u *fname;
11410 struct stat st;
11411 char_u *perm = NULL;
11412 char_u flags[] = "rwx";
11413 int i;
11414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011418 if (mch_stat((char *)fname, &st) >= 0)
11419 {
11420 perm = vim_strsave((char_u *)"---------");
11421 if (perm != NULL)
11422 {
11423 for (i = 0; i < 9; i++)
11424 {
11425 if (st.st_mode & (1 << (8 - i)))
11426 perm[i] = flags[i % 3];
11427 }
11428 }
11429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011430 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011431}
11432
11433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434 * "getfsize({fname})" function
11435 */
11436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *argvars;
11439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440{
11441 char_u *fname;
11442 struct stat st;
11443
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
11448 if (mch_stat((char *)fname, &st) >= 0)
11449 {
11450 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011455
11456 /* non-perfect check for overflow */
11457 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11458 rettv->vval.v_number = -2;
11459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 }
11461 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
11465/*
11466 * "getftime({fname})" function
11467 */
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
11473 char_u *fname;
11474 struct stat st;
11475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
11478 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011479 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482}
11483
11484/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011485 * "getftype({fname})" function
11486 */
11487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011489 typval_T *argvars;
11490 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011491{
11492 char_u *fname;
11493 struct stat st;
11494 char_u *type = NULL;
11495 char *t;
11496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011500 if (mch_lstat((char *)fname, &st) >= 0)
11501 {
11502#ifdef S_ISREG
11503 if (S_ISREG(st.st_mode))
11504 t = "file";
11505 else if (S_ISDIR(st.st_mode))
11506 t = "dir";
11507# ifdef S_ISLNK
11508 else if (S_ISLNK(st.st_mode))
11509 t = "link";
11510# endif
11511# ifdef S_ISBLK
11512 else if (S_ISBLK(st.st_mode))
11513 t = "bdev";
11514# endif
11515# ifdef S_ISCHR
11516 else if (S_ISCHR(st.st_mode))
11517 t = "cdev";
11518# endif
11519# ifdef S_ISFIFO
11520 else if (S_ISFIFO(st.st_mode))
11521 t = "fifo";
11522# endif
11523# ifdef S_ISSOCK
11524 else if (S_ISSOCK(st.st_mode))
11525 t = "fifo";
11526# endif
11527 else
11528 t = "other";
11529#else
11530# ifdef S_IFMT
11531 switch (st.st_mode & S_IFMT)
11532 {
11533 case S_IFREG: t = "file"; break;
11534 case S_IFDIR: t = "dir"; break;
11535# ifdef S_IFLNK
11536 case S_IFLNK: t = "link"; break;
11537# endif
11538# ifdef S_IFBLK
11539 case S_IFBLK: t = "bdev"; break;
11540# endif
11541# ifdef S_IFCHR
11542 case S_IFCHR: t = "cdev"; break;
11543# endif
11544# ifdef S_IFIFO
11545 case S_IFIFO: t = "fifo"; break;
11546# endif
11547# ifdef S_IFSOCK
11548 case S_IFSOCK: t = "socket"; break;
11549# endif
11550 default: t = "other";
11551 }
11552# else
11553 if (mch_isdir(fname))
11554 t = "dir";
11555 else
11556 t = "file";
11557# endif
11558#endif
11559 type = vim_strsave((char_u *)t);
11560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011561 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011562}
11563
11564/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011565 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011566 */
11567 static void
11568f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011569 typval_T *argvars;
11570 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011571{
11572 linenr_T lnum;
11573 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011574 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011575
11576 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011577 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011578 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011579 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011580 retlist = FALSE;
11581 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011583 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011584 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011585 retlist = TRUE;
11586 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011587
Bram Moolenaar342337a2005-07-21 21:11:17 +000011588 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589}
11590
11591/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011592 * "getmatches()" function
11593 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011594 static void
11595f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011596 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011597 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011598{
11599#ifdef FEAT_SEARCH_EXTRA
11600 dict_T *dict;
11601 matchitem_T *cur = curwin->w_match_head;
11602
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011603 if (rettv_list_alloc(rettv) == OK)
11604 {
11605 while (cur != NULL)
11606 {
11607 dict = dict_alloc();
11608 if (dict == NULL)
11609 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011610 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11611 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11612 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11613 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11614 list_append_dict(rettv->vval.v_list, dict);
11615 cur = cur->next;
11616 }
11617 }
11618#endif
11619}
11620
11621/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011622 * "getpid()" function
11623 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011624 static void
11625f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011626 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011627 typval_T *rettv;
11628{
11629 rettv->vval.v_number = mch_get_pid();
11630}
11631
11632/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011633 * "getpos(string)" function
11634 */
11635 static void
11636f_getpos(argvars, rettv)
11637 typval_T *argvars;
11638 typval_T *rettv;
11639{
11640 pos_T *fp;
11641 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011642 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011643
11644 if (rettv_list_alloc(rettv) == OK)
11645 {
11646 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011647 fp = var2fpos(&argvars[0], TRUE, &fnum);
11648 if (fnum != -1)
11649 list_append_number(l, (varnumber_T)fnum);
11650 else
11651 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011652 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11653 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011654 list_append_number(l, (fp != NULL)
11655 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011656 : (varnumber_T)0);
11657 list_append_number(l,
11658#ifdef FEAT_VIRTUALEDIT
11659 (fp != NULL) ? (varnumber_T)fp->coladd :
11660#endif
11661 (varnumber_T)0);
11662 }
11663 else
11664 rettv->vval.v_number = FALSE;
11665}
11666
11667/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011668 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011669 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011670 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011671f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011672 typval_T *argvars UNUSED;
11673 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011674{
11675#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011676 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011677#endif
11678
Bram Moolenaar2641f772005-03-25 21:58:17 +000011679#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011680 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011681 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011682 wp = NULL;
11683 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11684 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011685 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011686 if (wp == NULL)
11687 return;
11688 }
11689
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011690 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011691 }
11692#endif
11693}
11694
11695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696 * "getreg()" function
11697 */
11698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 typval_T *argvars;
11701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702{
11703 char_u *strregname;
11704 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011705 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011708 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 strregname = get_tv_string_chk(&argvars[0]);
11711 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011712 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011716 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011717 regname = (strregname == NULL ? '"' : *strregname);
11718 if (regname == 0)
11719 regname = '"';
11720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011722 rettv->vval.v_string = error ? NULL :
11723 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724}
11725
11726/*
11727 * "getregtype()" function
11728 */
11729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 typval_T *argvars;
11732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
11734 char_u *strregname;
11735 int regname;
11736 char_u buf[NUMBUFLEN + 2];
11737 long reglen = 0;
11738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011739 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011740 {
11741 strregname = get_tv_string_chk(&argvars[0]);
11742 if (strregname == NULL) /* type error; errmsg already given */
11743 {
11744 rettv->v_type = VAR_STRING;
11745 rettv->vval.v_string = NULL;
11746 return;
11747 }
11748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 else
11750 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752
11753 regname = (strregname == NULL ? '"' : *strregname);
11754 if (regname == 0)
11755 regname = '"';
11756
11757 buf[0] = NUL;
11758 buf[1] = NUL;
11759 switch (get_reg_type(regname, &reglen))
11760 {
11761 case MLINE: buf[0] = 'V'; break;
11762 case MCHAR: buf[0] = 'v'; break;
11763#ifdef FEAT_VISUAL
11764 case MBLOCK:
11765 buf[0] = Ctrl_V;
11766 sprintf((char *)buf + 1, "%ld", reglen + 1);
11767 break;
11768#endif
11769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 rettv->v_type = VAR_STRING;
11771 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772}
11773
11774/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011775 * "gettabvar()" function
11776 */
11777 static void
11778f_gettabvar(argvars, rettv)
11779 typval_T *argvars;
11780 typval_T *rettv;
11781{
11782 tabpage_T *tp;
11783 dictitem_T *v;
11784 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011785 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011786
11787 rettv->v_type = VAR_STRING;
11788 rettv->vval.v_string = NULL;
11789
11790 varname = get_tv_string_chk(&argvars[1]);
11791 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11792 if (tp != NULL && varname != NULL)
11793 {
11794 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011795 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011796 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011797 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011798 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011799 done = TRUE;
11800 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011801 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011802
11803 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011804 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011805}
11806
11807/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011808 * "gettabwinvar()" function
11809 */
11810 static void
11811f_gettabwinvar(argvars, rettv)
11812 typval_T *argvars;
11813 typval_T *rettv;
11814{
11815 getwinvar(argvars, rettv, 1);
11816}
11817
11818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 * "getwinposx()" function
11820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827#ifdef FEAT_GUI
11828 if (gui.in_use)
11829 {
11830 int x, y;
11831
11832 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 }
11835#endif
11836}
11837
11838/*
11839 * "getwinposy()" function
11840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011843 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847#ifdef FEAT_GUI
11848 if (gui.in_use)
11849 {
11850 int x, y;
11851
11852 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011854 }
11855#endif
11856}
11857
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011858/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011859 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011860 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011861 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011862find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011863 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011864 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011865{
11866#ifdef FEAT_WINDOWS
11867 win_T *wp;
11868#endif
11869 int nr;
11870
11871 nr = get_tv_number_chk(vp, NULL);
11872
11873#ifdef FEAT_WINDOWS
11874 if (nr < 0)
11875 return NULL;
11876 if (nr == 0)
11877 return curwin;
11878
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011879 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11880 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011881 if (--nr <= 0)
11882 break;
11883 return wp;
11884#else
11885 if (nr == 0 || nr == 1)
11886 return curwin;
11887 return NULL;
11888#endif
11889}
11890
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891/*
11892 * "getwinvar()" function
11893 */
11894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011895f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 typval_T *argvars;
11897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011899 getwinvar(argvars, rettv, 0);
11900}
11901
11902/*
11903 * getwinvar() and gettabwinvar()
11904 */
11905 static void
11906getwinvar(argvars, rettv, off)
11907 typval_T *argvars;
11908 typval_T *rettv;
11909 int off; /* 1 for gettabwinvar() */
11910{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 win_T *win, *oldcurwin;
11912 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011913 dictitem_T *v;
Bram Moolenaar105bc352013-05-17 16:03:57 +020011914 tabpage_T *tp, *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011915 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011917#ifdef FEAT_WINDOWS
11918 if (off == 1)
11919 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11920 else
11921 tp = curtab;
11922#endif
11923 win = find_win_by_nr(&argvars[off], tp);
11924 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011925 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011927 rettv->v_type = VAR_STRING;
11928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929
11930 if (win != NULL && varname != NULL)
11931 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011932 /* Set curwin to be our win, temporarily. Also set the tabpage,
11933 * otherwise the window is not valid. */
11934 switch_win(&oldcurwin, &oldtabpage, win, tp);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011935
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011937 {
11938 if (get_option_tv(&varname, rettv, 1) == OK)
11939 done = TRUE;
11940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 else
11942 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011943 /* Look up the variable. */
11944 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11945 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011946 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011949 done = TRUE;
11950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011952
11953 /* restore previous notion of curwin */
Bram Moolenaar105bc352013-05-17 16:03:57 +020011954 restore_win(oldcurwin, oldtabpage);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011955 }
11956
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011957 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11958 /* use the default return value */
11959 copy_tv(&argvars[off + 2], rettv);
11960
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 --emsg_off;
11962}
11963
11964/*
11965 * "glob()" function
11966 */
11967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011968f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 typval_T *argvars;
11970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011971{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011972 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011974 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011976 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011977 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011979 if (argvars[1].v_type != VAR_UNKNOWN)
11980 {
11981 if (get_tv_number_chk(&argvars[1], &error))
11982 options |= WILD_KEEP_ALL;
11983 if (argvars[2].v_type != VAR_UNKNOWN
11984 && get_tv_number_chk(&argvars[2], &error))
11985 {
11986 rettv->v_type = VAR_LIST;
11987 rettv->vval.v_list = NULL;
11988 }
11989 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011990 if (!error)
11991 {
11992 ExpandInit(&xpc);
11993 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011994 if (p_wic)
11995 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011996 if (rettv->v_type == VAR_STRING)
11997 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011998 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011999 else if (rettv_list_alloc(rettv) != FAIL)
12000 {
12001 int i;
12002
12003 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12004 NULL, options, WILD_ALL_KEEP);
12005 for (i = 0; i < xpc.xp_numfiles; i++)
12006 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12007
12008 ExpandCleanup(&xpc);
12009 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012010 }
12011 else
12012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013}
12014
12015/*
12016 * "globpath()" function
12017 */
12018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T *argvars;
12021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012023 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012026 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012028 /* When the optional second argument is non-zero, don't remove matches
12029 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12030 if (argvars[2].v_type != VAR_UNKNOWN
12031 && get_tv_number_chk(&argvars[2], &error))
12032 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012034 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012035 rettv->vval.v_string = NULL;
12036 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012037 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12038 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039}
12040
12041/*
12042 * "has()" function
12043 */
12044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 typval_T *argvars;
12047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048{
12049 int i;
12050 char_u *name;
12051 int n = FALSE;
12052 static char *(has_list[]) =
12053 {
12054#ifdef AMIGA
12055 "amiga",
12056# ifdef FEAT_ARP
12057 "arp",
12058# endif
12059#endif
12060#ifdef __BEOS__
12061 "beos",
12062#endif
12063#ifdef MSDOS
12064# ifdef DJGPP
12065 "dos32",
12066# else
12067 "dos16",
12068# endif
12069#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012070#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 "mac",
12072#endif
12073#if defined(MACOS_X_UNIX)
12074 "macunix",
12075#endif
12076#ifdef OS2
12077 "os2",
12078#endif
12079#ifdef __QNX__
12080 "qnx",
12081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082#ifdef UNIX
12083 "unix",
12084#endif
12085#ifdef VMS
12086 "vms",
12087#endif
12088#ifdef WIN16
12089 "win16",
12090#endif
12091#ifdef WIN32
12092 "win32",
12093#endif
12094#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12095 "win32unix",
12096#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012097#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098 "win64",
12099#endif
12100#ifdef EBCDIC
12101 "ebcdic",
12102#endif
12103#ifndef CASE_INSENSITIVE_FILENAME
12104 "fname_case",
12105#endif
12106#ifdef FEAT_ARABIC
12107 "arabic",
12108#endif
12109#ifdef FEAT_AUTOCMD
12110 "autocmd",
12111#endif
12112#ifdef FEAT_BEVAL
12113 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012114# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12115 "balloon_multiline",
12116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#endif
12118#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12119 "builtin_terms",
12120# ifdef ALL_BUILTIN_TCAPS
12121 "all_builtin_terms",
12122# endif
12123#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012124#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12125 || defined(FEAT_GUI_W32) \
12126 || defined(FEAT_GUI_MOTIF))
12127 "browsefilter",
12128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129#ifdef FEAT_BYTEOFF
12130 "byte_offset",
12131#endif
12132#ifdef FEAT_CINDENT
12133 "cindent",
12134#endif
12135#ifdef FEAT_CLIENTSERVER
12136 "clientserver",
12137#endif
12138#ifdef FEAT_CLIPBOARD
12139 "clipboard",
12140#endif
12141#ifdef FEAT_CMDL_COMPL
12142 "cmdline_compl",
12143#endif
12144#ifdef FEAT_CMDHIST
12145 "cmdline_hist",
12146#endif
12147#ifdef FEAT_COMMENTS
12148 "comments",
12149#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012150#ifdef FEAT_CONCEAL
12151 "conceal",
12152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153#ifdef FEAT_CRYPT
12154 "cryptv",
12155#endif
12156#ifdef FEAT_CSCOPE
12157 "cscope",
12158#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012159#ifdef FEAT_CURSORBIND
12160 "cursorbind",
12161#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012162#ifdef CURSOR_SHAPE
12163 "cursorshape",
12164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165#ifdef DEBUG
12166 "debug",
12167#endif
12168#ifdef FEAT_CON_DIALOG
12169 "dialog_con",
12170#endif
12171#ifdef FEAT_GUI_DIALOG
12172 "dialog_gui",
12173#endif
12174#ifdef FEAT_DIFF
12175 "diff",
12176#endif
12177#ifdef FEAT_DIGRAPHS
12178 "digraphs",
12179#endif
12180#ifdef FEAT_DND
12181 "dnd",
12182#endif
12183#ifdef FEAT_EMACS_TAGS
12184 "emacs_tags",
12185#endif
12186 "eval", /* always present, of course! */
12187#ifdef FEAT_EX_EXTRA
12188 "ex_extra",
12189#endif
12190#ifdef FEAT_SEARCH_EXTRA
12191 "extra_search",
12192#endif
12193#ifdef FEAT_FKMAP
12194 "farsi",
12195#endif
12196#ifdef FEAT_SEARCHPATH
12197 "file_in_path",
12198#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012199#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012200 "filterpipe",
12201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202#ifdef FEAT_FIND_ID
12203 "find_in_path",
12204#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012205#ifdef FEAT_FLOAT
12206 "float",
12207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208#ifdef FEAT_FOLDING
12209 "folding",
12210#endif
12211#ifdef FEAT_FOOTER
12212 "footer",
12213#endif
12214#if !defined(USE_SYSTEM) && defined(UNIX)
12215 "fork",
12216#endif
12217#ifdef FEAT_GETTEXT
12218 "gettext",
12219#endif
12220#ifdef FEAT_GUI
12221 "gui",
12222#endif
12223#ifdef FEAT_GUI_ATHENA
12224# ifdef FEAT_GUI_NEXTAW
12225 "gui_neXtaw",
12226# else
12227 "gui_athena",
12228# endif
12229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230#ifdef FEAT_GUI_GTK
12231 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012234#ifdef FEAT_GUI_GNOME
12235 "gui_gnome",
12236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237#ifdef FEAT_GUI_MAC
12238 "gui_mac",
12239#endif
12240#ifdef FEAT_GUI_MOTIF
12241 "gui_motif",
12242#endif
12243#ifdef FEAT_GUI_PHOTON
12244 "gui_photon",
12245#endif
12246#ifdef FEAT_GUI_W16
12247 "gui_win16",
12248#endif
12249#ifdef FEAT_GUI_W32
12250 "gui_win32",
12251#endif
12252#ifdef FEAT_HANGULIN
12253 "hangul_input",
12254#endif
12255#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12256 "iconv",
12257#endif
12258#ifdef FEAT_INS_EXPAND
12259 "insert_expand",
12260#endif
12261#ifdef FEAT_JUMPLIST
12262 "jumplist",
12263#endif
12264#ifdef FEAT_KEYMAP
12265 "keymap",
12266#endif
12267#ifdef FEAT_LANGMAP
12268 "langmap",
12269#endif
12270#ifdef FEAT_LIBCALL
12271 "libcall",
12272#endif
12273#ifdef FEAT_LINEBREAK
12274 "linebreak",
12275#endif
12276#ifdef FEAT_LISP
12277 "lispindent",
12278#endif
12279#ifdef FEAT_LISTCMDS
12280 "listcmds",
12281#endif
12282#ifdef FEAT_LOCALMAP
12283 "localmap",
12284#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012285#ifdef FEAT_LUA
12286# ifndef DYNAMIC_LUA
12287 "lua",
12288# endif
12289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290#ifdef FEAT_MENU
12291 "menu",
12292#endif
12293#ifdef FEAT_SESSION
12294 "mksession",
12295#endif
12296#ifdef FEAT_MODIFY_FNAME
12297 "modify_fname",
12298#endif
12299#ifdef FEAT_MOUSE
12300 "mouse",
12301#endif
12302#ifdef FEAT_MOUSESHAPE
12303 "mouseshape",
12304#endif
12305#if defined(UNIX) || defined(VMS)
12306# ifdef FEAT_MOUSE_DEC
12307 "mouse_dec",
12308# endif
12309# ifdef FEAT_MOUSE_GPM
12310 "mouse_gpm",
12311# endif
12312# ifdef FEAT_MOUSE_JSB
12313 "mouse_jsbterm",
12314# endif
12315# ifdef FEAT_MOUSE_NET
12316 "mouse_netterm",
12317# endif
12318# ifdef FEAT_MOUSE_PTERM
12319 "mouse_pterm",
12320# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012321# ifdef FEAT_MOUSE_SGR
12322 "mouse_sgr",
12323# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012324# ifdef FEAT_SYSMOUSE
12325 "mouse_sysmouse",
12326# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012327# ifdef FEAT_MOUSE_URXVT
12328 "mouse_urxvt",
12329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330# ifdef FEAT_MOUSE_XTERM
12331 "mouse_xterm",
12332# endif
12333#endif
12334#ifdef FEAT_MBYTE
12335 "multi_byte",
12336#endif
12337#ifdef FEAT_MBYTE_IME
12338 "multi_byte_ime",
12339#endif
12340#ifdef FEAT_MULTI_LANG
12341 "multi_lang",
12342#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012343#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012344#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012345 "mzscheme",
12346#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348#ifdef FEAT_OLE
12349 "ole",
12350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351#ifdef FEAT_PATH_EXTRA
12352 "path_extra",
12353#endif
12354#ifdef FEAT_PERL
12355#ifndef DYNAMIC_PERL
12356 "perl",
12357#endif
12358#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012359#ifdef FEAT_PERSISTENT_UNDO
12360 "persistent_undo",
12361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362#ifdef FEAT_PYTHON
12363#ifndef DYNAMIC_PYTHON
12364 "python",
12365#endif
12366#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012367#ifdef FEAT_PYTHON3
12368#ifndef DYNAMIC_PYTHON3
12369 "python3",
12370#endif
12371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372#ifdef FEAT_POSTSCRIPT
12373 "postscript",
12374#endif
12375#ifdef FEAT_PRINTER
12376 "printer",
12377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012378#ifdef FEAT_PROFILE
12379 "profile",
12380#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012381#ifdef FEAT_RELTIME
12382 "reltime",
12383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384#ifdef FEAT_QUICKFIX
12385 "quickfix",
12386#endif
12387#ifdef FEAT_RIGHTLEFT
12388 "rightleft",
12389#endif
12390#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12391 "ruby",
12392#endif
12393#ifdef FEAT_SCROLLBIND
12394 "scrollbind",
12395#endif
12396#ifdef FEAT_CMDL_INFO
12397 "showcmd",
12398 "cmdline_info",
12399#endif
12400#ifdef FEAT_SIGNS
12401 "signs",
12402#endif
12403#ifdef FEAT_SMARTINDENT
12404 "smartindent",
12405#endif
12406#ifdef FEAT_SNIFF
12407 "sniff",
12408#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012409#ifdef STARTUPTIME
12410 "startuptime",
12411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412#ifdef FEAT_STL_OPT
12413 "statusline",
12414#endif
12415#ifdef FEAT_SUN_WORKSHOP
12416 "sun_workshop",
12417#endif
12418#ifdef FEAT_NETBEANS_INTG
12419 "netbeans_intg",
12420#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012421#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012422 "spell",
12423#endif
12424#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 "syntax",
12426#endif
12427#if defined(USE_SYSTEM) || !defined(UNIX)
12428 "system",
12429#endif
12430#ifdef FEAT_TAG_BINS
12431 "tag_binary",
12432#endif
12433#ifdef FEAT_TAG_OLDSTATIC
12434 "tag_old_static",
12435#endif
12436#ifdef FEAT_TAG_ANYWHITE
12437 "tag_any_white",
12438#endif
12439#ifdef FEAT_TCL
12440# ifndef DYNAMIC_TCL
12441 "tcl",
12442# endif
12443#endif
12444#ifdef TERMINFO
12445 "terminfo",
12446#endif
12447#ifdef FEAT_TERMRESPONSE
12448 "termresponse",
12449#endif
12450#ifdef FEAT_TEXTOBJ
12451 "textobjects",
12452#endif
12453#ifdef HAVE_TGETENT
12454 "tgetent",
12455#endif
12456#ifdef FEAT_TITLE
12457 "title",
12458#endif
12459#ifdef FEAT_TOOLBAR
12460 "toolbar",
12461#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012462#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12463 "unnamedplus",
12464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465#ifdef FEAT_USR_CMDS
12466 "user-commands", /* was accidentally included in 5.4 */
12467 "user_commands",
12468#endif
12469#ifdef FEAT_VIMINFO
12470 "viminfo",
12471#endif
12472#ifdef FEAT_VERTSPLIT
12473 "vertsplit",
12474#endif
12475#ifdef FEAT_VIRTUALEDIT
12476 "virtualedit",
12477#endif
12478#ifdef FEAT_VISUAL
12479 "visual",
12480#endif
12481#ifdef FEAT_VISUALEXTRA
12482 "visualextra",
12483#endif
12484#ifdef FEAT_VREPLACE
12485 "vreplace",
12486#endif
12487#ifdef FEAT_WILDIGN
12488 "wildignore",
12489#endif
12490#ifdef FEAT_WILDMENU
12491 "wildmenu",
12492#endif
12493#ifdef FEAT_WINDOWS
12494 "windows",
12495#endif
12496#ifdef FEAT_WAK
12497 "winaltkeys",
12498#endif
12499#ifdef FEAT_WRITEBACKUP
12500 "writebackup",
12501#endif
12502#ifdef FEAT_XIM
12503 "xim",
12504#endif
12505#ifdef FEAT_XFONTSET
12506 "xfontset",
12507#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012508#ifdef FEAT_XPM_W32
12509 "xpm_w32",
12510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511#ifdef USE_XSMP
12512 "xsmp",
12513#endif
12514#ifdef USE_XSMP_INTERACT
12515 "xsmp_interact",
12516#endif
12517#ifdef FEAT_XCLIPBOARD
12518 "xterm_clipboard",
12519#endif
12520#ifdef FEAT_XTERM_SAVE
12521 "xterm_save",
12522#endif
12523#if defined(UNIX) && defined(FEAT_X11)
12524 "X11",
12525#endif
12526 NULL
12527 };
12528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 for (i = 0; has_list[i] != NULL; ++i)
12531 if (STRICMP(name, has_list[i]) == 0)
12532 {
12533 n = TRUE;
12534 break;
12535 }
12536
12537 if (n == FALSE)
12538 {
12539 if (STRNICMP(name, "patch", 5) == 0)
12540 n = has_patch(atoi((char *)name + 5));
12541 else if (STRICMP(name, "vim_starting") == 0)
12542 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012543#ifdef FEAT_MBYTE
12544 else if (STRICMP(name, "multi_byte_encoding") == 0)
12545 n = has_mbyte;
12546#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012547#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12548 else if (STRICMP(name, "balloon_multiline") == 0)
12549 n = multiline_balloon_available();
12550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551#ifdef DYNAMIC_TCL
12552 else if (STRICMP(name, "tcl") == 0)
12553 n = tcl_enabled(FALSE);
12554#endif
12555#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12556 else if (STRICMP(name, "iconv") == 0)
12557 n = iconv_enabled(FALSE);
12558#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012559#ifdef DYNAMIC_LUA
12560 else if (STRICMP(name, "lua") == 0)
12561 n = lua_enabled(FALSE);
12562#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012563#ifdef DYNAMIC_MZSCHEME
12564 else if (STRICMP(name, "mzscheme") == 0)
12565 n = mzscheme_enabled(FALSE);
12566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567#ifdef DYNAMIC_RUBY
12568 else if (STRICMP(name, "ruby") == 0)
12569 n = ruby_enabled(FALSE);
12570#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012571#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572#ifdef DYNAMIC_PYTHON
12573 else if (STRICMP(name, "python") == 0)
12574 n = python_enabled(FALSE);
12575#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012576#endif
12577#ifdef FEAT_PYTHON3
12578#ifdef DYNAMIC_PYTHON3
12579 else if (STRICMP(name, "python3") == 0)
12580 n = python3_enabled(FALSE);
12581#endif
12582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583#ifdef DYNAMIC_PERL
12584 else if (STRICMP(name, "perl") == 0)
12585 n = perl_enabled(FALSE);
12586#endif
12587#ifdef FEAT_GUI
12588 else if (STRICMP(name, "gui_running") == 0)
12589 n = (gui.in_use || gui.starting);
12590# ifdef FEAT_GUI_W32
12591 else if (STRICMP(name, "gui_win32s") == 0)
12592 n = gui_is_win32s();
12593# endif
12594# ifdef FEAT_BROWSE
12595 else if (STRICMP(name, "browse") == 0)
12596 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12597# endif
12598#endif
12599#ifdef FEAT_SYN_HL
12600 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012601 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602#endif
12603#if defined(WIN3264)
12604 else if (STRICMP(name, "win95") == 0)
12605 n = mch_windows95();
12606#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012607#ifdef FEAT_NETBEANS_INTG
12608 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012609 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 }
12612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614}
12615
12616/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012617 * "has_key()" function
12618 */
12619 static void
12620f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012621 typval_T *argvars;
12622 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012623{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012624 if (argvars[0].v_type != VAR_DICT)
12625 {
12626 EMSG(_(e_dictreq));
12627 return;
12628 }
12629 if (argvars[0].vval.v_dict == NULL)
12630 return;
12631
12632 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012633 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012634}
12635
12636/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012637 * "haslocaldir()" function
12638 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012639 static void
12640f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012641 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012642 typval_T *rettv;
12643{
12644 rettv->vval.v_number = (curwin->w_localdir != NULL);
12645}
12646
12647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648 * "hasmapto()" function
12649 */
12650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012652 typval_T *argvars;
12653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654{
12655 char_u *name;
12656 char_u *mode;
12657 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012658 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012661 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 mode = (char_u *)"nvo";
12663 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012665 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012666 if (argvars[2].v_type != VAR_UNKNOWN)
12667 abbr = get_tv_number(&argvars[2]);
12668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669
Bram Moolenaar2c932302006-03-18 21:42:09 +000012670 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674}
12675
12676/*
12677 * "histadd()" function
12678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683{
12684#ifdef FEAT_CMDHIST
12685 int histype;
12686 char_u *str;
12687 char_u buf[NUMBUFLEN];
12688#endif
12689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012690 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 if (check_restricted() || check_secure())
12692 return;
12693#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12695 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 if (histype >= 0)
12697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 if (*str != NUL)
12700 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012701 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 return;
12705 }
12706 }
12707#endif
12708}
12709
12710/*
12711 * "histdel()" function
12712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012715 typval_T *argvars UNUSED;
12716 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
12718#ifdef FEAT_CMDHIST
12719 int n;
12720 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012723 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12724 if (str == NULL)
12725 n = 0;
12726 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012729 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 else
12734 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 get_tv_string_buf(&argvars[1], buf));
12737 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738#endif
12739}
12740
12741/*
12742 * "histget()" function
12743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748{
12749#ifdef FEAT_CMDHIST
12750 int type;
12751 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12755 if (str == NULL)
12756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012758 {
12759 type = get_histtype(str);
12760 if (argvars[1].v_type == VAR_UNKNOWN)
12761 idx = get_history_idx(type);
12762 else
12763 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12764 /* -1 on type error */
12765 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771}
12772
12773/*
12774 * "histnr()" function
12775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012778 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
12781 int i;
12782
12783#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784 char_u *history = get_tv_string_chk(&argvars[0]);
12785
12786 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 if (i >= HIST_CMD && i < HIST_COUNT)
12788 i = get_history_idx(i);
12789 else
12790#endif
12791 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793}
12794
12795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 * "highlightID(name)" function
12797 */
12798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012800 typval_T *argvars;
12801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804}
12805
12806/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012807 * "highlight_exists()" function
12808 */
12809 static void
12810f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *argvars;
12812 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012813{
12814 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12815}
12816
12817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 * "hostname()" function
12819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012822 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824{
12825 char_u hostname[256];
12826
12827 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->v_type = VAR_STRING;
12829 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830}
12831
12832/*
12833 * iconv() function
12834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839{
12840#ifdef FEAT_MBYTE
12841 char_u buf1[NUMBUFLEN];
12842 char_u buf2[NUMBUFLEN];
12843 char_u *from, *to, *str;
12844 vimconv_T vimconv;
12845#endif
12846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012847 rettv->v_type = VAR_STRING;
12848 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849
12850#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 str = get_tv_string(&argvars[0]);
12852 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12853 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 vimconv.vc_type = CONV_NONE;
12855 convert_setup(&vimconv, from, to);
12856
12857 /* If the encodings are equal, no conversion needed. */
12858 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862
12863 convert_setup(&vimconv, NULL, NULL);
12864 vim_free(from);
12865 vim_free(to);
12866#endif
12867}
12868
12869/*
12870 * "indent()" function
12871 */
12872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012874 typval_T *argvars;
12875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876{
12877 linenr_T lnum;
12878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884}
12885
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012886/*
12887 * "index()" function
12888 */
12889 static void
12890f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012891 typval_T *argvars;
12892 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012893{
Bram Moolenaar33570922005-01-25 22:26:29 +000012894 list_T *l;
12895 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012896 long idx = 0;
12897 int ic = FALSE;
12898
12899 rettv->vval.v_number = -1;
12900 if (argvars[0].v_type != VAR_LIST)
12901 {
12902 EMSG(_(e_listreq));
12903 return;
12904 }
12905 l = argvars[0].vval.v_list;
12906 if (l != NULL)
12907 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012908 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012909 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012911 int error = FALSE;
12912
Bram Moolenaar758711c2005-02-02 23:11:38 +000012913 /* Start at specified item. Use the cached index that list_find()
12914 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012915 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012916 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012917 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012918 ic = get_tv_number_chk(&argvars[3], &error);
12919 if (error)
12920 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012921 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012922
Bram Moolenaar758711c2005-02-02 23:11:38 +000012923 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012924 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012925 {
12926 rettv->vval.v_number = idx;
12927 break;
12928 }
12929 }
12930}
12931
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932static int inputsecret_flag = 0;
12933
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012934static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12935
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012937 * This function is used by f_input() and f_inputdialog() functions. The third
12938 * argument to f_input() specifies the type of completion to use at the
12939 * prompt. The third argument to f_inputdialog() specifies the value to return
12940 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 */
12942 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012943get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012944 typval_T *argvars;
12945 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012946 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012948 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 char_u *p = NULL;
12950 int c;
12951 char_u buf[NUMBUFLEN];
12952 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012953 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012954 int xp_type = EXPAND_NOTHING;
12955 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012958 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959
12960#ifdef NO_CONSOLE_INPUT
12961 /* While starting up, there is no place to enter text. */
12962 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964#endif
12965
12966 cmd_silent = FALSE; /* Want to see the prompt. */
12967 if (prompt != NULL)
12968 {
12969 /* Only the part of the message after the last NL is considered as
12970 * prompt for the command line */
12971 p = vim_strrchr(prompt, '\n');
12972 if (p == NULL)
12973 p = prompt;
12974 else
12975 {
12976 ++p;
12977 c = *p;
12978 *p = NUL;
12979 msg_start();
12980 msg_clr_eos();
12981 msg_puts_attr(prompt, echo_attr);
12982 msg_didout = FALSE;
12983 msg_starthere();
12984 *p = c;
12985 }
12986 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012988 if (argvars[1].v_type != VAR_UNKNOWN)
12989 {
12990 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12991 if (defstr != NULL)
12992 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012994 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012995 {
12996 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012997 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012998 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012999
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013000 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013001 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013002
Bram Moolenaar4463f292005-09-25 22:20:24 +000013003 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13004 if (xp_name == NULL)
13005 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013006
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013007 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013008
Bram Moolenaar4463f292005-09-25 22:20:24 +000013009 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13010 &xp_arg) == FAIL)
13011 return;
13012 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013013 }
13014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 if (defstr != NULL)
13016 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013017 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13018 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013019 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013020 && argvars[1].v_type != VAR_UNKNOWN
13021 && argvars[2].v_type != VAR_UNKNOWN)
13022 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13023 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013024
13025 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013027 /* since the user typed this, no need to wait for return */
13028 need_wait_return = FALSE;
13029 msg_didout = FALSE;
13030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 cmd_silent = cmd_silent_save;
13032}
13033
13034/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013035 * "input()" function
13036 * Also handles inputsecret() when inputsecret is set.
13037 */
13038 static void
13039f_input(argvars, rettv)
13040 typval_T *argvars;
13041 typval_T *rettv;
13042{
13043 get_user_input(argvars, rettv, FALSE);
13044}
13045
13046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 * "inputdialog()" function
13048 */
13049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013053{
13054#if defined(FEAT_GUI_TEXTDIALOG)
13055 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13056 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13057 {
13058 char_u *message;
13059 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 message = get_tv_string_chk(&argvars[0]);
13063 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013064 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013065 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066 else
13067 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068 if (message != NULL && defstr != NULL
13069 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013070 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 else
13073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013074 if (message != NULL && defstr != NULL
13075 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013076 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->vval.v_string = vim_strsave(
13078 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 }
13084 else
13085#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013086 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087}
13088
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013089/*
13090 * "inputlist()" function
13091 */
13092 static void
13093f_inputlist(argvars, rettv)
13094 typval_T *argvars;
13095 typval_T *rettv;
13096{
13097 listitem_T *li;
13098 int selected;
13099 int mouse_used;
13100
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013101#ifdef NO_CONSOLE_INPUT
13102 /* While starting up, there is no place to enter text. */
13103 if (no_console_input())
13104 return;
13105#endif
13106 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13107 {
13108 EMSG2(_(e_listarg), "inputlist()");
13109 return;
13110 }
13111
13112 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013113 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013114 lines_left = Rows; /* avoid more prompt */
13115 msg_scroll = TRUE;
13116 msg_clr_eos();
13117
13118 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13119 {
13120 msg_puts(get_tv_string(&li->li_tv));
13121 msg_putchar('\n');
13122 }
13123
13124 /* Ask for choice. */
13125 selected = prompt_for_number(&mouse_used);
13126 if (mouse_used)
13127 selected -= lines_left;
13128
13129 rettv->vval.v_number = selected;
13130}
13131
13132
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13134
13135/*
13136 * "inputrestore()" function
13137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013140 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142{
13143 if (ga_userinput.ga_len > 0)
13144 {
13145 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13147 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013148 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 }
13150 else if (p_verbose > 1)
13151 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013152 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154 }
13155}
13156
13157/*
13158 * "inputsave()" function
13159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013165 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 if (ga_grow(&ga_userinput, 1) == OK)
13167 {
13168 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13169 + ga_userinput.ga_len);
13170 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013171 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013172 }
13173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175}
13176
13177/*
13178 * "inputsecret()" function
13179 */
13180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013182 typval_T *argvars;
13183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184{
13185 ++cmdline_star;
13186 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 --cmdline_star;
13189 --inputsecret_flag;
13190}
13191
13192/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013193 * "insert()" function
13194 */
13195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 typval_T *argvars;
13198 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013199{
13200 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013201 listitem_T *item;
13202 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013204
13205 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013207 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013208 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013209 {
13210 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 before = get_tv_number_chk(&argvars[2], &error);
13212 if (error)
13213 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013214
Bram Moolenaar758711c2005-02-02 23:11:38 +000013215 if (before == l->lv_len)
13216 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217 else
13218 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013219 item = list_find(l, before);
13220 if (item == NULL)
13221 {
13222 EMSGN(_(e_listidx), before);
13223 l = NULL;
13224 }
13225 }
13226 if (l != NULL)
13227 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013228 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013229 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013230 }
13231 }
13232}
13233
13234/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013235 * "invert(expr)" function
13236 */
13237 static void
13238f_invert(argvars, rettv)
13239 typval_T *argvars;
13240 typval_T *rettv;
13241{
13242 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13243}
13244
13245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 * "isdirectory()" function
13247 */
13248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *argvars;
13251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013253 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254}
13255
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013256/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013257 * "islocked()" function
13258 */
13259 static void
13260f_islocked(argvars, rettv)
13261 typval_T *argvars;
13262 typval_T *rettv;
13263{
13264 lval_T lv;
13265 char_u *end;
13266 dictitem_T *di;
13267
13268 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013269 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13270 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013271 if (end != NULL && lv.ll_name != NULL)
13272 {
13273 if (*end != NUL)
13274 EMSG(_(e_trailing));
13275 else
13276 {
13277 if (lv.ll_tv == NULL)
13278 {
13279 if (check_changedtick(lv.ll_name))
13280 rettv->vval.v_number = 1; /* always locked */
13281 else
13282 {
13283 di = find_var(lv.ll_name, NULL);
13284 if (di != NULL)
13285 {
13286 /* Consider a variable locked when:
13287 * 1. the variable itself is locked
13288 * 2. the value of the variable is locked.
13289 * 3. the List or Dict value is locked.
13290 */
13291 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13292 || tv_islocked(&di->di_tv));
13293 }
13294 }
13295 }
13296 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013297 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013298 else if (lv.ll_newkey != NULL)
13299 EMSG2(_(e_dictkey), lv.ll_newkey);
13300 else if (lv.ll_list != NULL)
13301 /* List item. */
13302 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13303 else
13304 /* Dictionary item. */
13305 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13306 }
13307 }
13308
13309 clear_lval(&lv);
13310}
13311
Bram Moolenaar33570922005-01-25 22:26:29 +000013312static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013313
13314/*
13315 * Turn a dict into a list:
13316 * "what" == 0: list of keys
13317 * "what" == 1: list of values
13318 * "what" == 2: list of items
13319 */
13320 static void
13321dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013322 typval_T *argvars;
13323 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013324 int what;
13325{
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 list_T *l2;
13327 dictitem_T *di;
13328 hashitem_T *hi;
13329 listitem_T *li;
13330 listitem_T *li2;
13331 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013332 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013333
Bram Moolenaar8c711452005-01-14 21:53:12 +000013334 if (argvars[0].v_type != VAR_DICT)
13335 {
13336 EMSG(_(e_dictreq));
13337 return;
13338 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013339 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013340 return;
13341
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013342 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013343 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013344
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013345 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013347 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013348 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013350 --todo;
13351 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013352
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013353 li = listitem_alloc();
13354 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013355 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013356 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013357
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013358 if (what == 0)
13359 {
13360 /* keys() */
13361 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013362 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013363 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13364 }
13365 else if (what == 1)
13366 {
13367 /* values() */
13368 copy_tv(&di->di_tv, &li->li_tv);
13369 }
13370 else
13371 {
13372 /* items() */
13373 l2 = list_alloc();
13374 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013375 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013376 li->li_tv.vval.v_list = l2;
13377 if (l2 == NULL)
13378 break;
13379 ++l2->lv_refcount;
13380
13381 li2 = listitem_alloc();
13382 if (li2 == NULL)
13383 break;
13384 list_append(l2, li2);
13385 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013386 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013387 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13388
13389 li2 = listitem_alloc();
13390 if (li2 == NULL)
13391 break;
13392 list_append(l2, li2);
13393 copy_tv(&di->di_tv, &li2->li_tv);
13394 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013395 }
13396 }
13397}
13398
13399/*
13400 * "items(dict)" function
13401 */
13402 static void
13403f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013404 typval_T *argvars;
13405 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013406{
13407 dict_list(argvars, rettv, 2);
13408}
13409
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411 * "join()" function
13412 */
13413 static void
13414f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013415 typval_T *argvars;
13416 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013417{
13418 garray_T ga;
13419 char_u *sep;
13420
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013421 if (argvars[0].v_type != VAR_LIST)
13422 {
13423 EMSG(_(e_listreq));
13424 return;
13425 }
13426 if (argvars[0].vval.v_list == NULL)
13427 return;
13428 if (argvars[1].v_type == VAR_UNKNOWN)
13429 sep = (char_u *)" ";
13430 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013432
13433 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434
13435 if (sep != NULL)
13436 {
13437 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013438 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 ga_append(&ga, NUL);
13440 rettv->vval.v_string = (char_u *)ga.ga_data;
13441 }
13442 else
13443 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013444}
13445
13446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013447 * "keys()" function
13448 */
13449 static void
13450f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013451 typval_T *argvars;
13452 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013453{
13454 dict_list(argvars, rettv, 0);
13455}
13456
13457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 * "last_buffer_nr()" function.
13459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013461f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013462 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464{
13465 int n = 0;
13466 buf_T *buf;
13467
13468 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13469 if (n < buf->b_fnum)
13470 n = buf->b_fnum;
13471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013473}
13474
13475/*
13476 * "len()" function
13477 */
13478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013479f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 typval_T *argvars;
13481 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013482{
13483 switch (argvars[0].v_type)
13484 {
13485 case VAR_STRING:
13486 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013487 rettv->vval.v_number = (varnumber_T)STRLEN(
13488 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013489 break;
13490 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013493 case VAR_DICT:
13494 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13495 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013496 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013497 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013498 break;
13499 }
13500}
13501
Bram Moolenaar33570922005-01-25 22:26:29 +000013502static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013503
13504 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013506 typval_T *argvars;
13507 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013508 int type;
13509{
13510#ifdef FEAT_LIBCALL
13511 char_u *string_in;
13512 char_u **string_result;
13513 int nr_result;
13514#endif
13515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013517 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013518 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013519
13520 if (check_restricted() || check_secure())
13521 return;
13522
13523#ifdef FEAT_LIBCALL
13524 /* The first two args must be strings, otherwise its meaningless */
13525 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13526 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013527 string_in = NULL;
13528 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013529 string_in = argvars[2].vval.v_string;
13530 if (type == VAR_NUMBER)
13531 string_result = NULL;
13532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013534 if (mch_libcall(argvars[0].vval.v_string,
13535 argvars[1].vval.v_string,
13536 string_in,
13537 argvars[2].vval.v_number,
13538 string_result,
13539 &nr_result) == OK
13540 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013541 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013542 }
13543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544}
13545
13546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013547 * "libcall()" function
13548 */
13549 static void
13550f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013551 typval_T *argvars;
13552 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013553{
13554 libcall_common(argvars, rettv, VAR_STRING);
13555}
13556
13557/*
13558 * "libcallnr()" function
13559 */
13560 static void
13561f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013562 typval_T *argvars;
13563 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013564{
13565 libcall_common(argvars, rettv, VAR_NUMBER);
13566}
13567
13568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 * "line(string)" function
13570 */
13571 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 typval_T *argvars;
13574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575{
13576 linenr_T lnum = 0;
13577 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013578 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013580 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 if (fp != NULL)
13582 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584}
13585
13586/*
13587 * "line2byte(lnum)" function
13588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593{
13594#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596#else
13597 linenr_T lnum;
13598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013599 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013603 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13604 if (rettv->vval.v_number >= 0)
13605 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606#endif
13607}
13608
13609/*
13610 * "lispindent(lnum)" function
13611 */
13612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013614 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
13617#ifdef FEAT_LISP
13618 pos_T pos;
13619 linenr_T lnum;
13620
13621 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13624 {
13625 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 curwin->w_cursor = pos;
13628 }
13629 else
13630#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632}
13633
13634/*
13635 * "localtime()" function
13636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013638f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643}
13644
Bram Moolenaar33570922005-01-25 22:26:29 +000013645static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646
13647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 int exact;
13652{
13653 char_u *keys;
13654 char_u *which;
13655 char_u buf[NUMBUFLEN];
13656 char_u *keys_buf = NULL;
13657 char_u *rhs;
13658 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013659 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013660 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013661 mapblock_T *mp;
13662 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663
13664 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 rettv->v_type = VAR_STRING;
13666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013668 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 if (*keys == NUL)
13670 return;
13671
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013674 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013675 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013676 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013677 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013678 if (argvars[3].v_type != VAR_UNKNOWN)
13679 get_dict = get_tv_number(&argvars[3]);
13680 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 else
13683 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013684 if (which == NULL)
13685 return;
13686
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 mode = get_map_mode(&which, 0);
13688
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013689 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013690 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013692
13693 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013695 /* Return a string. */
13696 if (rhs != NULL)
13697 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698
Bram Moolenaarbd743252010-10-20 21:23:33 +020013699 }
13700 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13701 {
13702 /* Return a dictionary. */
13703 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13704 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13705 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706
Bram Moolenaarbd743252010-10-20 21:23:33 +020013707 dict_add_nr_str(dict, "lhs", 0L, lhs);
13708 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13709 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13710 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13711 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13712 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13713 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13714 dict_add_nr_str(dict, "mode", 0L, mapmode);
13715
13716 vim_free(lhs);
13717 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 }
13719}
13720
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013721#ifdef FEAT_FLOAT
13722/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013723 * "log()" function
13724 */
13725 static void
13726f_log(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 = log(f);
13735 else
13736 rettv->vval.v_float = 0.0;
13737}
13738
13739/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013740 * "log10()" function
13741 */
13742 static void
13743f_log10(argvars, rettv)
13744 typval_T *argvars;
13745 typval_T *rettv;
13746{
13747 float_T f;
13748
13749 rettv->v_type = VAR_FLOAT;
13750 if (get_float_arg(argvars, &f) == OK)
13751 rettv->vval.v_float = log10(f);
13752 else
13753 rettv->vval.v_float = 0.0;
13754}
13755#endif
13756
Bram Moolenaar1dced572012-04-05 16:54:08 +020013757#ifdef FEAT_LUA
13758/*
13759 * "luaeval()" function
13760 */
13761 static void
13762f_luaeval(argvars, rettv)
13763 typval_T *argvars;
13764 typval_T *rettv;
13765{
13766 char_u *str;
13767 char_u buf[NUMBUFLEN];
13768
13769 str = get_tv_string_buf(&argvars[0], buf);
13770 do_luaeval(str, argvars + 1, rettv);
13771}
13772#endif
13773
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013775 * "map()" function
13776 */
13777 static void
13778f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *argvars;
13780 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013781{
13782 filter_map(argvars, rettv, TRUE);
13783}
13784
13785/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 */
13788 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013789f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *argvars;
13791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794}
13795
13796/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013797 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 */
13799 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013800f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013803{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013804 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805}
13806
Bram Moolenaar33570922005-01-25 22:26:29 +000013807static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808
13809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013810find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013811 typval_T *argvars;
13812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 int type;
13814{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013815 char_u *str = NULL;
13816 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 char_u *pat;
13818 regmatch_T regmatch;
13819 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013820 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 char_u *save_cpo;
13822 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013823 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013824 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013825 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 list_T *l = NULL;
13827 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013828 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013829 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830
13831 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13832 save_cpo = p_cpo;
13833 p_cpo = (char_u *)"";
13834
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013835 rettv->vval.v_number = -1;
13836 if (type == 3)
13837 {
13838 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013839 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013840 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013841 }
13842 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844 rettv->v_type = VAR_STRING;
13845 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013848 if (argvars[0].v_type == VAR_LIST)
13849 {
13850 if ((l = argvars[0].vval.v_list) == NULL)
13851 goto theend;
13852 li = l->lv_first;
13853 }
13854 else
13855 expr = str = get_tv_string(&argvars[0]);
13856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13858 if (pat == NULL)
13859 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013860
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013861 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 int error = FALSE;
13864
13865 start = get_tv_number_chk(&argvars[2], &error);
13866 if (error)
13867 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013868 if (l != NULL)
13869 {
13870 li = list_find(l, start);
13871 if (li == NULL)
13872 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013873 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013874 }
13875 else
13876 {
13877 if (start < 0)
13878 start = 0;
13879 if (start > (long)STRLEN(str))
13880 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013881 /* When "count" argument is there ignore matches before "start",
13882 * otherwise skip part of the string. Differs when pattern is "^"
13883 * or "\<". */
13884 if (argvars[3].v_type != VAR_UNKNOWN)
13885 startcol = start;
13886 else
13887 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013888 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013889
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013890 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891 nth = get_tv_number_chk(&argvars[3], &error);
13892 if (error)
13893 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 }
13895
13896 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13897 if (regmatch.regprog != NULL)
13898 {
13899 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013900
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013901 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013902 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013903 if (l != NULL)
13904 {
13905 if (li == NULL)
13906 {
13907 match = FALSE;
13908 break;
13909 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013910 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013911 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013912 if (str == NULL)
13913 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013914 }
13915
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013916 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013917
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013918 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013919 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013920 if (l == NULL && !match)
13921 break;
13922
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013923 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013924 if (l != NULL)
13925 {
13926 li = li->li_next;
13927 ++idx;
13928 }
13929 else
13930 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013931#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013932 startcol = (colnr_T)(regmatch.startp[0]
13933 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013934#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013935 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013936#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013938 }
13939
13940 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013942 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013944 int i;
13945
13946 /* return list with matched string and submatches */
13947 for (i = 0; i < NSUBEXP; ++i)
13948 {
13949 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013950 {
13951 if (list_append_string(rettv->vval.v_list,
13952 (char_u *)"", 0) == FAIL)
13953 break;
13954 }
13955 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013956 regmatch.startp[i],
13957 (int)(regmatch.endp[i] - regmatch.startp[i]))
13958 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013959 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013960 }
13961 }
13962 else if (type == 2)
13963 {
13964 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 if (l != NULL)
13966 copy_tv(&li->li_tv, rettv);
13967 else
13968 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 }
13971 else if (l != NULL)
13972 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973 else
13974 {
13975 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 (varnumber_T)(regmatch.startp[0] - str);
13978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013981 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 }
13983 }
13984 vim_free(regmatch.regprog);
13985 }
13986
13987theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013988 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989 p_cpo = save_cpo;
13990}
13991
13992/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993 * "match()" function
13994 */
13995 static void
13996f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013997 typval_T *argvars;
13998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013999{
14000 find_some_match(argvars, rettv, 1);
14001}
14002
14003/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014004 * "matchadd()" function
14005 */
14006 static void
14007f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014008 typval_T *argvars UNUSED;
14009 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014010{
14011#ifdef FEAT_SEARCH_EXTRA
14012 char_u buf[NUMBUFLEN];
14013 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14014 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14015 int prio = 10; /* default priority */
14016 int id = -1;
14017 int error = FALSE;
14018
14019 rettv->vval.v_number = -1;
14020
14021 if (grp == NULL || pat == NULL)
14022 return;
14023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014024 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014025 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014026 if (argvars[3].v_type != VAR_UNKNOWN)
14027 id = get_tv_number_chk(&argvars[3], &error);
14028 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014029 if (error == TRUE)
14030 return;
14031 if (id >= 1 && id <= 3)
14032 {
14033 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14034 return;
14035 }
14036
14037 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14038#endif
14039}
14040
14041/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014042 * "matcharg()" function
14043 */
14044 static void
14045f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014046 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014047 typval_T *rettv;
14048{
14049 if (rettv_list_alloc(rettv) == OK)
14050 {
14051#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014052 int id = get_tv_number(&argvars[0]);
14053 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014054
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014055 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014056 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014057 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14058 {
14059 list_append_string(rettv->vval.v_list,
14060 syn_id2name(m->hlg_id), -1);
14061 list_append_string(rettv->vval.v_list, m->pattern, -1);
14062 }
14063 else
14064 {
14065 list_append_string(rettv->vval.v_list, NUL, -1);
14066 list_append_string(rettv->vval.v_list, NUL, -1);
14067 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014068 }
14069#endif
14070 }
14071}
14072
14073/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014074 * "matchdelete()" function
14075 */
14076 static void
14077f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014078 typval_T *argvars UNUSED;
14079 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014080{
14081#ifdef FEAT_SEARCH_EXTRA
14082 rettv->vval.v_number = match_delete(curwin,
14083 (int)get_tv_number(&argvars[0]), TRUE);
14084#endif
14085}
14086
14087/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014088 * "matchend()" function
14089 */
14090 static void
14091f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014092 typval_T *argvars;
14093 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014094{
14095 find_some_match(argvars, rettv, 0);
14096}
14097
14098/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014099 * "matchlist()" function
14100 */
14101 static void
14102f_matchlist(argvars, rettv)
14103 typval_T *argvars;
14104 typval_T *rettv;
14105{
14106 find_some_match(argvars, rettv, 3);
14107}
14108
14109/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014110 * "matchstr()" function
14111 */
14112 static void
14113f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014114 typval_T *argvars;
14115 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014116{
14117 find_some_match(argvars, rettv, 2);
14118}
14119
Bram Moolenaar33570922005-01-25 22:26:29 +000014120static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014121
14122 static void
14123max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014124 typval_T *argvars;
14125 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014126 int domax;
14127{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014128 long n = 0;
14129 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014131
14132 if (argvars[0].v_type == VAR_LIST)
14133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 list_T *l;
14135 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014136
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014137 l = argvars[0].vval.v_list;
14138 if (l != NULL)
14139 {
14140 li = l->lv_first;
14141 if (li != NULL)
14142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014144 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014145 {
14146 li = li->li_next;
14147 if (li == NULL)
14148 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014150 if (domax ? i > n : i < n)
14151 n = i;
14152 }
14153 }
14154 }
14155 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014156 else if (argvars[0].v_type == VAR_DICT)
14157 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014159 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014160 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014161 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014162
14163 d = argvars[0].vval.v_dict;
14164 if (d != NULL)
14165 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014166 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014167 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014168 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014169 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014170 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014171 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014173 if (first)
14174 {
14175 n = i;
14176 first = FALSE;
14177 }
14178 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014179 n = i;
14180 }
14181 }
14182 }
14183 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014184 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014185 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014186 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014187}
14188
14189/*
14190 * "max()" function
14191 */
14192 static void
14193f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014194 typval_T *argvars;
14195 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014196{
14197 max_min(argvars, rettv, TRUE);
14198}
14199
14200/*
14201 * "min()" function
14202 */
14203 static void
14204f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014205 typval_T *argvars;
14206 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014207{
14208 max_min(argvars, rettv, FALSE);
14209}
14210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014211static int mkdir_recurse __ARGS((char_u *dir, int prot));
14212
14213/*
14214 * Create the directory in which "dir" is located, and higher levels when
14215 * needed.
14216 */
14217 static int
14218mkdir_recurse(dir, prot)
14219 char_u *dir;
14220 int prot;
14221{
14222 char_u *p;
14223 char_u *updir;
14224 int r = FAIL;
14225
14226 /* Get end of directory name in "dir".
14227 * We're done when it's "/" or "c:/". */
14228 p = gettail_sep(dir);
14229 if (p <= get_past_head(dir))
14230 return OK;
14231
14232 /* If the directory exists we're done. Otherwise: create it.*/
14233 updir = vim_strnsave(dir, (int)(p - dir));
14234 if (updir == NULL)
14235 return FAIL;
14236 if (mch_isdir(updir))
14237 r = OK;
14238 else if (mkdir_recurse(updir, prot) == OK)
14239 r = vim_mkdir_emsg(updir, prot);
14240 vim_free(updir);
14241 return r;
14242}
14243
14244#ifdef vim_mkdir
14245/*
14246 * "mkdir()" function
14247 */
14248 static void
14249f_mkdir(argvars, rettv)
14250 typval_T *argvars;
14251 typval_T *rettv;
14252{
14253 char_u *dir;
14254 char_u buf[NUMBUFLEN];
14255 int prot = 0755;
14256
14257 rettv->vval.v_number = FAIL;
14258 if (check_restricted() || check_secure())
14259 return;
14260
14261 dir = get_tv_string_buf(&argvars[0], buf);
14262 if (argvars[1].v_type != VAR_UNKNOWN)
14263 {
14264 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 prot = get_tv_number_chk(&argvars[2], NULL);
14266 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014267 mkdir_recurse(dir, prot);
14268 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014270}
14271#endif
14272
Bram Moolenaar0d660222005-01-07 21:51:51 +000014273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 * "mode()" function
14275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014278 typval_T *argvars;
14279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014281 char_u buf[3];
14282
14283 buf[1] = NUL;
14284 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285
14286#ifdef FEAT_VISUAL
14287 if (VIsual_active)
14288 {
14289 if (VIsual_select)
14290 buf[0] = VIsual_mode + 's' - 'v';
14291 else
14292 buf[0] = VIsual_mode;
14293 }
14294 else
14295#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014296 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14297 || State == CONFIRM)
14298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014300 if (State == ASKMORE)
14301 buf[1] = 'm';
14302 else if (State == CONFIRM)
14303 buf[1] = '?';
14304 }
14305 else if (State == EXTERNCMD)
14306 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 else if (State & INSERT)
14308 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014309#ifdef FEAT_VREPLACE
14310 if (State & VREPLACE_FLAG)
14311 {
14312 buf[0] = 'R';
14313 buf[1] = 'v';
14314 }
14315 else
14316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 if (State & REPLACE_FLAG)
14318 buf[0] = 'R';
14319 else
14320 buf[0] = 'i';
14321 }
14322 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014325 if (exmode_active)
14326 buf[1] = 'v';
14327 }
14328 else if (exmode_active)
14329 {
14330 buf[0] = 'c';
14331 buf[1] = 'e';
14332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014336 if (finish_op)
14337 buf[1] = 'o';
14338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014340 /* Clear out the minor mode when the argument is not a non-zero number or
14341 * non-empty string. */
14342 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014343 buf[1] = NUL;
14344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_string = vim_strsave(buf);
14346 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347}
14348
Bram Moolenaar429fa852013-04-15 12:27:36 +020014349#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014350/*
14351 * "mzeval()" function
14352 */
14353 static void
14354f_mzeval(argvars, rettv)
14355 typval_T *argvars;
14356 typval_T *rettv;
14357{
14358 char_u *str;
14359 char_u buf[NUMBUFLEN];
14360
14361 str = get_tv_string_buf(&argvars[0], buf);
14362 do_mzeval(str, rettv);
14363}
Bram Moolenaar75676462013-01-30 14:55:42 +010014364
14365 void
14366mzscheme_call_vim(name, args, rettv)
14367 char_u *name;
14368 typval_T *args;
14369 typval_T *rettv;
14370{
14371 typval_T argvars[3];
14372
14373 argvars[0].v_type = VAR_STRING;
14374 argvars[0].vval.v_string = name;
14375 copy_tv(args, &argvars[1]);
14376 argvars[2].v_type = VAR_UNKNOWN;
14377 f_call(argvars, rettv);
14378 clear_tv(&argvars[1]);
14379}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014380#endif
14381
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014383 * "nextnonblank()" function
14384 */
14385 static void
14386f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014387 typval_T *argvars;
14388 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014389{
14390 linenr_T lnum;
14391
14392 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014395 {
14396 lnum = 0;
14397 break;
14398 }
14399 if (*skipwhite(ml_get(lnum)) != NUL)
14400 break;
14401 }
14402 rettv->vval.v_number = lnum;
14403}
14404
14405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 * "nr2char()" function
14407 */
14408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 typval_T *argvars;
14411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412{
14413 char_u buf[NUMBUFLEN];
14414
14415#ifdef FEAT_MBYTE
14416 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014417 {
14418 int utf8 = 0;
14419
14420 if (argvars[1].v_type != VAR_UNKNOWN)
14421 utf8 = get_tv_number_chk(&argvars[1], NULL);
14422 if (utf8)
14423 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14424 else
14425 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 else
14428#endif
14429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 buf[1] = NUL;
14432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 rettv->v_type = VAR_STRING;
14434 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014435}
14436
14437/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014438 * "or(expr, expr)" function
14439 */
14440 static void
14441f_or(argvars, rettv)
14442 typval_T *argvars;
14443 typval_T *rettv;
14444{
14445 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14446 | get_tv_number_chk(&argvars[1], NULL);
14447}
14448
14449/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014450 * "pathshorten()" function
14451 */
14452 static void
14453f_pathshorten(argvars, rettv)
14454 typval_T *argvars;
14455 typval_T *rettv;
14456{
14457 char_u *p;
14458
14459 rettv->v_type = VAR_STRING;
14460 p = get_tv_string_chk(&argvars[0]);
14461 if (p == NULL)
14462 rettv->vval.v_string = NULL;
14463 else
14464 {
14465 p = vim_strsave(p);
14466 rettv->vval.v_string = p;
14467 if (p != NULL)
14468 shorten_dir(p);
14469 }
14470}
14471
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014472#ifdef FEAT_FLOAT
14473/*
14474 * "pow()" function
14475 */
14476 static void
14477f_pow(argvars, rettv)
14478 typval_T *argvars;
14479 typval_T *rettv;
14480{
14481 float_T fx, fy;
14482
14483 rettv->v_type = VAR_FLOAT;
14484 if (get_float_arg(argvars, &fx) == OK
14485 && get_float_arg(&argvars[1], &fy) == OK)
14486 rettv->vval.v_float = pow(fx, fy);
14487 else
14488 rettv->vval.v_float = 0.0;
14489}
14490#endif
14491
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014492/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014493 * "prevnonblank()" function
14494 */
14495 static void
14496f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014497 typval_T *argvars;
14498 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499{
14500 linenr_T lnum;
14501
14502 lnum = get_tv_lnum(argvars);
14503 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14504 lnum = 0;
14505 else
14506 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14507 --lnum;
14508 rettv->vval.v_number = lnum;
14509}
14510
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014511#ifdef HAVE_STDARG_H
14512/* This dummy va_list is here because:
14513 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14514 * - locally in the function results in a "used before set" warning
14515 * - using va_start() to initialize it gives "function with fixed args" error */
14516static va_list ap;
14517#endif
14518
Bram Moolenaar8c711452005-01-14 21:53:12 +000014519/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014520 * "printf()" function
14521 */
14522 static void
14523f_printf(argvars, rettv)
14524 typval_T *argvars;
14525 typval_T *rettv;
14526{
14527 rettv->v_type = VAR_STRING;
14528 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014529#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014530 {
14531 char_u buf[NUMBUFLEN];
14532 int len;
14533 char_u *s;
14534 int saved_did_emsg = did_emsg;
14535 char *fmt;
14536
14537 /* Get the required length, allocate the buffer and do it for real. */
14538 did_emsg = FALSE;
14539 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014540 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014541 if (!did_emsg)
14542 {
14543 s = alloc(len + 1);
14544 if (s != NULL)
14545 {
14546 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014547 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014548 }
14549 }
14550 did_emsg |= saved_did_emsg;
14551 }
14552#endif
14553}
14554
14555/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014556 * "pumvisible()" function
14557 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014558 static void
14559f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014560 typval_T *argvars UNUSED;
14561 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014562{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014563#ifdef FEAT_INS_EXPAND
14564 if (pum_visible())
14565 rettv->vval.v_number = 1;
14566#endif
14567}
14568
Bram Moolenaardb913952012-06-29 12:54:53 +020014569#ifdef FEAT_PYTHON3
14570/*
14571 * "py3eval()" function
14572 */
14573 static void
14574f_py3eval(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_py3eval(str, rettv);
14583}
14584#endif
14585
14586#ifdef FEAT_PYTHON
14587/*
14588 * "pyeval()" function
14589 */
14590 static void
14591f_pyeval(argvars, rettv)
14592 typval_T *argvars;
14593 typval_T *rettv;
14594{
14595 char_u *str;
14596 char_u buf[NUMBUFLEN];
14597
14598 str = get_tv_string_buf(&argvars[0], buf);
14599 do_pyeval(str, rettv);
14600}
14601#endif
14602
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014603/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014604 * "range()" function
14605 */
14606 static void
14607f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014608 typval_T *argvars;
14609 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014610{
14611 long start;
14612 long end;
14613 long stride = 1;
14614 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618 if (argvars[1].v_type == VAR_UNKNOWN)
14619 {
14620 end = start - 1;
14621 start = 0;
14622 }
14623 else
14624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014627 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 }
14629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 if (error)
14631 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014633 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014634 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014635 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014636 else
14637 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014638 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014639 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014640 if (list_append_number(rettv->vval.v_list,
14641 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014642 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014643 }
14644}
14645
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014646/*
14647 * "readfile()" function
14648 */
14649 static void
14650f_readfile(argvars, rettv)
14651 typval_T *argvars;
14652 typval_T *rettv;
14653{
14654 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014655 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014656 char_u *fname;
14657 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014658 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14659 int io_size = sizeof(buf);
14660 int readlen; /* size of last fread() */
14661 char_u *prev = NULL; /* previously read bytes, if any */
14662 long prevlen = 0; /* length of data in prev */
14663 long prevsize = 0; /* size of prev buffer */
14664 long maxline = MAXLNUM;
14665 long cnt = 0;
14666 char_u *p; /* position in buf */
14667 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014668
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014669 if (argvars[1].v_type != VAR_UNKNOWN)
14670 {
14671 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14672 binary = TRUE;
14673 if (argvars[2].v_type != VAR_UNKNOWN)
14674 maxline = get_tv_number(&argvars[2]);
14675 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014676
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014677 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014678 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014679
14680 /* Always open the file in binary mode, library functions have a mind of
14681 * their own about CR-LF conversion. */
14682 fname = get_tv_string(&argvars[0]);
14683 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14684 {
14685 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14686 return;
14687 }
14688
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014689 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014690 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014691 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014692
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014693 /* This for loop processes what was read, but is also entered at end
14694 * of file so that either:
14695 * - an incomplete line gets written
14696 * - a "binary" file gets an empty line at the end if it ends in a
14697 * newline. */
14698 for (p = buf, start = buf;
14699 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14700 ++p)
14701 {
14702 if (*p == '\n' || readlen <= 0)
14703 {
14704 listitem_T *li;
14705 char_u *s = NULL;
14706 long_u len = p - start;
14707
14708 /* Finished a line. Remove CRs before NL. */
14709 if (readlen > 0 && !binary)
14710 {
14711 while (len > 0 && start[len - 1] == '\r')
14712 --len;
14713 /* removal may cross back to the "prev" string */
14714 if (len == 0)
14715 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14716 --prevlen;
14717 }
14718 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014719 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014720 else
14721 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014722 /* Change "prev" buffer to be the right size. This way
14723 * the bytes are only copied once, and very long lines are
14724 * allocated only once. */
14725 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014727 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014729 prev = NULL; /* the list will own the string */
14730 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014731 }
14732 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014733 if (s == NULL)
14734 {
14735 do_outofmem_msg((long_u) prevlen + len + 1);
14736 failed = TRUE;
14737 break;
14738 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014739
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014740 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741 {
14742 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014744 break;
14745 }
14746 li->li_tv.v_type = VAR_STRING;
14747 li->li_tv.v_lock = 0;
14748 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014749 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014751 start = p + 1; /* step over newline */
14752 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753 break;
14754 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014755 else if (*p == NUL)
14756 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014757#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14759 * when finding the BF and check the previous two bytes. */
14760 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014761 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014762 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14763 * + 1, these may be in the "prev" string. */
14764 char_u back1 = p >= buf + 1 ? p[-1]
14765 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14766 char_u back2 = p >= buf + 2 ? p[-2]
14767 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14768 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014769
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014770 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014771 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014772 char_u *dest = p - 2;
14773
14774 /* Usually a BOM is at the beginning of a file, and so at
14775 * the beginning of a line; then we can just step over it.
14776 */
14777 if (start == dest)
14778 start = p + 1;
14779 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014780 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014781 /* have to shuffle buf to close gap */
14782 int adjust_prevlen = 0;
14783
14784 if (dest < buf)
14785 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014786 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014787 dest = buf;
14788 }
14789 if (readlen > p - buf + 1)
14790 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14791 readlen -= 3 - adjust_prevlen;
14792 prevlen -= adjust_prevlen;
14793 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014794 }
14795 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014796 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014797#endif
14798 } /* for */
14799
14800 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14801 break;
14802 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014803 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014804 /* There's part of a line in buf, store it in "prev". */
14805 if (p - start + prevlen >= prevsize)
14806 {
14807 /* need bigger "prev" buffer */
14808 char_u *newprev;
14809
14810 /* A common use case is ordinary text files and "prev" gets a
14811 * fragment of a line, so the first allocation is made
14812 * small, to avoid repeatedly 'allocing' large and
14813 * 'reallocing' small. */
14814 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014815 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014816 else
14817 {
14818 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014819 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014820 prevsize = grow50pc > growmin ? grow50pc : growmin;
14821 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014822 newprev = prev == NULL ? alloc(prevsize)
14823 : vim_realloc(prev, prevsize);
14824 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014825 {
14826 do_outofmem_msg((long_u)prevsize);
14827 failed = TRUE;
14828 break;
14829 }
14830 prev = newprev;
14831 }
14832 /* Add the line part to end of "prev". */
14833 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014834 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014835 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014836 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014837
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014838 /*
14839 * For a negative line count use only the lines at the end of the file,
14840 * free the rest.
14841 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014842 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014843 while (cnt > -maxline)
14844 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014845 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014846 --cnt;
14847 }
14848
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014849 if (failed)
14850 {
14851 list_free(rettv->vval.v_list, TRUE);
14852 /* readfile doc says an empty list is returned on error */
14853 rettv->vval.v_list = list_alloc();
14854 }
14855
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014856 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 fclose(fd);
14858}
14859
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014860#if defined(FEAT_RELTIME)
14861static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14862
14863/*
14864 * Convert a List to proftime_T.
14865 * Return FAIL when there is something wrong.
14866 */
14867 static int
14868list2proftime(arg, tm)
14869 typval_T *arg;
14870 proftime_T *tm;
14871{
14872 long n1, n2;
14873 int error = FALSE;
14874
14875 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14876 || arg->vval.v_list->lv_len != 2)
14877 return FAIL;
14878 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14879 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14880# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014881 tm->HighPart = n1;
14882 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014883# else
14884 tm->tv_sec = n1;
14885 tm->tv_usec = n2;
14886# endif
14887 return error ? FAIL : OK;
14888}
14889#endif /* FEAT_RELTIME */
14890
14891/*
14892 * "reltime()" function
14893 */
14894 static void
14895f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014896 typval_T *argvars UNUSED;
14897 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014898{
14899#ifdef FEAT_RELTIME
14900 proftime_T res;
14901 proftime_T start;
14902
14903 if (argvars[0].v_type == VAR_UNKNOWN)
14904 {
14905 /* No arguments: get current time. */
14906 profile_start(&res);
14907 }
14908 else if (argvars[1].v_type == VAR_UNKNOWN)
14909 {
14910 if (list2proftime(&argvars[0], &res) == FAIL)
14911 return;
14912 profile_end(&res);
14913 }
14914 else
14915 {
14916 /* Two arguments: compute the difference. */
14917 if (list2proftime(&argvars[0], &start) == FAIL
14918 || list2proftime(&argvars[1], &res) == FAIL)
14919 return;
14920 profile_sub(&res, &start);
14921 }
14922
14923 if (rettv_list_alloc(rettv) == OK)
14924 {
14925 long n1, n2;
14926
14927# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014928 n1 = res.HighPart;
14929 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014930# else
14931 n1 = res.tv_sec;
14932 n2 = res.tv_usec;
14933# endif
14934 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14935 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14936 }
14937#endif
14938}
14939
14940/*
14941 * "reltimestr()" function
14942 */
14943 static void
14944f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014945 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014946 typval_T *rettv;
14947{
14948#ifdef FEAT_RELTIME
14949 proftime_T tm;
14950#endif
14951
14952 rettv->v_type = VAR_STRING;
14953 rettv->vval.v_string = NULL;
14954#ifdef FEAT_RELTIME
14955 if (list2proftime(&argvars[0], &tm) == OK)
14956 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14957#endif
14958}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014959
Bram Moolenaar0d660222005-01-07 21:51:51 +000014960#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14961static void make_connection __ARGS((void));
14962static int check_connection __ARGS((void));
14963
14964 static void
14965make_connection()
14966{
14967 if (X_DISPLAY == NULL
14968# ifdef FEAT_GUI
14969 && !gui.in_use
14970# endif
14971 )
14972 {
14973 x_force_connect = TRUE;
14974 setup_term_clip();
14975 x_force_connect = FALSE;
14976 }
14977}
14978
14979 static int
14980check_connection()
14981{
14982 make_connection();
14983 if (X_DISPLAY == NULL)
14984 {
14985 EMSG(_("E240: No connection to Vim server"));
14986 return FAIL;
14987 }
14988 return OK;
14989}
14990#endif
14991
14992#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014993static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014994
14995 static void
14996remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 typval_T *argvars;
14998 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014999 int expr;
15000{
15001 char_u *server_name;
15002 char_u *keys;
15003 char_u *r = NULL;
15004 char_u buf[NUMBUFLEN];
15005# ifdef WIN32
15006 HWND w;
15007# else
15008 Window w;
15009# endif
15010
15011 if (check_restricted() || check_secure())
15012 return;
15013
15014# ifdef FEAT_X11
15015 if (check_connection() == FAIL)
15016 return;
15017# endif
15018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015019 server_name = get_tv_string_chk(&argvars[0]);
15020 if (server_name == NULL)
15021 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015022 keys = get_tv_string_buf(&argvars[1], buf);
15023# ifdef WIN32
15024 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15025# else
15026 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15027 < 0)
15028# endif
15029 {
15030 if (r != NULL)
15031 EMSG(r); /* sending worked but evaluation failed */
15032 else
15033 EMSG2(_("E241: Unable to send to %s"), server_name);
15034 return;
15035 }
15036
15037 rettv->vval.v_string = r;
15038
15039 if (argvars[2].v_type != VAR_UNKNOWN)
15040 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015041 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015042 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015043 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015045 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015046 v.di_tv.v_type = VAR_STRING;
15047 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015048 idvar = get_tv_string_chk(&argvars[2]);
15049 if (idvar != NULL)
15050 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015052 }
15053}
15054#endif
15055
15056/*
15057 * "remote_expr()" function
15058 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059 static void
15060f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015063{
15064 rettv->v_type = VAR_STRING;
15065 rettv->vval.v_string = NULL;
15066#ifdef FEAT_CLIENTSERVER
15067 remote_common(argvars, rettv, TRUE);
15068#endif
15069}
15070
15071/*
15072 * "remote_foreground()" function
15073 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015074 static void
15075f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015076 typval_T *argvars UNUSED;
15077 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015078{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015079#ifdef FEAT_CLIENTSERVER
15080# ifdef WIN32
15081 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015082 {
15083 char_u *server_name = get_tv_string_chk(&argvars[0]);
15084
15085 if (server_name != NULL)
15086 serverForeground(server_name);
15087 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015088# else
15089 /* Send a foreground() expression to the server. */
15090 argvars[1].v_type = VAR_STRING;
15091 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15092 argvars[2].v_type = VAR_UNKNOWN;
15093 remote_common(argvars, rettv, TRUE);
15094 vim_free(argvars[1].vval.v_string);
15095# endif
15096#endif
15097}
15098
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 static void
15100f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015101 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103{
15104#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015105 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015106 char_u *s = NULL;
15107# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015108 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015110 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111
15112 if (check_restricted() || check_secure())
15113 {
15114 rettv->vval.v_number = -1;
15115 return;
15116 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015117 serverid = get_tv_string_chk(&argvars[0]);
15118 if (serverid == NULL)
15119 {
15120 rettv->vval.v_number = -1;
15121 return; /* type error; errmsg already given */
15122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015124 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125 if (n == 0)
15126 rettv->vval.v_number = -1;
15127 else
15128 {
15129 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15130 rettv->vval.v_number = (s != NULL);
15131 }
15132# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133 if (check_connection() == FAIL)
15134 return;
15135
15136 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015137 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138# endif
15139
15140 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 char_u *retvar;
15143
Bram Moolenaar33570922005-01-25 22:26:29 +000015144 v.di_tv.v_type = VAR_STRING;
15145 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015146 retvar = get_tv_string_chk(&argvars[1]);
15147 if (retvar != NULL)
15148 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015149 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 }
15151#else
15152 rettv->vval.v_number = -1;
15153#endif
15154}
15155
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 static void
15157f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015158 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015159 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160{
15161 char_u *r = NULL;
15162
15163#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015164 char_u *serverid = get_tv_string_chk(&argvars[0]);
15165
15166 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 {
15168# ifdef WIN32
15169 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015170 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015172 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173 if (n != 0)
15174 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15175 if (r == NULL)
15176# else
15177 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015178 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179# endif
15180 EMSG(_("E277: Unable to read a server reply"));
15181 }
15182#endif
15183 rettv->v_type = VAR_STRING;
15184 rettv->vval.v_string = r;
15185}
15186
15187/*
15188 * "remote_send()" function
15189 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015190 static void
15191f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015192 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015193 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015194{
15195 rettv->v_type = VAR_STRING;
15196 rettv->vval.v_string = NULL;
15197#ifdef FEAT_CLIENTSERVER
15198 remote_common(argvars, rettv, FALSE);
15199#endif
15200}
15201
15202/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015203 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015204 */
15205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015206f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *argvars;
15208 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015209{
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 list_T *l;
15211 listitem_T *item, *item2;
15212 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015213 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015214 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015215 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015216 dict_T *d;
15217 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015218 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015219
Bram Moolenaar8c711452005-01-14 21:53:12 +000015220 if (argvars[0].v_type == VAR_DICT)
15221 {
15222 if (argvars[2].v_type != VAR_UNKNOWN)
15223 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015224 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015225 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015227 key = get_tv_string_chk(&argvars[1]);
15228 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 di = dict_find(d, key, -1);
15231 if (di == NULL)
15232 EMSG2(_(e_dictkey), key);
15233 else
15234 {
15235 *rettv = di->di_tv;
15236 init_tv(&di->di_tv);
15237 dictitem_remove(d, di);
15238 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015239 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 }
15241 }
15242 else if (argvars[0].v_type != VAR_LIST)
15243 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015244 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015245 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 int error = FALSE;
15248
15249 idx = get_tv_number_chk(&argvars[1], &error);
15250 if (error)
15251 ; /* type error: do nothing, errmsg already given */
15252 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015253 EMSGN(_(e_listidx), idx);
15254 else
15255 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015256 if (argvars[2].v_type == VAR_UNKNOWN)
15257 {
15258 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015259 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015260 *rettv = item->li_tv;
15261 vim_free(item);
15262 }
15263 else
15264 {
15265 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266 end = get_tv_number_chk(&argvars[2], &error);
15267 if (error)
15268 ; /* type error: do nothing */
15269 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015270 EMSGN(_(e_listidx), end);
15271 else
15272 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015273 int cnt = 0;
15274
15275 for (li = item; li != NULL; li = li->li_next)
15276 {
15277 ++cnt;
15278 if (li == item2)
15279 break;
15280 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015281 if (li == NULL) /* didn't find "item2" after "item" */
15282 EMSG(_(e_invrange));
15283 else
15284 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015285 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015286 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015287 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015288 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015289 l->lv_first = item;
15290 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015291 item->li_prev = NULL;
15292 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015293 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015294 }
15295 }
15296 }
15297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015298 }
15299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015300}
15301
15302/*
15303 * "rename({from}, {to})" function
15304 */
15305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *argvars;
15308 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309{
15310 char_u buf[NUMBUFLEN];
15311
15312 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015315 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15316 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015317}
15318
15319/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320 * "repeat()" function
15321 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015322 static void
15323f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015324 typval_T *argvars;
15325 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015326{
15327 char_u *p;
15328 int n;
15329 int slen;
15330 int len;
15331 char_u *r;
15332 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015333
15334 n = get_tv_number(&argvars[1]);
15335 if (argvars[0].v_type == VAR_LIST)
15336 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015337 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015338 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015339 if (list_extend(rettv->vval.v_list,
15340 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015341 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015342 }
15343 else
15344 {
15345 p = get_tv_string(&argvars[0]);
15346 rettv->v_type = VAR_STRING;
15347 rettv->vval.v_string = NULL;
15348
15349 slen = (int)STRLEN(p);
15350 len = slen * n;
15351 if (len <= 0)
15352 return;
15353
15354 r = alloc(len + 1);
15355 if (r != NULL)
15356 {
15357 for (i = 0; i < n; i++)
15358 mch_memmove(r + i * slen, p, (size_t)slen);
15359 r[len] = NUL;
15360 }
15361
15362 rettv->vval.v_string = r;
15363 }
15364}
15365
15366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 * "resolve()" function
15368 */
15369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015371 typval_T *argvars;
15372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373{
15374 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015375#ifdef HAVE_READLINK
15376 char_u *buf = NULL;
15377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015379 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380#ifdef FEAT_SHORTCUT
15381 {
15382 char_u *v = NULL;
15383
15384 v = mch_resolve_shortcut(p);
15385 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 }
15390#else
15391# ifdef HAVE_READLINK
15392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 char_u *cpy;
15394 int len;
15395 char_u *remain = NULL;
15396 char_u *q;
15397 int is_relative_to_current = FALSE;
15398 int has_trailing_pathsep = FALSE;
15399 int limit = 100;
15400
15401 p = vim_strsave(p);
15402
15403 if (p[0] == '.' && (vim_ispathsep(p[1])
15404 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15405 is_relative_to_current = TRUE;
15406
15407 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015408 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015411 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413
15414 q = getnextcomp(p);
15415 if (*q != NUL)
15416 {
15417 /* Separate the first path component in "p", and keep the
15418 * remainder (beginning with the path separator). */
15419 remain = vim_strsave(q - 1);
15420 q[-1] = NUL;
15421 }
15422
Bram Moolenaard9462e32011-04-11 21:35:11 +020015423 buf = alloc(MAXPATHL + 1);
15424 if (buf == NULL)
15425 goto fail;
15426
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 for (;;)
15428 {
15429 for (;;)
15430 {
15431 len = readlink((char *)p, (char *)buf, MAXPATHL);
15432 if (len <= 0)
15433 break;
15434 buf[len] = NUL;
15435
15436 if (limit-- == 0)
15437 {
15438 vim_free(p);
15439 vim_free(remain);
15440 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 goto fail;
15443 }
15444
15445 /* Ensure that the result will have a trailing path separator
15446 * if the argument has one. */
15447 if (remain == NULL && has_trailing_pathsep)
15448 add_pathsep(buf);
15449
15450 /* Separate the first path component in the link value and
15451 * concatenate the remainders. */
15452 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15453 if (*q != NUL)
15454 {
15455 if (remain == NULL)
15456 remain = vim_strsave(q - 1);
15457 else
15458 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015459 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 if (cpy != NULL)
15461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 vim_free(remain);
15463 remain = cpy;
15464 }
15465 }
15466 q[-1] = NUL;
15467 }
15468
15469 q = gettail(p);
15470 if (q > p && *q == NUL)
15471 {
15472 /* Ignore trailing path separator. */
15473 q[-1] = NUL;
15474 q = gettail(p);
15475 }
15476 if (q > p && !mch_isFullName(buf))
15477 {
15478 /* symlink is relative to directory of argument */
15479 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15480 if (cpy != NULL)
15481 {
15482 STRCPY(cpy, p);
15483 STRCPY(gettail(cpy), buf);
15484 vim_free(p);
15485 p = cpy;
15486 }
15487 }
15488 else
15489 {
15490 vim_free(p);
15491 p = vim_strsave(buf);
15492 }
15493 }
15494
15495 if (remain == NULL)
15496 break;
15497
15498 /* Append the first path component of "remain" to "p". */
15499 q = getnextcomp(remain + 1);
15500 len = q - remain - (*q != NUL);
15501 cpy = vim_strnsave(p, STRLEN(p) + len);
15502 if (cpy != NULL)
15503 {
15504 STRNCAT(cpy, remain, len);
15505 vim_free(p);
15506 p = cpy;
15507 }
15508 /* Shorten "remain". */
15509 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015510 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 else
15512 {
15513 vim_free(remain);
15514 remain = NULL;
15515 }
15516 }
15517
15518 /* If the result is a relative path name, make it explicitly relative to
15519 * the current directory if and only if the argument had this form. */
15520 if (!vim_ispathsep(*p))
15521 {
15522 if (is_relative_to_current
15523 && *p != NUL
15524 && !(p[0] == '.'
15525 && (p[1] == NUL
15526 || vim_ispathsep(p[1])
15527 || (p[1] == '.'
15528 && (p[2] == NUL
15529 || vim_ispathsep(p[2]))))))
15530 {
15531 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015532 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 if (cpy != NULL)
15534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 vim_free(p);
15536 p = cpy;
15537 }
15538 }
15539 else if (!is_relative_to_current)
15540 {
15541 /* Strip leading "./". */
15542 q = p;
15543 while (q[0] == '.' && vim_ispathsep(q[1]))
15544 q += 2;
15545 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015546 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 }
15548 }
15549
15550 /* Ensure that the result will have no trailing path separator
15551 * if the argument had none. But keep "/" or "//". */
15552 if (!has_trailing_pathsep)
15553 {
15554 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015555 if (after_pathsep(p, q))
15556 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 }
15558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 }
15561# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563# endif
15564#endif
15565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567
15568#ifdef HAVE_READLINK
15569fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015570 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015572 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573}
15574
15575/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 */
15578 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015579f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015580 typval_T *argvars;
15581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582{
Bram Moolenaar33570922005-01-25 22:26:29 +000015583 list_T *l;
15584 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586 if (argvars[0].v_type != VAR_LIST)
15587 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015588 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015589 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590 {
15591 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015592 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015593 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015594 while (li != NULL)
15595 {
15596 ni = li->li_prev;
15597 list_append(l, li);
15598 li = ni;
15599 }
15600 rettv->vval.v_list = l;
15601 rettv->v_type = VAR_LIST;
15602 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015603 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605}
15606
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015607#define SP_NOMOVE 0x01 /* don't move cursor */
15608#define SP_REPEAT 0x02 /* repeat to find outer pair */
15609#define SP_RETCOUNT 0x04 /* return matchcount */
15610#define SP_SETPCMARK 0x08 /* set previous context mark */
15611#define SP_START 0x10 /* accept match at start position */
15612#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15613#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015614
Bram Moolenaar33570922005-01-25 22:26:29 +000015615static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015616
15617/*
15618 * Get flags for a search function.
15619 * Possibly sets "p_ws".
15620 * Returns BACKWARD, FORWARD or zero (for an error).
15621 */
15622 static int
15623get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015624 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015625 int *flagsp;
15626{
15627 int dir = FORWARD;
15628 char_u *flags;
15629 char_u nbuf[NUMBUFLEN];
15630 int mask;
15631
15632 if (varp->v_type != VAR_UNKNOWN)
15633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015634 flags = get_tv_string_buf_chk(varp, nbuf);
15635 if (flags == NULL)
15636 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637 while (*flags != NUL)
15638 {
15639 switch (*flags)
15640 {
15641 case 'b': dir = BACKWARD; break;
15642 case 'w': p_ws = TRUE; break;
15643 case 'W': p_ws = FALSE; break;
15644 default: mask = 0;
15645 if (flagsp != NULL)
15646 switch (*flags)
15647 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015648 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015649 case 'e': mask = SP_END; break;
15650 case 'm': mask = SP_RETCOUNT; break;
15651 case 'n': mask = SP_NOMOVE; break;
15652 case 'p': mask = SP_SUBPAT; break;
15653 case 'r': mask = SP_REPEAT; break;
15654 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655 }
15656 if (mask == 0)
15657 {
15658 EMSG2(_(e_invarg2), flags);
15659 dir = 0;
15660 }
15661 else
15662 *flagsp |= mask;
15663 }
15664 if (dir == 0)
15665 break;
15666 ++flags;
15667 }
15668 }
15669 return dir;
15670}
15671
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015673 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015675 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015676search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015677 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015678 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015679 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015681 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 char_u *pat;
15683 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015684 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 int save_p_ws = p_ws;
15686 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015687 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015688 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015689 proftime_T tm;
15690#ifdef FEAT_RELTIME
15691 long time_limit = 0;
15692#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015693 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015694 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015696 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015697 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015698 if (dir == 0)
15699 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015700 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015701 if (flags & SP_START)
15702 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015703 if (flags & SP_END)
15704 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015705
Bram Moolenaar76929292008-01-06 19:07:36 +000015706 /* Optional arguments: line number to stop searching and timeout. */
15707 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015708 {
15709 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15710 if (lnum_stop < 0)
15711 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015712#ifdef FEAT_RELTIME
15713 if (argvars[3].v_type != VAR_UNKNOWN)
15714 {
15715 time_limit = get_tv_number_chk(&argvars[3], NULL);
15716 if (time_limit < 0)
15717 goto theend;
15718 }
15719#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015720 }
15721
Bram Moolenaar76929292008-01-06 19:07:36 +000015722#ifdef FEAT_RELTIME
15723 /* Set the time limit, if there is one. */
15724 profile_setlimit(time_limit, &tm);
15725#endif
15726
Bram Moolenaar231334e2005-07-25 20:46:57 +000015727 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015728 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015729 * Check to make sure only those flags are set.
15730 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15731 * flags cannot be set. Check for that condition also.
15732 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015733 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015734 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015736 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015737 goto theend;
15738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015740 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015741 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015742 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015743 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015745 if (flags & SP_SUBPAT)
15746 retval = subpatnum;
15747 else
15748 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015749 if (flags & SP_SETPCMARK)
15750 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015752 if (match_pos != NULL)
15753 {
15754 /* Store the match cursor position */
15755 match_pos->lnum = pos.lnum;
15756 match_pos->col = pos.col + 1;
15757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 /* "/$" will put the cursor after the end of the line, may need to
15759 * correct that here */
15760 check_cursor();
15761 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015762
15763 /* If 'n' flag is used: restore cursor position. */
15764 if (flags & SP_NOMOVE)
15765 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015766 else
15767 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015768theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015770
15771 return retval;
15772}
15773
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015774#ifdef FEAT_FLOAT
15775/*
15776 * "round({float})" function
15777 */
15778 static void
15779f_round(argvars, rettv)
15780 typval_T *argvars;
15781 typval_T *rettv;
15782{
15783 float_T f;
15784
15785 rettv->v_type = VAR_FLOAT;
15786 if (get_float_arg(argvars, &f) == OK)
15787 /* round() is not in C90, use ceil() or floor() instead. */
15788 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15789 else
15790 rettv->vval.v_float = 0.0;
15791}
15792#endif
15793
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015794/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015795 * "screencol()" function
15796 *
15797 * First column is 1 to be consistent with virtcol().
15798 */
15799 static void
15800f_screencol(argvars, rettv)
15801 typval_T *argvars UNUSED;
15802 typval_T *rettv;
15803{
15804 rettv->vval.v_number = screen_screencol() + 1;
15805}
15806
15807/*
15808 * "screenrow()" function
15809 */
15810 static void
15811f_screenrow(argvars, rettv)
15812 typval_T *argvars UNUSED;
15813 typval_T *rettv;
15814{
15815 rettv->vval.v_number = screen_screenrow() + 1;
15816}
15817
15818/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015819 * "search()" function
15820 */
15821 static void
15822f_search(argvars, rettv)
15823 typval_T *argvars;
15824 typval_T *rettv;
15825{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015826 int flags = 0;
15827
15828 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829}
15830
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015832 * "searchdecl()" function
15833 */
15834 static void
15835f_searchdecl(argvars, rettv)
15836 typval_T *argvars;
15837 typval_T *rettv;
15838{
15839 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015840 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015841 int error = FALSE;
15842 char_u *name;
15843
15844 rettv->vval.v_number = 1; /* default: FAIL */
15845
15846 name = get_tv_string_chk(&argvars[0]);
15847 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015848 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015849 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015850 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15851 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15852 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015853 if (!error && name != NULL)
15854 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015855 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015856}
15857
15858/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015859 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015861 static int
15862searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015863 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015864 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865{
15866 char_u *spat, *mpat, *epat;
15867 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 int dir;
15870 int flags = 0;
15871 char_u nbuf1[NUMBUFLEN];
15872 char_u nbuf2[NUMBUFLEN];
15873 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015874 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015875 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015876 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015879 spat = get_tv_string_chk(&argvars[0]);
15880 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15881 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15882 if (spat == NULL || mpat == NULL || epat == NULL)
15883 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 /* Handle the optional fourth argument: flags */
15886 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015887 if (dir == 0)
15888 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015889
15890 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015891 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15892 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015893 if ((flags & (SP_END | SP_SUBPAT)) != 0
15894 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015895 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015896 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015897 goto theend;
15898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015900 /* Using 'r' implies 'W', otherwise it doesn't work. */
15901 if (flags & SP_REPEAT)
15902 p_ws = FALSE;
15903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015904 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015905 if (argvars[3].v_type == VAR_UNKNOWN
15906 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 skip = (char_u *)"";
15908 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015911 if (argvars[5].v_type != VAR_UNKNOWN)
15912 {
15913 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15914 if (lnum_stop < 0)
15915 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015916#ifdef FEAT_RELTIME
15917 if (argvars[6].v_type != VAR_UNKNOWN)
15918 {
15919 time_limit = get_tv_number_chk(&argvars[6], NULL);
15920 if (time_limit < 0)
15921 goto theend;
15922 }
15923#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015924 }
15925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015926 if (skip == NULL)
15927 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015929 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015930 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015931
15932theend:
15933 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015934
15935 return retval;
15936}
15937
15938/*
15939 * "searchpair()" function
15940 */
15941 static void
15942f_searchpair(argvars, rettv)
15943 typval_T *argvars;
15944 typval_T *rettv;
15945{
15946 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15947}
15948
15949/*
15950 * "searchpairpos()" function
15951 */
15952 static void
15953f_searchpairpos(argvars, rettv)
15954 typval_T *argvars;
15955 typval_T *rettv;
15956{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015957 pos_T match_pos;
15958 int lnum = 0;
15959 int col = 0;
15960
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015962 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015963
15964 if (searchpair_cmn(argvars, &match_pos) > 0)
15965 {
15966 lnum = match_pos.lnum;
15967 col = match_pos.col;
15968 }
15969
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015970 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15971 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015972}
15973
15974/*
15975 * Search for a start/middle/end thing.
15976 * Used by searchpair(), see its documentation for the details.
15977 * Returns 0 or -1 for no match,
15978 */
15979 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015980do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15981 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015982 char_u *spat; /* start pattern */
15983 char_u *mpat; /* middle pattern */
15984 char_u *epat; /* end pattern */
15985 int dir; /* BACKWARD or FORWARD */
15986 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015987 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015988 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015989 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015990 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015991{
15992 char_u *save_cpo;
15993 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15994 long retval = 0;
15995 pos_T pos;
15996 pos_T firstpos;
15997 pos_T foundpos;
15998 pos_T save_cursor;
15999 pos_T save_pos;
16000 int n;
16001 int r;
16002 int nest = 1;
16003 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016004 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016005 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016006
16007 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16008 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016009 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016010
Bram Moolenaar76929292008-01-06 19:07:36 +000016011#ifdef FEAT_RELTIME
16012 /* Set the time limit, if there is one. */
16013 profile_setlimit(time_limit, &tm);
16014#endif
16015
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016016 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16017 * start/middle/end (pat3, for the top pair). */
16018 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16019 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16020 if (pat2 == NULL || pat3 == NULL)
16021 goto theend;
16022 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16023 if (*mpat == NUL)
16024 STRCPY(pat3, pat2);
16025 else
16026 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16027 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016028 if (flags & SP_START)
16029 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016030
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 save_cursor = curwin->w_cursor;
16032 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016033 clearpos(&firstpos);
16034 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035 pat = pat3;
16036 for (;;)
16037 {
16038 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016039 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16041 /* didn't find it or found the first match again: FAIL */
16042 break;
16043
16044 if (firstpos.lnum == 0)
16045 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016046 if (equalpos(pos, foundpos))
16047 {
16048 /* Found the same position again. Can happen with a pattern that
16049 * has "\zs" at the end and searching backwards. Advance one
16050 * character and try again. */
16051 if (dir == BACKWARD)
16052 decl(&pos);
16053 else
16054 incl(&pos);
16055 }
16056 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016058 /* clear the start flag to avoid getting stuck here */
16059 options &= ~SEARCH_START;
16060
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 /* If the skip pattern matches, ignore this match. */
16062 if (*skip != NUL)
16063 {
16064 save_pos = curwin->w_cursor;
16065 curwin->w_cursor = pos;
16066 r = eval_to_bool(skip, &err, NULL, FALSE);
16067 curwin->w_cursor = save_pos;
16068 if (err)
16069 {
16070 /* Evaluating {skip} caused an error, break here. */
16071 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016072 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 break;
16074 }
16075 if (r)
16076 continue;
16077 }
16078
16079 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16080 {
16081 /* Found end when searching backwards or start when searching
16082 * forward: nested pair. */
16083 ++nest;
16084 pat = pat2; /* nested, don't search for middle */
16085 }
16086 else
16087 {
16088 /* Found end when searching forward or start when searching
16089 * backward: end of (nested) pair; or found middle in outer pair. */
16090 if (--nest == 1)
16091 pat = pat3; /* outer level, search for middle */
16092 }
16093
16094 if (nest == 0)
16095 {
16096 /* Found the match: return matchcount or line number. */
16097 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016098 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016100 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016101 if (flags & SP_SETPCMARK)
16102 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 curwin->w_cursor = pos;
16104 if (!(flags & SP_REPEAT))
16105 break;
16106 nest = 1; /* search for next unmatched */
16107 }
16108 }
16109
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016110 if (match_pos != NULL)
16111 {
16112 /* Store the match cursor position */
16113 match_pos->lnum = curwin->w_cursor.lnum;
16114 match_pos->col = curwin->w_cursor.col + 1;
16115 }
16116
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016118 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119 curwin->w_cursor = save_cursor;
16120
16121theend:
16122 vim_free(pat2);
16123 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016124 if (p_cpo == empty_option)
16125 p_cpo = save_cpo;
16126 else
16127 /* Darn, evaluating the {skip} expression changed the value. */
16128 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016129
16130 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131}
16132
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133/*
16134 * "searchpos()" function
16135 */
16136 static void
16137f_searchpos(argvars, rettv)
16138 typval_T *argvars;
16139 typval_T *rettv;
16140{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016141 pos_T match_pos;
16142 int lnum = 0;
16143 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016144 int n;
16145 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016146
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016147 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016148 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016149
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016150 n = search_cmn(argvars, &match_pos, &flags);
16151 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016152 {
16153 lnum = match_pos.lnum;
16154 col = match_pos.col;
16155 }
16156
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016157 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16158 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016159 if (flags & SP_SUBPAT)
16160 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016161}
16162
16163
Bram Moolenaar0d660222005-01-07 21:51:51 +000016164 static void
16165f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016169#ifdef FEAT_CLIENTSERVER
16170 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016171 char_u *server = get_tv_string_chk(&argvars[0]);
16172 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173
Bram Moolenaar0d660222005-01-07 21:51:51 +000016174 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016175 if (server == NULL || reply == NULL)
16176 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016177 if (check_restricted() || check_secure())
16178 return;
16179# ifdef FEAT_X11
16180 if (check_connection() == FAIL)
16181 return;
16182# endif
16183
16184 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 EMSG(_("E258: Unable to send to client"));
16187 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 rettv->vval.v_number = 0;
16190#else
16191 rettv->vval.v_number = -1;
16192#endif
16193}
16194
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195 static void
16196f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016197 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199{
16200 char_u *r = NULL;
16201
16202#ifdef FEAT_CLIENTSERVER
16203# ifdef WIN32
16204 r = serverGetVimNames();
16205# else
16206 make_connection();
16207 if (X_DISPLAY != NULL)
16208 r = serverGetVimNames(X_DISPLAY);
16209# endif
16210#endif
16211 rettv->v_type = VAR_STRING;
16212 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213}
16214
16215/*
16216 * "setbufvar()" function
16217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016219f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016220 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016221 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222{
16223 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016225 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227 char_u nbuf[NUMBUFLEN];
16228
16229 if (check_restricted() || check_secure())
16230 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016231 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16232 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016233 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 varp = &argvars[2];
16235
16236 if (buf != NULL && varname != NULL && varp != NULL)
16237 {
16238 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240
16241 if (*varname == '&')
16242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016243 long numval;
16244 char_u *strval;
16245 int error = FALSE;
16246
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016248 numval = get_tv_number_chk(varp, &error);
16249 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016250 if (!error && strval != NULL)
16251 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 }
16253 else
16254 {
16255 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16256 if (bufvarname != NULL)
16257 {
16258 STRCPY(bufvarname, "b:");
16259 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016260 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261 vim_free(bufvarname);
16262 }
16263 }
16264
16265 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268}
16269
16270/*
16271 * "setcmdpos()" function
16272 */
16273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016275 typval_T *argvars;
16276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016278 int pos = (int)get_tv_number(&argvars[0]) - 1;
16279
16280 if (pos >= 0)
16281 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282}
16283
16284/*
16285 * "setline()" function
16286 */
16287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 typval_T *argvars;
16290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291{
16292 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016293 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016294 list_T *l = NULL;
16295 listitem_T *li = NULL;
16296 long added = 0;
16297 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016299 lnum = get_tv_lnum(&argvars[0]);
16300 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016302 l = argvars[1].vval.v_list;
16303 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016305 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016306 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016307
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016308 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016309 for (;;)
16310 {
16311 if (l != NULL)
16312 {
16313 /* list argument, get next string */
16314 if (li == NULL)
16315 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016316 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016317 li = li->li_next;
16318 }
16319
16320 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016321 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016322 break;
16323 if (lnum <= curbuf->b_ml.ml_line_count)
16324 {
16325 /* existing line, replace it */
16326 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16327 {
16328 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016329 if (lnum == curwin->w_cursor.lnum)
16330 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016331 rettv->vval.v_number = 0; /* OK */
16332 }
16333 }
16334 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16335 {
16336 /* lnum is one past the last line, append the line */
16337 ++added;
16338 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16339 rettv->vval.v_number = 0; /* OK */
16340 }
16341
16342 if (l == NULL) /* only one string argument */
16343 break;
16344 ++lnum;
16345 }
16346
16347 if (added > 0)
16348 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349}
16350
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016351static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16352
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016354 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016355 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016356 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016357set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016358 win_T *wp UNUSED;
16359 typval_T *list_arg UNUSED;
16360 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016361 typval_T *rettv;
16362{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016363#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016364 char_u *act;
16365 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016366#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016367
Bram Moolenaar2641f772005-03-25 21:58:17 +000016368 rettv->vval.v_number = -1;
16369
16370#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016371 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016372 EMSG(_(e_listreq));
16373 else
16374 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016375 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016376
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016377 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016378 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016379 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016380 if (act == NULL)
16381 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016382 if (*act == 'a' || *act == 'r')
16383 action = *act;
16384 }
16385
Bram Moolenaar81484f42012-12-05 15:16:47 +010016386 if (l != NULL && set_errorlist(wp, l, action,
16387 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016388 rettv->vval.v_number = 0;
16389 }
16390#endif
16391}
16392
16393/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016394 * "setloclist()" function
16395 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016396 static void
16397f_setloclist(argvars, rettv)
16398 typval_T *argvars;
16399 typval_T *rettv;
16400{
16401 win_T *win;
16402
16403 rettv->vval.v_number = -1;
16404
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016405 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016406 if (win != NULL)
16407 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16408}
16409
16410/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016411 * "setmatches()" function
16412 */
16413 static void
16414f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016415 typval_T *argvars UNUSED;
16416 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016417{
16418#ifdef FEAT_SEARCH_EXTRA
16419 list_T *l;
16420 listitem_T *li;
16421 dict_T *d;
16422
16423 rettv->vval.v_number = -1;
16424 if (argvars[0].v_type != VAR_LIST)
16425 {
16426 EMSG(_(e_listreq));
16427 return;
16428 }
16429 if ((l = argvars[0].vval.v_list) != NULL)
16430 {
16431
16432 /* To some extent make sure that we are dealing with a list from
16433 * "getmatches()". */
16434 li = l->lv_first;
16435 while (li != NULL)
16436 {
16437 if (li->li_tv.v_type != VAR_DICT
16438 || (d = li->li_tv.vval.v_dict) == NULL)
16439 {
16440 EMSG(_(e_invarg));
16441 return;
16442 }
16443 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16444 && dict_find(d, (char_u *)"pattern", -1) != NULL
16445 && dict_find(d, (char_u *)"priority", -1) != NULL
16446 && dict_find(d, (char_u *)"id", -1) != NULL))
16447 {
16448 EMSG(_(e_invarg));
16449 return;
16450 }
16451 li = li->li_next;
16452 }
16453
16454 clear_matches(curwin);
16455 li = l->lv_first;
16456 while (li != NULL)
16457 {
16458 d = li->li_tv.vval.v_dict;
16459 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16460 get_dict_string(d, (char_u *)"pattern", FALSE),
16461 (int)get_dict_number(d, (char_u *)"priority"),
16462 (int)get_dict_number(d, (char_u *)"id"));
16463 li = li->li_next;
16464 }
16465 rettv->vval.v_number = 0;
16466 }
16467#endif
16468}
16469
16470/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016471 * "setpos()" function
16472 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016473 static void
16474f_setpos(argvars, rettv)
16475 typval_T *argvars;
16476 typval_T *rettv;
16477{
16478 pos_T pos;
16479 int fnum;
16480 char_u *name;
16481
Bram Moolenaar08250432008-02-13 11:42:46 +000016482 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016483 name = get_tv_string_chk(argvars);
16484 if (name != NULL)
16485 {
16486 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16487 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016488 if (--pos.col < 0)
16489 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016490 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016491 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016492 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016493 if (fnum == curbuf->b_fnum)
16494 {
16495 curwin->w_cursor = pos;
16496 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016497 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016498 }
16499 else
16500 EMSG(_(e_invarg));
16501 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016502 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16503 {
16504 /* set mark */
16505 if (setmark_pos(name[1], &pos, fnum) == OK)
16506 rettv->vval.v_number = 0;
16507 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016508 else
16509 EMSG(_(e_invarg));
16510 }
16511 }
16512}
16513
16514/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016515 * "setqflist()" function
16516 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016517 static void
16518f_setqflist(argvars, rettv)
16519 typval_T *argvars;
16520 typval_T *rettv;
16521{
16522 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16523}
16524
16525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016526 * "setreg()" function
16527 */
16528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016529f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016530 typval_T *argvars;
16531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532{
16533 int regname;
16534 char_u *strregname;
16535 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 int append;
16538 char_u yank_type;
16539 long block_len;
16540
16541 block_len = -1;
16542 yank_type = MAUTO;
16543 append = FALSE;
16544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016545 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016546 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016548 if (strregname == NULL)
16549 return; /* type error; errmsg already given */
16550 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 if (regname == 0 || regname == '@')
16552 regname = '"';
16553 else if (regname == '=')
16554 return;
16555
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016558 stropt = get_tv_string_chk(&argvars[2]);
16559 if (stropt == NULL)
16560 return; /* type error */
16561 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 switch (*stropt)
16563 {
16564 case 'a': case 'A': /* append */
16565 append = TRUE;
16566 break;
16567 case 'v': case 'c': /* character-wise selection */
16568 yank_type = MCHAR;
16569 break;
16570 case 'V': case 'l': /* line-wise selection */
16571 yank_type = MLINE;
16572 break;
16573#ifdef FEAT_VISUAL
16574 case 'b': case Ctrl_V: /* block-wise selection */
16575 yank_type = MBLOCK;
16576 if (VIM_ISDIGIT(stropt[1]))
16577 {
16578 ++stropt;
16579 block_len = getdigits(&stropt) - 1;
16580 --stropt;
16581 }
16582 break;
16583#endif
16584 }
16585 }
16586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016587 strval = get_tv_string_chk(&argvars[1]);
16588 if (strval != NULL)
16589 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016591 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592}
16593
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016594/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016595 * "settabvar()" function
16596 */
16597 static void
16598f_settabvar(argvars, rettv)
16599 typval_T *argvars;
16600 typval_T *rettv;
16601{
16602 tabpage_T *save_curtab;
16603 char_u *varname, *tabvarname;
16604 typval_T *varp;
16605 tabpage_T *tp;
16606
16607 rettv->vval.v_number = 0;
16608
16609 if (check_restricted() || check_secure())
16610 return;
16611
16612 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16613 varname = get_tv_string_chk(&argvars[1]);
16614 varp = &argvars[2];
16615
16616 if (tp != NULL && varname != NULL && varp != NULL)
16617 {
16618 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016619 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016620
16621 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16622 if (tabvarname != NULL)
16623 {
16624 STRCPY(tabvarname, "t:");
16625 STRCPY(tabvarname + 2, varname);
16626 set_var(tabvarname, varp, TRUE);
16627 vim_free(tabvarname);
16628 }
16629
16630 /* Restore current tabpage */
16631 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016632 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016633 }
16634}
16635
16636/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016637 * "settabwinvar()" function
16638 */
16639 static void
16640f_settabwinvar(argvars, rettv)
16641 typval_T *argvars;
16642 typval_T *rettv;
16643{
16644 setwinvar(argvars, rettv, 1);
16645}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016646
16647/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016648 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016652 typval_T *argvars;
16653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016655 setwinvar(argvars, rettv, 0);
16656}
16657
16658/*
16659 * "setwinvar()" and "settabwinvar()" functions
16660 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016661
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016662 static void
16663setwinvar(argvars, rettv, off)
16664 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016665 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016666 int off;
16667{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016668 win_T *win;
16669#ifdef FEAT_WINDOWS
16670 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016671 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672#endif
16673 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016674 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016676 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677
16678 if (check_restricted() || check_secure())
16679 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016680
16681#ifdef FEAT_WINDOWS
16682 if (off == 1)
16683 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16684 else
16685 tp = curtab;
16686#endif
16687 win = find_win_by_nr(&argvars[off], tp);
16688 varname = get_tv_string_chk(&argvars[off + 1]);
16689 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690
16691 if (win != NULL && varname != NULL && varp != NULL)
16692 {
16693#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016694 if (switch_win(&save_curwin, &save_curtab, win, tp) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016695 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696#endif
16697
16698 if (*varname == '&')
16699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 long numval;
16701 char_u *strval;
16702 int error = FALSE;
16703
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016705 numval = get_tv_number_chk(varp, &error);
16706 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016707 if (!error && strval != NULL)
16708 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 }
16710 else
16711 {
16712 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16713 if (winvarname != NULL)
16714 {
16715 STRCPY(winvarname, "w:");
16716 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016717 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 vim_free(winvarname);
16719 }
16720 }
16721
16722#ifdef FEAT_WINDOWS
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016723 restore_win(save_curwin, save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724#endif
16725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016726}
16727
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016728#ifdef FEAT_CRYPT
16729/*
16730 * "sha256({string})" function
16731 */
16732 static void
16733f_sha256(argvars, rettv)
16734 typval_T *argvars;
16735 typval_T *rettv;
16736{
16737 char_u *p;
16738
16739 p = get_tv_string(&argvars[0]);
16740 rettv->vval.v_string = vim_strsave(
16741 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16742 rettv->v_type = VAR_STRING;
16743}
16744#endif /* FEAT_CRYPT */
16745
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016747 * "shellescape({string})" function
16748 */
16749 static void
16750f_shellescape(argvars, rettv)
16751 typval_T *argvars;
16752 typval_T *rettv;
16753{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016754 rettv->vval.v_string = vim_strsave_shellescape(
16755 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016756 rettv->v_type = VAR_STRING;
16757}
16758
16759/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016760 * shiftwidth() function
16761 */
16762 static void
16763f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016764 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016765 typval_T *rettv;
16766{
16767 rettv->vval.v_number = get_sw_value();
16768}
16769
16770/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016771 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 */
16773 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016774f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016775 typval_T *argvars;
16776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016778 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779
Bram Moolenaar0d660222005-01-07 21:51:51 +000016780 p = get_tv_string(&argvars[0]);
16781 rettv->vval.v_string = vim_strsave(p);
16782 simplify_filename(rettv->vval.v_string); /* simplify in place */
16783 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784}
16785
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016786#ifdef FEAT_FLOAT
16787/*
16788 * "sin()" function
16789 */
16790 static void
16791f_sin(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 = sin(f);
16800 else
16801 rettv->vval.v_float = 0.0;
16802}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016803
16804/*
16805 * "sinh()" function
16806 */
16807 static void
16808f_sinh(argvars, rettv)
16809 typval_T *argvars;
16810 typval_T *rettv;
16811{
16812 float_T f;
16813
16814 rettv->v_type = VAR_FLOAT;
16815 if (get_float_arg(argvars, &f) == OK)
16816 rettv->vval.v_float = sinh(f);
16817 else
16818 rettv->vval.v_float = 0.0;
16819}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016820#endif
16821
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822static int
16823#ifdef __BORLANDC__
16824 _RTLENTRYF
16825#endif
16826 item_compare __ARGS((const void *s1, const void *s2));
16827static int
16828#ifdef __BORLANDC__
16829 _RTLENTRYF
16830#endif
16831 item_compare2 __ARGS((const void *s1, const void *s2));
16832
16833static int item_compare_ic;
16834static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016835static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016836static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016837#define ITEM_COMPARE_FAIL 999
16838
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016840 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016842 static int
16843#ifdef __BORLANDC__
16844_RTLENTRYF
16845#endif
16846item_compare(s1, s2)
16847 const void *s1;
16848 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850 char_u *p1, *p2;
16851 char_u *tofree1, *tofree2;
16852 int res;
16853 char_u numbuf1[NUMBUFLEN];
16854 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016855
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016856 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16857 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016858 if (p1 == NULL)
16859 p1 = (char_u *)"";
16860 if (p2 == NULL)
16861 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 if (item_compare_ic)
16863 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 res = STRCMP(p1, p2);
16866 vim_free(tofree1);
16867 vim_free(tofree2);
16868 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869}
16870
16871 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872#ifdef __BORLANDC__
16873_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875item_compare2(s1, s2)
16876 const void *s1;
16877 const void *s2;
16878{
16879 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016881 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016882 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 /* shortcut after failure in previous call; compare all items equal */
16885 if (item_compare_func_err)
16886 return 0;
16887
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016888 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16889 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16891 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892
16893 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016894 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016895 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16896 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897 clear_tv(&argv[0]);
16898 clear_tv(&argv[1]);
16899
16900 if (res == FAIL)
16901 res = ITEM_COMPARE_FAIL;
16902 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016903 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16904 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016905 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 clear_tv(&rettv);
16907 return res;
16908}
16909
16910/*
16911 * "sort({list})" function
16912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016915 typval_T *argvars;
16916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917{
Bram Moolenaar33570922005-01-25 22:26:29 +000016918 list_T *l;
16919 listitem_T *li;
16920 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016921 long len;
16922 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016923
Bram Moolenaar0d660222005-01-07 21:51:51 +000016924 if (argvars[0].v_type != VAR_LIST)
16925 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 else
16927 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016929 if (l == NULL || tv_check_lock(l->lv_lock,
16930 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 return;
16932 rettv->vval.v_list = l;
16933 rettv->v_type = VAR_LIST;
16934 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 len = list_len(l);
16937 if (len <= 1)
16938 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939
Bram Moolenaar0d660222005-01-07 21:51:51 +000016940 item_compare_ic = FALSE;
16941 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016942 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 if (argvars[1].v_type != VAR_UNKNOWN)
16944 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020016945 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016947 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 else
16949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 int error = FALSE;
16951
16952 i = get_tv_number_chk(&argvars[1], &error);
16953 if (error)
16954 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 if (i == 1)
16956 item_compare_ic = TRUE;
16957 else
16958 item_compare_func = get_tv_string(&argvars[1]);
16959 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020016960
16961 if (argvars[2].v_type != VAR_UNKNOWN)
16962 {
16963 /* optional third argument: {dict} */
16964 if (argvars[2].v_type != VAR_DICT)
16965 {
16966 EMSG(_(e_dictreq));
16967 return;
16968 }
16969 item_compare_selfdict = argvars[2].vval.v_dict;
16970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016974 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 if (ptrs == NULL)
16976 return;
16977 i = 0;
16978 for (li = l->lv_first; li != NULL; li = li->li_next)
16979 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016981 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 /* test the compare function */
16983 if (item_compare_func != NULL
16984 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16985 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016986 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988 {
16989 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016990 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991 item_compare_func == NULL ? item_compare : item_compare2);
16992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 if (!item_compare_func_err)
16994 {
16995 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016996 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016997 l->lv_len = 0;
16998 for (i = 0; i < len; ++i)
16999 list_append(l, ptrs[i]);
17000 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001 }
17002
17003 vim_free(ptrs);
17004 }
17005}
17006
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017007/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017008 * "soundfold({word})" function
17009 */
17010 static void
17011f_soundfold(argvars, rettv)
17012 typval_T *argvars;
17013 typval_T *rettv;
17014{
17015 char_u *s;
17016
17017 rettv->v_type = VAR_STRING;
17018 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017019#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017020 rettv->vval.v_string = eval_soundfold(s);
17021#else
17022 rettv->vval.v_string = vim_strsave(s);
17023#endif
17024}
17025
17026/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017027 * "spellbadword()" function
17028 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017029 static void
17030f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017031 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017032 typval_T *rettv;
17033{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017034 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017035 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017036 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017037
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017038 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017039 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017040
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017041#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017042 if (argvars[0].v_type == VAR_UNKNOWN)
17043 {
17044 /* Find the start and length of the badly spelled word. */
17045 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17046 if (len != 0)
17047 word = ml_get_cursor();
17048 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017049 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017050 {
17051 char_u *str = get_tv_string_chk(&argvars[0]);
17052 int capcol = -1;
17053
17054 if (str != NULL)
17055 {
17056 /* Check the argument for spelling. */
17057 while (*str != NUL)
17058 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017059 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017060 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017061 {
17062 word = str;
17063 break;
17064 }
17065 str += len;
17066 }
17067 }
17068 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017069#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017070
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017071 list_append_string(rettv->vval.v_list, word, len);
17072 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017073 attr == HLF_SPB ? "bad" :
17074 attr == HLF_SPR ? "rare" :
17075 attr == HLF_SPL ? "local" :
17076 attr == HLF_SPC ? "caps" :
17077 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017078}
17079
17080/*
17081 * "spellsuggest()" function
17082 */
17083 static void
17084f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017085 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017086 typval_T *rettv;
17087{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017088#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017089 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017090 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017091 int maxcount;
17092 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017093 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017094 listitem_T *li;
17095 int need_capital = FALSE;
17096#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017097
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017098 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017099 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017100
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017101#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017102 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017103 {
17104 str = get_tv_string(&argvars[0]);
17105 if (argvars[1].v_type != VAR_UNKNOWN)
17106 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017107 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017108 if (maxcount <= 0)
17109 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017110 if (argvars[2].v_type != VAR_UNKNOWN)
17111 {
17112 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17113 if (typeerr)
17114 return;
17115 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017116 }
17117 else
17118 maxcount = 25;
17119
Bram Moolenaar4770d092006-01-12 23:22:24 +000017120 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017121
17122 for (i = 0; i < ga.ga_len; ++i)
17123 {
17124 str = ((char_u **)ga.ga_data)[i];
17125
17126 li = listitem_alloc();
17127 if (li == NULL)
17128 vim_free(str);
17129 else
17130 {
17131 li->li_tv.v_type = VAR_STRING;
17132 li->li_tv.v_lock = 0;
17133 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017134 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017135 }
17136 }
17137 ga_clear(&ga);
17138 }
17139#endif
17140}
17141
Bram Moolenaar0d660222005-01-07 21:51:51 +000017142 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017143f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 typval_T *argvars;
17145 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146{
17147 char_u *str;
17148 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017149 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 regmatch_T regmatch;
17151 char_u patbuf[NUMBUFLEN];
17152 char_u *save_cpo;
17153 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017154 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017155 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157
17158 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17159 save_cpo = p_cpo;
17160 p_cpo = (char_u *)"";
17161
17162 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017163 if (argvars[1].v_type != VAR_UNKNOWN)
17164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17166 if (pat == NULL)
17167 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017168 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017170 }
17171 if (pat == NULL || *pat == NUL)
17172 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017174 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017176 if (typeerr)
17177 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17180 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017183 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017185 if (*str == NUL)
17186 match = FALSE; /* empty item at the end */
17187 else
17188 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 if (match)
17190 end = regmatch.startp[0];
17191 else
17192 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017193 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17194 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017196 if (list_append_string(rettv->vval.v_list, str,
17197 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017199 }
17200 if (!match)
17201 break;
17202 /* Advance to just after the match. */
17203 if (regmatch.endp[0] > str)
17204 col = 0;
17205 else
17206 {
17207 /* Don't get stuck at the same match. */
17208#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017209 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210#else
17211 col = 1;
17212#endif
17213 }
17214 str = regmatch.endp[0];
17215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219
Bram Moolenaar0d660222005-01-07 21:51:51 +000017220 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221}
17222
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017223#ifdef FEAT_FLOAT
17224/*
17225 * "sqrt()" function
17226 */
17227 static void
17228f_sqrt(argvars, rettv)
17229 typval_T *argvars;
17230 typval_T *rettv;
17231{
17232 float_T f;
17233
17234 rettv->v_type = VAR_FLOAT;
17235 if (get_float_arg(argvars, &f) == OK)
17236 rettv->vval.v_float = sqrt(f);
17237 else
17238 rettv->vval.v_float = 0.0;
17239}
17240
17241/*
17242 * "str2float()" function
17243 */
17244 static void
17245f_str2float(argvars, rettv)
17246 typval_T *argvars;
17247 typval_T *rettv;
17248{
17249 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17250
17251 if (*p == '+')
17252 p = skipwhite(p + 1);
17253 (void)string2float(p, &rettv->vval.v_float);
17254 rettv->v_type = VAR_FLOAT;
17255}
17256#endif
17257
Bram Moolenaar2c932302006-03-18 21:42:09 +000017258/*
17259 * "str2nr()" function
17260 */
17261 static void
17262f_str2nr(argvars, rettv)
17263 typval_T *argvars;
17264 typval_T *rettv;
17265{
17266 int base = 10;
17267 char_u *p;
17268 long n;
17269
17270 if (argvars[1].v_type != VAR_UNKNOWN)
17271 {
17272 base = get_tv_number(&argvars[1]);
17273 if (base != 8 && base != 10 && base != 16)
17274 {
17275 EMSG(_(e_invarg));
17276 return;
17277 }
17278 }
17279
17280 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017281 if (*p == '+')
17282 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017283 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17284 rettv->vval.v_number = n;
17285}
17286
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287#ifdef HAVE_STRFTIME
17288/*
17289 * "strftime({format}[, {time}])" function
17290 */
17291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017292f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017293 typval_T *argvars;
17294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295{
17296 char_u result_buf[256];
17297 struct tm *curtime;
17298 time_t seconds;
17299 char_u *p;
17300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017301 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017303 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017304 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 seconds = time(NULL);
17306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017307 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 curtime = localtime(&seconds);
17309 /* MSVC returns NULL for an invalid value of seconds. */
17310 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 else
17313 {
17314# ifdef FEAT_MBYTE
17315 vimconv_T conv;
17316 char_u *enc;
17317
17318 conv.vc_type = CONV_NONE;
17319 enc = enc_locale();
17320 convert_setup(&conv, p_enc, enc);
17321 if (conv.vc_type != CONV_NONE)
17322 p = string_convert(&conv, p, NULL);
17323# endif
17324 if (p != NULL)
17325 (void)strftime((char *)result_buf, sizeof(result_buf),
17326 (char *)p, curtime);
17327 else
17328 result_buf[0] = NUL;
17329
17330# ifdef FEAT_MBYTE
17331 if (conv.vc_type != CONV_NONE)
17332 vim_free(p);
17333 convert_setup(&conv, enc, p_enc);
17334 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 else
17337# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339
17340# ifdef FEAT_MBYTE
17341 /* Release conversion descriptors */
17342 convert_setup(&conv, NULL, NULL);
17343 vim_free(enc);
17344# endif
17345 }
17346}
17347#endif
17348
17349/*
17350 * "stridx()" function
17351 */
17352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017353f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017354 typval_T *argvars;
17355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356{
17357 char_u buf[NUMBUFLEN];
17358 char_u *needle;
17359 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017362 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017364 needle = get_tv_string_chk(&argvars[1]);
17365 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017366 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 if (needle == NULL || haystack == NULL)
17368 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 if (argvars[2].v_type != VAR_UNKNOWN)
17371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017372 int error = FALSE;
17373
17374 start_idx = get_tv_number_chk(&argvars[2], &error);
17375 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017376 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017377 if (start_idx >= 0)
17378 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017379 }
17380
17381 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17382 if (pos != NULL)
17383 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384}
17385
17386/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017387 * "string()" function
17388 */
17389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017391 typval_T *argvars;
17392 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017393{
17394 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017395 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017397 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017398 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017399 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017400 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402}
17403
17404/*
17405 * "strlen()" function
17406 */
17407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017408f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017409 typval_T *argvars;
17410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017412 rettv->vval.v_number = (varnumber_T)(STRLEN(
17413 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414}
17415
17416/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017417 * "strchars()" function
17418 */
17419 static void
17420f_strchars(argvars, rettv)
17421 typval_T *argvars;
17422 typval_T *rettv;
17423{
17424 char_u *s = get_tv_string(&argvars[0]);
17425#ifdef FEAT_MBYTE
17426 varnumber_T len = 0;
17427
17428 while (*s != NUL)
17429 {
17430 mb_cptr2char_adv(&s);
17431 ++len;
17432 }
17433 rettv->vval.v_number = len;
17434#else
17435 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17436#endif
17437}
17438
17439/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017440 * "strdisplaywidth()" function
17441 */
17442 static void
17443f_strdisplaywidth(argvars, rettv)
17444 typval_T *argvars;
17445 typval_T *rettv;
17446{
17447 char_u *s = get_tv_string(&argvars[0]);
17448 int col = 0;
17449
17450 if (argvars[1].v_type != VAR_UNKNOWN)
17451 col = get_tv_number(&argvars[1]);
17452
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017453 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017454}
17455
17456/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017457 * "strwidth()" function
17458 */
17459 static void
17460f_strwidth(argvars, rettv)
17461 typval_T *argvars;
17462 typval_T *rettv;
17463{
17464 char_u *s = get_tv_string(&argvars[0]);
17465
17466 rettv->vval.v_number = (varnumber_T)(
17467#ifdef FEAT_MBYTE
17468 mb_string2cells(s, -1)
17469#else
17470 STRLEN(s)
17471#endif
17472 );
17473}
17474
17475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 * "strpart()" function
17477 */
17478 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017479f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017480 typval_T *argvars;
17481 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482{
17483 char_u *p;
17484 int n;
17485 int len;
17486 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017487 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 slen = (int)STRLEN(p);
17491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017492 n = get_tv_number_chk(&argvars[1], &error);
17493 if (error)
17494 len = 0;
17495 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 else
17498 len = slen - n; /* default len: all bytes that are available. */
17499
17500 /*
17501 * Only return the overlap between the specified part and the actual
17502 * string.
17503 */
17504 if (n < 0)
17505 {
17506 len += n;
17507 n = 0;
17508 }
17509 else if (n > slen)
17510 n = slen;
17511 if (len < 0)
17512 len = 0;
17513 else if (n + len > slen)
17514 len = slen - n;
17515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017516 rettv->v_type = VAR_STRING;
17517 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518}
17519
17520/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017521 * "strridx()" function
17522 */
17523 static void
17524f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 typval_T *argvars;
17526 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017527{
17528 char_u buf[NUMBUFLEN];
17529 char_u *needle;
17530 char_u *haystack;
17531 char_u *rest;
17532 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017533 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017535 needle = get_tv_string_chk(&argvars[1]);
17536 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537
17538 rettv->vval.v_number = -1;
17539 if (needle == NULL || haystack == NULL)
17540 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017541
17542 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017543 if (argvars[2].v_type != VAR_UNKNOWN)
17544 {
17545 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017546 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017547 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017548 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017549 }
17550 else
17551 end_idx = haystack_len;
17552
Bram Moolenaar0d660222005-01-07 21:51:51 +000017553 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017554 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017556 lastmatch = haystack + end_idx;
17557 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017558 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017559 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560 for (rest = haystack; *rest != '\0'; ++rest)
17561 {
17562 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017563 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 break;
17565 lastmatch = rest;
17566 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017567 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017568
17569 if (lastmatch == NULL)
17570 rettv->vval.v_number = -1;
17571 else
17572 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17573}
17574
17575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017576 * "strtrans()" function
17577 */
17578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017579f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017580 typval_T *argvars;
17581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017583 rettv->v_type = VAR_STRING;
17584 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585}
17586
17587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 * "submatch()" function
17589 */
17590 static void
17591f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017592 typval_T *argvars;
17593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017594{
17595 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017596 rettv->vval.v_string =
17597 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017598}
17599
17600/*
17601 * "substitute()" function
17602 */
17603 static void
17604f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017605 typval_T *argvars;
17606 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017607{
17608 char_u patbuf[NUMBUFLEN];
17609 char_u subbuf[NUMBUFLEN];
17610 char_u flagsbuf[NUMBUFLEN];
17611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017612 char_u *str = get_tv_string_chk(&argvars[0]);
17613 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17614 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17615 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17616
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017618 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17619 rettv->vval.v_string = NULL;
17620 else
17621 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017622}
17623
17624/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017625 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017629 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631{
17632 int id = 0;
17633#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017634 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 long col;
17636 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017637 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017639 lnum = get_tv_lnum(argvars); /* -1 on type error */
17640 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17641 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017643 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017644 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017645 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646#endif
17647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017648 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649}
17650
17651/*
17652 * "synIDattr(id, what [, mode])" function
17653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017656 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658{
17659 char_u *p = NULL;
17660#ifdef FEAT_SYN_HL
17661 int id;
17662 char_u *what;
17663 char_u *mode;
17664 char_u modebuf[NUMBUFLEN];
17665 int modec;
17666
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017667 id = get_tv_number(&argvars[0]);
17668 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017669 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017673 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 modec = 0; /* replace invalid with current */
17675 }
17676 else
17677 {
17678#ifdef FEAT_GUI
17679 if (gui.in_use)
17680 modec = 'g';
17681 else
17682#endif
17683 if (t_colors > 1)
17684 modec = 'c';
17685 else
17686 modec = 't';
17687 }
17688
17689
17690 switch (TOLOWER_ASC(what[0]))
17691 {
17692 case 'b':
17693 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17694 p = highlight_color(id, what, modec);
17695 else /* bold */
17696 p = highlight_has_attr(id, HL_BOLD, modec);
17697 break;
17698
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017699 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 p = highlight_color(id, what, modec);
17701 break;
17702
17703 case 'i':
17704 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17705 p = highlight_has_attr(id, HL_INVERSE, modec);
17706 else /* italic */
17707 p = highlight_has_attr(id, HL_ITALIC, modec);
17708 break;
17709
17710 case 'n': /* name */
17711 p = get_highlight_name(NULL, id - 1);
17712 break;
17713
17714 case 'r': /* reverse */
17715 p = highlight_has_attr(id, HL_INVERSE, modec);
17716 break;
17717
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017718 case 's':
17719 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17720 p = highlight_color(id, what, modec);
17721 else /* standout */
17722 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 break;
17724
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017725 case 'u':
17726 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17727 /* underline */
17728 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17729 else
17730 /* undercurl */
17731 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 break;
17733 }
17734
17735 if (p != NULL)
17736 p = vim_strsave(p);
17737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738 rettv->v_type = VAR_STRING;
17739 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740}
17741
17742/*
17743 * "synIDtrans(id)" function
17744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017746f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749{
17750 int id;
17751
17752#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754
17755 if (id > 0)
17756 id = syn_get_final_id(id);
17757 else
17758#endif
17759 id = 0;
17760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017761 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762}
17763
17764/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017765 * "synconcealed(lnum, col)" function
17766 */
17767 static void
17768f_synconcealed(argvars, rettv)
17769 typval_T *argvars UNUSED;
17770 typval_T *rettv;
17771{
17772#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17773 long lnum;
17774 long col;
17775 int syntax_flags = 0;
17776 int cchar;
17777 int matchid = 0;
17778 char_u str[NUMBUFLEN];
17779#endif
17780
17781 rettv->v_type = VAR_LIST;
17782 rettv->vval.v_list = NULL;
17783
17784#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17785 lnum = get_tv_lnum(argvars); /* -1 on type error */
17786 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17787
17788 vim_memset(str, NUL, sizeof(str));
17789
17790 if (rettv_list_alloc(rettv) != FAIL)
17791 {
17792 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17793 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17794 && curwin->w_p_cole > 0)
17795 {
17796 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17797 syntax_flags = get_syntax_info(&matchid);
17798
17799 /* get the conceal character */
17800 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17801 {
17802 cchar = syn_get_sub_char();
17803 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17804 cchar = lcs_conceal;
17805 if (cchar != NUL)
17806 {
17807# ifdef FEAT_MBYTE
17808 if (has_mbyte)
17809 (*mb_char2bytes)(cchar, str);
17810 else
17811# endif
17812 str[0] = cchar;
17813 }
17814 }
17815 }
17816
17817 list_append_number(rettv->vval.v_list,
17818 (syntax_flags & HL_CONCEAL) != 0);
17819 /* -1 to auto-determine strlen */
17820 list_append_string(rettv->vval.v_list, str, -1);
17821 list_append_number(rettv->vval.v_list, matchid);
17822 }
17823#endif
17824}
17825
17826/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017827 * "synstack(lnum, col)" function
17828 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017829 static void
17830f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017831 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017832 typval_T *rettv;
17833{
17834#ifdef FEAT_SYN_HL
17835 long lnum;
17836 long col;
17837 int i;
17838 int id;
17839#endif
17840
17841 rettv->v_type = VAR_LIST;
17842 rettv->vval.v_list = NULL;
17843
17844#ifdef FEAT_SYN_HL
17845 lnum = get_tv_lnum(argvars); /* -1 on type error */
17846 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17847
17848 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017849 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017850 && rettv_list_alloc(rettv) != FAIL)
17851 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017852 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017853 for (i = 0; ; ++i)
17854 {
17855 id = syn_get_stack_item(i);
17856 if (id < 0)
17857 break;
17858 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17859 break;
17860 }
17861 }
17862#endif
17863}
17864
17865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 * "system()" function
17867 */
17868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017870 typval_T *argvars;
17871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017873 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017875 char_u *infile = NULL;
17876 char_u buf[NUMBUFLEN];
17877 int err = FALSE;
17878 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017880 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017881 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017882
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017884 {
17885 /*
17886 * Write the string to a temp file, to be used for input of the shell
17887 * command.
17888 */
17889 if ((infile = vim_tempname('i')) == NULL)
17890 {
17891 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017892 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017893 }
17894
17895 fd = mch_fopen((char *)infile, WRITEBIN);
17896 if (fd == NULL)
17897 {
17898 EMSG2(_(e_notopen), infile);
17899 goto done;
17900 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017901 p = get_tv_string_buf_chk(&argvars[1], buf);
17902 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017903 {
17904 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017905 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017906 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017907 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17908 err = TRUE;
17909 if (fclose(fd) != 0)
17910 err = TRUE;
17911 if (err)
17912 {
17913 EMSG(_("E677: Error writing temp file"));
17914 goto done;
17915 }
17916 }
17917
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017918 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17919 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017920
Bram Moolenaar071d4272004-06-13 20:20:40 +000017921#ifdef USE_CR
17922 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017923 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 {
17925 char_u *s;
17926
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017927 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 {
17929 if (*s == CAR)
17930 *s = NL;
17931 }
17932 }
17933#else
17934# ifdef USE_CRNL
17935 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017936 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 {
17938 char_u *s, *d;
17939
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017940 d = res;
17941 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 {
17943 if (s[0] == CAR && s[1] == NL)
17944 ++s;
17945 *d++ = *s;
17946 }
17947 *d = NUL;
17948 }
17949# endif
17950#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017951
17952done:
17953 if (infile != NULL)
17954 {
17955 mch_remove(infile);
17956 vim_free(infile);
17957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958 rettv->v_type = VAR_STRING;
17959 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960}
17961
17962/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017963 * "tabpagebuflist()" function
17964 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017965 static void
17966f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017967 typval_T *argvars UNUSED;
17968 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017969{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017970#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017971 tabpage_T *tp;
17972 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017973
17974 if (argvars[0].v_type == VAR_UNKNOWN)
17975 wp = firstwin;
17976 else
17977 {
17978 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17979 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017980 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017981 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017982 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017983 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017984 for (; wp != NULL; wp = wp->w_next)
17985 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017986 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017987 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017988 }
17989#endif
17990}
17991
17992
17993/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017994 * "tabpagenr()" function
17995 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017996 static void
17997f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017998 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017999 typval_T *rettv;
18000{
18001 int nr = 1;
18002#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018003 char_u *arg;
18004
18005 if (argvars[0].v_type != VAR_UNKNOWN)
18006 {
18007 arg = get_tv_string_chk(&argvars[0]);
18008 nr = 0;
18009 if (arg != NULL)
18010 {
18011 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018012 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018013 else
18014 EMSG2(_(e_invexpr2), arg);
18015 }
18016 }
18017 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018018 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018019#endif
18020 rettv->vval.v_number = nr;
18021}
18022
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018023
18024#ifdef FEAT_WINDOWS
18025static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18026
18027/*
18028 * Common code for tabpagewinnr() and winnr().
18029 */
18030 static int
18031get_winnr(tp, argvar)
18032 tabpage_T *tp;
18033 typval_T *argvar;
18034{
18035 win_T *twin;
18036 int nr = 1;
18037 win_T *wp;
18038 char_u *arg;
18039
18040 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18041 if (argvar->v_type != VAR_UNKNOWN)
18042 {
18043 arg = get_tv_string_chk(argvar);
18044 if (arg == NULL)
18045 nr = 0; /* type error; errmsg already given */
18046 else if (STRCMP(arg, "$") == 0)
18047 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18048 else if (STRCMP(arg, "#") == 0)
18049 {
18050 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18051 if (twin == NULL)
18052 nr = 0;
18053 }
18054 else
18055 {
18056 EMSG2(_(e_invexpr2), arg);
18057 nr = 0;
18058 }
18059 }
18060
18061 if (nr > 0)
18062 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18063 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018064 {
18065 if (wp == NULL)
18066 {
18067 /* didn't find it in this tabpage */
18068 nr = 0;
18069 break;
18070 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018071 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018072 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018073 return nr;
18074}
18075#endif
18076
18077/*
18078 * "tabpagewinnr()" function
18079 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018080 static void
18081f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018082 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018083 typval_T *rettv;
18084{
18085 int nr = 1;
18086#ifdef FEAT_WINDOWS
18087 tabpage_T *tp;
18088
18089 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18090 if (tp == NULL)
18091 nr = 0;
18092 else
18093 nr = get_winnr(tp, &argvars[1]);
18094#endif
18095 rettv->vval.v_number = nr;
18096}
18097
18098
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018099/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018100 * "tagfiles()" function
18101 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018102 static void
18103f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018104 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018105 typval_T *rettv;
18106{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018107 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018108 tagname_T tn;
18109 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018110
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018111 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018112 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018113 fname = alloc(MAXPATHL);
18114 if (fname == NULL)
18115 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018116
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018117 for (first = TRUE; ; first = FALSE)
18118 if (get_tagfname(&tn, first, fname) == FAIL
18119 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018120 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018121 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018122 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018123}
18124
18125/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018126 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018127 */
18128 static void
18129f_taglist(argvars, rettv)
18130 typval_T *argvars;
18131 typval_T *rettv;
18132{
18133 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018134
18135 tag_pattern = get_tv_string(&argvars[0]);
18136
18137 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018138 if (*tag_pattern == NUL)
18139 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018140
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018141 if (rettv_list_alloc(rettv) == OK)
18142 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018143}
18144
18145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 * "tempname()" function
18147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018149f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018150 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152{
18153 static int x = 'A';
18154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155 rettv->v_type = VAR_STRING;
18156 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157
18158 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18159 * names. Skip 'I' and 'O', they are used for shell redirection. */
18160 do
18161 {
18162 if (x == 'Z')
18163 x = '0';
18164 else if (x == '9')
18165 x = 'A';
18166 else
18167 {
18168#ifdef EBCDIC
18169 if (x == 'I')
18170 x = 'J';
18171 else if (x == 'R')
18172 x = 'S';
18173 else
18174#endif
18175 ++x;
18176 }
18177 } while (x == 'I' || x == 'O');
18178}
18179
18180/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018181 * "test(list)" function: Just checking the walls...
18182 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018183 static void
18184f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018185 typval_T *argvars UNUSED;
18186 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018187{
18188 /* Used for unit testing. Change the code below to your liking. */
18189#if 0
18190 listitem_T *li;
18191 list_T *l;
18192 char_u *bad, *good;
18193
18194 if (argvars[0].v_type != VAR_LIST)
18195 return;
18196 l = argvars[0].vval.v_list;
18197 if (l == NULL)
18198 return;
18199 li = l->lv_first;
18200 if (li == NULL)
18201 return;
18202 bad = get_tv_string(&li->li_tv);
18203 li = li->li_next;
18204 if (li == NULL)
18205 return;
18206 good = get_tv_string(&li->li_tv);
18207 rettv->vval.v_number = test_edit_score(bad, good);
18208#endif
18209}
18210
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018211#ifdef FEAT_FLOAT
18212/*
18213 * "tan()" function
18214 */
18215 static void
18216f_tan(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 = tan(f);
18225 else
18226 rettv->vval.v_float = 0.0;
18227}
18228
18229/*
18230 * "tanh()" function
18231 */
18232 static void
18233f_tanh(argvars, rettv)
18234 typval_T *argvars;
18235 typval_T *rettv;
18236{
18237 float_T f;
18238
18239 rettv->v_type = VAR_FLOAT;
18240 if (get_float_arg(argvars, &f) == OK)
18241 rettv->vval.v_float = tanh(f);
18242 else
18243 rettv->vval.v_float = 0.0;
18244}
18245#endif
18246
Bram Moolenaard52d9742005-08-21 22:20:28 +000018247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 * "tolower(string)" function
18249 */
18250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018251f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018252 typval_T *argvars;
18253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254{
18255 char_u *p;
18256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257 p = vim_strsave(get_tv_string(&argvars[0]));
18258 rettv->v_type = VAR_STRING;
18259 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260
18261 if (p != NULL)
18262 while (*p != NUL)
18263 {
18264#ifdef FEAT_MBYTE
18265 int l;
18266
18267 if (enc_utf8)
18268 {
18269 int c, lc;
18270
18271 c = utf_ptr2char(p);
18272 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018273 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 /* TODO: reallocate string when byte count changes. */
18275 if (utf_char2len(lc) == l)
18276 utf_char2bytes(lc, p);
18277 p += l;
18278 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018279 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 p += l; /* skip multi-byte character */
18281 else
18282#endif
18283 {
18284 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18285 ++p;
18286 }
18287 }
18288}
18289
18290/*
18291 * "toupper(string)" function
18292 */
18293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018294f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018295 typval_T *argvars;
18296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018298 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018299 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300}
18301
18302/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018303 * "tr(string, fromstr, tostr)" function
18304 */
18305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018307 typval_T *argvars;
18308 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018309{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018310 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018311 char_u *fromstr;
18312 char_u *tostr;
18313 char_u *p;
18314#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018315 int inlen;
18316 int fromlen;
18317 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018318 int idx;
18319 char_u *cpstr;
18320 int cplen;
18321 int first = TRUE;
18322#endif
18323 char_u buf[NUMBUFLEN];
18324 char_u buf2[NUMBUFLEN];
18325 garray_T ga;
18326
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018327 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018328 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18329 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018330
18331 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018332 rettv->v_type = VAR_STRING;
18333 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018334 if (fromstr == NULL || tostr == NULL)
18335 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018336 ga_init2(&ga, (int)sizeof(char), 80);
18337
18338#ifdef FEAT_MBYTE
18339 if (!has_mbyte)
18340#endif
18341 /* not multi-byte: fromstr and tostr must be the same length */
18342 if (STRLEN(fromstr) != STRLEN(tostr))
18343 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018344#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018345error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018346#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018347 EMSG2(_(e_invarg2), fromstr);
18348 ga_clear(&ga);
18349 return;
18350 }
18351
18352 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018353 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018354 {
18355#ifdef FEAT_MBYTE
18356 if (has_mbyte)
18357 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018358 inlen = (*mb_ptr2len)(in_str);
18359 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018360 cplen = inlen;
18361 idx = 0;
18362 for (p = fromstr; *p != NUL; p += fromlen)
18363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018364 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018365 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018366 {
18367 for (p = tostr; *p != NUL; p += tolen)
18368 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018369 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018370 if (idx-- == 0)
18371 {
18372 cplen = tolen;
18373 cpstr = p;
18374 break;
18375 }
18376 }
18377 if (*p == NUL) /* tostr is shorter than fromstr */
18378 goto error;
18379 break;
18380 }
18381 ++idx;
18382 }
18383
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018384 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018385 {
18386 /* Check that fromstr and tostr have the same number of
18387 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018388 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018389 first = FALSE;
18390 for (p = tostr; *p != NUL; p += tolen)
18391 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018392 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018393 --idx;
18394 }
18395 if (idx != 0)
18396 goto error;
18397 }
18398
18399 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018400 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018401 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018402
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018403 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018404 }
18405 else
18406#endif
18407 {
18408 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018409 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018410 if (p != NULL)
18411 ga_append(&ga, tostr[p - fromstr]);
18412 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018413 ga_append(&ga, *in_str);
18414 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018415 }
18416 }
18417
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018418 /* add a terminating NUL */
18419 ga_grow(&ga, 1);
18420 ga_append(&ga, NUL);
18421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018422 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018423}
18424
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018425#ifdef FEAT_FLOAT
18426/*
18427 * "trunc({float})" function
18428 */
18429 static void
18430f_trunc(argvars, rettv)
18431 typval_T *argvars;
18432 typval_T *rettv;
18433{
18434 float_T f;
18435
18436 rettv->v_type = VAR_FLOAT;
18437 if (get_float_arg(argvars, &f) == OK)
18438 /* trunc() is not in C90, use floor() or ceil() instead. */
18439 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18440 else
18441 rettv->vval.v_float = 0.0;
18442}
18443#endif
18444
Bram Moolenaar8299df92004-07-10 09:47:34 +000018445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446 * "type(expr)" function
18447 */
18448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018450 typval_T *argvars;
18451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018453 int n;
18454
18455 switch (argvars[0].v_type)
18456 {
18457 case VAR_NUMBER: n = 0; break;
18458 case VAR_STRING: n = 1; break;
18459 case VAR_FUNC: n = 2; break;
18460 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018461 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018462#ifdef FEAT_FLOAT
18463 case VAR_FLOAT: n = 5; break;
18464#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018465 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18466 }
18467 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468}
18469
18470/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018471 * "undofile(name)" function
18472 */
18473 static void
18474f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018475 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018476 typval_T *rettv;
18477{
18478 rettv->v_type = VAR_STRING;
18479#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018480 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018481 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018482
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018483 if (*fname == NUL)
18484 {
18485 /* If there is no file name there will be no undo file. */
18486 rettv->vval.v_string = NULL;
18487 }
18488 else
18489 {
18490 char_u *ffname = FullName_save(fname, FALSE);
18491
18492 if (ffname != NULL)
18493 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18494 vim_free(ffname);
18495 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018496 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018497#else
18498 rettv->vval.v_string = NULL;
18499#endif
18500}
18501
18502/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018503 * "undotree()" function
18504 */
18505 static void
18506f_undotree(argvars, rettv)
18507 typval_T *argvars UNUSED;
18508 typval_T *rettv;
18509{
18510 if (rettv_dict_alloc(rettv) == OK)
18511 {
18512 dict_T *dict = rettv->vval.v_dict;
18513 list_T *list;
18514
Bram Moolenaar730cde92010-06-27 05:18:54 +020018515 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018516 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018517 dict_add_nr_str(dict, "save_last",
18518 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018519 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18520 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018521 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018522
18523 list = list_alloc();
18524 if (list != NULL)
18525 {
18526 u_eval_tree(curbuf->b_u_oldhead, list);
18527 dict_add_list(dict, "entries", list);
18528 }
18529 }
18530}
18531
18532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018533 * "values(dict)" function
18534 */
18535 static void
18536f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018537 typval_T *argvars;
18538 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018539{
18540 dict_list(argvars, rettv, 1);
18541}
18542
18543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 * "virtcol(string)" function
18545 */
18546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018548 typval_T *argvars;
18549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550{
18551 colnr_T vcol = 0;
18552 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018553 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018555 fp = var2fpos(&argvars[0], FALSE, &fnum);
18556 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18557 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018558 {
18559 getvvcol(curwin, fp, NULL, NULL, &vcol);
18560 ++vcol;
18561 }
18562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018563 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564}
18565
18566/*
18567 * "visualmode()" function
18568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018570f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018571 typval_T *argvars UNUSED;
18572 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573{
18574#ifdef FEAT_VISUAL
18575 char_u str[2];
18576
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 str[0] = curbuf->b_visual_mode_eval;
18579 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018580 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581
18582 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018583 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585#endif
18586}
18587
18588/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018589 * "wildmenumode()" function
18590 */
18591 static void
18592f_wildmenumode(argvars, rettv)
18593 typval_T *argvars UNUSED;
18594 typval_T *rettv UNUSED;
18595{
18596#ifdef FEAT_WILDMENU
18597 if (wild_menu_showing)
18598 rettv->vval.v_number = 1;
18599#endif
18600}
18601
18602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 * "winbufnr(nr)" function
18604 */
18605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018607 typval_T *argvars;
18608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609{
18610 win_T *wp;
18611
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018612 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018614 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617}
18618
18619/*
18620 * "wincol()" function
18621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018623f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018624 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626{
18627 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018628 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629}
18630
18631/*
18632 * "winheight(nr)" function
18633 */
18634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T *argvars;
18637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638{
18639 win_T *wp;
18640
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018641 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018642 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018643 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018645 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646}
18647
18648/*
18649 * "winline()" function
18650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018652f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018653 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655{
18656 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658}
18659
18660/*
18661 * "winnr()" function
18662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018665 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
18668 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018669
Bram Moolenaar071d4272004-06-13 20:20:40 +000018670#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018671 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018673 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018674}
18675
18676/*
18677 * "winrestcmd()" function
18678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683{
18684#ifdef FEAT_WINDOWS
18685 win_T *wp;
18686 int winnr = 1;
18687 garray_T ga;
18688 char_u buf[50];
18689
18690 ga_init2(&ga, (int)sizeof(char), 70);
18691 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18692 {
18693 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18694 ga_concat(&ga, buf);
18695# ifdef FEAT_VERTSPLIT
18696 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18697 ga_concat(&ga, buf);
18698# endif
18699 ++winnr;
18700 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018701 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018703 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708}
18709
18710/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018711 * "winrestview()" function
18712 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018713 static void
18714f_winrestview(argvars, rettv)
18715 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018716 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018717{
18718 dict_T *dict;
18719
18720 if (argvars[0].v_type != VAR_DICT
18721 || (dict = argvars[0].vval.v_dict) == NULL)
18722 EMSG(_(e_invarg));
18723 else
18724 {
18725 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18726 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18727#ifdef FEAT_VIRTUALEDIT
18728 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18729#endif
18730 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018731 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018732
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018733 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018734#ifdef FEAT_DIFF
18735 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18736#endif
18737 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18738 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18739
18740 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018741 win_new_height(curwin, curwin->w_height);
18742# ifdef FEAT_VERTSPLIT
18743 win_new_width(curwin, W_WIDTH(curwin));
18744# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018745 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018746
18747 if (curwin->w_topline == 0)
18748 curwin->w_topline = 1;
18749 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18750 curwin->w_topline = curbuf->b_ml.ml_line_count;
18751#ifdef FEAT_DIFF
18752 check_topfill(curwin, TRUE);
18753#endif
18754 }
18755}
18756
18757/*
18758 * "winsaveview()" function
18759 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018760 static void
18761f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018762 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018763 typval_T *rettv;
18764{
18765 dict_T *dict;
18766
Bram Moolenaara800b422010-06-27 01:15:55 +020018767 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018768 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018769 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018770
18771 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18772 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18773#ifdef FEAT_VIRTUALEDIT
18774 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18775#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018776 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018777 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18778
18779 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18780#ifdef FEAT_DIFF
18781 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18782#endif
18783 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18784 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18785}
18786
18787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788 * "winwidth(nr)" function
18789 */
18790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018791f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018792 typval_T *argvars;
18793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794{
18795 win_T *wp;
18796
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018797 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 else
18801#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018802 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805#endif
18806}
18807
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018809 * "writefile()" function
18810 */
18811 static void
18812f_writefile(argvars, rettv)
18813 typval_T *argvars;
18814 typval_T *rettv;
18815{
18816 int binary = FALSE;
18817 char_u *fname;
18818 FILE *fd;
18819 listitem_T *li;
18820 char_u *s;
18821 int ret = 0;
18822 int c;
18823
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018824 if (check_restricted() || check_secure())
18825 return;
18826
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018827 if (argvars[0].v_type != VAR_LIST)
18828 {
18829 EMSG2(_(e_listarg), "writefile()");
18830 return;
18831 }
18832 if (argvars[0].vval.v_list == NULL)
18833 return;
18834
18835 if (argvars[2].v_type != VAR_UNKNOWN
18836 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18837 binary = TRUE;
18838
18839 /* Always open the file in binary mode, library functions have a mind of
18840 * their own about CR-LF conversion. */
18841 fname = get_tv_string(&argvars[1]);
18842 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18843 {
18844 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18845 ret = -1;
18846 }
18847 else
18848 {
18849 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18850 li = li->li_next)
18851 {
18852 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18853 {
18854 if (*s == '\n')
18855 c = putc(NUL, fd);
18856 else
18857 c = putc(*s, fd);
18858 if (c == EOF)
18859 {
18860 ret = -1;
18861 break;
18862 }
18863 }
18864 if (!binary || li->li_next != NULL)
18865 if (putc('\n', fd) == EOF)
18866 {
18867 ret = -1;
18868 break;
18869 }
18870 if (ret < 0)
18871 {
18872 EMSG(_(e_write));
18873 break;
18874 }
18875 }
18876 fclose(fd);
18877 }
18878
18879 rettv->vval.v_number = ret;
18880}
18881
18882/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018883 * "xor(expr, expr)" function
18884 */
18885 static void
18886f_xor(argvars, rettv)
18887 typval_T *argvars;
18888 typval_T *rettv;
18889{
18890 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18891 ^ get_tv_number_chk(&argvars[1], NULL);
18892}
18893
18894
18895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018897 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898 */
18899 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018900var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018902 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018903 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018905 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018907 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908
Bram Moolenaara5525202006-03-02 22:52:09 +000018909 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018910 if (varp->v_type == VAR_LIST)
18911 {
18912 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018913 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018914 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018915 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018916
18917 l = varp->vval.v_list;
18918 if (l == NULL)
18919 return NULL;
18920
18921 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018922 pos.lnum = list_find_nr(l, 0L, &error);
18923 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018924 return NULL; /* invalid line number */
18925
18926 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018927 pos.col = list_find_nr(l, 1L, &error);
18928 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018929 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018930 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018931
18932 /* We accept "$" for the column number: last column. */
18933 li = list_find(l, 1L);
18934 if (li != NULL && li->li_tv.v_type == VAR_STRING
18935 && li->li_tv.vval.v_string != NULL
18936 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18937 pos.col = len + 1;
18938
Bram Moolenaara5525202006-03-02 22:52:09 +000018939 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018940 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018941 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018942 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018943
Bram Moolenaara5525202006-03-02 22:52:09 +000018944#ifdef FEAT_VIRTUALEDIT
18945 /* Get the virtual offset. Defaults to zero. */
18946 pos.coladd = list_find_nr(l, 2L, &error);
18947 if (error)
18948 pos.coladd = 0;
18949#endif
18950
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018951 return &pos;
18952 }
18953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018954 name = get_tv_string_chk(varp);
18955 if (name == NULL)
18956 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018957 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018959#ifdef FEAT_VISUAL
18960 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18961 {
18962 if (VIsual_active)
18963 return &VIsual;
18964 return &curwin->w_cursor;
18965 }
18966#endif
18967 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010018969 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18971 return NULL;
18972 return pp;
18973 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018974
18975#ifdef FEAT_VIRTUALEDIT
18976 pos.coladd = 0;
18977#endif
18978
Bram Moolenaar477933c2007-07-17 14:32:23 +000018979 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018980 {
18981 pos.col = 0;
18982 if (name[1] == '0') /* "w0": first visible line */
18983 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018984 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018985 pos.lnum = curwin->w_topline;
18986 return &pos;
18987 }
18988 else if (name[1] == '$') /* "w$": last visible line */
18989 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018990 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018991 pos.lnum = curwin->w_botline - 1;
18992 return &pos;
18993 }
18994 }
18995 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018997 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 {
18999 pos.lnum = curbuf->b_ml.ml_line_count;
19000 pos.col = 0;
19001 }
19002 else
19003 {
19004 pos.lnum = curwin->w_cursor.lnum;
19005 pos.col = (colnr_T)STRLEN(ml_get_curline());
19006 }
19007 return &pos;
19008 }
19009 return NULL;
19010}
19011
19012/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019013 * Convert list in "arg" into a position and optional file number.
19014 * When "fnump" is NULL there is no file number, only 3 items.
19015 * Note that the column is passed on as-is, the caller may want to decrement
19016 * it to use 1 for the first column.
19017 * Return FAIL when conversion is not possible, doesn't check the position for
19018 * validity.
19019 */
19020 static int
19021list2fpos(arg, posp, fnump)
19022 typval_T *arg;
19023 pos_T *posp;
19024 int *fnump;
19025{
19026 list_T *l = arg->vval.v_list;
19027 long i = 0;
19028 long n;
19029
Bram Moolenaarbde35262006-07-23 20:12:24 +000019030 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19031 * when "fnump" isn't NULL and "coladd" is optional. */
19032 if (arg->v_type != VAR_LIST
19033 || l == NULL
19034 || l->lv_len < (fnump == NULL ? 2 : 3)
19035 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019036 return FAIL;
19037
19038 if (fnump != NULL)
19039 {
19040 n = list_find_nr(l, i++, NULL); /* fnum */
19041 if (n < 0)
19042 return FAIL;
19043 if (n == 0)
19044 n = curbuf->b_fnum; /* current buffer */
19045 *fnump = n;
19046 }
19047
19048 n = list_find_nr(l, i++, NULL); /* lnum */
19049 if (n < 0)
19050 return FAIL;
19051 posp->lnum = n;
19052
19053 n = list_find_nr(l, i++, NULL); /* col */
19054 if (n < 0)
19055 return FAIL;
19056 posp->col = n;
19057
19058#ifdef FEAT_VIRTUALEDIT
19059 n = list_find_nr(l, i, NULL);
19060 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019061 posp->coladd = 0;
19062 else
19063 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019064#endif
19065
19066 return OK;
19067}
19068
19069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070 * Get the length of an environment variable name.
19071 * Advance "arg" to the first character after the name.
19072 * Return 0 for error.
19073 */
19074 static int
19075get_env_len(arg)
19076 char_u **arg;
19077{
19078 char_u *p;
19079 int len;
19080
19081 for (p = *arg; vim_isIDc(*p); ++p)
19082 ;
19083 if (p == *arg) /* no name found */
19084 return 0;
19085
19086 len = (int)(p - *arg);
19087 *arg = p;
19088 return len;
19089}
19090
19091/*
19092 * Get the length of the name of a function or internal variable.
19093 * "arg" is advanced to the first non-white character after the name.
19094 * Return 0 if something is wrong.
19095 */
19096 static int
19097get_id_len(arg)
19098 char_u **arg;
19099{
19100 char_u *p;
19101 int len;
19102
19103 /* Find the end of the name. */
19104 for (p = *arg; eval_isnamec(*p); ++p)
19105 ;
19106 if (p == *arg) /* no name found */
19107 return 0;
19108
19109 len = (int)(p - *arg);
19110 *arg = skipwhite(p);
19111
19112 return len;
19113}
19114
19115/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019116 * Get the length of the name of a variable or function.
19117 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019119 * Return -1 if curly braces expansion failed.
19120 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 * If the name contains 'magic' {}'s, expand them and return the
19122 * expanded name in an allocated string via 'alias' - caller must free.
19123 */
19124 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019125get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 char_u **arg;
19127 char_u **alias;
19128 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019129 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130{
19131 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 char_u *p;
19133 char_u *expr_start;
19134 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
19136 *alias = NULL; /* default to no alias */
19137
19138 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19139 && (*arg)[2] == (int)KE_SNR)
19140 {
19141 /* hard coded <SNR>, already translated */
19142 *arg += 3;
19143 return get_id_len(arg) + 3;
19144 }
19145 len = eval_fname_script(*arg);
19146 if (len > 0)
19147 {
19148 /* literal "<SID>", "s:" or "<SNR>" */
19149 *arg += len;
19150 }
19151
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019153 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019155 p = find_name_end(*arg, &expr_start, &expr_end,
19156 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157 if (expr_start != NULL)
19158 {
19159 char_u *temp_string;
19160
19161 if (!evaluate)
19162 {
19163 len += (int)(p - *arg);
19164 *arg = skipwhite(p);
19165 return len;
19166 }
19167
19168 /*
19169 * Include any <SID> etc in the expanded string:
19170 * Thus the -len here.
19171 */
19172 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19173 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019174 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 *alias = temp_string;
19176 *arg = skipwhite(p);
19177 return (int)STRLEN(temp_string);
19178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179
19180 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019181 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 EMSG2(_(e_invexpr2), *arg);
19183
19184 return len;
19185}
19186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019187/*
19188 * Find the end of a variable or function name, taking care of magic braces.
19189 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19190 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019191 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019192 * Return a pointer to just after the name. Equal to "arg" if there is no
19193 * valid name.
19194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019196find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 char_u *arg;
19198 char_u **expr_start;
19199 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019200 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 int mb_nest = 0;
19203 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 char_u *p;
19205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019206 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019208 *expr_start = NULL;
19209 *expr_end = NULL;
19210 }
19211
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019212 /* Quick check for valid starting character. */
19213 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19214 return arg;
19215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216 for (p = arg; *p != NUL
19217 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019218 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019219 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019221 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019222 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019223 if (*p == '\'')
19224 {
19225 /* skip over 'string' to avoid counting [ and ] inside it. */
19226 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19227 ;
19228 if (*p == NUL)
19229 break;
19230 }
19231 else if (*p == '"')
19232 {
19233 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19234 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19235 if (*p == '\\' && p[1] != NUL)
19236 ++p;
19237 if (*p == NUL)
19238 break;
19239 }
19240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019241 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019243 if (*p == '[')
19244 ++br_nest;
19245 else if (*p == ']')
19246 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019249 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251 if (*p == '{')
19252 {
19253 mb_nest++;
19254 if (expr_start != NULL && *expr_start == NULL)
19255 *expr_start = p;
19256 }
19257 else if (*p == '}')
19258 {
19259 mb_nest--;
19260 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19261 *expr_end = p;
19262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264 }
19265
19266 return p;
19267}
19268
19269/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019270 * Expands out the 'magic' {}'s in a variable/function name.
19271 * Note that this can call itself recursively, to deal with
19272 * constructs like foo{bar}{baz}{bam}
19273 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19274 * "in_start" ^
19275 * "expr_start" ^
19276 * "expr_end" ^
19277 * "in_end" ^
19278 *
19279 * Returns a new allocated string, which the caller must free.
19280 * Returns NULL for failure.
19281 */
19282 static char_u *
19283make_expanded_name(in_start, expr_start, expr_end, in_end)
19284 char_u *in_start;
19285 char_u *expr_start;
19286 char_u *expr_end;
19287 char_u *in_end;
19288{
19289 char_u c1;
19290 char_u *retval = NULL;
19291 char_u *temp_result;
19292 char_u *nextcmd = NULL;
19293
19294 if (expr_end == NULL || in_end == NULL)
19295 return NULL;
19296 *expr_start = NUL;
19297 *expr_end = NUL;
19298 c1 = *in_end;
19299 *in_end = NUL;
19300
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019301 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019302 if (temp_result != NULL && nextcmd == NULL)
19303 {
19304 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19305 + (in_end - expr_end) + 1));
19306 if (retval != NULL)
19307 {
19308 STRCPY(retval, in_start);
19309 STRCAT(retval, temp_result);
19310 STRCAT(retval, expr_end + 1);
19311 }
19312 }
19313 vim_free(temp_result);
19314
19315 *in_end = c1; /* put char back for error messages */
19316 *expr_start = '{';
19317 *expr_end = '}';
19318
19319 if (retval != NULL)
19320 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019321 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019322 if (expr_start != NULL)
19323 {
19324 /* Further expansion! */
19325 temp_result = make_expanded_name(retval, expr_start,
19326 expr_end, temp_result);
19327 vim_free(retval);
19328 retval = temp_result;
19329 }
19330 }
19331
19332 return retval;
19333}
19334
19335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019337 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 */
19339 static int
19340eval_isnamec(c)
19341 int c;
19342{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019343 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19344}
19345
19346/*
19347 * Return TRUE if character "c" can be used as the first character in a
19348 * variable or function name (excluding '{' and '}').
19349 */
19350 static int
19351eval_isnamec1(c)
19352 int c;
19353{
19354 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355}
19356
19357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 * Set number v: variable to "val".
19359 */
19360 void
19361set_vim_var_nr(idx, val)
19362 int idx;
19363 long val;
19364{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019365 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366}
19367
19368/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019369 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 */
19371 long
19372get_vim_var_nr(idx)
19373 int idx;
19374{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019375 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376}
19377
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019378/*
19379 * Get string v: variable value. Uses a static buffer, can only be used once.
19380 */
19381 char_u *
19382get_vim_var_str(idx)
19383 int idx;
19384{
19385 return get_tv_string(&vimvars[idx].vv_tv);
19386}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019387
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019389 * Get List v: variable value. Caller must take care of reference count when
19390 * needed.
19391 */
19392 list_T *
19393get_vim_var_list(idx)
19394 int idx;
19395{
19396 return vimvars[idx].vv_list;
19397}
19398
19399/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019400 * Set v:char to character "c".
19401 */
19402 void
19403set_vim_var_char(c)
19404 int c;
19405{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019406 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019407
19408#ifdef FEAT_MBYTE
19409 if (has_mbyte)
19410 buf[(*mb_char2bytes)(c, buf)] = NUL;
19411 else
19412#endif
19413 {
19414 buf[0] = c;
19415 buf[1] = NUL;
19416 }
19417 set_vim_var_string(VV_CHAR, buf, -1);
19418}
19419
19420/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019421 * Set v:count to "count" and v:count1 to "count1".
19422 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 */
19424 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019425set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 long count;
19427 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019428 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019430 if (set_prevcount)
19431 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019432 vimvars[VV_COUNT].vv_nr = count;
19433 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434}
19435
19436/*
19437 * Set string v: variable to a copy of "val".
19438 */
19439 void
19440set_vim_var_string(idx, val, len)
19441 int idx;
19442 char_u *val;
19443 int len; /* length of "val" to use or -1 (whole string) */
19444{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019445 /* Need to do this (at least) once, since we can't initialize a union.
19446 * Will always be invoked when "v:progname" is set. */
19447 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19448
Bram Moolenaare9a41262005-01-15 22:18:47 +000019449 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019451 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019453 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019455 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456}
19457
19458/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019459 * Set List v: variable to "val".
19460 */
19461 void
19462set_vim_var_list(idx, val)
19463 int idx;
19464 list_T *val;
19465{
19466 list_unref(vimvars[idx].vv_list);
19467 vimvars[idx].vv_list = val;
19468 if (val != NULL)
19469 ++val->lv_refcount;
19470}
19471
19472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 * Set v:register if needed.
19474 */
19475 void
19476set_reg_var(c)
19477 int c;
19478{
19479 char_u regname;
19480
19481 if (c == 0 || c == ' ')
19482 regname = '"';
19483 else
19484 regname = c;
19485 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019486 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 set_vim_var_string(VV_REG, &regname, 1);
19488}
19489
19490/*
19491 * Get or set v:exception. 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:exception! Does not
19494 * take care of memory allocations.
19495 */
19496 char_u *
19497v_exception(oldval)
19498 char_u *oldval;
19499{
19500 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019501 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502
Bram Moolenaare9a41262005-01-15 22:18:47 +000019503 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 return NULL;
19505}
19506
19507/*
19508 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19509 * Otherwise, restore the value to "oldval" and return NULL.
19510 * Must always be called in pairs to save and restore v:throwpoint! Does not
19511 * take care of memory allocations.
19512 */
19513 char_u *
19514v_throwpoint(oldval)
19515 char_u *oldval;
19516{
19517 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019518 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519
Bram Moolenaare9a41262005-01-15 22:18:47 +000019520 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521 return NULL;
19522}
19523
19524#if defined(FEAT_AUTOCMD) || defined(PROTO)
19525/*
19526 * Set v:cmdarg.
19527 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19528 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19529 * Must always be called in pairs!
19530 */
19531 char_u *
19532set_cmdarg(eap, oldarg)
19533 exarg_T *eap;
19534 char_u *oldarg;
19535{
19536 char_u *oldval;
19537 char_u *newval;
19538 unsigned len;
19539
Bram Moolenaare9a41262005-01-15 22:18:47 +000019540 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019541 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019543 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019544 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019545 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546 }
19547
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019548 if (eap->force_bin == FORCE_BIN)
19549 len = 6;
19550 else if (eap->force_bin == FORCE_NOBIN)
19551 len = 8;
19552 else
19553 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019554
19555 if (eap->read_edit)
19556 len += 7;
19557
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019558 if (eap->force_ff != 0)
19559 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19560# ifdef FEAT_MBYTE
19561 if (eap->force_enc != 0)
19562 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019563 if (eap->bad_char != 0)
19564 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019565# endif
19566
19567 newval = alloc(len + 1);
19568 if (newval == NULL)
19569 return NULL;
19570
19571 if (eap->force_bin == FORCE_BIN)
19572 sprintf((char *)newval, " ++bin");
19573 else if (eap->force_bin == FORCE_NOBIN)
19574 sprintf((char *)newval, " ++nobin");
19575 else
19576 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019577
19578 if (eap->read_edit)
19579 STRCAT(newval, " ++edit");
19580
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019581 if (eap->force_ff != 0)
19582 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19583 eap->cmd + eap->force_ff);
19584# ifdef FEAT_MBYTE
19585 if (eap->force_enc != 0)
19586 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19587 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019588 if (eap->bad_char == BAD_KEEP)
19589 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19590 else if (eap->bad_char == BAD_DROP)
19591 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19592 else if (eap->bad_char != 0)
19593 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019594# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019595 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019596 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597}
19598#endif
19599
19600/*
19601 * Get the value of internal variable "name".
19602 * Return OK or FAIL.
19603 */
19604 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019605get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 char_u *name;
19607 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019608 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019609 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610{
19611 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019612 typval_T *tv = NULL;
19613 typval_T atv;
19614 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616
19617 /* truncate the name, so that we can use strcmp() */
19618 cc = name[len];
19619 name[len] = NUL;
19620
19621 /*
19622 * Check for "b:changedtick".
19623 */
19624 if (STRCMP(name, "b:changedtick") == 0)
19625 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019626 atv.v_type = VAR_NUMBER;
19627 atv.vval.v_number = curbuf->b_changedtick;
19628 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629 }
19630
19631 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 * Check for user-defined variables.
19633 */
19634 else
19635 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019636 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 }
19640
Bram Moolenaare9a41262005-01-15 22:18:47 +000019641 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019643 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019644 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 ret = FAIL;
19646 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019647 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019648 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649
19650 name[len] = cc;
19651
19652 return ret;
19653}
19654
19655/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019656 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19657 * Also handle function call with Funcref variable: func(expr)
19658 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19659 */
19660 static int
19661handle_subscript(arg, rettv, evaluate, verbose)
19662 char_u **arg;
19663 typval_T *rettv;
19664 int evaluate; /* do more than finding the end */
19665 int verbose; /* give error messages */
19666{
19667 int ret = OK;
19668 dict_T *selfdict = NULL;
19669 char_u *s;
19670 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019671 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019672
19673 while (ret == OK
19674 && (**arg == '['
19675 || (**arg == '.' && rettv->v_type == VAR_DICT)
19676 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19677 && !vim_iswhite(*(*arg - 1)))
19678 {
19679 if (**arg == '(')
19680 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019681 /* need to copy the funcref so that we can clear rettv */
19682 functv = *rettv;
19683 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019684
19685 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019686 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019687 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019688 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19689 &len, evaluate, selfdict);
19690
19691 /* Clear the funcref afterwards, so that deleting it while
19692 * evaluating the arguments is possible (see test55). */
19693 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019694
19695 /* Stop the expression evaluation when immediately aborting on
19696 * error, or when an interrupt occurred or an exception was thrown
19697 * but not caught. */
19698 if (aborting())
19699 {
19700 if (ret == OK)
19701 clear_tv(rettv);
19702 ret = FAIL;
19703 }
19704 dict_unref(selfdict);
19705 selfdict = NULL;
19706 }
19707 else /* **arg == '[' || **arg == '.' */
19708 {
19709 dict_unref(selfdict);
19710 if (rettv->v_type == VAR_DICT)
19711 {
19712 selfdict = rettv->vval.v_dict;
19713 if (selfdict != NULL)
19714 ++selfdict->dv_refcount;
19715 }
19716 else
19717 selfdict = NULL;
19718 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19719 {
19720 clear_tv(rettv);
19721 ret = FAIL;
19722 }
19723 }
19724 }
19725 dict_unref(selfdict);
19726 return ret;
19727}
19728
19729/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019730 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019731 * value).
19732 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019733 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019734alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019735{
Bram Moolenaar33570922005-01-25 22:26:29 +000019736 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019737}
19738
19739/*
19740 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 * The string "s" must have been allocated, it is consumed.
19742 * Return NULL for out of memory, the variable otherwise.
19743 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019744 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 char_u *s;
19747{
Bram Moolenaar33570922005-01-25 22:26:29 +000019748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019750 rettv = alloc_tv();
19751 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019753 rettv->v_type = VAR_STRING;
19754 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 }
19756 else
19757 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019758 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759}
19760
19761/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019762 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019764 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019766 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
19768 if (varp != NULL)
19769 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019770 switch (varp->v_type)
19771 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019772 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019773 func_unref(varp->vval.v_string);
19774 /*FALLTHROUGH*/
19775 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019776 vim_free(varp->vval.v_string);
19777 break;
19778 case VAR_LIST:
19779 list_unref(varp->vval.v_list);
19780 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019781 case VAR_DICT:
19782 dict_unref(varp->vval.v_dict);
19783 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019784 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019785#ifdef FEAT_FLOAT
19786 case VAR_FLOAT:
19787#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019788 case VAR_UNKNOWN:
19789 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019791 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019792 break;
19793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 vim_free(varp);
19795 }
19796}
19797
19798/*
19799 * Free the memory for a variable value and set the value to NULL or 0.
19800 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019801 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019802clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019803 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804{
19805 if (varp != NULL)
19806 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019807 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019809 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019810 func_unref(varp->vval.v_string);
19811 /*FALLTHROUGH*/
19812 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019813 vim_free(varp->vval.v_string);
19814 varp->vval.v_string = NULL;
19815 break;
19816 case VAR_LIST:
19817 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019818 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019819 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019820 case VAR_DICT:
19821 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019822 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019823 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019824 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019825 varp->vval.v_number = 0;
19826 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019827#ifdef FEAT_FLOAT
19828 case VAR_FLOAT:
19829 varp->vval.v_float = 0.0;
19830 break;
19831#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832 case VAR_UNKNOWN:
19833 break;
19834 default:
19835 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019837 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 }
19839}
19840
19841/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 * Set the value of a variable to NULL without freeing items.
19843 */
19844 static void
19845init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019846 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019847{
19848 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019850}
19851
19852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019853 * Get the number value of a variable.
19854 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019855 * For incompatible types, return 0.
19856 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19857 * caller of incompatible types: it sets *denote to TRUE if "denote"
19858 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 */
19860 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019864 int error = FALSE;
19865
19866 return get_tv_number_chk(varp, &error); /* return 0L on error */
19867}
19868
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019869 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019870get_tv_number_chk(varp, denote)
19871 typval_T *varp;
19872 int *denote;
19873{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019874 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019876 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019878 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019879 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019880#ifdef FEAT_FLOAT
19881 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019882 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019883 break;
19884#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019885 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019886 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 break;
19888 case VAR_STRING:
19889 if (varp->vval.v_string != NULL)
19890 vim_str2nr(varp->vval.v_string, NULL, NULL,
19891 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019892 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019893 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019894 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019895 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019896 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019897 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019898 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019899 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019900 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019903 if (denote == NULL) /* useful for values that must be unsigned */
19904 n = -1;
19905 else
19906 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908}
19909
19910/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019911 * Get the lnum from the first argument.
19912 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019913 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 */
19915 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019916get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019917 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918{
Bram Moolenaar33570922005-01-25 22:26:29 +000019919 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 linenr_T lnum;
19921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019922 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 if (lnum == 0) /* no valid number, try using line() */
19924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925 rettv.v_type = VAR_NUMBER;
19926 f_line(argvars, &rettv);
19927 lnum = rettv.vval.v_number;
19928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 }
19930 return lnum;
19931}
19932
19933/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019934 * Get the lnum from the first argument.
19935 * Also accepts "$", then "buf" is used.
19936 * Returns 0 on error.
19937 */
19938 static linenr_T
19939get_tv_lnum_buf(argvars, buf)
19940 typval_T *argvars;
19941 buf_T *buf;
19942{
19943 if (argvars[0].v_type == VAR_STRING
19944 && argvars[0].vval.v_string != NULL
19945 && argvars[0].vval.v_string[0] == '$'
19946 && buf != NULL)
19947 return buf->b_ml.ml_line_count;
19948 return get_tv_number_chk(&argvars[0], NULL);
19949}
19950
19951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 * Get the string value of a variable.
19953 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019954 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19955 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019956 * If the String variable has never been set, return an empty string.
19957 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19959 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 */
19961 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019962get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019963 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964{
19965 static char_u mybuf[NUMBUFLEN];
19966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968}
19969
19970 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019972 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019973 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 char_u *res = get_tv_string_buf_chk(varp, buf);
19976
19977 return res != NULL ? res : (char_u *)"";
19978}
19979
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019980 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019981get_tv_string_chk(varp)
19982 typval_T *varp;
19983{
19984 static char_u mybuf[NUMBUFLEN];
19985
19986 return get_tv_string_buf_chk(varp, mybuf);
19987}
19988
19989 static char_u *
19990get_tv_string_buf_chk(varp, buf)
19991 typval_T *varp;
19992 char_u *buf;
19993{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019994 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019996 case VAR_NUMBER:
19997 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19998 return buf;
19999 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020000 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020001 break;
20002 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020003 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020004 break;
20005 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020006 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020007 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020008#ifdef FEAT_FLOAT
20009 case VAR_FLOAT:
20010 EMSG(_("E806: using Float as a String"));
20011 break;
20012#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020013 case VAR_STRING:
20014 if (varp->vval.v_string != NULL)
20015 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020016 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020017 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020021 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022}
20023
20024/*
20025 * Find variable "name" in the list of variables.
20026 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020027 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020028 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020031 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020032find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038
Bram Moolenaara7043832005-01-21 11:56:39 +000020039 ht = find_var_ht(name, &varname);
20040 if (htp != NULL)
20041 *htp = ht;
20042 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020044 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045}
20046
20047/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020048 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020049 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020051 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020052find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020054 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020055 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020056 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020057{
Bram Moolenaar33570922005-01-25 22:26:29 +000020058 hashitem_T *hi;
20059
20060 if (*varname == NUL)
20061 {
20062 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020063 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020065 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 case 'g': return &globvars_var;
20067 case 'v': return &vimvars_var;
20068 case 'b': return &curbuf->b_bufvar;
20069 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020070#ifdef FEAT_WINDOWS
20071 case 't': return &curtab->tp_winvar;
20072#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020073 case 'l': return current_funccal == NULL
20074 ? NULL : &current_funccal->l_vars_var;
20075 case 'a': return current_funccal == NULL
20076 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020077 }
20078 return NULL;
20079 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020080
20081 hi = hash_find(ht, varname);
20082 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020083 {
20084 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020085 * worked find the variable again. Don't auto-load a script if it was
20086 * loaded already, otherwise it would be loaded every time when
20087 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020088 if (ht == &globvarht && !writing)
20089 {
20090 /* Note: script_autoload() may make "hi" invalid. It must either
20091 * be obtained again or not used. */
20092 if (!script_autoload(varname, FALSE) || aborting())
20093 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020094 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020095 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020096 if (HASHITEM_EMPTY(hi))
20097 return NULL;
20098 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020099 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020100}
20101
20102/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020103 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020104 * Set "varname" to the start of name without ':'.
20105 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020107find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 char_u *name;
20109 char_u **varname;
20110{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020111 hashitem_T *hi;
20112
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 if (name[1] != ':')
20114 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020115 /* The name must not start with a colon or #. */
20116 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 return NULL;
20118 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020119
20120 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020121 hi = hash_find(&compat_hashtab, name);
20122 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020123 return &compat_hashtab;
20124
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020126 return &globvarht; /* global variable */
20127 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 }
20129 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020130 if (*name == 'g') /* global variable */
20131 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020132 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20133 */
20134 if (vim_strchr(name + 2, ':') != NULL
20135 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020136 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020138 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020140 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020141#ifdef FEAT_WINDOWS
20142 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020143 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020144#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020145 if (*name == 'v') /* v: variable */
20146 return &vimvarht;
20147 if (*name == 'a' && current_funccal != NULL) /* function argument */
20148 return &current_funccal->l_avars.dv_hashtab;
20149 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20150 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 if (*name == 's' /* script variable */
20152 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20153 return &SCRIPT_VARS(current_SID);
20154 return NULL;
20155}
20156
20157/*
20158 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020159 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 * Returns NULL when it doesn't exist.
20161 */
20162 char_u *
20163get_var_value(name)
20164 char_u *name;
20165{
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167
Bram Moolenaara7043832005-01-21 11:56:39 +000020168 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 if (v == NULL)
20170 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020171 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172}
20173
20174/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 * sourcing this script and when executing functions defined in the script.
20177 */
20178 void
20179new_script_vars(id)
20180 scid_T id;
20181{
Bram Moolenaara7043832005-01-21 11:56:39 +000020182 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 hashtab_T *ht;
20184 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020185
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20187 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020188 /* Re-allocating ga_data means that an ht_array pointing to
20189 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020190 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020191 for (i = 1; i <= ga_scripts.ga_len; ++i)
20192 {
20193 ht = &SCRIPT_VARS(i);
20194 if (ht->ht_mask == HT_INIT_SIZE - 1)
20195 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020196 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020197 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020198 }
20199
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 while (ga_scripts.ga_len < id)
20201 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020202 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020203 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020204 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 }
20207 }
20208}
20209
20210/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20212 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 */
20214 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020215init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 dict_T *dict;
20217 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020218 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219{
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020221 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020222 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020223 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020224 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 dict_var->di_tv.vval.v_dict = dict;
20226 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020227 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20229 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230}
20231
20232/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020233 * Unreference a dictionary initialized by init_var_dict().
20234 */
20235 void
20236unref_var_dict(dict)
20237 dict_T *dict;
20238{
20239 /* Now the dict needs to be freed if no one else is using it, go back to
20240 * normal reference counting. */
20241 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20242 dict_unref(dict);
20243}
20244
20245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 * Frees all allocated variables and the value they contain.
20248 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 */
20250 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020251vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020252 hashtab_T *ht;
20253{
20254 vars_clear_ext(ht, TRUE);
20255}
20256
20257/*
20258 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20259 */
20260 static void
20261vars_clear_ext(ht, free_val)
20262 hashtab_T *ht;
20263 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264{
Bram Moolenaara7043832005-01-21 11:56:39 +000020265 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020266 hashitem_T *hi;
20267 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020270 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020271 for (hi = ht->ht_array; todo > 0; ++hi)
20272 {
20273 if (!HASHITEM_EMPTY(hi))
20274 {
20275 --todo;
20276
Bram Moolenaar33570922005-01-25 22:26:29 +000020277 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020278 * ht_array might change then. hash_clear() takes care of it
20279 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 v = HI2DI(hi);
20281 if (free_val)
20282 clear_tv(&v->di_tv);
20283 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20284 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020285 }
20286 }
20287 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020288 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289}
20290
Bram Moolenaara7043832005-01-21 11:56:39 +000020291/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 * Delete a variable from hashtab "ht" at item "hi".
20293 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020296delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020297 hashtab_T *ht;
20298 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299{
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020301
20302 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020303 clear_tv(&di->di_tv);
20304 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305}
20306
20307/*
20308 * List the value of one internal variable.
20309 */
20310 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020311list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020312 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020314 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020316 char_u *tofree;
20317 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020318 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020319
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020320 current_copyID += COPYID_INC;
20321 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020322 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020323 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020324 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325}
20326
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020328list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 char_u *prefix;
20330 char_u *name;
20331 int type;
20332 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020333 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334{
Bram Moolenaar31859182007-08-14 20:41:13 +000020335 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20336 msg_start();
20337 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 if (name != NULL) /* "a:" vars don't have a name stored */
20339 msg_puts(name);
20340 msg_putchar(' ');
20341 msg_advance(22);
20342 if (type == VAR_NUMBER)
20343 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020344 else if (type == VAR_FUNC)
20345 msg_putchar('*');
20346 else if (type == VAR_LIST)
20347 {
20348 msg_putchar('[');
20349 if (*string == '[')
20350 ++string;
20351 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020352 else if (type == VAR_DICT)
20353 {
20354 msg_putchar('{');
20355 if (*string == '{')
20356 ++string;
20357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358 else
20359 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020360
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020362
20363 if (type == VAR_FUNC)
20364 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020365 if (*first)
20366 {
20367 msg_clr_eos();
20368 *first = FALSE;
20369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370}
20371
20372/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020373 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 * If the variable already exists, the value is updated.
20375 * Otherwise the variable is created.
20376 */
20377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020378set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020380 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382{
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020385 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020387 ht = find_var_ht(name, &varname);
20388 if (ht == NULL || *varname == NUL)
20389 {
20390 EMSG2(_(e_illvar), name);
20391 return;
20392 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020393 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020394
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020395 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20396 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397
Bram Moolenaar33570922005-01-25 22:26:29 +000020398 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020400 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020401 if (var_check_ro(v->di_flags, name)
20402 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 return;
20404 if (v->di_tv.v_type != tv->v_type
20405 && !((v->di_tv.v_type == VAR_STRING
20406 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020407 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020408 || tv->v_type == VAR_NUMBER))
20409#ifdef FEAT_FLOAT
20410 && !((v->di_tv.v_type == VAR_NUMBER
20411 || v->di_tv.v_type == VAR_FLOAT)
20412 && (tv->v_type == VAR_NUMBER
20413 || tv->v_type == VAR_FLOAT))
20414#endif
20415 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020416 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020417 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020418 return;
20419 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020420
20421 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020422 * Handle setting internal v: variables separately: we don't change
20423 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020424 */
20425 if (ht == &vimvarht)
20426 {
20427 if (v->di_tv.v_type == VAR_STRING)
20428 {
20429 vim_free(v->di_tv.vval.v_string);
20430 if (copy || tv->v_type != VAR_STRING)
20431 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20432 else
20433 {
20434 /* Take over the string to avoid an extra alloc/free. */
20435 v->di_tv.vval.v_string = tv->vval.v_string;
20436 tv->vval.v_string = NULL;
20437 }
20438 }
20439 else if (v->di_tv.v_type != VAR_NUMBER)
20440 EMSG2(_(e_intern2), "set_var()");
20441 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020443 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020444 if (STRCMP(varname, "searchforward") == 0)
20445 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20446 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 return;
20448 }
20449
20450 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 }
20452 else /* add a new variable */
20453 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020454 /* Can't add "v:" variable. */
20455 if (ht == &vimvarht)
20456 {
20457 EMSG2(_(e_illvar), name);
20458 return;
20459 }
20460
Bram Moolenaar92124a32005-06-17 22:03:40 +000020461 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020462 if (!valid_varname(varname))
20463 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020464
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020465 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20466 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020467 if (v == NULL)
20468 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020469 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020472 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 return;
20474 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020475 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020477
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020478 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020479 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020480 else
20481 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020483 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020484 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486}
20487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020488/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020489 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020490 * Also give an error message.
20491 */
20492 static int
20493var_check_ro(flags, name)
20494 int flags;
20495 char_u *name;
20496{
20497 if (flags & DI_FLAGS_RO)
20498 {
20499 EMSG2(_(e_readonlyvar), name);
20500 return TRUE;
20501 }
20502 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20503 {
20504 EMSG2(_(e_readonlysbx), name);
20505 return TRUE;
20506 }
20507 return FALSE;
20508}
20509
20510/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020511 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20512 * Also give an error message.
20513 */
20514 static int
20515var_check_fixed(flags, name)
20516 int flags;
20517 char_u *name;
20518{
20519 if (flags & DI_FLAGS_FIX)
20520 {
20521 EMSG2(_("E795: Cannot delete variable %s"), name);
20522 return TRUE;
20523 }
20524 return FALSE;
20525}
20526
20527/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020528 * Check if a funcref is assigned to a valid variable name.
20529 * Return TRUE and give an error if not.
20530 */
20531 static int
20532var_check_func_name(name, new_var)
20533 char_u *name; /* points to start of variable name */
20534 int new_var; /* TRUE when creating the variable */
20535{
20536 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20537 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20538 ? name[2] : name[0]))
20539 {
20540 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20541 name);
20542 return TRUE;
20543 }
20544 /* Don't allow hiding a function. When "v" is not NULL we might be
20545 * assigning another function to the same var, the type is checked
20546 * below. */
20547 if (new_var && function_exists(name))
20548 {
20549 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20550 name);
20551 return TRUE;
20552 }
20553 return FALSE;
20554}
20555
20556/*
20557 * Check if a variable name is valid.
20558 * Return FALSE and give an error if not.
20559 */
20560 static int
20561valid_varname(varname)
20562 char_u *varname;
20563{
20564 char_u *p;
20565
20566 for (p = varname; *p != NUL; ++p)
20567 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20568 && *p != AUTOLOAD_CHAR)
20569 {
20570 EMSG2(_(e_illvar), varname);
20571 return FALSE;
20572 }
20573 return TRUE;
20574}
20575
20576/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020577 * Return TRUE if typeval "tv" is set to be locked (immutable).
20578 * Also give an error message, using "name".
20579 */
20580 static int
20581tv_check_lock(lock, name)
20582 int lock;
20583 char_u *name;
20584{
20585 if (lock & VAR_LOCKED)
20586 {
20587 EMSG2(_("E741: Value is locked: %s"),
20588 name == NULL ? (char_u *)_("Unknown") : name);
20589 return TRUE;
20590 }
20591 if (lock & VAR_FIXED)
20592 {
20593 EMSG2(_("E742: Cannot change value of %s"),
20594 name == NULL ? (char_u *)_("Unknown") : name);
20595 return TRUE;
20596 }
20597 return FALSE;
20598}
20599
20600/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020601 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020602 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020603 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020604 * It is OK for "from" and "to" to point to the same item. This is used to
20605 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020606 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020607 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020608copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020609 typval_T *from;
20610 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020612 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020613 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020614 switch (from->v_type)
20615 {
20616 case VAR_NUMBER:
20617 to->vval.v_number = from->vval.v_number;
20618 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020619#ifdef FEAT_FLOAT
20620 case VAR_FLOAT:
20621 to->vval.v_float = from->vval.v_float;
20622 break;
20623#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020624 case VAR_STRING:
20625 case VAR_FUNC:
20626 if (from->vval.v_string == NULL)
20627 to->vval.v_string = NULL;
20628 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020630 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020631 if (from->v_type == VAR_FUNC)
20632 func_ref(to->vval.v_string);
20633 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020634 break;
20635 case VAR_LIST:
20636 if (from->vval.v_list == NULL)
20637 to->vval.v_list = NULL;
20638 else
20639 {
20640 to->vval.v_list = from->vval.v_list;
20641 ++to->vval.v_list->lv_refcount;
20642 }
20643 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020644 case VAR_DICT:
20645 if (from->vval.v_dict == NULL)
20646 to->vval.v_dict = NULL;
20647 else
20648 {
20649 to->vval.v_dict = from->vval.v_dict;
20650 ++to->vval.v_dict->dv_refcount;
20651 }
20652 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020653 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020654 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020655 break;
20656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657}
20658
20659/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020660 * Make a copy of an item.
20661 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020662 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20663 * reference to an already copied list/dict can be used.
20664 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020665 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020666 static int
20667item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020668 typval_T *from;
20669 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020670 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020671 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020672{
20673 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020674 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020675
Bram Moolenaar33570922005-01-25 22:26:29 +000020676 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020677 {
20678 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020679 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020680 }
20681 ++recurse;
20682
20683 switch (from->v_type)
20684 {
20685 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020686#ifdef FEAT_FLOAT
20687 case VAR_FLOAT:
20688#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020689 case VAR_STRING:
20690 case VAR_FUNC:
20691 copy_tv(from, to);
20692 break;
20693 case VAR_LIST:
20694 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020695 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020696 if (from->vval.v_list == NULL)
20697 to->vval.v_list = NULL;
20698 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20699 {
20700 /* use the copy made earlier */
20701 to->vval.v_list = from->vval.v_list->lv_copylist;
20702 ++to->vval.v_list->lv_refcount;
20703 }
20704 else
20705 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20706 if (to->vval.v_list == NULL)
20707 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020708 break;
20709 case VAR_DICT:
20710 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020711 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020712 if (from->vval.v_dict == NULL)
20713 to->vval.v_dict = NULL;
20714 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20715 {
20716 /* use the copy made earlier */
20717 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20718 ++to->vval.v_dict->dv_refcount;
20719 }
20720 else
20721 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20722 if (to->vval.v_dict == NULL)
20723 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020724 break;
20725 default:
20726 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020727 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020728 }
20729 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020730 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020731}
20732
20733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 * ":echo expr1 ..." print each argument separated with a space, add a
20735 * newline at the end.
20736 * ":echon expr1 ..." print each argument plain.
20737 */
20738 void
20739ex_echo(eap)
20740 exarg_T *eap;
20741{
20742 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020743 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020744 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 char_u *p;
20746 int needclr = TRUE;
20747 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020748 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749
20750 if (eap->skip)
20751 ++emsg_skip;
20752 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20753 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020754 /* If eval1() causes an error message the text from the command may
20755 * still need to be cleared. E.g., "echo 22,44". */
20756 need_clr_eos = needclr;
20757
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020759 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 {
20761 /*
20762 * Report the invalid expression unless the expression evaluation
20763 * has been cancelled due to an aborting error, an interrupt, or an
20764 * exception.
20765 */
20766 if (!aborting())
20767 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020768 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 break;
20770 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020771 need_clr_eos = FALSE;
20772
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 if (!eap->skip)
20774 {
20775 if (atstart)
20776 {
20777 atstart = FALSE;
20778 /* Call msg_start() after eval1(), evaluating the expression
20779 * may cause a message to appear. */
20780 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020781 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020782 /* Mark the saved text as finishing the line, so that what
20783 * follows is displayed on a new line when scrolling back
20784 * at the more prompt. */
20785 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 }
20789 else if (eap->cmdidx == CMD_echo)
20790 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020791 current_copyID += COPYID_INC;
20792 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020793 if (p != NULL)
20794 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020796 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020798 if (*p != TAB && needclr)
20799 {
20800 /* remove any text still there from the command */
20801 msg_clr_eos();
20802 needclr = FALSE;
20803 }
20804 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 }
20806 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020807 {
20808#ifdef FEAT_MBYTE
20809 if (has_mbyte)
20810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020811 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020812
20813 (void)msg_outtrans_len_attr(p, i, echo_attr);
20814 p += i - 1;
20815 }
20816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020818 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020821 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020823 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 arg = skipwhite(arg);
20825 }
20826 eap->nextcmd = check_nextcmd(arg);
20827
20828 if (eap->skip)
20829 --emsg_skip;
20830 else
20831 {
20832 /* remove text that may still be there from the command */
20833 if (needclr)
20834 msg_clr_eos();
20835 if (eap->cmdidx == CMD_echo)
20836 msg_end();
20837 }
20838}
20839
20840/*
20841 * ":echohl {name}".
20842 */
20843 void
20844ex_echohl(eap)
20845 exarg_T *eap;
20846{
20847 int id;
20848
20849 id = syn_name2id(eap->arg);
20850 if (id == 0)
20851 echo_attr = 0;
20852 else
20853 echo_attr = syn_id2attr(id);
20854}
20855
20856/*
20857 * ":execute expr1 ..." execute the result of an expression.
20858 * ":echomsg expr1 ..." Print a message
20859 * ":echoerr expr1 ..." Print an error
20860 * Each gets spaces around each argument and a newline at the end for
20861 * echo commands
20862 */
20863 void
20864ex_execute(eap)
20865 exarg_T *eap;
20866{
20867 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020868 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 int ret = OK;
20870 char_u *p;
20871 garray_T ga;
20872 int len;
20873 int save_did_emsg;
20874
20875 ga_init2(&ga, 1, 80);
20876
20877 if (eap->skip)
20878 ++emsg_skip;
20879 while (*arg != NUL && *arg != '|' && *arg != '\n')
20880 {
20881 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020882 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883 {
20884 /*
20885 * Report the invalid expression unless the expression evaluation
20886 * has been cancelled due to an aborting error, an interrupt, or an
20887 * exception.
20888 */
20889 if (!aborting())
20890 EMSG2(_(e_invexpr2), p);
20891 ret = FAIL;
20892 break;
20893 }
20894
20895 if (!eap->skip)
20896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020897 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 len = (int)STRLEN(p);
20899 if (ga_grow(&ga, len + 2) == FAIL)
20900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 ret = FAIL;
20903 break;
20904 }
20905 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 ga.ga_len += len;
20909 }
20910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020911 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 arg = skipwhite(arg);
20913 }
20914
20915 if (ret != FAIL && ga.ga_data != NULL)
20916 {
20917 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020920 out_flush();
20921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 else if (eap->cmdidx == CMD_echoerr)
20923 {
20924 /* We don't want to abort following commands, restore did_emsg. */
20925 save_did_emsg = did_emsg;
20926 EMSG((char_u *)ga.ga_data);
20927 if (!force_abort)
20928 did_emsg = save_did_emsg;
20929 }
20930 else if (eap->cmdidx == CMD_execute)
20931 do_cmdline((char_u *)ga.ga_data,
20932 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20933 }
20934
20935 ga_clear(&ga);
20936
20937 if (eap->skip)
20938 --emsg_skip;
20939
20940 eap->nextcmd = check_nextcmd(arg);
20941}
20942
20943/*
20944 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20945 * "arg" points to the "&" or '+' when called, to "option" when returning.
20946 * Returns NULL when no option name found. Otherwise pointer to the char
20947 * after the option name.
20948 */
20949 static char_u *
20950find_option_end(arg, opt_flags)
20951 char_u **arg;
20952 int *opt_flags;
20953{
20954 char_u *p = *arg;
20955
20956 ++p;
20957 if (*p == 'g' && p[1] == ':')
20958 {
20959 *opt_flags = OPT_GLOBAL;
20960 p += 2;
20961 }
20962 else if (*p == 'l' && p[1] == ':')
20963 {
20964 *opt_flags = OPT_LOCAL;
20965 p += 2;
20966 }
20967 else
20968 *opt_flags = 0;
20969
20970 if (!ASCII_ISALPHA(*p))
20971 return NULL;
20972 *arg = p;
20973
20974 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20975 p += 4; /* termcap option */
20976 else
20977 while (ASCII_ISALPHA(*p))
20978 ++p;
20979 return p;
20980}
20981
20982/*
20983 * ":function"
20984 */
20985 void
20986ex_function(eap)
20987 exarg_T *eap;
20988{
20989 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020020990 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 int j;
20992 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 char_u *name = NULL;
20995 char_u *p;
20996 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020997 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 garray_T newargs;
20999 garray_T newlines;
21000 int varargs = FALSE;
21001 int mustend = FALSE;
21002 int flags = 0;
21003 ufunc_T *fp;
21004 int indent;
21005 int nesting;
21006 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021007 dictitem_T *v;
21008 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021009 static int func_nr = 0; /* number for nameless function */
21010 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021011 hashtab_T *ht;
21012 int todo;
21013 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021014 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015
21016 /*
21017 * ":function" without argument: list functions.
21018 */
21019 if (ends_excmd(*eap->arg))
21020 {
21021 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021022 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021023 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021024 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021025 {
21026 if (!HASHITEM_EMPTY(hi))
21027 {
21028 --todo;
21029 fp = HI2UF(hi);
21030 if (!isdigit(*fp->uf_name))
21031 list_func_head(fp, FALSE);
21032 }
21033 }
21034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 eap->nextcmd = check_nextcmd(eap->arg);
21036 return;
21037 }
21038
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021039 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021040 * ":function /pat": list functions matching pattern.
21041 */
21042 if (*eap->arg == '/')
21043 {
21044 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21045 if (!eap->skip)
21046 {
21047 regmatch_T regmatch;
21048
21049 c = *p;
21050 *p = NUL;
21051 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21052 *p = c;
21053 if (regmatch.regprog != NULL)
21054 {
21055 regmatch.rm_ic = p_ic;
21056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021057 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021058 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21059 {
21060 if (!HASHITEM_EMPTY(hi))
21061 {
21062 --todo;
21063 fp = HI2UF(hi);
21064 if (!isdigit(*fp->uf_name)
21065 && vim_regexec(&regmatch, fp->uf_name, 0))
21066 list_func_head(fp, FALSE);
21067 }
21068 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000021069 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021070 }
21071 }
21072 if (*p == '/')
21073 ++p;
21074 eap->nextcmd = check_nextcmd(p);
21075 return;
21076 }
21077
21078 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021079 * Get the function name. There are these situations:
21080 * func normal function name
21081 * "name" == func, "fudi.fd_dict" == NULL
21082 * dict.func new dictionary entry
21083 * "name" == NULL, "fudi.fd_dict" set,
21084 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21085 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021086 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021087 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21088 * dict.func existing dict entry that's not a Funcref
21089 * "name" == NULL, "fudi.fd_dict" set,
21090 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021093 name = trans_function_name(&p, eap->skip, 0, &fudi);
21094 paren = (vim_strchr(p, '(') != NULL);
21095 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 {
21097 /*
21098 * Return on an invalid expression in braces, unless the expression
21099 * evaluation has been cancelled due to an aborting error, an
21100 * interrupt, or an exception.
21101 */
21102 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021103 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021104 if (!eap->skip && fudi.fd_newkey != NULL)
21105 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021106 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021109 else
21110 eap->skip = TRUE;
21111 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021112
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 /* An error in a function call during evaluation of an expression in magic
21114 * braces should not cause the function not to be defined. */
21115 saved_did_emsg = did_emsg;
21116 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117
21118 /*
21119 * ":function func" with only function name: list function.
21120 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021121 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 {
21123 if (!ends_excmd(*skipwhite(p)))
21124 {
21125 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021126 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 }
21128 eap->nextcmd = check_nextcmd(p);
21129 if (eap->nextcmd != NULL)
21130 *p = NUL;
21131 if (!eap->skip && !got_int)
21132 {
21133 fp = find_func(name);
21134 if (fp != NULL)
21135 {
21136 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021137 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021139 if (FUNCLINE(fp, j) == NULL)
21140 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 msg_putchar('\n');
21142 msg_outnum((long)(j + 1));
21143 if (j < 9)
21144 msg_putchar(' ');
21145 if (j < 99)
21146 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021147 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 out_flush(); /* show a line at a time */
21149 ui_breakcheck();
21150 }
21151 if (!got_int)
21152 {
21153 msg_putchar('\n');
21154 msg_puts((char_u *)" endfunction");
21155 }
21156 }
21157 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021158 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021160 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 }
21162
21163 /*
21164 * ":function name(arg1, arg2)" Define function.
21165 */
21166 p = skipwhite(p);
21167 if (*p != '(')
21168 {
21169 if (!eap->skip)
21170 {
21171 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021172 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 }
21174 /* attempt to continue by skipping some text */
21175 if (vim_strchr(p, '(') != NULL)
21176 p = vim_strchr(p, '(');
21177 }
21178 p = skipwhite(p + 1);
21179
21180 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21181 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21182
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021183 if (!eap->skip)
21184 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021185 /* Check the name of the function. Unless it's a dictionary function
21186 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021187 if (name != NULL)
21188 arg = name;
21189 else
21190 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021191 if (arg != NULL && (fudi.fd_di == NULL
21192 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021193 {
21194 if (*arg == K_SPECIAL)
21195 j = 3;
21196 else
21197 j = 0;
21198 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21199 : eval_isnamec(arg[j])))
21200 ++j;
21201 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021202 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021203 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021204 /* Disallow using the g: dict. */
21205 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21206 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021207 }
21208
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 /*
21210 * Isolate the arguments: "arg1, arg2, ...)"
21211 */
21212 while (*p != ')')
21213 {
21214 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21215 {
21216 varargs = TRUE;
21217 p += 3;
21218 mustend = TRUE;
21219 }
21220 else
21221 {
21222 arg = p;
21223 while (ASCII_ISALNUM(*p) || *p == '_')
21224 ++p;
21225 if (arg == p || isdigit(*arg)
21226 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21227 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21228 {
21229 if (!eap->skip)
21230 EMSG2(_("E125: Illegal argument: %s"), arg);
21231 break;
21232 }
21233 if (ga_grow(&newargs, 1) == FAIL)
21234 goto erret;
21235 c = *p;
21236 *p = NUL;
21237 arg = vim_strsave(arg);
21238 if (arg == NULL)
21239 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021240
21241 /* Check for duplicate argument name. */
21242 for (i = 0; i < newargs.ga_len; ++i)
21243 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21244 {
21245 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21246 goto erret;
21247 }
21248
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21250 *p = c;
21251 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 if (*p == ',')
21253 ++p;
21254 else
21255 mustend = TRUE;
21256 }
21257 p = skipwhite(p);
21258 if (mustend && *p != ')')
21259 {
21260 if (!eap->skip)
21261 EMSG2(_(e_invarg2), eap->arg);
21262 break;
21263 }
21264 }
21265 ++p; /* skip the ')' */
21266
Bram Moolenaare9a41262005-01-15 22:18:47 +000021267 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 for (;;)
21269 {
21270 p = skipwhite(p);
21271 if (STRNCMP(p, "range", 5) == 0)
21272 {
21273 flags |= FC_RANGE;
21274 p += 5;
21275 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021276 else if (STRNCMP(p, "dict", 4) == 0)
21277 {
21278 flags |= FC_DICT;
21279 p += 4;
21280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 else if (STRNCMP(p, "abort", 5) == 0)
21282 {
21283 flags |= FC_ABORT;
21284 p += 5;
21285 }
21286 else
21287 break;
21288 }
21289
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021290 /* When there is a line break use what follows for the function body.
21291 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21292 if (*p == '\n')
21293 line_arg = p + 1;
21294 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 EMSG(_(e_trailing));
21296
21297 /*
21298 * Read the body of the function, until ":endfunction" is found.
21299 */
21300 if (KeyTyped)
21301 {
21302 /* Check if the function already exists, don't let the user type the
21303 * whole function before telling him it doesn't work! For a script we
21304 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021305 if (!eap->skip && !eap->forceit)
21306 {
21307 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21308 EMSG(_(e_funcdict));
21309 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021310 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021313 if (!eap->skip && did_emsg)
21314 goto erret;
21315
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 msg_putchar('\n'); /* don't overwrite the function name */
21317 cmdline_row = msg_row;
21318 }
21319
21320 indent = 2;
21321 nesting = 0;
21322 for (;;)
21323 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021324 if (KeyTyped)
21325 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021327 sourcing_lnum_off = sourcing_lnum;
21328
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021329 if (line_arg != NULL)
21330 {
21331 /* Use eap->arg, split up in parts by line breaks. */
21332 theline = line_arg;
21333 p = vim_strchr(theline, '\n');
21334 if (p == NULL)
21335 line_arg += STRLEN(line_arg);
21336 else
21337 {
21338 *p = NUL;
21339 line_arg = p + 1;
21340 }
21341 }
21342 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343 theline = getcmdline(':', 0L, indent);
21344 else
21345 theline = eap->getline(':', eap->cookie, indent);
21346 if (KeyTyped)
21347 lines_left = Rows - 1;
21348 if (theline == NULL)
21349 {
21350 EMSG(_("E126: Missing :endfunction"));
21351 goto erret;
21352 }
21353
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021354 /* Detect line continuation: sourcing_lnum increased more than one. */
21355 if (sourcing_lnum > sourcing_lnum_off + 1)
21356 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21357 else
21358 sourcing_lnum_off = 0;
21359
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 if (skip_until != NULL)
21361 {
21362 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21363 * don't check for ":endfunc". */
21364 if (STRCMP(theline, skip_until) == 0)
21365 {
21366 vim_free(skip_until);
21367 skip_until = NULL;
21368 }
21369 }
21370 else
21371 {
21372 /* skip ':' and blanks*/
21373 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21374 ;
21375
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021376 /* Check for "endfunction". */
21377 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021379 if (line_arg == NULL)
21380 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 break;
21382 }
21383
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021384 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 * at "end". */
21386 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21387 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021388 else if (STRNCMP(p, "if", 2) == 0
21389 || STRNCMP(p, "wh", 2) == 0
21390 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 || STRNCMP(p, "try", 3) == 0)
21392 indent += 2;
21393
21394 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021395 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021397 if (*p == '!')
21398 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 p += eval_fname_script(p);
21400 if (ASCII_ISALPHA(*p))
21401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021402 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 if (*skipwhite(p) == '(')
21404 {
21405 ++nesting;
21406 indent += 2;
21407 }
21408 }
21409 }
21410
21411 /* Check for ":append" or ":insert". */
21412 p = skip_range(p, NULL);
21413 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21414 || (p[0] == 'i'
21415 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21416 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21417 skip_until = vim_strsave((char_u *)".");
21418
21419 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21420 arg = skipwhite(skiptowhite(p));
21421 if (arg[0] == '<' && arg[1] =='<'
21422 && ((p[0] == 'p' && p[1] == 'y'
21423 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21424 || (p[0] == 'p' && p[1] == 'e'
21425 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21426 || (p[0] == 't' && p[1] == 'c'
21427 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021428 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21429 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21431 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021432 || (p[0] == 'm' && p[1] == 'z'
21433 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 ))
21435 {
21436 /* ":python <<" continues until a dot, like ":append" */
21437 p = skipwhite(arg + 2);
21438 if (*p == NUL)
21439 skip_until = vim_strsave((char_u *)".");
21440 else
21441 skip_until = vim_strsave(p);
21442 }
21443 }
21444
21445 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021446 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021447 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021448 if (line_arg == NULL)
21449 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021451 }
21452
21453 /* Copy the line to newly allocated memory. get_one_sourceline()
21454 * allocates 250 bytes per line, this saves 80% on average. The cost
21455 * is an extra alloc/free. */
21456 p = vim_strsave(theline);
21457 if (p != NULL)
21458 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021459 if (line_arg == NULL)
21460 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021461 theline = p;
21462 }
21463
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021464 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21465
21466 /* Add NULL lines for continuation lines, so that the line count is
21467 * equal to the index in the growarray. */
21468 while (sourcing_lnum_off-- > 0)
21469 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021470
21471 /* Check for end of eap->arg. */
21472 if (line_arg != NULL && *line_arg == NUL)
21473 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 }
21475
21476 /* Don't define the function when skipping commands or when an error was
21477 * detected. */
21478 if (eap->skip || did_emsg)
21479 goto erret;
21480
21481 /*
21482 * If there are no errors, add the function
21483 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021484 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021485 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021486 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021488 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021489 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021490 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021491 goto erret;
21492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021494 fp = find_func(name);
21495 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021497 if (!eap->forceit)
21498 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021499 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021500 goto erret;
21501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021502 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021503 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021504 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 name);
21506 goto erret;
21507 }
21508 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021509 ga_clear_strings(&(fp->uf_args));
21510 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021511 vim_free(name);
21512 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 }
21515 else
21516 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021517 char numbuf[20];
21518
21519 fp = NULL;
21520 if (fudi.fd_newkey == NULL && !eap->forceit)
21521 {
21522 EMSG(_(e_funcdict));
21523 goto erret;
21524 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021525 if (fudi.fd_di == NULL)
21526 {
21527 /* Can't add a function to a locked dictionary */
21528 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21529 goto erret;
21530 }
21531 /* Can't change an existing function if it is locked */
21532 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21533 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021534
21535 /* Give the function a sequential number. Can only be used with a
21536 * Funcref! */
21537 vim_free(name);
21538 sprintf(numbuf, "%d", ++func_nr);
21539 name = vim_strsave((char_u *)numbuf);
21540 if (name == NULL)
21541 goto erret;
21542 }
21543
21544 if (fp == NULL)
21545 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021546 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021547 {
21548 int slen, plen;
21549 char_u *scriptname;
21550
21551 /* Check that the autoload name matches the script name. */
21552 j = FAIL;
21553 if (sourcing_name != NULL)
21554 {
21555 scriptname = autoload_name(name);
21556 if (scriptname != NULL)
21557 {
21558 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021559 plen = (int)STRLEN(p);
21560 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021561 if (slen > plen && fnamecmp(p,
21562 sourcing_name + slen - plen) == 0)
21563 j = OK;
21564 vim_free(scriptname);
21565 }
21566 }
21567 if (j == FAIL)
21568 {
21569 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21570 goto erret;
21571 }
21572 }
21573
21574 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 if (fp == NULL)
21576 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021577
21578 if (fudi.fd_dict != NULL)
21579 {
21580 if (fudi.fd_di == NULL)
21581 {
21582 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021583 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021584 if (fudi.fd_di == NULL)
21585 {
21586 vim_free(fp);
21587 goto erret;
21588 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021589 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21590 {
21591 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021592 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021593 goto erret;
21594 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021595 }
21596 else
21597 /* overwrite existing dict entry */
21598 clear_tv(&fudi.fd_di->di_tv);
21599 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021600 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021601 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021602 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021603
21604 /* behave like "dict" was used */
21605 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021606 }
21607
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021609 STRCPY(fp->uf_name, name);
21610 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021612 fp->uf_args = newargs;
21613 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021614#ifdef FEAT_PROFILE
21615 fp->uf_tml_count = NULL;
21616 fp->uf_tml_total = NULL;
21617 fp->uf_tml_self = NULL;
21618 fp->uf_profiling = FALSE;
21619 if (prof_def_func())
21620 func_do_profile(fp);
21621#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021622 fp->uf_varargs = varargs;
21623 fp->uf_flags = flags;
21624 fp->uf_calls = 0;
21625 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627
21628erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 ga_clear_strings(&newargs);
21630 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021631ret_free:
21632 vim_free(skip_until);
21633 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636}
21637
21638/*
21639 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021640 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642 * flags:
21643 * TFN_INT: internal function name OK
21644 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 * Advances "pp" to just after the function name (if no error).
21646 */
21647 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021648trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 char_u **pp;
21650 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021652 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021654 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 char_u *start;
21656 char_u *end;
21657 int lead;
21658 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021661
21662 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021665
21666 /* Check for hard coded <SNR>: already translated function ID (from a user
21667 * command). */
21668 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21669 && (*pp)[2] == (int)KE_SNR)
21670 {
21671 *pp += 3;
21672 len = get_id_len(pp) + 3;
21673 return vim_strnsave(start, len);
21674 }
21675
21676 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21677 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021679 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021681
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021682 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21683 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021684 if (end == start)
21685 {
21686 if (!skip)
21687 EMSG(_("E129: Function name required"));
21688 goto theend;
21689 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021690 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021691 {
21692 /*
21693 * Report an invalid expression in braces, unless the expression
21694 * evaluation has been cancelled due to an aborting error, an
21695 * interrupt, or an exception.
21696 */
21697 if (!aborting())
21698 {
21699 if (end != NULL)
21700 EMSG2(_(e_invarg2), start);
21701 }
21702 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021703 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021704 goto theend;
21705 }
21706
21707 if (lv.ll_tv != NULL)
21708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021709 if (fdp != NULL)
21710 {
21711 fdp->fd_dict = lv.ll_dict;
21712 fdp->fd_newkey = lv.ll_newkey;
21713 lv.ll_newkey = NULL;
21714 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021716 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21717 {
21718 name = vim_strsave(lv.ll_tv->vval.v_string);
21719 *pp = end;
21720 }
21721 else
21722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021723 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21724 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021725 EMSG(_(e_funcref));
21726 else
21727 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021728 name = NULL;
21729 }
21730 goto theend;
21731 }
21732
21733 if (lv.ll_name == NULL)
21734 {
21735 /* Error found, but continue after the function name. */
21736 *pp = end;
21737 goto theend;
21738 }
21739
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021740 /* Check if the name is a Funcref. If so, use the value. */
21741 if (lv.ll_exp_name != NULL)
21742 {
21743 len = (int)STRLEN(lv.ll_exp_name);
21744 name = deref_func_name(lv.ll_exp_name, &len);
21745 if (name == lv.ll_exp_name)
21746 name = NULL;
21747 }
21748 else
21749 {
21750 len = (int)(end - *pp);
21751 name = deref_func_name(*pp, &len);
21752 if (name == *pp)
21753 name = NULL;
21754 }
21755 if (name != NULL)
21756 {
21757 name = vim_strsave(name);
21758 *pp = end;
21759 goto theend;
21760 }
21761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021762 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021763 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021764 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021765 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21766 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21767 {
21768 /* When there was "s:" already or the name expanded to get a
21769 * leading "s:" then remove it. */
21770 lv.ll_name += 2;
21771 len -= 2;
21772 lead = 2;
21773 }
21774 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021775 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021776 {
21777 if (lead == 2) /* skip over "s:" */
21778 lv.ll_name += 2;
21779 len = (int)(end - lv.ll_name);
21780 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021781
21782 /*
21783 * Copy the function name to allocated memory.
21784 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21785 * Accept <SNR>123_name() outside a script.
21786 */
21787 if (skip)
21788 lead = 0; /* do nothing */
21789 else if (lead > 0)
21790 {
21791 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021792 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21793 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021794 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021795 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021796 if (current_SID <= 0)
21797 {
21798 EMSG(_(e_usingsid));
21799 goto theend;
21800 }
21801 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21802 lead += (int)STRLEN(sid_buf);
21803 }
21804 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021805 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021806 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021807 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021808 goto theend;
21809 }
21810 name = alloc((unsigned)(len + lead + 1));
21811 if (name != NULL)
21812 {
21813 if (lead > 0)
21814 {
21815 name[0] = K_SPECIAL;
21816 name[1] = KS_EXTRA;
21817 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021818 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021819 STRCPY(name + 3, sid_buf);
21820 }
21821 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21822 name[len + lead] = NUL;
21823 }
21824 *pp = end;
21825
21826theend:
21827 clear_lval(&lv);
21828 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829}
21830
21831/*
21832 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21833 * Return 2 if "p" starts with "s:".
21834 * Return 0 otherwise.
21835 */
21836 static int
21837eval_fname_script(p)
21838 char_u *p;
21839{
21840 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21841 || STRNICMP(p + 1, "SNR>", 4) == 0))
21842 return 5;
21843 if (p[0] == 's' && p[1] == ':')
21844 return 2;
21845 return 0;
21846}
21847
21848/*
21849 * Return TRUE if "p" starts with "<SID>" or "s:".
21850 * Only works if eval_fname_script() returned non-zero for "p"!
21851 */
21852 static int
21853eval_fname_sid(p)
21854 char_u *p;
21855{
21856 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21857}
21858
21859/*
21860 * List the head of the function: "name(arg1, arg2)".
21861 */
21862 static void
21863list_func_head(fp, indent)
21864 ufunc_T *fp;
21865 int indent;
21866{
21867 int j;
21868
21869 msg_start();
21870 if (indent)
21871 MSG_PUTS(" ");
21872 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021873 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 {
21875 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021876 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 }
21878 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021879 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021881 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 {
21883 if (j)
21884 MSG_PUTS(", ");
21885 msg_puts(FUNCARG(fp, j));
21886 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021887 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 {
21889 if (j)
21890 MSG_PUTS(", ");
21891 MSG_PUTS("...");
21892 }
21893 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021894 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021895 if (p_verbose > 0)
21896 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897}
21898
21899/*
21900 * Find a function by name, return pointer to it in ufuncs.
21901 * Return NULL for unknown function.
21902 */
21903 static ufunc_T *
21904find_func(name)
21905 char_u *name;
21906{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021907 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021909 hi = hash_find(&func_hashtab, name);
21910 if (!HASHITEM_EMPTY(hi))
21911 return HI2UF(hi);
21912 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913}
21914
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021915#if defined(EXITFREE) || defined(PROTO)
21916 void
21917free_all_functions()
21918{
21919 hashitem_T *hi;
21920
21921 /* Need to start all over every time, because func_free() may change the
21922 * hash table. */
21923 while (func_hashtab.ht_used > 0)
21924 for (hi = func_hashtab.ht_array; ; ++hi)
21925 if (!HASHITEM_EMPTY(hi))
21926 {
21927 func_free(HI2UF(hi));
21928 break;
21929 }
21930}
21931#endif
21932
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021933 int
21934translated_function_exists(name)
21935 char_u *name;
21936{
21937 if (builtin_function(name))
21938 return find_internal_func(name) >= 0;
21939 return find_func(name) != NULL;
21940}
21941
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021942/*
21943 * Return TRUE if a function "name" exists.
21944 */
21945 static int
21946function_exists(name)
21947 char_u *name;
21948{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021949 char_u *nm = name;
21950 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021951 int n = FALSE;
21952
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021953 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021954 nm = skipwhite(nm);
21955
21956 /* Only accept "funcname", "funcname ", "funcname (..." and
21957 * "funcname(...", not "funcname!...". */
21958 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021959 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000021960 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021961 return n;
21962}
21963
Bram Moolenaara1544c02013-05-30 12:35:52 +020021964 char_u *
21965get_expanded_name(name, check)
21966 char_u *name;
21967 int check;
21968{
21969 char_u *nm = name;
21970 char_u *p;
21971
21972 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
21973
21974 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021975 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020021976 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020021977
Bram Moolenaara1544c02013-05-30 12:35:52 +020021978 vim_free(p);
21979 return NULL;
21980}
21981
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021982/*
21983 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021984 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021985 */
21986 static int
21987builtin_function(name)
21988 char_u *name;
21989{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021990 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21991 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021992}
21993
Bram Moolenaar05159a02005-02-26 23:04:13 +000021994#if defined(FEAT_PROFILE) || defined(PROTO)
21995/*
21996 * Start profiling function "fp".
21997 */
21998 static void
21999func_do_profile(fp)
22000 ufunc_T *fp;
22001{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022002 int len = fp->uf_lines.ga_len;
22003
22004 if (len == 0)
22005 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022006 fp->uf_tm_count = 0;
22007 profile_zero(&fp->uf_tm_self);
22008 profile_zero(&fp->uf_tm_total);
22009 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022010 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022011 if (fp->uf_tml_total == NULL)
22012 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022013 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022014 if (fp->uf_tml_self == NULL)
22015 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022016 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022017 fp->uf_tml_idx = -1;
22018 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22019 || fp->uf_tml_self == NULL)
22020 return; /* out of memory */
22021
22022 fp->uf_profiling = TRUE;
22023}
22024
22025/*
22026 * Dump the profiling results for all functions in file "fd".
22027 */
22028 void
22029func_dump_profile(fd)
22030 FILE *fd;
22031{
22032 hashitem_T *hi;
22033 int todo;
22034 ufunc_T *fp;
22035 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022036 ufunc_T **sorttab;
22037 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022038
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022039 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022040 if (todo == 0)
22041 return; /* nothing to dump */
22042
Bram Moolenaar73830342005-02-28 22:48:19 +000022043 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22044
Bram Moolenaar05159a02005-02-26 23:04:13 +000022045 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22046 {
22047 if (!HASHITEM_EMPTY(hi))
22048 {
22049 --todo;
22050 fp = HI2UF(hi);
22051 if (fp->uf_profiling)
22052 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022053 if (sorttab != NULL)
22054 sorttab[st_len++] = fp;
22055
Bram Moolenaar05159a02005-02-26 23:04:13 +000022056 if (fp->uf_name[0] == K_SPECIAL)
22057 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22058 else
22059 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22060 if (fp->uf_tm_count == 1)
22061 fprintf(fd, "Called 1 time\n");
22062 else
22063 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22064 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22065 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22066 fprintf(fd, "\n");
22067 fprintf(fd, "count total (s) self (s)\n");
22068
22069 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22070 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022071 if (FUNCLINE(fp, i) == NULL)
22072 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022073 prof_func_line(fd, fp->uf_tml_count[i],
22074 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022075 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22076 }
22077 fprintf(fd, "\n");
22078 }
22079 }
22080 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022081
22082 if (sorttab != NULL && st_len > 0)
22083 {
22084 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22085 prof_total_cmp);
22086 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22087 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22088 prof_self_cmp);
22089 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22090 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022091
22092 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022093}
Bram Moolenaar73830342005-02-28 22:48:19 +000022094
22095 static void
22096prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22097 FILE *fd;
22098 ufunc_T **sorttab;
22099 int st_len;
22100 char *title;
22101 int prefer_self; /* when equal print only self time */
22102{
22103 int i;
22104 ufunc_T *fp;
22105
22106 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22107 fprintf(fd, "count total (s) self (s) function\n");
22108 for (i = 0; i < 20 && i < st_len; ++i)
22109 {
22110 fp = sorttab[i];
22111 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22112 prefer_self);
22113 if (fp->uf_name[0] == K_SPECIAL)
22114 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22115 else
22116 fprintf(fd, " %s()\n", fp->uf_name);
22117 }
22118 fprintf(fd, "\n");
22119}
22120
22121/*
22122 * Print the count and times for one function or function line.
22123 */
22124 static void
22125prof_func_line(fd, count, total, self, prefer_self)
22126 FILE *fd;
22127 int count;
22128 proftime_T *total;
22129 proftime_T *self;
22130 int prefer_self; /* when equal print only self time */
22131{
22132 if (count > 0)
22133 {
22134 fprintf(fd, "%5d ", count);
22135 if (prefer_self && profile_equal(total, self))
22136 fprintf(fd, " ");
22137 else
22138 fprintf(fd, "%s ", profile_msg(total));
22139 if (!prefer_self && profile_equal(total, self))
22140 fprintf(fd, " ");
22141 else
22142 fprintf(fd, "%s ", profile_msg(self));
22143 }
22144 else
22145 fprintf(fd, " ");
22146}
22147
22148/*
22149 * Compare function for total time sorting.
22150 */
22151 static int
22152#ifdef __BORLANDC__
22153_RTLENTRYF
22154#endif
22155prof_total_cmp(s1, s2)
22156 const void *s1;
22157 const void *s2;
22158{
22159 ufunc_T *p1, *p2;
22160
22161 p1 = *(ufunc_T **)s1;
22162 p2 = *(ufunc_T **)s2;
22163 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22164}
22165
22166/*
22167 * Compare function for self time sorting.
22168 */
22169 static int
22170#ifdef __BORLANDC__
22171_RTLENTRYF
22172#endif
22173prof_self_cmp(s1, s2)
22174 const void *s1;
22175 const void *s2;
22176{
22177 ufunc_T *p1, *p2;
22178
22179 p1 = *(ufunc_T **)s1;
22180 p2 = *(ufunc_T **)s2;
22181 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22182}
22183
Bram Moolenaar05159a02005-02-26 23:04:13 +000022184#endif
22185
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022186/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022187 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022188 * Return TRUE if a package was loaded.
22189 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022190 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022191script_autoload(name, reload)
22192 char_u *name;
22193 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022194{
22195 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022196 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022197 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022198 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022199
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022200 /* Return quickly when autoload disabled. */
22201 if (no_autoload)
22202 return FALSE;
22203
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022204 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022205 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022206 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022207 return FALSE;
22208
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022209 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022210
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022211 /* Find the name in the list of previously loaded package names. Skip
22212 * "autoload/", it's always the same. */
22213 for (i = 0; i < ga_loaded.ga_len; ++i)
22214 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22215 break;
22216 if (!reload && i < ga_loaded.ga_len)
22217 ret = FALSE; /* was loaded already */
22218 else
22219 {
22220 /* Remember the name if it wasn't loaded already. */
22221 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22222 {
22223 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22224 tofree = NULL;
22225 }
22226
22227 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022228 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022229 ret = TRUE;
22230 }
22231
22232 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022233 return ret;
22234}
22235
22236/*
22237 * Return the autoload script name for a function or variable name.
22238 * Returns NULL when out of memory.
22239 */
22240 static char_u *
22241autoload_name(name)
22242 char_u *name;
22243{
22244 char_u *p;
22245 char_u *scriptname;
22246
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022247 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022248 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22249 if (scriptname == NULL)
22250 return FALSE;
22251 STRCPY(scriptname, "autoload/");
22252 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022253 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022254 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022255 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022256 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022257 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022258}
22259
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22261
22262/*
22263 * Function given to ExpandGeneric() to obtain the list of user defined
22264 * function names.
22265 */
22266 char_u *
22267get_user_func_name(xp, idx)
22268 expand_T *xp;
22269 int idx;
22270{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022271 static long_u done;
22272 static hashitem_T *hi;
22273 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274
22275 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022277 done = 0;
22278 hi = func_hashtab.ht_array;
22279 }
22280 if (done < func_hashtab.ht_used)
22281 {
22282 if (done++ > 0)
22283 ++hi;
22284 while (HASHITEM_EMPTY(hi))
22285 ++hi;
22286 fp = HI2UF(hi);
22287
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022288 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022289 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022290
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022291 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22292 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293
22294 cat_func_name(IObuff, fp);
22295 if (xp->xp_context != EXPAND_USER_FUNC)
22296 {
22297 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022298 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 STRCAT(IObuff, ")");
22300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 return IObuff;
22302 }
22303 return NULL;
22304}
22305
22306#endif /* FEAT_CMDL_COMPL */
22307
22308/*
22309 * Copy the function name of "fp" to buffer "buf".
22310 * "buf" must be able to hold the function name plus three bytes.
22311 * Takes care of script-local function names.
22312 */
22313 static void
22314cat_func_name(buf, fp)
22315 char_u *buf;
22316 ufunc_T *fp;
22317{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022318 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 {
22320 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022321 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 }
22323 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022324 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325}
22326
22327/*
22328 * ":delfunction {name}"
22329 */
22330 void
22331ex_delfunction(eap)
22332 exarg_T *eap;
22333{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022334 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 char_u *p;
22336 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022337 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338
22339 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022340 name = trans_function_name(&p, eap->skip, 0, &fudi);
22341 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022343 {
22344 if (fudi.fd_dict != NULL && !eap->skip)
22345 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 if (!ends_excmd(*skipwhite(p)))
22349 {
22350 vim_free(name);
22351 EMSG(_(e_trailing));
22352 return;
22353 }
22354 eap->nextcmd = check_nextcmd(p);
22355 if (eap->nextcmd != NULL)
22356 *p = NUL;
22357
22358 if (!eap->skip)
22359 fp = find_func(name);
22360 vim_free(name);
22361
22362 if (!eap->skip)
22363 {
22364 if (fp == NULL)
22365 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022366 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 return;
22368 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022369 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 {
22371 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22372 return;
22373 }
22374
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022375 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022377 /* Delete the dict item that refers to the function, it will
22378 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022379 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022381 else
22382 func_free(fp);
22383 }
22384}
22385
22386/*
22387 * Free a function and remove it from the list of functions.
22388 */
22389 static void
22390func_free(fp)
22391 ufunc_T *fp;
22392{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022393 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022394
22395 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022396 ga_clear_strings(&(fp->uf_args));
22397 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022398#ifdef FEAT_PROFILE
22399 vim_free(fp->uf_tml_count);
22400 vim_free(fp->uf_tml_total);
22401 vim_free(fp->uf_tml_self);
22402#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022403
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022404 /* remove the function from the function hashtable */
22405 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22406 if (HASHITEM_EMPTY(hi))
22407 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022408 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022409 hash_remove(&func_hashtab, hi);
22410
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022411 vim_free(fp);
22412}
22413
22414/*
22415 * Unreference a Function: decrement the reference count and free it when it
22416 * becomes zero. Only for numbered functions.
22417 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022418 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419func_unref(name)
22420 char_u *name;
22421{
22422 ufunc_T *fp;
22423
22424 if (name != NULL && isdigit(*name))
22425 {
22426 fp = find_func(name);
22427 if (fp == NULL)
22428 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022429 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022430 {
22431 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022432 * when "uf_calls" becomes zero. */
22433 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022434 func_free(fp);
22435 }
22436 }
22437}
22438
22439/*
22440 * Count a reference to a Function.
22441 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022442 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022443func_ref(name)
22444 char_u *name;
22445{
22446 ufunc_T *fp;
22447
22448 if (name != NULL && isdigit(*name))
22449 {
22450 fp = find_func(name);
22451 if (fp == NULL)
22452 EMSG2(_(e_intern2), "func_ref()");
22453 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022454 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 }
22456}
22457
22458/*
22459 * Call a user function.
22460 */
22461 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022462call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 ufunc_T *fp; /* pointer to function */
22464 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 typval_T *argvars; /* arguments */
22466 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 linenr_T firstline; /* first line of range */
22468 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022469 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470{
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 char_u *save_sourcing_name;
22472 linenr_T save_sourcing_lnum;
22473 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022474 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 int save_did_emsg;
22476 static int depth = 0;
22477 dictitem_T *v;
22478 int fixvar_idx = 0; /* index in fixvar[] */
22479 int i;
22480 int ai;
22481 char_u numbuf[NUMBUFLEN];
22482 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022483#ifdef FEAT_PROFILE
22484 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022485 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487
22488 /* If depth of calling is getting too high, don't execute the function */
22489 if (depth >= p_mfd)
22490 {
22491 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022492 rettv->v_type = VAR_NUMBER;
22493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 return;
22495 }
22496 ++depth;
22497
22498 line_breakcheck(); /* check for CTRL-C hit */
22499
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022500 fc = (funccall_T *)alloc(sizeof(funccall_T));
22501 fc->caller = current_funccal;
22502 current_funccal = fc;
22503 fc->func = fp;
22504 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022505 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022506 fc->linenr = 0;
22507 fc->returned = FALSE;
22508 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022510 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22511 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512
Bram Moolenaar33570922005-01-25 22:26:29 +000022513 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022514 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22516 * each argument variable and saves a lot of time.
22517 */
22518 /*
22519 * Init l: variables.
22520 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022521 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022522 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022523 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022524 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22525 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022526 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022527 name = v->di_key;
22528 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022529 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022530 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022531 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022532 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 v->di_tv.vval.v_dict = selfdict;
22534 ++selfdict->dv_refcount;
22535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022536
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 /*
22538 * Init a: variables.
22539 * Set a:0 to "argcount".
22540 * Set a:000 to a list with room for the "..." arguments.
22541 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022542 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022543 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022544 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022545 /* Use "name" to avoid a warning from some compiler that checks the
22546 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022547 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022548 name = v->di_key;
22549 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022551 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022552 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022553 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022554 v->di_tv.vval.v_list = &fc->l_varlist;
22555 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22556 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22557 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022558
22559 /*
22560 * Set a:firstline to "firstline" and a:lastline to "lastline".
22561 * Set a:name to named arguments.
22562 * Set a:N to the "..." arguments.
22563 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022564 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022565 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022566 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 (varnumber_T)lastline);
22568 for (i = 0; i < argcount; ++i)
22569 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022570 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022571 if (ai < 0)
22572 /* named argument a:name */
22573 name = FUNCARG(fp, i);
22574 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022575 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 /* "..." argument a:1, a:2, etc. */
22577 sprintf((char *)numbuf, "%d", ai + 1);
22578 name = numbuf;
22579 }
22580 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22581 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022582 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22584 }
22585 else
22586 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022587 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22588 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 if (v == NULL)
22590 break;
22591 v->di_flags = DI_FLAGS_RO;
22592 }
22593 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022594 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022596 /* Note: the values are copied directly to avoid alloc/free.
22597 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022599 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022600
22601 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22602 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022603 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22604 fc->l_listitems[ai].li_tv = argvars[i];
22605 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022606 }
22607 }
22608
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 /* Don't redraw while executing the function. */
22610 ++RedrawingDisabled;
22611 save_sourcing_name = sourcing_name;
22612 save_sourcing_lnum = sourcing_lnum;
22613 sourcing_lnum = 1;
22614 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022615 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 if (sourcing_name != NULL)
22617 {
22618 if (save_sourcing_name != NULL
22619 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22620 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22621 else
22622 STRCPY(sourcing_name, "function ");
22623 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22624
22625 if (p_verbose >= 12)
22626 {
22627 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022628 verbose_enter_scroll();
22629
Bram Moolenaar555b2802005-05-19 21:08:39 +000022630 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 if (p_verbose >= 14)
22632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022634 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022635 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022636 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637
22638 msg_puts((char_u *)"(");
22639 for (i = 0; i < argcount; ++i)
22640 {
22641 if (i > 0)
22642 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022643 if (argvars[i].v_type == VAR_NUMBER)
22644 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 else
22646 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022647 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22648 if (s != NULL)
22649 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022650 if (vim_strsize(s) > MSG_BUF_CLEN)
22651 {
22652 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22653 s = buf;
22654 }
22655 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022656 vim_free(tofree);
22657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 }
22659 }
22660 msg_puts((char_u *)")");
22661 }
22662 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022663
22664 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 --no_wait_return;
22666 }
22667 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022668#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022669 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022670 {
22671 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22672 func_do_profile(fp);
22673 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022674 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022675 {
22676 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022677 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022678 profile_zero(&fp->uf_tm_children);
22679 }
22680 script_prof_save(&wait_start);
22681 }
22682#endif
22683
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022685 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 save_did_emsg = did_emsg;
22687 did_emsg = FALSE;
22688
22689 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022690 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22692
22693 --RedrawingDisabled;
22694
22695 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022696 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022698 clear_tv(rettv);
22699 rettv->v_type = VAR_NUMBER;
22700 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 }
22702
Bram Moolenaar05159a02005-02-26 23:04:13 +000022703#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022704 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022705 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022706 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022707 profile_end(&call_start);
22708 profile_sub_wait(&wait_start, &call_start);
22709 profile_add(&fp->uf_tm_total, &call_start);
22710 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022711 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022712 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022713 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22714 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022715 }
22716 }
22717#endif
22718
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 /* when being verbose, mention the return value */
22720 if (p_verbose >= 12)
22721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022723 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022726 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022727 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022728 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022729 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022732 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022733 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022734 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022735 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022736
Bram Moolenaar555b2802005-05-19 21:08:39 +000022737 /* The value may be very long. Skip the middle part, so that we
22738 * have some idea how it starts and ends. smsg() would always
22739 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022740 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022741 if (s != NULL)
22742 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022743 if (vim_strsize(s) > MSG_BUF_CLEN)
22744 {
22745 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22746 s = buf;
22747 }
22748 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022749 vim_free(tofree);
22750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 }
22752 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022753
22754 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 --no_wait_return;
22756 }
22757
22758 vim_free(sourcing_name);
22759 sourcing_name = save_sourcing_name;
22760 sourcing_lnum = save_sourcing_lnum;
22761 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022762#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022763 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022764 script_prof_restore(&wait_start);
22765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766
22767 if (p_verbose >= 12 && sourcing_name != NULL)
22768 {
22769 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022770 verbose_enter_scroll();
22771
Bram Moolenaar555b2802005-05-19 21:08:39 +000022772 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022774
22775 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 --no_wait_return;
22777 }
22778
22779 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022780 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022782
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022783 /* If the a:000 list and the l: and a: dicts are not referenced we can
22784 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022785 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22786 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22787 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22788 {
22789 free_funccal(fc, FALSE);
22790 }
22791 else
22792 {
22793 hashitem_T *hi;
22794 listitem_T *li;
22795 int todo;
22796
22797 /* "fc" is still in use. This can happen when returning "a:000" or
22798 * assigning "l:" to a global variable.
22799 * Link "fc" in the list for garbage collection later. */
22800 fc->caller = previous_funccal;
22801 previous_funccal = fc;
22802
22803 /* Make a copy of the a: variables, since we didn't do that above. */
22804 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22805 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22806 {
22807 if (!HASHITEM_EMPTY(hi))
22808 {
22809 --todo;
22810 v = HI2DI(hi);
22811 copy_tv(&v->di_tv, &v->di_tv);
22812 }
22813 }
22814
22815 /* Make a copy of the a:000 items, since we didn't do that above. */
22816 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22817 copy_tv(&li->li_tv, &li->li_tv);
22818 }
22819}
22820
22821/*
22822 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022823 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022824 */
22825 static int
22826can_free_funccal(fc, copyID)
22827 funccall_T *fc;
22828 int copyID;
22829{
22830 return (fc->l_varlist.lv_copyID != copyID
22831 && fc->l_vars.dv_copyID != copyID
22832 && fc->l_avars.dv_copyID != copyID);
22833}
22834
22835/*
22836 * Free "fc" and what it contains.
22837 */
22838 static void
22839free_funccal(fc, free_val)
22840 funccall_T *fc;
22841 int free_val; /* a: vars were allocated */
22842{
22843 listitem_T *li;
22844
22845 /* The a: variables typevals may not have been allocated, only free the
22846 * allocated variables. */
22847 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22848
22849 /* free all l: variables */
22850 vars_clear(&fc->l_vars.dv_hashtab);
22851
22852 /* Free the a:000 variables if they were allocated. */
22853 if (free_val)
22854 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22855 clear_tv(&li->li_tv);
22856
22857 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858}
22859
22860/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022861 * Add a number variable "name" to dict "dp" with value "nr".
22862 */
22863 static void
22864add_nr_var(dp, v, name, nr)
22865 dict_T *dp;
22866 dictitem_T *v;
22867 char *name;
22868 varnumber_T nr;
22869{
22870 STRCPY(v->di_key, name);
22871 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22872 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22873 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022874 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022875 v->di_tv.vval.v_number = nr;
22876}
22877
22878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 * ":return [expr]"
22880 */
22881 void
22882ex_return(eap)
22883 exarg_T *eap;
22884{
22885 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 int returning = FALSE;
22888
22889 if (current_funccal == NULL)
22890 {
22891 EMSG(_("E133: :return not inside a function"));
22892 return;
22893 }
22894
22895 if (eap->skip)
22896 ++emsg_skip;
22897
22898 eap->nextcmd = NULL;
22899 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022900 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 {
22902 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022903 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022905 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 }
22907 /* It's safer to return also on error. */
22908 else if (!eap->skip)
22909 {
22910 /*
22911 * Return unless the expression evaluation has been cancelled due to an
22912 * aborting error, an interrupt, or an exception.
22913 */
22914 if (!aborting())
22915 returning = do_return(eap, FALSE, TRUE, NULL);
22916 }
22917
22918 /* When skipping or the return gets pending, advance to the next command
22919 * in this line (!returning). Otherwise, ignore the rest of the line.
22920 * Following lines will be ignored by get_func_line(). */
22921 if (returning)
22922 eap->nextcmd = NULL;
22923 else if (eap->nextcmd == NULL) /* no argument */
22924 eap->nextcmd = check_nextcmd(arg);
22925
22926 if (eap->skip)
22927 --emsg_skip;
22928}
22929
22930/*
22931 * Return from a function. Possibly makes the return pending. Also called
22932 * for a pending return at the ":endtry" or after returning from an extra
22933 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022934 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022935 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 * FALSE when the return gets pending.
22937 */
22938 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022939do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 exarg_T *eap;
22941 int reanimate;
22942 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944{
22945 int idx;
22946 struct condstack *cstack = eap->cstack;
22947
22948 if (reanimate)
22949 /* Undo the return. */
22950 current_funccal->returned = FALSE;
22951
22952 /*
22953 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22954 * not in its finally clause (which then is to be executed next) is found.
22955 * In this case, make the ":return" pending for execution at the ":endtry".
22956 * Otherwise, return normally.
22957 */
22958 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22959 if (idx >= 0)
22960 {
22961 cstack->cs_pending[idx] = CSTP_RETURN;
22962
22963 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022964 /* A pending return again gets pending. "rettv" points to an
22965 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022967 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 else
22969 {
22970 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022971 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022973 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022975 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 {
22977 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022978 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 else
22981 EMSG(_(e_outofmem));
22982 }
22983 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022984 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985
22986 if (reanimate)
22987 {
22988 /* The pending return value could be overwritten by a ":return"
22989 * without argument in a finally clause; reset the default
22990 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022991 current_funccal->rettv->v_type = VAR_NUMBER;
22992 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 }
22994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022995 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 }
22997 else
22998 {
22999 current_funccal->returned = TRUE;
23000
23001 /* If the return is carried out now, store the return value. For
23002 * a return immediately after reanimation, the value is already
23003 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023004 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023006 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023007 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023009 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 }
23011 }
23012
23013 return idx < 0;
23014}
23015
23016/*
23017 * Free the variable with a pending return value.
23018 */
23019 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023020discard_pending_return(rettv)
23021 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022{
Bram Moolenaar33570922005-01-25 22:26:29 +000023023 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024}
23025
23026/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023027 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 * is an allocated string. Used by report_pending() for verbose messages.
23029 */
23030 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023031get_return_cmd(rettv)
23032 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023034 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023035 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023036 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023038 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023039 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023040 if (s == NULL)
23041 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023042
23043 STRCPY(IObuff, ":return ");
23044 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23045 if (STRLEN(s) + 8 >= IOSIZE)
23046 STRCPY(IObuff + IOSIZE - 4, "...");
23047 vim_free(tofree);
23048 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049}
23050
23051/*
23052 * Get next function line.
23053 * Called by do_cmdline() to get the next line.
23054 * Returns allocated string, or NULL for end of function.
23055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 char_u *
23057get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023058 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023060 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061{
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023063 ufunc_T *fp = fcp->func;
23064 char_u *retval;
23065 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066
23067 /* If breakpoints have been added/deleted need to check for it. */
23068 if (fcp->dbg_tick != debug_tick)
23069 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023070 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 sourcing_lnum);
23072 fcp->dbg_tick = debug_tick;
23073 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023074#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023075 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023076 func_line_end(cookie);
23077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078
Bram Moolenaar05159a02005-02-26 23:04:13 +000023079 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023080 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23081 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 retval = NULL;
23083 else
23084 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023085 /* Skip NULL lines (continuation lines). */
23086 while (fcp->linenr < gap->ga_len
23087 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23088 ++fcp->linenr;
23089 if (fcp->linenr >= gap->ga_len)
23090 retval = NULL;
23091 else
23092 {
23093 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23094 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023095#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023096 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023097 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023098#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 }
23101
23102 /* Did we encounter a breakpoint? */
23103 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23104 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023105 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023107 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 sourcing_lnum);
23109 fcp->dbg_tick = debug_tick;
23110 }
23111
23112 return retval;
23113}
23114
Bram Moolenaar05159a02005-02-26 23:04:13 +000023115#if defined(FEAT_PROFILE) || defined(PROTO)
23116/*
23117 * Called when starting to read a function line.
23118 * "sourcing_lnum" must be correct!
23119 * When skipping lines it may not actually be executed, but we won't find out
23120 * until later and we need to store the time now.
23121 */
23122 void
23123func_line_start(cookie)
23124 void *cookie;
23125{
23126 funccall_T *fcp = (funccall_T *)cookie;
23127 ufunc_T *fp = fcp->func;
23128
23129 if (fp->uf_profiling && sourcing_lnum >= 1
23130 && sourcing_lnum <= fp->uf_lines.ga_len)
23131 {
23132 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023133 /* Skip continuation lines. */
23134 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23135 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023136 fp->uf_tml_execed = FALSE;
23137 profile_start(&fp->uf_tml_start);
23138 profile_zero(&fp->uf_tml_children);
23139 profile_get_wait(&fp->uf_tml_wait);
23140 }
23141}
23142
23143/*
23144 * Called when actually executing a function line.
23145 */
23146 void
23147func_line_exec(cookie)
23148 void *cookie;
23149{
23150 funccall_T *fcp = (funccall_T *)cookie;
23151 ufunc_T *fp = fcp->func;
23152
23153 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23154 fp->uf_tml_execed = TRUE;
23155}
23156
23157/*
23158 * Called when done with a function line.
23159 */
23160 void
23161func_line_end(cookie)
23162 void *cookie;
23163{
23164 funccall_T *fcp = (funccall_T *)cookie;
23165 ufunc_T *fp = fcp->func;
23166
23167 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23168 {
23169 if (fp->uf_tml_execed)
23170 {
23171 ++fp->uf_tml_count[fp->uf_tml_idx];
23172 profile_end(&fp->uf_tml_start);
23173 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023174 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023175 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23176 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023177 }
23178 fp->uf_tml_idx = -1;
23179 }
23180}
23181#endif
23182
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183/*
23184 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023185 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 */
23187 int
23188func_has_ended(cookie)
23189 void *cookie;
23190{
Bram Moolenaar33570922005-01-25 22:26:29 +000023191 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192
23193 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23194 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023195 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 || fcp->returned);
23197}
23198
23199/*
23200 * return TRUE if cookie indicates a function which "abort"s on errors.
23201 */
23202 int
23203func_has_abort(cookie)
23204 void *cookie;
23205{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023206 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207}
23208
23209#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23210typedef enum
23211{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023212 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23213 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23214 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215} var_flavour_T;
23216
23217static var_flavour_T var_flavour __ARGS((char_u *varname));
23218
23219 static var_flavour_T
23220var_flavour(varname)
23221 char_u *varname;
23222{
23223 char_u *p = varname;
23224
23225 if (ASCII_ISUPPER(*p))
23226 {
23227 while (*(++p))
23228 if (ASCII_ISLOWER(*p))
23229 return VAR_FLAVOUR_SESSION;
23230 return VAR_FLAVOUR_VIMINFO;
23231 }
23232 else
23233 return VAR_FLAVOUR_DEFAULT;
23234}
23235#endif
23236
23237#if defined(FEAT_VIMINFO) || defined(PROTO)
23238/*
23239 * Restore global vars that start with a capital from the viminfo file
23240 */
23241 int
23242read_viminfo_varlist(virp, writing)
23243 vir_T *virp;
23244 int writing;
23245{
23246 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023247 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023248 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249
23250 if (!writing && (find_viminfo_parameter('!') != NULL))
23251 {
23252 tab = vim_strchr(virp->vir_line + 1, '\t');
23253 if (tab != NULL)
23254 {
23255 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023256 switch (*tab)
23257 {
23258 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023259#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023260 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023261#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023262 case 'D': type = VAR_DICT; break;
23263 case 'L': type = VAR_LIST; break;
23264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265
23266 tab = vim_strchr(tab, '\t');
23267 if (tab != NULL)
23268 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023269 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023270 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023271 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023273#ifdef FEAT_FLOAT
23274 else if (type == VAR_FLOAT)
23275 (void)string2float(tab + 1, &tv.vval.v_float);
23276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023278 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023279 if (type == VAR_DICT || type == VAR_LIST)
23280 {
23281 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23282
23283 if (etv == NULL)
23284 /* Failed to parse back the dict or list, use it as a
23285 * string. */
23286 tv.v_type = VAR_STRING;
23287 else
23288 {
23289 vim_free(tv.vval.v_string);
23290 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023291 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023292 }
23293 }
23294
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023295 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023296
23297 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023298 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023299 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23300 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 }
23302 }
23303 }
23304
23305 return viminfo_readline(virp);
23306}
23307
23308/*
23309 * Write global vars that start with a capital to the viminfo file
23310 */
23311 void
23312write_viminfo_varlist(fp)
23313 FILE *fp;
23314{
Bram Moolenaar33570922005-01-25 22:26:29 +000023315 hashitem_T *hi;
23316 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023317 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023318 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023319 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023320 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023321 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322
23323 if (find_viminfo_parameter('!') == NULL)
23324 return;
23325
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023326 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023327
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023328 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023329 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023333 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023334 this_var = HI2DI(hi);
23335 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023336 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023338 {
23339 case VAR_STRING: s = "STR"; break;
23340 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023341#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023342 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023343#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023344 case VAR_DICT: s = "DIC"; break;
23345 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023346 default: continue;
23347 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023348 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023349 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023350 if (p != NULL)
23351 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023352 vim_free(tofree);
23353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
23355 }
23356}
23357#endif
23358
23359#if defined(FEAT_SESSION) || defined(PROTO)
23360 int
23361store_session_globals(fd)
23362 FILE *fd;
23363{
Bram Moolenaar33570922005-01-25 22:26:29 +000023364 hashitem_T *hi;
23365 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023366 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 char_u *p, *t;
23368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023369 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023370 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023374 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023375 this_var = HI2DI(hi);
23376 if ((this_var->di_tv.v_type == VAR_NUMBER
23377 || this_var->di_tv.v_type == VAR_STRING)
23378 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023380 /* Escape special characters with a backslash. Turn a LF and
23381 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023382 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023383 (char_u *)"\\\"\n\r");
23384 if (p == NULL) /* out of memory */
23385 break;
23386 for (t = p; *t != NUL; ++t)
23387 if (*t == '\n')
23388 *t = 'n';
23389 else if (*t == '\r')
23390 *t = 'r';
23391 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023392 this_var->di_key,
23393 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23394 : ' ',
23395 p,
23396 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23397 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023398 || put_eol(fd) == FAIL)
23399 {
23400 vim_free(p);
23401 return FAIL;
23402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403 vim_free(p);
23404 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023405#ifdef FEAT_FLOAT
23406 else if (this_var->di_tv.v_type == VAR_FLOAT
23407 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23408 {
23409 float_T f = this_var->di_tv.vval.v_float;
23410 int sign = ' ';
23411
23412 if (f < 0)
23413 {
23414 f = -f;
23415 sign = '-';
23416 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023417 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023418 this_var->di_key, sign, f) < 0)
23419 || put_eol(fd) == FAIL)
23420 return FAIL;
23421 }
23422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 }
23424 }
23425 return OK;
23426}
23427#endif
23428
Bram Moolenaar661b1822005-07-28 22:36:45 +000023429/*
23430 * Display script name where an item was last set.
23431 * Should only be invoked when 'verbose' is non-zero.
23432 */
23433 void
23434last_set_msg(scriptID)
23435 scid_T scriptID;
23436{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023437 char_u *p;
23438
Bram Moolenaar661b1822005-07-28 22:36:45 +000023439 if (scriptID != 0)
23440 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023441 p = home_replace_save(NULL, get_scriptname(scriptID));
23442 if (p != NULL)
23443 {
23444 verbose_enter();
23445 MSG_PUTS(_("\n\tLast set from "));
23446 MSG_PUTS(p);
23447 vim_free(p);
23448 verbose_leave();
23449 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023450 }
23451}
23452
Bram Moolenaard812df62008-11-09 12:46:09 +000023453/*
23454 * List v:oldfiles in a nice way.
23455 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023456 void
23457ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023458 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023459{
23460 list_T *l = vimvars[VV_OLDFILES].vv_list;
23461 listitem_T *li;
23462 int nr = 0;
23463
23464 if (l == NULL)
23465 msg((char_u *)_("No old files"));
23466 else
23467 {
23468 msg_start();
23469 msg_scroll = TRUE;
23470 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23471 {
23472 msg_outnum((long)++nr);
23473 MSG_PUTS(": ");
23474 msg_outtrans(get_tv_string(&li->li_tv));
23475 msg_putchar('\n');
23476 out_flush(); /* output one line at a time */
23477 ui_breakcheck();
23478 }
23479 /* Assume "got_int" was set to truncate the listing. */
23480 got_int = FALSE;
23481
23482#ifdef FEAT_BROWSE_CMD
23483 if (cmdmod.browse)
23484 {
23485 quit_more = FALSE;
23486 nr = prompt_for_number(FALSE);
23487 msg_starthere();
23488 if (nr > 0)
23489 {
23490 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23491 (long)nr);
23492
23493 if (p != NULL)
23494 {
23495 p = expand_env_save(p);
23496 eap->arg = p;
23497 eap->cmdidx = CMD_edit;
23498 cmdmod.browse = FALSE;
23499 do_exedit(eap, NULL);
23500 vim_free(p);
23501 }
23502 }
23503 }
23504#endif
23505 }
23506}
23507
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508#endif /* FEAT_EVAL */
23509
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023511#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512
23513#ifdef WIN3264
23514/*
23515 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23516 */
23517static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23518static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23519static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23520
23521/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023522 * Get the short path (8.3) for the filename in "fnamep".
23523 * Only works for a valid file name.
23524 * When the path gets longer "fnamep" is changed and the allocated buffer
23525 * is put in "bufp".
23526 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23527 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 */
23529 static int
23530get_short_pathname(fnamep, bufp, fnamelen)
23531 char_u **fnamep;
23532 char_u **bufp;
23533 int *fnamelen;
23534{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023535 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 char_u *newbuf;
23537
23538 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 l = GetShortPathName(*fnamep, *fnamep, len);
23540 if (l > len - 1)
23541 {
23542 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023543 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 newbuf = vim_strnsave(*fnamep, l);
23545 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023546 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547
23548 vim_free(*bufp);
23549 *fnamep = *bufp = newbuf;
23550
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023551 /* Really should always succeed, as the buffer is big enough. */
23552 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 }
23554
23555 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023556 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557}
23558
23559/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023560 * Get the short path (8.3) for the filename in "fname". The converted
23561 * path is returned in "bufp".
23562 *
23563 * Some of the directories specified in "fname" may not exist. This function
23564 * will shorten the existing directories at the beginning of the path and then
23565 * append the remaining non-existing path.
23566 *
23567 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023568 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023569 * bufp - Pointer to an allocated buffer for the filename.
23570 * fnamelen - Length of the filename pointed to by fname
23571 *
23572 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 */
23574 static int
23575shortpath_for_invalid_fname(fname, bufp, fnamelen)
23576 char_u **fname;
23577 char_u **bufp;
23578 int *fnamelen;
23579{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023580 char_u *short_fname, *save_fname, *pbuf_unused;
23581 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023583 int old_len, len;
23584 int new_len, sfx_len;
23585 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586
23587 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023588 old_len = *fnamelen;
23589 save_fname = vim_strnsave(*fname, old_len);
23590 pbuf_unused = NULL;
23591 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023593 endp = save_fname + old_len - 1; /* Find the end of the copy */
23594 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023596 /*
23597 * Try shortening the supplied path till it succeeds by removing one
23598 * directory at a time from the tail of the path.
23599 */
23600 len = 0;
23601 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023603 /* go back one path-separator */
23604 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23605 --endp;
23606 if (endp <= save_fname)
23607 break; /* processed the complete path */
23608
23609 /*
23610 * Replace the path separator with a NUL and try to shorten the
23611 * resulting path.
23612 */
23613 ch = *endp;
23614 *endp = 0;
23615 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023616 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023617 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23618 {
23619 retval = FAIL;
23620 goto theend;
23621 }
23622 *endp = ch; /* preserve the string */
23623
23624 if (len > 0)
23625 break; /* successfully shortened the path */
23626
23627 /* failed to shorten the path. Skip the path separator */
23628 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 }
23630
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023631 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023633 /*
23634 * Succeeded in shortening the path. Now concatenate the shortened
23635 * path with the remaining path at the tail.
23636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023638 /* Compute the length of the new path. */
23639 sfx_len = (int)(save_endp - endp) + 1;
23640 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023642 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023644 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023646 /* There is not enough space in the currently allocated string,
23647 * copy it to a buffer big enough. */
23648 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023650 {
23651 retval = FAIL;
23652 goto theend;
23653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 }
23655 else
23656 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023657 /* Transfer short_fname to the main buffer (it's big enough),
23658 * unless get_short_pathname() did its work in-place. */
23659 *fname = *bufp = save_fname;
23660 if (short_fname != save_fname)
23661 vim_strncpy(save_fname, short_fname, len);
23662 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023664
23665 /* concat the not-shortened part of the path */
23666 vim_strncpy(*fname + len, endp, sfx_len);
23667 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023669
23670theend:
23671 vim_free(pbuf_unused);
23672 vim_free(save_fname);
23673
23674 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675}
23676
23677/*
23678 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023679 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 */
23681 static int
23682shortpath_for_partial(fnamep, bufp, fnamelen)
23683 char_u **fnamep;
23684 char_u **bufp;
23685 int *fnamelen;
23686{
23687 int sepcount, len, tflen;
23688 char_u *p;
23689 char_u *pbuf, *tfname;
23690 int hasTilde;
23691
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023692 /* Count up the path separators from the RHS.. so we know which part
23693 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023694 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023695 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 if (vim_ispathsep(*p))
23697 ++sepcount;
23698
23699 /* Need full path first (use expand_env() to remove a "~/") */
23700 hasTilde = (**fnamep == '~');
23701 if (hasTilde)
23702 pbuf = tfname = expand_env_save(*fnamep);
23703 else
23704 pbuf = tfname = FullName_save(*fnamep, FALSE);
23705
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023706 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023708 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23709 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710
23711 if (len == 0)
23712 {
23713 /* Don't have a valid filename, so shorten the rest of the
23714 * path if we can. This CAN give us invalid 8.3 filenames, but
23715 * there's not a lot of point in guessing what it might be.
23716 */
23717 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023718 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23719 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 }
23721
23722 /* Count the paths backward to find the beginning of the desired string. */
23723 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023724 {
23725#ifdef FEAT_MBYTE
23726 if (has_mbyte)
23727 p -= mb_head_off(tfname, p);
23728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 if (vim_ispathsep(*p))
23730 {
23731 if (sepcount == 0 || (hasTilde && sepcount == 1))
23732 break;
23733 else
23734 sepcount --;
23735 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 if (hasTilde)
23738 {
23739 --p;
23740 if (p >= tfname)
23741 *p = '~';
23742 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023743 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744 }
23745 else
23746 ++p;
23747
23748 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23749 vim_free(*bufp);
23750 *fnamelen = (int)STRLEN(p);
23751 *bufp = pbuf;
23752 *fnamep = p;
23753
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023754 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755}
23756#endif /* WIN3264 */
23757
23758/*
23759 * Adjust a filename, according to a string of modifiers.
23760 * *fnamep must be NUL terminated when called. When returning, the length is
23761 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023762 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 * When there is an error, *fnamep is set to NULL.
23764 */
23765 int
23766modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23767 char_u *src; /* string with modifiers */
23768 int *usedlen; /* characters after src that are used */
23769 char_u **fnamep; /* file name so far */
23770 char_u **bufp; /* buffer for allocated file name or NULL */
23771 int *fnamelen; /* length of fnamep */
23772{
23773 int valid = 0;
23774 char_u *tail;
23775 char_u *s, *p, *pbuf;
23776 char_u dirname[MAXPATHL];
23777 int c;
23778 int has_fullname = 0;
23779#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023780 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 int has_shortname = 0;
23782#endif
23783
23784repeat:
23785 /* ":p" - full path/file_name */
23786 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23787 {
23788 has_fullname = 1;
23789
23790 valid |= VALID_PATH;
23791 *usedlen += 2;
23792
23793 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23794 if ((*fnamep)[0] == '~'
23795#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23796 && ((*fnamep)[1] == '/'
23797# ifdef BACKSLASH_IN_FILENAME
23798 || (*fnamep)[1] == '\\'
23799# endif
23800 || (*fnamep)[1] == NUL)
23801
23802#endif
23803 )
23804 {
23805 *fnamep = expand_env_save(*fnamep);
23806 vim_free(*bufp); /* free any allocated file name */
23807 *bufp = *fnamep;
23808 if (*fnamep == NULL)
23809 return -1;
23810 }
23811
23812 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023813 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 {
23815 if (vim_ispathsep(*p)
23816 && p[1] == '.'
23817 && (p[2] == NUL
23818 || vim_ispathsep(p[2])
23819 || (p[2] == '.'
23820 && (p[3] == NUL || vim_ispathsep(p[3])))))
23821 break;
23822 }
23823
23824 /* FullName_save() is slow, don't use it when not needed. */
23825 if (*p != NUL || !vim_isAbsName(*fnamep))
23826 {
23827 *fnamep = FullName_save(*fnamep, *p != NUL);
23828 vim_free(*bufp); /* free any allocated file name */
23829 *bufp = *fnamep;
23830 if (*fnamep == NULL)
23831 return -1;
23832 }
23833
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023834#ifdef WIN3264
23835# if _WIN32_WINNT >= 0x0500
23836 if (vim_strchr(*fnamep, '~') != NULL)
23837 {
23838 /* Expand 8.3 filename to full path. Needed to make sure the same
23839 * file does not have two different names.
23840 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23841 p = alloc(_MAX_PATH + 1);
23842 if (p != NULL)
23843 {
23844 if (GetLongPathName(*fnamep, p, MAXPATHL))
23845 {
23846 vim_free(*bufp);
23847 *bufp = *fnamep = p;
23848 }
23849 else
23850 vim_free(p);
23851 }
23852 }
23853# endif
23854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 /* Append a path separator to a directory. */
23856 if (mch_isdir(*fnamep))
23857 {
23858 /* Make room for one or two extra characters. */
23859 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23860 vim_free(*bufp); /* free any allocated file name */
23861 *bufp = *fnamep;
23862 if (*fnamep == NULL)
23863 return -1;
23864 add_pathsep(*fnamep);
23865 }
23866 }
23867
23868 /* ":." - path relative to the current directory */
23869 /* ":~" - path relative to the home directory */
23870 /* ":8" - shortname path - postponed till after */
23871 while (src[*usedlen] == ':'
23872 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23873 {
23874 *usedlen += 2;
23875 if (c == '8')
23876 {
23877#ifdef WIN3264
23878 has_shortname = 1; /* Postpone this. */
23879#endif
23880 continue;
23881 }
23882 pbuf = NULL;
23883 /* Need full path first (use expand_env() to remove a "~/") */
23884 if (!has_fullname)
23885 {
23886 if (c == '.' && **fnamep == '~')
23887 p = pbuf = expand_env_save(*fnamep);
23888 else
23889 p = pbuf = FullName_save(*fnamep, FALSE);
23890 }
23891 else
23892 p = *fnamep;
23893
23894 has_fullname = 0;
23895
23896 if (p != NULL)
23897 {
23898 if (c == '.')
23899 {
23900 mch_dirname(dirname, MAXPATHL);
23901 s = shorten_fname(p, dirname);
23902 if (s != NULL)
23903 {
23904 *fnamep = s;
23905 if (pbuf != NULL)
23906 {
23907 vim_free(*bufp); /* free any allocated file name */
23908 *bufp = pbuf;
23909 pbuf = NULL;
23910 }
23911 }
23912 }
23913 else
23914 {
23915 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23916 /* Only replace it when it starts with '~' */
23917 if (*dirname == '~')
23918 {
23919 s = vim_strsave(dirname);
23920 if (s != NULL)
23921 {
23922 *fnamep = s;
23923 vim_free(*bufp);
23924 *bufp = s;
23925 }
23926 }
23927 }
23928 vim_free(pbuf);
23929 }
23930 }
23931
23932 tail = gettail(*fnamep);
23933 *fnamelen = (int)STRLEN(*fnamep);
23934
23935 /* ":h" - head, remove "/file_name", can be repeated */
23936 /* Don't remove the first "/" or "c:\" */
23937 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23938 {
23939 valid |= VALID_HEAD;
23940 *usedlen += 2;
23941 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023942 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023943 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 *fnamelen = (int)(tail - *fnamep);
23945#ifdef VMS
23946 if (*fnamelen > 0)
23947 *fnamelen += 1; /* the path separator is part of the path */
23948#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023949 if (*fnamelen == 0)
23950 {
23951 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23952 p = vim_strsave((char_u *)".");
23953 if (p == NULL)
23954 return -1;
23955 vim_free(*bufp);
23956 *bufp = *fnamep = tail = p;
23957 *fnamelen = 1;
23958 }
23959 else
23960 {
23961 while (tail > s && !after_pathsep(s, tail))
23962 mb_ptr_back(*fnamep, tail);
23963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 }
23965
23966 /* ":8" - shortname */
23967 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23968 {
23969 *usedlen += 2;
23970#ifdef WIN3264
23971 has_shortname = 1;
23972#endif
23973 }
23974
23975#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023976 /*
23977 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 */
23979 if (has_shortname)
23980 {
Bram Moolenaardc935552011-08-17 15:23:23 +020023981 /* Copy the string if it is shortened by :h and when it wasn't copied
23982 * yet, because we are going to change it in place. Avoids changing
23983 * the buffer name for "%:8". */
23984 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 {
23986 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020023987 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 return -1;
23989 vim_free(*bufp);
23990 *bufp = *fnamep = p;
23991 }
23992
23993 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020023994 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 if (!has_fullname && !vim_isAbsName(*fnamep))
23996 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023997 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 return -1;
23999 }
24000 else
24001 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024002 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003
Bram Moolenaardc935552011-08-17 15:23:23 +020024004 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024006 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 return -1;
24008
24009 if (l == 0)
24010 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024011 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024013 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 return -1;
24015 }
24016 *fnamelen = l;
24017 }
24018 }
24019#endif /* WIN3264 */
24020
24021 /* ":t" - tail, just the basename */
24022 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24023 {
24024 *usedlen += 2;
24025 *fnamelen -= (int)(tail - *fnamep);
24026 *fnamep = tail;
24027 }
24028
24029 /* ":e" - extension, can be repeated */
24030 /* ":r" - root, without extension, can be repeated */
24031 while (src[*usedlen] == ':'
24032 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24033 {
24034 /* find a '.' in the tail:
24035 * - for second :e: before the current fname
24036 * - otherwise: The last '.'
24037 */
24038 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24039 s = *fnamep - 2;
24040 else
24041 s = *fnamep + *fnamelen - 1;
24042 for ( ; s > tail; --s)
24043 if (s[0] == '.')
24044 break;
24045 if (src[*usedlen + 1] == 'e') /* :e */
24046 {
24047 if (s > tail)
24048 {
24049 *fnamelen += (int)(*fnamep - (s + 1));
24050 *fnamep = s + 1;
24051#ifdef VMS
24052 /* cut version from the extension */
24053 s = *fnamep + *fnamelen - 1;
24054 for ( ; s > *fnamep; --s)
24055 if (s[0] == ';')
24056 break;
24057 if (s > *fnamep)
24058 *fnamelen = s - *fnamep;
24059#endif
24060 }
24061 else if (*fnamep <= tail)
24062 *fnamelen = 0;
24063 }
24064 else /* :r */
24065 {
24066 if (s > tail) /* remove one extension */
24067 *fnamelen = (int)(s - *fnamep);
24068 }
24069 *usedlen += 2;
24070 }
24071
24072 /* ":s?pat?foo?" - substitute */
24073 /* ":gs?pat?foo?" - global substitute */
24074 if (src[*usedlen] == ':'
24075 && (src[*usedlen + 1] == 's'
24076 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24077 {
24078 char_u *str;
24079 char_u *pat;
24080 char_u *sub;
24081 int sep;
24082 char_u *flags;
24083 int didit = FALSE;
24084
24085 flags = (char_u *)"";
24086 s = src + *usedlen + 2;
24087 if (src[*usedlen + 1] == 'g')
24088 {
24089 flags = (char_u *)"g";
24090 ++s;
24091 }
24092
24093 sep = *s++;
24094 if (sep)
24095 {
24096 /* find end of pattern */
24097 p = vim_strchr(s, sep);
24098 if (p != NULL)
24099 {
24100 pat = vim_strnsave(s, (int)(p - s));
24101 if (pat != NULL)
24102 {
24103 s = p + 1;
24104 /* find end of substitution */
24105 p = vim_strchr(s, sep);
24106 if (p != NULL)
24107 {
24108 sub = vim_strnsave(s, (int)(p - s));
24109 str = vim_strnsave(*fnamep, *fnamelen);
24110 if (sub != NULL && str != NULL)
24111 {
24112 *usedlen = (int)(p + 1 - src);
24113 s = do_string_sub(str, pat, sub, flags);
24114 if (s != NULL)
24115 {
24116 *fnamep = s;
24117 *fnamelen = (int)STRLEN(s);
24118 vim_free(*bufp);
24119 *bufp = s;
24120 didit = TRUE;
24121 }
24122 }
24123 vim_free(sub);
24124 vim_free(str);
24125 }
24126 vim_free(pat);
24127 }
24128 }
24129 /* after using ":s", repeat all the modifiers */
24130 if (didit)
24131 goto repeat;
24132 }
24133 }
24134
24135 return valid;
24136}
24137
24138/*
24139 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24140 * "flags" can be "g" to do a global substitute.
24141 * Returns an allocated string, NULL for error.
24142 */
24143 char_u *
24144do_string_sub(str, pat, sub, flags)
24145 char_u *str;
24146 char_u *pat;
24147 char_u *sub;
24148 char_u *flags;
24149{
24150 int sublen;
24151 regmatch_T regmatch;
24152 int i;
24153 int do_all;
24154 char_u *tail;
24155 garray_T ga;
24156 char_u *ret;
24157 char_u *save_cpo;
24158
24159 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24160 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024161 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162
24163 ga_init2(&ga, 1, 200);
24164
24165 do_all = (flags[0] == 'g');
24166
24167 regmatch.rm_ic = p_ic;
24168 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24169 if (regmatch.regprog != NULL)
24170 {
24171 tail = str;
24172 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24173 {
24174 /*
24175 * Get some space for a temporary buffer to do the substitution
24176 * into. It will contain:
24177 * - The text up to where the match is.
24178 * - The substituted text.
24179 * - The text after the match.
24180 */
24181 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24182 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24183 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24184 {
24185 ga_clear(&ga);
24186 break;
24187 }
24188
24189 /* copy the text up to where the match is */
24190 i = (int)(regmatch.startp[0] - tail);
24191 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24192 /* add the substituted text */
24193 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24194 + ga.ga_len + i, TRUE, TRUE, FALSE);
24195 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 /* avoid getting stuck on a match with an empty string */
24197 if (tail == regmatch.endp[0])
24198 {
24199 if (*tail == NUL)
24200 break;
24201 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24202 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 }
24204 else
24205 {
24206 tail = regmatch.endp[0];
24207 if (*tail == NUL)
24208 break;
24209 }
24210 if (!do_all)
24211 break;
24212 }
24213
24214 if (ga.ga_data != NULL)
24215 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24216
24217 vim_free(regmatch.regprog);
24218 }
24219
24220 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24221 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024222 if (p_cpo == empty_option)
24223 p_cpo = save_cpo;
24224 else
24225 /* Darn, evaluating {sub} expression changed the value. */
24226 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227
24228 return ret;
24229}
24230
24231#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */